From: <svn...@op...> - 2009-04-05 17:12:25
|
Author: bricks Date: Sun Apr 5 19:12:15 2009 New Revision: 5541 URL: http://www.opensync.org/changeset/5541 Log: specified more functions for new db interface also started to implement first functions Added: branches/osyncdb2/opensync/db2/opensync_db.c branches/osyncdb2/opensync/db2/opensync_db_env.c Modified: branches/osyncdb2/opensync/db2/opensync_db.h branches/osyncdb2/opensync/db2/opensync_db_connection.h branches/osyncdb2/opensync/db2/opensync_db_connection_private.h branches/osyncdb2/opensync/db2/opensync_db_env.h branches/osyncdb2/opensync/db2/opensync_db_env_private.h branches/osyncdb2/opensync/db2/opensync_db_private.h branches/osyncdb2/opensync/module/opensync_module.c Added: branches/osyncdb2/opensync/db2/opensync_db.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ branches/osyncdb2/opensync/db2/opensync_db.c Sun Apr 5 19:12:15 2009 (r5541) @@ -0,0 +1,128 @@ +/* + * libopensync - A synchronization framework + * Copyright (C) 2009 Bjoern Ricks <bjo...@gm...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "opensync_db.h" +#include "opensync_db_private.h" + +OSyncDB *osync_db_new(OSyncError **error) { + OSyncDB *db = NULL; + osync_trace(TRACE_ENTRY, "%s(%p)", __func__, error); + + env = osync_try_malloc0(sizeof(OSyncDB), error); + if (!env) { + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return NULL; + } + + db->ref_count = 1; + osync_trace(TRACE_EXIT, "%s: %p", __func__, db); + return db; +} + +OSyncDB *osync_db_ref(OSyncDB *db) { + osync_assert(db); + + g_atomic_int_inc(&(db->ref_count)); + + return db; +} + +void osync_db_unref(OSyncDB *db) { + osync_assert(db); + + if (g_atomic_int_dec_and_test(&(db->ref_count))) { + if (db->name) { + osync_free(db->name); + } + osync_free(db); + } +} + +void osync_db_free_list(OSyncList *queryresult) { + OSyncList *row; + osync_trace(TRACE_ENTRY, "%s(%p)", __func__, queryresult); + + for (row = queryresult; row; row = row->next) { + osync_list_foreach((OSyncList *) row->data, (GFunc) osync_free, NULL); + osync_list_free((OSyncList *) row->data); + } + + osync_list_free(list); + + osync_trace(TRACE_EXIT, "%s", __func__); +} + +void osync_db_set_name(OSyncDB *db, const char *name) { + osync_assert(db); + if (db->name) { + osync_free(db->name); + } + db->name = osync_strdup(name); +} + +const char *osync_db_get_name(OSyncDB *db) { + osync_assert(db); + return db->name; +} + +void osync_db_set_initialize_func(OSyncDB *db, OSyncDBInitializeFn init_func) { + osync_assert(db); + db->initialize = init_func; +} + +void osync_db_set_finalize_func(OSyncDB *db, OSyncDBFinalizeFn fin_func) { + osync_assert(db); + db->finalize = fin_func; +} + +void osync_db_set_connect_func(OSyncDB *db, OSyncDBConnectFn connect_func) { + osync_assert(db); + db->connect = connect_func; +} + +void osync_db_set_userdata(OSyncDB *db, void *userdata) { + osync_assert(db); + db->userdata = userdata; +} + +void *osync_db_get_userdata(OSyncDB *db) { + osync_assert(db); + return db->userdata; +} + +void osync_db_initialize(OSyncDB *db, OSyncError **error) { + osync_assert(db); + if ( db->initialize ) { + void *userdata = db->initialize(db, error); + osync_db_set_userdata(db, userdata); + } +} + +void osync_db_finalize(OSyncDB *db) { + osync_assert(db); + if ( db->finialize ) { + db->finalize(db, osync_db_get_userdata(db)); + } +} + +OSyncDBConnection* osync_db_connect(OSyncDB *db, const char *dbname, const char *username, const *password, OSyncError **error) { + osync_assert(db); + return db->connect(db, dbname, username, password, osync_db_get_userdata(db), error); +} Modified: branches/osyncdb2/opensync/db2/opensync_db.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -33,10 +33,22 @@ OSYNC_EXPORT void osync_db_unref(OSyncDB *db); -OSYNC_EXPORT void osync_db_set_initialize(OSyncDB *db, OSyncDBInitializeFn); +OSYNC_EXPORT void osync_db_free_list(OSyncList *query_result); -OSYNC_EXPORT void osync_db_set_finalize(OSyncDB *db, OSyncDBFinalizeFn); +OSYNC_EXPORT const char *osync_db_get_name(OSyncDB *db); -OSYNC_EXPORT void osync_db_set_connect(OSyncDB *db, OSyncDBConnectFn); +OSYNC_EXPORT void osync_db_set_name(OSyncDB *db); + +OSYNC_EXPORT void osync_db_set_initialize_func(OSyncDB *db, OSyncDBInitializeFn init_func); + +OSYNC_EXPORT void osync_db_set_finalize_func(OSyncDB *db, OSyncDBFinalizeFn fin_func); + +OSYNC_EXPORT void osync_db_set_connect_func(OSyncDB *db, OSyncDBConnectFn connect_func); + +OSYNC_EXPORT void osync_db_initialize(OSyncDB *db); + +OSYNC_EXPORT void osync_db_finalize(OSyncDB *db); + +OSYNC_EXPOT OSyncDBConnection* osync_db_connect(OSyncDB *db, const char *dbname, const char *username, const *password, OSyncError **error); #endif /* _OPENSYNC_DB_H_ */ Modified: branches/osyncdb2/opensync/db2/opensync_db_connection.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db_connection.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db_connection.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -23,13 +23,7 @@ typedef void (* OSyncDBConnectionDisconnectFn) (OSyncDBConnection *dbcon, void *data, OSyncError **error); -typedef void * (* OSyncDBConnectionQueryFn) (OSyncDBConnection *dbcon, const char *query, void *data, OSyncError **error); /*TODO return value type ???*/ - -typedef struct OSyncDBConnectionFunctions { - OSyncDBConnectionQueryFn query; - OSyncDBConnectionDisconnectFn disconnect; - /*TODO to be continued */ -} OSyncDBConnectionFunctions; +typedef OSyncList * (* OSyncDBConnectionQueryFn) (OSyncDBConnection *dbcon, const char *query, void *data, OSyncError **error); OSYNC_EXPORT OSyncDBConnection *osync_db_connection_new(OSyncDB *db, OSyncError *error); @@ -37,6 +31,14 @@ OSYNC_EXPORT void osync_db_connection_unref(OSyncDBConnection *dbcon); -OSYNC_EXPORT void osync_db_connection_set_functions(OSyncDBConnection *dbcon, OSyncDBConnectionFunctions functions. void *data); +OSYNC_EXPORT OSyncList *osync_db_connection_query(OSyncDBConnection *dbcon, const char *query, OSyncError **error); + +OSYNC_EXPORT void osync_db_connection_disconnect(OSyncDBConnection *dbcon, OSyncError **error); + +OSYNC_EXPORT void osync_db_connection_set_disconnect_func(OSyncDBConnection *dbcon, OSyncDBConnectionDisconnectFn disconnect_func); + +OSYNC_EXPORT void osync_db_connection_set_query_func(OSyncDBConnection *dbcon, OSyncDBConnectionQueryFn query_func); + +OSYNC_EXPORT void osync_db_connection_set_userdata(OSyncDBConnection *dbcon, void *userdata); #endif /* _OPENSYNC_DB_CONNECTION_H_ */ Modified: branches/osyncdb2/opensync/db2/opensync_db_connection_private.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db_connection_private.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db_connection_private.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -23,7 +23,9 @@ typedef struct OSyncDBConnection { OSyncDB *db; - OSyncDBConnectionFunctions functions; + OSyncDBConnectionQueryFn query; + OSyncDBConnectionDisconnectFn disconnect; + /*TODO to be continued */ void *data; } OSyncDBConnection; Added: branches/osyncdb2/opensync/db2/opensync_db_env.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ branches/osyncdb2/opensync/db2/opensync_db_env.c Sun Apr 5 19:12:15 2009 (r5541) @@ -0,0 +1,202 @@ +/* + * libopensync - A synchronization framework + * Copyright (C) 2009 Bjoern Ricks <bjo...@gm...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "opensync_db_env.h" +#include "opensync_db_env_private.h" + +OSyncDBEnv *osync_db_env_new(OSyncError **error) +{ + OSyncDBEnv *env = NULL; + osync_trace(TRACE_ENTRY, "%s(%p)", __func__, error); + + env = osync_try_malloc0(sizeof(OSyncDBEnv), error); + if (!env) { + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return NULL; + } + + env->ref_count = 1; + osync_trace(TRACE_EXIT, "%s: %p", __func__, env); + return env; +} + +void osync_db_env_unref(OSyncDBEnv *env) { + + osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); + osync_assert(env); + + if (g_atomic_int_dec_and_test(&(env->ref_count))) { + /* Free the plugins */ + while (env->plugins) { + osync_db_unref(env->plugins->data); + env->dbs = osync_list_remove(env->dbs, env->dbs->data); + } + + /* Unload all modules */ + while (env->modules) { + osync_module_unload(env->modules->data); + osync_module_unref(env->modules->data); + env->modules = osync_list_remove(env->modules, env->modules->data); + } + osync_free(env); + } + + osync_trace(TRACE_EXIT, "%s", __func__); +} + +OSyncDBEnv *osync_db_env_ref(OSyncDBEnv *env) { + osync_assert(env); + + g_atomic_int_inc(&(env->ref_count)); + + return env; +} + +osync_bool osync_db_env_load(OSyncDBEnv *env, const char *path, OSyncError **error) { + osync_bool must_exist = TRUE; + GDir *dir = NULL; + GError *gerror = NULL; + char *filename = NULL; + const gchar *de = NULL; + + osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, env, __NULLSTR(path), error); + + if (!path) { + path = OPENSYNC_DBPLUGINDIR; + must_exist = FALSE; + } + + //Load all available shared libraries + if (!g_file_test(path, G_FILE_TEST_IS_DIR)) { + if (must_exist) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable"); + goto error; + } else { + osync_trace(TRACE_EXIT, "%s: Directory %s does not exist (non-fatal)", __func__, path); + return TRUE; + } + } + + dir = g_dir_open(path, 0, &gerror); + if (!dir) { + osync_error_set(error, OSYNC_ERROR_IO_ERROR, "Unable to open directory %s: %s", path, gerror->message); + g_error_free(gerror); + goto error; + } + + while ((de = g_dir_read_name(dir))) { + filename = osync_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR, de); + + if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR) || !g_pattern_match_simple("*."G_MODULE_SUFFIX, filename)) { + osync_free(filename); + continue; + } + + if (!osync_db_env_load_module(env, filename, error)) { + osync_trace(TRACE_ERROR, "Unable to load module: %s", osync_error_print(error)); + } + + osync_free(filename); + } + + g_dir_close(dir); + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + +osync_bool osync_db_env_load_module(OSyncDBEnv *env, const char *filename, OSyncError **error) { + OSyncModule *module = NULL; + + osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, env, filename, error); + osync_assert(env); + osync_assert(filename); + + module = osync_module_new(error); + if (!module) + goto error; + + if (!osync_module_load(module, filename, error)) { + osync_trace(TRACE_INTERNAL, "Unable to load module %s: %s", filename, osync_error_print(error)); + osync_module_unref(module); + } else { + if (!osync_module_check(module, error)) { + if (osync_error_is_set(error)) { + osync_trace(TRACE_INTERNAL, "Module check error for %s: %s", filename, osync_error_print(error)); + } + osync_module_unload(module); + osync_module_unref(module); + osync_trace(TRACE_EXIT, "%s: Unable to load module", __func__); + return FALSE; + } + + if (!osync_module_get_db_info(module, env, error)) { + if (osync_error_is_set(error)) + goto error_free_module; + + osync_module_unload(module); + osync_module_unref(module); + osync_trace(TRACE_EXIT, "%s: No get_db_info function", __func__); + return FALSE; + } + env->modules = osync_list_append(env->modules, module); + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error_free_module: + osync_module_unload(module); + osync_module_unref(module); +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + +void osync_db_env_register_db(OSyncPluginEnv *env, OSyncDB *db) { + osync_assert(env); + osync_assert(db); + + if ( db->connect ) { + env->dbs = osync_list_append(env->dbs, dbs); + osync_db_ref(db); + } + else { + /* TODO raise error! */ + } +} + +OSyncDB *osync_db_env_find_db(OSyncDBEnv *env, const char *name) { + OSyncList *db = NULL; + for (db = env->dbs; db; db = db->next) { + OSyncDB *osyncdb = (OSyncDB*)db->data; + if (g_ascii_strcasecmp(osync_db_get_name(osyncdb), name) == 0) + return osyncdb; + } + return NULL; +} + +OSyncList *osync_db_env_get_dbs(OSyncDBEnv *env) { + return osync_list_copy(env->dbs); +} Modified: branches/osyncdb2/opensync/db2/opensync_db_env.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db_env.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db_env.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -33,4 +33,6 @@ OSYNC_EXPORT OSyncList *osync_db_env_get_dbs(OSyncDBEnv *env); +OSYNC_EXPORT void osync_db_env_register_db(OSyncPluginEnv *env, OSyncDB *db) + #endif /* _OPENSYNC_DB_ENV_H_ */ Modified: branches/osyncdb2/opensync/db2/opensync_db_env_private.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db_env_private.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db_env_private.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -22,7 +22,9 @@ #define _OSYNC_DB_ENV_PRIVATE_H_ typedef struct OSyncDBEnv { - + OSyncList *dbs; + OSyncList *modules; + int ref_count; } OSyncDBEnv; #endif /* _OSYNC_DB_ENV_PRIVATE_H_ */ Modified: branches/osyncdb2/opensync/db2/opensync_db_private.h ============================================================================== --- branches/osyncdb2/opensync/db2/opensync_db_private.h Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/db2/opensync_db_private.h Sun Apr 5 19:12:15 2009 (r5541) @@ -13,7 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, wri te to the Free Software + * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ @@ -23,10 +23,14 @@ typedef struct OSyncDB { char *name; - GList connections; + void *userdata; + OSyncList connections; OSyncDBInitializeFn initialize; OSyncDBConnectFn connect; OSyncDBFinalizeFn finalize; + int ref_count; } OSyncDB; +void osync_db_set_data(OSyncDB *db, void *userdata); + #endif /* _OPENSYNC_DB_PRIVATE_H_ */ Modified: branches/osyncdb2/opensync/module/opensync_module.c ============================================================================== --- branches/osyncdb2/opensync/module/opensync_module.c Sun Apr 5 16:14:48 2009 (r5540) +++ branches/osyncdb2/opensync/module/opensync_module.c Sun Apr 5 19:12:15 2009 (r5541) @@ -26,6 +26,7 @@ typedef osync_bool (* fkt_b_plugenv_error)(OSyncPluginEnv *env, OSyncError **error); typedef osync_bool (* fkt_b_fmtenv_error)(OSyncFormatEnv *env, OSyncError **error); +typedef osync_bool (* fkt_b_dbenv_error)(OSyncDBEnv *env, OSyncError **error); OSyncModule *osync_module_new(OSyncError **error) { @@ -91,6 +92,28 @@ return function; } +osync_bool osync_module_get_db_info(OSyncModule *module, OSyncDBEnv *env, OSyncError **error) { + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, module, env, error); + + /* Load the get_db_info symbol */ + fct_info = (fkt_b_dbenv_error) osync_module_get_function(module, "get_db_info", error); + if (!fct_info) { + osync_trace(TRACE_EXIT_ERROR, "%s: Not get_db_info function", __func__); + return FALSE; + } + + /* Call the get_info function */ + if (!fct_info(env, error)) + goto error; + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + osync_bool osync_module_get_sync_info(OSyncModule *module, OSyncPluginEnv *env, OSyncError **error) { fkt_b_plugenv_error fct_info = NULL; |