1 /*
2 * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
3 * Copyright (C) 2010 David King <davidk@openismus.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include <glib/gi18n-lib.h>
22 #include <string.h>
23 #include "gda-vprovider-hub.h"
24 #include "gda-vconnection-hub.h"
25
26 struct _GdaVproviderHubPrivate {
27 int foo;
28 };
29
30 /* properties */
31 enum
32 {
33 PROP_0,
34 };
35
36 static void gda_vprovider_hub_class_init (GdaVproviderHubClass *klass);
37 static void gda_vprovider_hub_init (GdaVproviderHub *prov, GdaVproviderHubClass *klass);
38 static void gda_vprovider_hub_finalize (GObject *object);
39 static void gda_vprovider_hub_set_property (GObject *object,
40 guint param_id,
41 const GValue *value,
42 GParamSpec *pspec);
43 static void gda_vprovider_hub_get_property (GObject *object,
44 guint param_id,
45 GValue *value,
46 GParamSpec *pspec);
47 static GObjectClass *parent_class = NULL;
48
49 static GdaConnection *gda_vprovider_hub_create_connection (GdaServerProvider *provider);
50 static gboolean gda_vprovider_hub_open_connection (GdaServerProvider *provider, GdaConnection *cnc,
51 GdaQuarkList *params, GdaQuarkList *auth,
52 guint *task_id, GdaServerProviderAsyncCallback async_cb,
53 gpointer cb_data);
54 static gboolean gda_vprovider_hub_close_connection (GdaServerProvider *provider, GdaConnection *cnc);
55
56
57 /*
58 * GdaVproviderHub class implementation
59 */
60 static void
gda_vprovider_hub_class_init(GdaVproviderHubClass * klass)61 gda_vprovider_hub_class_init (GdaVproviderHubClass *klass)
62 {
63 GObjectClass *object_class = G_OBJECT_CLASS (klass);
64 GdaServerProviderClass *server_class = GDA_SERVER_PROVIDER_CLASS (klass);
65
66 parent_class = g_type_class_peek_parent (klass);
67
68 object_class->finalize = gda_vprovider_hub_finalize;
69 server_class->create_connection = gda_vprovider_hub_create_connection;
70 server_class->open_connection = gda_vprovider_hub_open_connection;
71 server_class->close_connection = gda_vprovider_hub_close_connection;
72
73 /* Properties */
74 object_class->set_property = gda_vprovider_hub_set_property;
75 object_class->get_property = gda_vprovider_hub_get_property;
76 }
77
78 static void
gda_vprovider_hub_init(GdaVproviderHub * prov,G_GNUC_UNUSED GdaVproviderHubClass * klass)79 gda_vprovider_hub_init (GdaVproviderHub *prov, G_GNUC_UNUSED GdaVproviderHubClass *klass)
80 {
81 prov->priv = g_new (GdaVproviderHubPrivate, 1);
82 }
83
84 static void
gda_vprovider_hub_finalize(GObject * object)85 gda_vprovider_hub_finalize (GObject *object)
86 {
87 GdaVproviderHub *prov = (GdaVproviderHub *) object;
88
89 g_return_if_fail (GDA_IS_VPROVIDER_HUB (prov));
90
91 /* free memory */
92 g_free (prov->priv);
93 prov->priv = NULL;
94
95 /* chain to parent class */
96 parent_class->finalize (object);
97 }
98
99 GType
gda_vprovider_hub_get_type(void)100 gda_vprovider_hub_get_type (void)
101 {
102 static GType type = 0;
103
104 if (G_UNLIKELY (type == 0)) {
105 static GMutex registering;
106 if (type == 0) {
107 static GTypeInfo info = {
108 sizeof (GdaVproviderHubClass),
109 (GBaseInitFunc) NULL,
110 (GBaseFinalizeFunc) NULL,
111 (GClassInitFunc) gda_vprovider_hub_class_init,
112 NULL, NULL,
113 sizeof (GdaVproviderHub),
114 0,
115 (GInstanceInitFunc) gda_vprovider_hub_init,
116 0
117 };
118
119 g_mutex_lock (®istering);
120 if (type == 0)
121 type = g_type_register_static (GDA_TYPE_VPROVIDER_DATA_MODEL, "GdaVproviderHub", &info, 0);
122 g_mutex_unlock (®istering);
123 }
124 }
125
126 return type;
127 }
128
129 static void
gda_vprovider_hub_set_property(GObject * object,guint param_id,G_GNUC_UNUSED const GValue * value,GParamSpec * pspec)130 gda_vprovider_hub_set_property (GObject *object,
131 guint param_id,
132 G_GNUC_UNUSED const GValue *value,
133 GParamSpec *pspec)
134 {
135 GdaVproviderHub *prov;
136
137 prov = GDA_VPROVIDER_HUB (object);
138 if (prov->priv) {
139 switch (param_id) {
140 default:
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
142 break;
143 }
144 }
145 }
146
147 static void
gda_vprovider_hub_get_property(GObject * object,guint param_id,G_GNUC_UNUSED GValue * value,GParamSpec * pspec)148 gda_vprovider_hub_get_property (GObject *object,
149 guint param_id,
150 G_GNUC_UNUSED GValue *value,
151 GParamSpec *pspec)
152 {
153 GdaVproviderHub *prov;
154
155 prov = GDA_VPROVIDER_HUB (object);
156 if (prov->priv) {
157 switch (param_id) {
158 default:
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
160 break;
161 }
162 }
163 }
164
165
166 /**
167 * gda_vprovider_hub_new
168 *
169 * Creates a new GdaVirtualProvider object which allows one to
170 * add and remove GdaDataModel objects as tables within a connection
171 *
172 * Returns: a new #GdaVirtualProvider object.
173 */
174 GdaVirtualProvider *
gda_vprovider_hub_new(void)175 gda_vprovider_hub_new (void)
176 {
177 GdaVirtualProvider *provider;
178
179 provider = g_object_new (gda_vprovider_hub_get_type (), NULL);
180 return provider;
181 }
182
183 static GdaConnection *
gda_vprovider_hub_create_connection(GdaServerProvider * provider)184 gda_vprovider_hub_create_connection (GdaServerProvider *provider)
185 {
186 GdaConnection *cnc;
187 g_return_val_if_fail (GDA_IS_VPROVIDER_HUB (provider), NULL);
188
189 cnc = g_object_new (GDA_TYPE_VCONNECTION_HUB, "provider", provider, NULL);
190
191 return cnc;
192 }
193
194 static gboolean
gda_vprovider_hub_open_connection(GdaServerProvider * provider,GdaConnection * cnc,GdaQuarkList * params,GdaQuarkList * auth,guint * task_id,GdaServerProviderAsyncCallback async_cb,gpointer cb_data)195 gda_vprovider_hub_open_connection (GdaServerProvider *provider, GdaConnection *cnc,
196 GdaQuarkList *params, GdaQuarkList *auth,
197 guint *task_id, GdaServerProviderAsyncCallback async_cb, gpointer cb_data)
198 {
199 /* nothing to do here */
200 return GDA_SERVER_PROVIDER_CLASS (parent_class)->open_connection (GDA_SERVER_PROVIDER (provider),
201 cnc, params, auth,
202 task_id, async_cb, cb_data);
203 }
204
205 static void
cnc_close_foreach_func(GdaConnection * cnc,G_GNUC_UNUSED const gchar * ns,GdaVconnectionHub * hub)206 cnc_close_foreach_func (GdaConnection *cnc, G_GNUC_UNUSED const gchar *ns, GdaVconnectionHub *hub)
207 {
208 /*g_print ("---- FOREACH: Removing connection %p ('%s') from HUB\n", cnc, ns);*/
209 if (! gda_vconnection_hub_remove (hub, cnc, NULL))
210 g_warning ("Internal GdaVproviderHub error");
211 }
212
213 static gboolean
gda_vprovider_hub_close_connection(GdaServerProvider * provider,GdaConnection * cnc)214 gda_vprovider_hub_close_connection (GdaServerProvider *provider, GdaConnection *cnc)
215 {
216 g_return_val_if_fail (GDA_IS_VPROVIDER_HUB (provider), FALSE);
217 g_return_val_if_fail (GDA_IS_VCONNECTION_HUB (cnc), FALSE);
218
219 gda_vconnection_hub_foreach (GDA_VCONNECTION_HUB (cnc),
220 (GdaVConnectionHubFunc) cnc_close_foreach_func, cnc);
221
222 return GDA_SERVER_PROVIDER_CLASS (parent_class)->close_connection (GDA_SERVER_PROVIDER (provider), cnc);
223 }
224