1 /*
2  * Copyright (C) 2006 - 2013 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2007 Armin Burgmeier <armin@openismus.com>
4  * Copyright (C) 2007 Murray Cumming <murrayc@murrayc.com>
5  * Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
6  * Copyright (C) 2010 David King <davidk@openismus.com>
7  * Copyright (C) 2010 Jonh Wendell <jwendell@gnome.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA  02110-1301, USA.
23  */
24 
25 #include "gda-handler-bin.h"
26 #include <string.h>
27 #include <glib/gi18n-lib.h>
28 #include <libgda/gda-server-provider.h>
29 #include <libgda/gda-util.h>
30 #include <libgda/gda-blob-op.h>
31 
32 static void gda_handler_bin_class_init (GdaHandlerBinClass * class);
33 static void gda_handler_bin_init (GdaHandlerBin * wid);
34 static void gda_handler_bin_dispose (GObject   * object);
35 
36 
37 /* GdaDataHandler interface */
38 static void         gda_handler_bin_data_handler_init      (GdaDataHandlerIface *iface);
39 static gchar       *gda_handler_bin_get_sql_from_value     (GdaDataHandler *dh, const GValue *value);
40 static gchar       *gda_handler_bin_get_str_from_value     (GdaDataHandler *dh, const GValue *value);
41 static GValue      *gda_handler_bin_get_value_from_sql     (GdaDataHandler *dh, const gchar *sql,
42 							    GType type);
43 static GValue      *gda_handler_bin_get_value_from_str     (GdaDataHandler *dh, const gchar *sql,
44 							    GType type);
45 static gboolean     gda_handler_bin_accepts_g_type       (GdaDataHandler * dh, GType type);
46 
47 static const gchar *gda_handler_bin_get_descr              (GdaDataHandler *dh);
48 
49 struct  _GdaHandlerBinPriv {
50 	guint              nb_g_types;
51 	GType             *valid_g_types;
52 };
53 
54 /* get a pointer to the parents to be able to call their destructor */
55 static GObjectClass *parent_class = NULL;
56 
57 GType
gda_handler_bin_get_type(void)58 gda_handler_bin_get_type (void)
59 {
60 	static GType type = 0;
61 
62 	if (G_UNLIKELY (type == 0)) {
63 		static GMutex registering;
64 		static const GTypeInfo info = {
65 			sizeof (GdaHandlerBinClass),
66 			(GBaseInitFunc) NULL,
67 			(GBaseFinalizeFunc) NULL,
68 			(GClassInitFunc) gda_handler_bin_class_init,
69 			NULL,
70 			NULL,
71 			sizeof (GdaHandlerBin),
72 			0,
73 			(GInstanceInitFunc) gda_handler_bin_init,
74 			NULL
75 		};
76 
77 		static const GInterfaceInfo data_entry_info = {
78 			(GInterfaceInitFunc) gda_handler_bin_data_handler_init,
79 			NULL,
80 			NULL
81 		};
82 
83 		g_mutex_lock (&registering);
84 		if (type == 0) {
85 			type = g_type_register_static (G_TYPE_OBJECT, "GdaHandlerBin", &info, 0);
86 			g_type_add_interface_static (type, GDA_TYPE_DATA_HANDLER, &data_entry_info);
87 		}
88 		g_mutex_unlock (&registering);
89 	}
90 	return type;
91 }
92 
93 static void
gda_handler_bin_data_handler_init(GdaDataHandlerIface * iface)94 gda_handler_bin_data_handler_init (GdaDataHandlerIface *iface)
95 {
96 	iface->get_sql_from_value = gda_handler_bin_get_sql_from_value;
97 	iface->get_str_from_value = gda_handler_bin_get_str_from_value;
98 	iface->get_value_from_sql = gda_handler_bin_get_value_from_sql;
99 	iface->get_value_from_str = gda_handler_bin_get_value_from_str;
100 	iface->get_sane_init_value = NULL;
101 	iface->accepts_g_type = gda_handler_bin_accepts_g_type;
102 	iface->get_descr = gda_handler_bin_get_descr;
103 }
104 
105 
106 static void
gda_handler_bin_class_init(GdaHandlerBinClass * class)107 gda_handler_bin_class_init (GdaHandlerBinClass * class)
108 {
109 	GObjectClass   *object_class = G_OBJECT_CLASS (class);
110 
111 	parent_class = g_type_class_peek_parent (class);
112 
113 	object_class->dispose = gda_handler_bin_dispose;
114 }
115 
116 static void
gda_handler_bin_init(GdaHandlerBin * hdl)117 gda_handler_bin_init (GdaHandlerBin * hdl)
118 {
119 	/* Private structure */
120 	hdl->priv = g_new0 (GdaHandlerBinPriv, 1);
121 	hdl->priv->nb_g_types = 2;
122 	hdl->priv->valid_g_types = g_new0 (GType, hdl->priv->nb_g_types);
123 	hdl->priv->valid_g_types[0] = GDA_TYPE_BINARY;
124 	hdl->priv->valid_g_types[1] = GDA_TYPE_BLOB;
125 
126 	g_object_set_data (G_OBJECT (hdl), "name", "InternalBin");
127 	g_object_set_data (G_OBJECT (hdl), "descr", _("Binary representation"));
128 }
129 
130 static void
gda_handler_bin_dispose(GObject * object)131 gda_handler_bin_dispose (GObject   * object)
132 {
133 	GdaHandlerBin *hdl;
134 
135 	g_return_if_fail (object != NULL);
136 	g_return_if_fail (GDA_IS_HANDLER_BIN (object));
137 
138 	hdl = GDA_HANDLER_BIN (object);
139 
140 	if (hdl->priv) {
141 		g_free (hdl->priv->valid_g_types);
142 		hdl->priv->valid_g_types = NULL;
143 
144 		g_free (hdl->priv);
145 		hdl->priv = NULL;
146 	}
147 
148 	/* for the parent class */
149 	parent_class->dispose (object);
150 }
151 
152 /**
153  * gda_handler_bin_new:
154  *
155  * Creates a data handler for binary values
156  *
157  * Returns: (transfer full): the new object
158  */
159 GdaDataHandler *
gda_handler_bin_new(void)160 gda_handler_bin_new (void)
161 {
162 	GObject *obj;
163 
164 	obj = g_object_new (GDA_TYPE_HANDLER_BIN, NULL);
165 
166 	return (GdaDataHandler *) obj;
167 }
168 
169 
170 static gchar *
gda_handler_bin_get_sql_from_value(G_GNUC_UNUSED GdaDataHandler * iface,const GValue * value)171 gda_handler_bin_get_sql_from_value (G_GNUC_UNUSED GdaDataHandler *iface, const GValue *value)
172 {
173 	g_assert (value);
174 
175 	gchar *retval;
176 	if (gda_value_isa ((GValue *) value, GDA_TYPE_BINARY)) {
177 		gchar *str, *str2;
178 		str = gda_binary_to_string (gda_value_get_binary ((GValue *) value), 0);
179 		str2 = gda_default_escape_string (str);
180 		g_free (str);
181 		retval = g_strdup_printf ("'%s'", str2);
182 		g_free (str2);
183 	}
184 	else {
185 		GdaBlob *blob;
186 		GdaBinary *bin;
187 		blob = (GdaBlob*) gda_value_get_blob ((GValue *) value);
188 		bin = (GdaBinary *) blob;
189 		if (blob->op &&
190 		    (bin->binary_length != gda_blob_op_get_length (blob->op)))
191 			gda_blob_op_read_all (blob->op, blob);
192 
193 		gchar *str, *str2;
194 		str = gda_binary_to_string (bin, 0);
195 		str2 = gda_default_escape_string (str);
196 		g_free (str);
197 		retval = g_strdup_printf ("'%s'", str2);
198 		g_free (str2);
199 	}
200 
201 	return retval;
202 }
203 
204 static gchar *
gda_handler_bin_get_str_from_value(G_GNUC_UNUSED GdaDataHandler * iface,const GValue * value)205 gda_handler_bin_get_str_from_value (G_GNUC_UNUSED GdaDataHandler *iface, const GValue *value)
206 {
207 	g_assert (value);
208 
209 	gchar *retval = NULL;
210 	if (value) {
211 		if (gda_value_isa ((GValue *) value, GDA_TYPE_BINARY))
212 			retval = gda_binary_to_string (gda_value_get_binary ((GValue *) value), 0);
213 		else {
214 			GdaBlob *blob;
215 			blob = (GdaBlob*) gda_value_get_blob ((GValue *) value);
216 			if (blob->op)
217 				gda_blob_op_read_all (blob->op, blob);
218 			retval = gda_binary_to_string ((GdaBinary *) blob, 0);
219 		}
220 	}
221 
222 	return retval;
223 }
224 
225 static GValue *
gda_handler_bin_get_value_from_sql(G_GNUC_UNUSED GdaDataHandler * iface,const gchar * sql,GType type)226 gda_handler_bin_get_value_from_sql (G_GNUC_UNUSED GdaDataHandler *iface, const gchar *sql, GType type)
227 {
228 	g_assert (sql);
229 
230 	GValue *value = NULL;
231 	if (*sql) {
232 		gint i = strlen (sql);
233 		if ((i>=2) && (*sql=='\'') && (sql[i-1]=='\'')) {
234 			gchar *str = g_strdup (sql);
235 			gchar *unstr;
236 
237 			str[i-1] = 0;
238 			unstr = gda_default_unescape_string (str+1);
239 			if (unstr) {
240 				value = gda_handler_bin_get_value_from_str (iface, unstr, type);
241 				g_free (unstr);
242 			}
243 			g_free (str);
244 		}
245 	}
246 
247 	return value;
248 }
249 
250 static GValue *
gda_handler_bin_get_value_from_str(G_GNUC_UNUSED GdaDataHandler * iface,const gchar * str,GType type)251 gda_handler_bin_get_value_from_str (G_GNUC_UNUSED GdaDataHandler *iface, const gchar *str, GType type)
252 {
253 	g_assert (str);
254 
255 	GValue *value = NULL;
256 	if (type == GDA_TYPE_BINARY) {
257 		GdaBinary *bin;
258 		bin = gda_string_to_binary (str);
259 		if (bin) {
260 			value = gda_value_new (GDA_TYPE_BINARY);
261 			gda_value_take_binary (value, bin);
262 		}
263 	}
264 	else {
265 		GdaBlob *blob;
266 		blob = gda_string_to_blob (str);
267 		if (blob) {
268 			value = gda_value_new (GDA_TYPE_BLOB);
269 			gda_value_take_blob (value, blob);
270 		}
271 	}
272 
273 	return value;
274 }
275 
276 static gboolean
gda_handler_bin_accepts_g_type(GdaDataHandler * iface,GType type)277 gda_handler_bin_accepts_g_type (GdaDataHandler *iface, GType type)
278 {
279 	GdaHandlerBin *hdl;
280 	guint i;
281 
282 	g_assert (iface);
283 	hdl = (GdaHandlerBin*) (iface);
284 
285 	for (i = 0; i < hdl->priv->nb_g_types; i++) {
286 		if (hdl->priv->valid_g_types [i] == type)
287 			return TRUE;
288 	}
289 
290 	return FALSE;
291 }
292 
293 static const gchar *
gda_handler_bin_get_descr(GdaDataHandler * iface)294 gda_handler_bin_get_descr (GdaDataHandler *iface)
295 {
296 	g_return_val_if_fail (GDA_IS_HANDLER_BIN (iface), NULL);
297 	return g_object_get_data (G_OBJECT (iface), "descr");
298 }
299