1 /*
2 * Copyright (C) 2006 - 2007 Murray Cumming <murrayc@murrayc.com>
3 * Copyright (C) 2006 - 2013 Vivien Malerba <malerba@gnome-db.org>
4 * Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
5 * Copyright (C) 2010 David King <davidk@openismus.com>
6 * Copyright (C) 2010 Jonh Wendell <jwendell@gnome.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "gda-handler-type.h"
25 #include <string.h>
26 #include <glib/gi18n-lib.h>
27 #include <gda-util.h>
28
29 static void gda_handler_type_class_init (GdaHandlerTypeClass * class);
30 static void gda_handler_type_init (GdaHandlerType * wid);
31 static void gda_handler_type_dispose (GObject * object);
32
33
34 /* GdaDataHandler interface */
35 static void gda_handler_type_data_handler_init (GdaDataHandlerIface *iface);
36 static gchar *gda_handler_type_get_sql_from_value (GdaDataHandler *dh, const GValue *value);
37 static gchar *gda_handler_type_get_str_from_value (GdaDataHandler *dh, const GValue *value);
38 static GValue *gda_handler_type_get_value_from_sql (GdaDataHandler *dh, const gchar *sql,
39 GType type);
40 static GValue *gda_handler_type_get_value_from_str (GdaDataHandler *dh, const gchar *sql,
41 GType type);
42 static gboolean gda_handler_type_accepts_g_type (GdaDataHandler * dh, GType type);
43
44 static const gchar *gda_handler_type_get_descr (GdaDataHandler *dh);
45
46 struct _GdaHandlerTypePriv {
47 gchar dummy;
48 };
49
50 /* get a pointer to the parents to be able to call their destructor */
51 static GObjectClass *parent_class = NULL;
52
53 GType
gda_handler_type_get_type(void)54 gda_handler_type_get_type (void)
55 {
56 static GType type = 0;
57
58 if (G_UNLIKELY (type == 0)) {
59 static GMutex registering;
60 static const GTypeInfo info = {
61 sizeof (GdaHandlerTypeClass),
62 (GBaseInitFunc) NULL,
63 (GBaseFinalizeFunc) NULL,
64 (GClassInitFunc) gda_handler_type_class_init,
65 NULL,
66 NULL,
67 sizeof (GdaHandlerType),
68 0,
69 (GInstanceInitFunc) gda_handler_type_init,
70 NULL
71 };
72
73 static const GInterfaceInfo data_entry_info = {
74 (GInterfaceInitFunc) gda_handler_type_data_handler_init,
75 NULL,
76 NULL
77 };
78
79 g_mutex_lock (®istering);
80 if (type == 0) {
81 type = g_type_register_static (G_TYPE_OBJECT, "GdaHandlerType", &info, 0);
82 g_type_add_interface_static (type, GDA_TYPE_DATA_HANDLER, &data_entry_info);
83 }
84 g_mutex_unlock (®istering);
85 }
86 return type;
87 }
88
89 static void
gda_handler_type_data_handler_init(GdaDataHandlerIface * iface)90 gda_handler_type_data_handler_init (GdaDataHandlerIface *iface)
91 {
92 iface->get_sql_from_value = gda_handler_type_get_sql_from_value;
93 iface->get_str_from_value = gda_handler_type_get_str_from_value;
94 iface->get_value_from_sql = gda_handler_type_get_value_from_sql;
95 iface->get_value_from_str = gda_handler_type_get_value_from_str;
96 iface->get_sane_init_value = NULL;
97 iface->accepts_g_type = gda_handler_type_accepts_g_type;
98 iface->get_descr = gda_handler_type_get_descr;
99 }
100
101
102 static void
gda_handler_type_class_init(GdaHandlerTypeClass * class)103 gda_handler_type_class_init (GdaHandlerTypeClass * class)
104 {
105 GObjectClass *object_class = G_OBJECT_CLASS (class);
106
107 parent_class = g_type_class_peek_parent (class);
108
109 object_class->dispose = gda_handler_type_dispose;
110 }
111
112 static void
gda_handler_type_init(GdaHandlerType * hdl)113 gda_handler_type_init (GdaHandlerType * hdl)
114 {
115 /* Private structure */
116 hdl->priv = g_new0 (GdaHandlerTypePriv, 1);
117
118 g_object_set_data (G_OBJECT (hdl), "descr", "InternalType");
119 g_object_set_data (G_OBJECT (hdl), "descr", _("Gda type representation"));
120 }
121
122 static void
gda_handler_type_dispose(GObject * object)123 gda_handler_type_dispose (GObject * object)
124 {
125 GdaHandlerType *hdl;
126
127 g_return_if_fail (object != NULL);
128 g_return_if_fail (GDA_IS_HANDLER_TYPE (object));
129
130 hdl = GDA_HANDLER_TYPE (object);
131
132 if (hdl->priv) {
133 g_free (hdl->priv);
134 hdl->priv = NULL;
135 }
136
137 /* for the parent class */
138 parent_class->dispose (object);
139 }
140
141 /**
142 * gda_handler_type_new:
143 *
144 * Creates a data handler for Gda types
145 *
146 * Returns: (transfer full): the new object
147 */
148 GdaDataHandler *
gda_handler_type_new(void)149 gda_handler_type_new (void)
150 {
151 GObject *obj;
152
153 obj = g_object_new (GDA_TYPE_HANDLER_TYPE, NULL);
154
155 return (GdaDataHandler *) obj;
156 }
157
158 static gchar *
gda_handler_type_get_sql_from_value(G_GNUC_UNUSED GdaDataHandler * iface,const GValue * value)159 gda_handler_type_get_sql_from_value (G_GNUC_UNUSED GdaDataHandler *iface, const GValue *value)
160 {
161 g_assert (value);
162
163 gchar *retval;
164 GTypeQuery tq;
165 g_type_query (g_value_get_gtype (value), &tq);
166 if (tq.type != 0) {
167 const gchar *str;
168 str = gda_g_type_to_string (g_value_get_gtype (value));
169 retval = g_strdup_printf ("'%s'", str);
170 }
171 else
172 retval = g_strdup ("NULL");
173
174 return retval;
175 }
176
177 static gchar *
gda_handler_type_get_str_from_value(G_GNUC_UNUSED GdaDataHandler * iface,const GValue * value)178 gda_handler_type_get_str_from_value (G_GNUC_UNUSED GdaDataHandler *iface, const GValue *value)
179 {
180 g_assert (value);
181
182 gchar *retval;
183 GTypeQuery tq;
184
185 g_type_query (g_value_get_gtype (value), &tq);
186 if (tq.type != 0)
187 retval = g_strdup (gda_g_type_to_string (g_value_get_gtype (value)));
188 else
189 retval = NULL;
190
191 return retval;
192 }
193
194 static GValue *
gda_handler_type_get_value_from_sql(G_GNUC_UNUSED GdaDataHandler * iface,const gchar * sql,G_GNUC_UNUSED GType type)195 gda_handler_type_get_value_from_sql (G_GNUC_UNUSED GdaDataHandler *iface, const gchar *sql, G_GNUC_UNUSED GType type)
196 {
197 g_assert (sql);
198
199 GValue *value = NULL;
200 if (*sql) {
201 gint i = strlen (sql);
202 if ((i>=2) && (*sql=='\'') && (sql[i-1]=='\'')) {
203 gchar *str = g_strdup (sql);
204 GType type;
205 str[i-1] = 0;
206 type = gda_g_type_from_string (str+1);
207 g_free (str);
208 if (type != G_TYPE_INVALID) {
209 value = g_value_init (g_new0 (GValue, 1), G_TYPE_GTYPE);
210 g_value_set_gtype (value, type);
211 }
212 }
213 }
214 else
215 value = gda_value_new_null ();
216 return value;
217 }
218
219 static GValue *
gda_handler_type_get_value_from_str(G_GNUC_UNUSED GdaDataHandler * iface,const gchar * str,G_GNUC_UNUSED GType type)220 gda_handler_type_get_value_from_str (G_GNUC_UNUSED GdaDataHandler *iface, const gchar *str, G_GNUC_UNUSED GType type)
221 {
222 g_assert (str);
223
224 GValue *value = NULL;
225 GType vtype;
226
227 vtype = gda_g_type_from_string (str);
228 if (vtype != G_TYPE_INVALID) {
229 value = g_value_init (g_new0 (GValue, 1), G_TYPE_GTYPE);
230 g_value_set_gtype (value, vtype);
231 }
232
233 return value;
234 }
235
236 static gboolean
gda_handler_type_accepts_g_type(GdaDataHandler * iface,GType type)237 gda_handler_type_accepts_g_type (GdaDataHandler *iface, GType type)
238 {
239 g_assert (iface);
240 return type == G_TYPE_GTYPE ? TRUE : FALSE;
241 }
242
243 static const gchar *
gda_handler_type_get_descr(GdaDataHandler * iface)244 gda_handler_type_get_descr (GdaDataHandler *iface)
245 {
246 g_return_val_if_fail (GDA_IS_HANDLER_TYPE (iface), NULL);
247 return g_object_get_data (G_OBJECT (iface), "descr");
248 }
249