00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00036 #include <dlfcn.h>
00037 #include <string.h>
00038
00039 #include <glib.h>
00040 #include <gmodule.h>
00041 #include "dui-initdb.h"
00042 #include "dui-initdb-p.h"
00043 #include "perr.h"
00044
00045
00046
00047 static GList *db_provider_list = NULL;
00048
00049 void
00050 dui_db_provider_register (DuiDBPlugin *plg)
00051 {
00052 db_provider_list = g_list_prepend (db_provider_list, plg);
00053 }
00054
00055 static void
00056 load_driver (const gchar *libname, const gchar * driver, const gchar * name)
00057 {
00058 gchar *fullpath;
00059 typedef void (*backend_init) (void);
00060 GModule *backend;
00061 backend_init gmod_init;
00062 gpointer g;
00063
00064 g_return_if_fail (g_module_supported ());
00065 fullpath = g_module_build_path (PKGLIBDIR, libname);
00066 backend = g_module_open (fullpath, G_MODULE_BIND_LAZY);
00067 if (!backend)
00068 {
00069 PERR (" No %s backend found. %s", name, g_module_error ());
00070 return;
00071 }
00072 g = &gmod_init;
00073 if (!g_module_symbol (backend, driver, g))
00074 {
00075 PERR (" %s backend did not initialise. %s", name, g_module_error ());
00076 return;
00077 }
00078 g_module_make_resident (backend);
00079 gmod_init ();
00080 g_free (fullpath);
00081 }
00082
00083 void
00084 dui_db_init (void)
00085 {
00086 }
00087
00088
00089
00090 static inline DuiDBPlugin *
00091 find_provider (GList *list, const char * provider)
00092 {
00093 GList *node;
00094 for (node=list; node; node=node->next)
00095 {
00096 DuiDBPlugin *cand = node->data;
00097 if (!strcmp (provider, cand->db_provider_name))
00098 {
00099 return cand;
00100 }
00101 }
00102 return NULL;
00103 }
00104
00105 DuiDBConnection *
00106 dui_connection_new (const char * provider,
00107 const char * dbname,
00108 const char * username,
00109 const char * authentication_token)
00110 {
00111 DuiDBPlugin *plg = NULL;
00112 DuiDBConnection *dbc = NULL;
00113
00114 if (!provider) return NULL;
00115
00116 plg = find_provider (db_provider_list, provider);
00117 if (!plg)
00118 {
00119 if (!strcmp (provider, "odbc"))
00120 {
00121 load_driver ("libdwi-db-odbc.so", "dui_odbc_init", "ODBC");
00122 }
00123 else
00124 if (!strcmp (provider, "libpg"))
00125 {
00126 load_driver ("libdwi-db-libpg.so", "dui_libpg_init", "Postgres");
00127 }
00128 else
00129 if (!strcmp (provider, "libdbi"))
00130 {
00131 load_driver ("libdwi-db-libdbi.so", "dui_libdbi_init", "DBI");
00132 }
00133
00134 plg = find_provider (db_provider_list, provider);
00135 }
00136 if (!plg)
00137 {
00138 PERR ("Can't find database provider \"%s\" for database \"%s\"",
00139 provider, dbname);
00140 return NULL;
00141 }
00142
00143 if (!plg->connection_new) return NULL;
00144
00145 dbc = (plg->connection_new) (dbname, username, authentication_token);
00146
00147 if (!dbc) return NULL;
00148 dbc->provider = plg;
00149 return dbc;
00150 }
00151
00152
00153
00154 void
00155 dui_connection_free (DuiDBConnection *conn)
00156 {
00157 DuiDBPlugin *plg;
00158 if (!conn) return;
00159
00160 plg = conn->provider;
00161 if (!plg) return;
00162
00163
00164 if (plg->connection_free)
00165 {
00166 (plg->connection_free) (conn);
00167 }
00168 }
00169
00170
00171
00172 int
00173 dui_connection_catch_error (DuiDBConnection *conn, char ** ret_str)
00174 {
00175 return 0;
00176 }
00177
00178
00179
00180 struct timespec
00181 dui_connection_get_now (DuiDBConnection *conn)
00182 {
00183 DuiDBPlugin *plg;
00184 struct timespec ts = {0,-1};
00185 if (!conn) return ts;
00186
00187 plg = conn->provider;
00188 if (!plg) return ts;
00189 if (!plg->get_now) return ts;
00190
00191 ts = (plg->get_now) (conn);
00192 return ts;
00193 }
00194
00195
00196
00197 void
00198 dui_connection_lock (DuiDBConnection *conn, const char * tablename)
00199 {
00200 DuiDBPlugin *plg;
00201
00202 if (!conn) return;
00203 plg = conn->provider;
00204 if (!plg) return;
00205 if (!plg->lock) return;
00206
00207 (plg->lock) (conn, tablename);
00208 }
00209
00210
00211
00212 void
00213 dui_connection_unlock (DuiDBConnection *conn, const char * tablename)
00214 {
00215 DuiDBPlugin *plg;
00216 if (!conn) return;
00217
00218 plg = conn->provider;
00219 if (!plg) return;
00220 if (!plg->unlock) return;
00221
00222 (plg->unlock) (conn, tablename);
00223 }
00224
00225
00226
00227 DuiDBRecordSet *
00228 dui_connection_exec (DuiDBConnection *conn, const char * buff)
00229 {
00230 DuiDBRecordSet *rs = NULL;
00231 DuiDBPlugin *plg;
00232
00233 if (!conn) return NULL;
00234
00235 plg = conn->provider;
00236 if (!plg) return NULL;
00237
00238 if (plg->connection_exec)
00239 {
00240 rs = (plg->connection_exec) (conn, buff);
00241 }
00242 if (!rs) return NULL;
00243 rs->provider = plg;
00244
00245 return rs;
00246 }
00247
00248
00249
00250 DuiDBRecordSet *
00251 dui_connection_tables (DuiDBConnection *conn)
00252 {
00253 DuiDBRecordSet *rs = NULL;
00254 DuiDBPlugin *plg;
00255
00256 if (!conn) return NULL;
00257
00258 plg = conn->provider;
00259 if (!plg) return NULL;
00260
00261 if (plg->connection_tables)
00262 {
00263 rs = (plg->connection_tables) (conn);
00264 }
00265 if (!rs) return NULL;
00266 rs->provider = plg;
00267
00268 return rs;
00269 }
00270
00271
00272
00273 DuiDBRecordSet *
00274 dui_connection_table_columns (DuiDBConnection *conn, const char * tablename)
00275 {
00276 DuiDBRecordSet *rs = NULL;
00277 DuiDBPlugin *plg;
00278
00279 if (!conn) return NULL;
00280
00281 plg = conn->provider;
00282 if (!plg) return NULL;
00283
00284 if (plg->connection_table_columns)
00285 {
00286 rs = (plg->connection_table_columns) (conn, tablename);
00287 }
00288 if (!rs) return NULL;
00289 rs->provider = plg;
00290
00291 return rs;
00292 }
00293
00294
00295
00296 void
00297 dui_recordset_free (DuiDBRecordSet *rs)
00298 {
00299 DuiDBPlugin *plg;
00300
00301 if (!rs) return;
00302
00303 plg = rs->provider;
00304 if (!plg) return;
00305
00306 if (plg->recordset_free)
00307 {
00308 (plg->recordset_free) (rs);
00309 }
00310 }
00311
00312
00313
00314 int
00315 dui_recordset_rewind (DuiDBRecordSet *rs)
00316 {
00317 DuiDBPlugin *plg;
00318
00319 if (!rs) return 0;
00320
00321 plg = rs->provider;
00322 if (!plg) return 0;
00323
00324 if (plg->recordset_rewind)
00325 {
00326 return (plg->recordset_rewind) (rs);
00327 }
00328 return 0;
00329 }
00330
00331
00332
00333 int
00334 dui_recordset_fetch_row (DuiDBRecordSet *rs)
00335 {
00336 DuiDBPlugin *plg;
00337
00338 if (!rs) return 0;
00339
00340 plg = rs->provider;
00341 if (!plg) return 0;
00342
00343 if (plg->recordset_fetch_row)
00344 {
00345 return (plg->recordset_fetch_row) (rs);
00346 }
00347 return 0;
00348 }
00349
00350
00351
00352 const char *
00353 dui_recordset_get_value (DuiDBRecordSet *rs, const char * fieldname)
00354 {
00355 DuiDBPlugin *plg;
00356
00357 if (!rs) return NULL;
00358
00359 plg = rs->provider;
00360 if (!plg) return NULL;
00361
00362 if (plg->recordset_get_value)
00363 {
00364 return (plg->recordset_get_value) (rs, fieldname);
00365 }
00366 return NULL;
00367 }
00368
00369
00370
00371 int
00372 dui_recordset_catch_error (DuiDBRecordSet *rs, char ** ret_str)
00373 {
00374 DuiDBPlugin *plg;
00375
00376 if (!rs) return 0;
00377
00378 plg = rs->provider;
00379 if (!plg) return 0;
00380
00381 if (plg->recordset_get_error)
00382 {
00383 return (plg->recordset_get_error) (rs, ret_str);
00384 }
00385 return 0;
00386 }
00387
00390