1 /*
2  * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2010 David King <davidk@openismus.com>
4  * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301, USA.
20  */
21 
22 #include <string.h>
23 #include <glib/gi18n-lib.h>
24 #include "gda-web-pstmt.h"
25 #include "gda-web-util.h"
26 
27 static void gda_web_pstmt_class_init (GdaWebPStmtClass *klass);
28 static void gda_web_pstmt_init       (GdaWebPStmt *pstmt, GdaWebPStmtClass *klass);
29 static void gda_web_pstmt_finalize    (GObject *object);
30 
31 static GObjectClass *parent_class = NULL;
32 
33 /**
34  * gda_web_pstmt_get_type
35  *
36  * Returns: the #GType of GdaWebPStmt.
37  */
38 GType
gda_web_pstmt_get_type(void)39 gda_web_pstmt_get_type (void)
40 {
41 	static GType type = 0;
42 
43 	if (G_UNLIKELY (type == 0)) {
44 		static GMutex registering;
45 		static const GTypeInfo info = {
46 			sizeof (GdaWebPStmtClass),
47 			(GBaseInitFunc) NULL,
48 			(GBaseFinalizeFunc) NULL,
49 			(GClassInitFunc) gda_web_pstmt_class_init,
50 			NULL,
51 			NULL,
52 			sizeof (GdaWebPStmt),
53 			0,
54 			(GInstanceInitFunc) gda_web_pstmt_init,
55 			0
56 		};
57 
58 		g_mutex_lock (&registering);
59 		if (type == 0)
60 			type = g_type_register_static (GDA_TYPE_PSTMT, "GdaWebPStmt", &info, 0);
61 		g_mutex_unlock (&registering);
62 	}
63 	return type;
64 }
65 
66 static void
gda_web_pstmt_class_init(GdaWebPStmtClass * klass)67 gda_web_pstmt_class_init (GdaWebPStmtClass *klass)
68 {
69 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
70 	parent_class = g_type_class_peek_parent (klass);
71 
72 	/* virtual functions */
73 	object_class->finalize = gda_web_pstmt_finalize;
74 }
75 
76 static void
gda_web_pstmt_init(GdaWebPStmt * pstmt,G_GNUC_UNUSED GdaWebPStmtClass * klass)77 gda_web_pstmt_init (GdaWebPStmt *pstmt, G_GNUC_UNUSED GdaWebPStmtClass *klass)
78 {
79 	g_return_if_fail (GDA_IS_PSTMT (pstmt));
80 
81 	pstmt->pstmt_hash = NULL;
82 }
83 
84 static void
gda_web_pstmt_finalize(GObject * object)85 gda_web_pstmt_finalize (GObject *object)
86 {
87 	GdaWebPStmt *pstmt = (GdaWebPStmt *) object;
88 
89 	g_return_if_fail (GDA_IS_PSTMT (pstmt));
90 
91 	if (pstmt->pstmt_hash) {
92 		WebConnectionData *cdata;
93 		cdata = (WebConnectionData*) gda_connection_internal_get_provider_data (pstmt->cnc);
94 		if (!cdata)
95 			goto next;
96 
97 		/* send command to deallocate prepared statement */
98 		xmlDocPtr doc;
99 		xmlNodePtr root, cmdnode;
100 		gchar *token;
101 		doc = xmlNewDoc (BAD_CAST "1.0");
102 		root = xmlNewNode (NULL, BAD_CAST "request");
103 		xmlDocSetRootElement (doc, root);
104 		token = _gda_web_compute_token (cdata);
105 		xmlNewChild (root, NULL, BAD_CAST "token", BAD_CAST token);
106 		g_free (token);
107 		cmdnode = xmlNewChild (root, NULL, BAD_CAST "cmd", BAD_CAST "UNPREPARE");
108 		xmlNewChild (cmdnode, NULL, BAD_CAST "preparehash", BAD_CAST pstmt->pstmt_hash);
109 
110 		xmlChar *cmde;
111 		xmlDocPtr replydoc;
112 		int size;
113 		gchar status;
114 
115 		xmlDocDumpMemory (doc, &cmde, &size);
116 		xmlFreeDoc (doc);
117 		replydoc = _gda_web_send_message_to_frontend (pstmt->cnc, cdata, MESSAGE_UNPREPARE, (gchar*) cmde,
118 							      cdata->key, &status);
119 		xmlFree (cmde);
120 		if (replydoc)
121 			xmlFreeDoc (replydoc);
122 
123 	next:
124 		/* free memory */
125 		g_free (pstmt->pstmt_hash);
126 	}
127 
128 	/* chain to parent class */
129 	parent_class->finalize (object);
130 }
131 
132 GdaWebPStmt *
gda_web_pstmt_new(GdaConnection * cnc,const gchar * pstmt_hash)133 gda_web_pstmt_new (GdaConnection *cnc, const gchar *pstmt_hash)
134 {
135 	GdaWebPStmt *pstmt;
136 	g_return_val_if_fail (pstmt_hash && *pstmt_hash, NULL);
137 
138         pstmt = (GdaWebPStmt *) g_object_new (GDA_TYPE_WEB_PSTMT, NULL);
139 	pstmt->cnc = cnc;
140         pstmt->pstmt_hash = g_strdup (pstmt_hash);
141 
142         return pstmt;
143 }
144