static char dbiw_dbi_init() { static char doneit = 0; if (doneit) return 1; doneit = 1; const int rc = #if USE_DEPRECATED_DBI_API dbi_initialize(NULL) #else dbi_initialize_r(NULL,&DBI_INSTANCE) #endif ; if (0 >= rc) { lerr("Could not initialize any DBI drivers!"); return 0; } atexit(dbiw_atexit); return 1; }
static int cdbi_init (void) /* {{{ */ { static int did_init = 0; int status; if (did_init != 0) return (0); if (queries_num == 0) { ERROR ("dbi plugin: No <Query> blocks have been found. Without them, " "this plugin can't do anything useful, so we will returns an error."); return (-1); } if (databases_num == 0) { ERROR ("dbi plugin: No <Database> blocks have been found. Without them, " "this plugin can't do anything useful, so we will returns an error."); return (-1); } status = dbi_initialize_r (/* driverdir = */ NULL, &dbi_instance); if (status < 0) { ERROR ("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.", status); return (-1); } else if (status == 0) { ERROR ("dbi plugin: `dbi_initialize_r' could not load any drivers. Please " "install at least one `DBD' or check your installation."); return (-1); } DEBUG ("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.", status, (status == 1) ? "" : "s"); return (0); } /* }}} int cdbi_init */
void swd::database::connect(const std::string& driver, const std::string& host, const std::string& port, const std::string& username, const std::string& password, const std::string& name, const std::string& encoding) { #if defined(HAVE_DBI_NEW) dbi_initialize_r(NULL, &instance_); conn_ = dbi_conn_new_r(driver.c_str(), instance_); #else dbi_initialize(NULL); conn_ = dbi_conn_new(driver.c_str()); #endif dbi_conn_set_option(conn_, "host", host.c_str()); dbi_conn_set_option(conn_, "port", port.c_str()); dbi_conn_set_option(conn_, "username", username.c_str()); dbi_conn_set_option(conn_, "password", password.c_str()); dbi_conn_set_option(conn_, "dbname", name.c_str()); dbi_conn_set_option(conn_, "encoding", encoding.c_str()); /* If the initial connection can not be established the process is shut down. */ if (dbi_conn_connect(conn_) < 0) { throw swd::exceptions::core_exception("Can't connect to database server"); } }
int dbi_initialize(const char *driverdir) { return (dbi_initialize_r(driverdir, &dbi_inst_legacy)); }
/** * check if requested driver is provided by local DBI installation * * @param driver_name string with the name of the driver * @return 1 if exists, 0 otherwise */ int tagsistant_driver_is_available(const char *driver_name) { int counter = 0; int driver_found = 0; dbi_driver driver = NULL; dbg('b', LOG_INFO, "Available drivers:"); #if TAGSISTANT_REENTRANT_DBI while ((driver = dbi_driver_list_r(driver, tagsistant.dbi_instance)) != NULL) { #else while ((driver = dbi_driver_list(driver)) != NULL) { #endif counter++; dbg('b', LOG_INFO, " Driver #%d: %s - %s", counter, dbi_driver_get_name(driver), dbi_driver_get_filename(driver)); if (g_strcmp0(dbi_driver_get_name(driver), driver_name) == 0) { driver_found = 1; } } if (!counter) { dbg('b', LOG_ERR, "No SQL driver found! Exiting now."); return(0); } if (!driver_found) { dbg('b', LOG_ERR, "No %s driver found!", driver_name); return(0); } return(1); } /** * Contains DBI parsed options */ struct { int backend; gchar *backend_name; gchar *host; gchar *db; gchar *username; gchar *password; } dboptions; #if TAGSISTANT_ENABLE_TAG_ID_CACHE /** a map tag_name -> tag_id */ GHashTable *tagsistant_tag_cache = NULL; #endif /** regular expressions used to escape query parameters */ GRegex *RX1, *RX2, *RX3; /** * Initialize libDBI structures */ void tagsistant_db_init() { // initialize DBI library #if TAGSISTANT_REENTRANT_DBI dbi_initialize_r(NULL, &(tagsistant.dbi_instance)); #else dbi_initialize(NULL); #endif #if TAGSISTANT_USE_QUERY_MUTEX g_mutex_init(&tagsistant_query_mutex); #endif #if TAGSISTANT_ENABLE_TAG_ID_CACHE tagsistant_tag_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); #endif // by default, DBI backend provides intersect tagsistant.sql_backend_have_intersect = 1; tagsistant.sql_database_driver = TAGSISTANT_NULL_BACKEND; dboptions.backend = TAGSISTANT_NULL_BACKEND; // if no database option has been passed, use default SQLite3 if (strlen(tagsistant.dboptions) == 0) { tagsistant.dboptions = g_strdup("sqlite3::::"); dboptions.backend_name = g_strdup("sqlite3"); dboptions.backend = TAGSISTANT_DBI_SQLITE_BACKEND; dbg('b', LOG_INFO, "Using default driver: sqlite3"); } dbg('b', LOG_INFO, "Database options: %s", tagsistant.dboptions); // split database option value up to 5 tokens gchar **_dboptions = g_strsplit(tagsistant.dboptions, ":", 5); // set failsafe DB options if (_dboptions[0]) { if (strcmp(_dboptions[0], "sqlite3") == 0) { tagsistant.sql_database_driver = TAGSISTANT_DBI_SQLITE_BACKEND; dboptions.backend = TAGSISTANT_DBI_SQLITE_BACKEND; dboptions.backend_name = g_strdup("sqlite3"); } else if (strcmp(_dboptions[0], "mysql") == 0) { tagsistant.sql_database_driver = TAGSISTANT_DBI_MYSQL_BACKEND; dboptions.backend = TAGSISTANT_DBI_MYSQL_BACKEND; dboptions.backend_name = g_strdup("mysql"); } } if (TAGSISTANT_DBI_MYSQL_BACKEND == dboptions.backend) { if (_dboptions[1] && strlen(_dboptions[1])) { dboptions.host = g_strdup(_dboptions[1]); if (_dboptions[2] && strlen(_dboptions[2])) { dboptions.db = g_strdup(_dboptions[2]); if (_dboptions[3] && strlen(_dboptions[3])) { dboptions.username = g_strdup(_dboptions[3]); if (_dboptions[4] && strlen(_dboptions[4])) { dboptions.password = g_strdup(_dboptions[4]); } else { dboptions.password = g_strdup("tagsistant"); } } else { dboptions.password = g_strdup("tagsistant"); dboptions.username = g_strdup("tagsistant"); } } else { dboptions.password = g_strdup("tagsistant"); dboptions.username = g_strdup("tagsistant"); dboptions.db = g_strdup("tagsistant"); } } else { dboptions.password = g_strdup("tagsistant"); dboptions.username = g_strdup("tagsistant"); dboptions.db = g_strdup("tagsistant"); dboptions.host = g_strdup("localhost"); } } g_strfreev(_dboptions); #if 0 dbg('b', LOG_INFO, "Database driver: %s", dboptions.backend_name); // list configured options const char *option = NULL; int counter = 0; dbg('b', LOG_INFO, "Connection settings: "); while ((option = dbi_conn_get_option_list(tagsistant_dbi_conn, option)) != NULL ) { counter++; dbg('b', LOG_INFO, " Option #%d: %s = %s", counter, option, dbi_conn_get_option(tagsistant_dbi_conn, option)); } // tell if backend have INTERSECT if (tagsistant.sql_backend_have_intersect) { dbg('b', LOG_INFO, "Database supports INTERSECT operator"); } else { dbg('b', LOG_INFO, "Database does not support INTERSECT operator"); } #endif /* initialize the regular expressions used to escape the SQL queries */ RX1 = g_regex_new("[\"']", 0, 0, NULL); RX2 = g_regex_new("'", 0, 0, NULL); RX3 = g_regex_new("<><>", 0, 0, NULL); }
void gnc_module_init_backend_dbi (void) { const char* driver_dir; int num_drivers; gboolean have_sqlite3_driver = FALSE; gboolean have_mysql_driver = FALSE; gboolean have_pgsql_driver = FALSE; /* Initialize libdbi and see which drivers are available. Only register qof backends which have drivers available. */ driver_dir = g_getenv ("GNC_DBD_DIR"); if (driver_dir == nullptr) { PINFO ("GNC_DBD_DIR not set: using libdbi built-in default\n"); } /* dbi_initialize returns -1 in case of errors */ #if HAVE_LIBDBI_R if (dbi_instance) return; num_drivers = dbi_initialize_r (driver_dir, &dbi_instance); #else num_drivers = dbi_initialize (driver_dir); #endif if (num_drivers <= 0) { gchar* dir = g_build_filename (gnc_path_get_libdir (), "dbd", nullptr); #if HAVE_LIBDBI_R if (dbi_instance) return; num_drivers = dbi_initialize_r (dir, &dbi_instance); #else num_drivers = dbi_initialize (dir); #endif g_free (dir); } if (num_drivers <= 0) { PWARN ("No DBD drivers found\n"); } else { dbi_driver driver = nullptr; PINFO ("%d DBD drivers found\n", num_drivers); do { #if HAVE_LIBDBI_R driver = dbi_driver_list_r (driver, dbi_instance); #else driver = dbi_driver_list (driver); #endif if (driver != nullptr) { const gchar* name = dbi_driver_get_name (driver); PINFO ("Driver: %s\n", name); if (strcmp (name, "sqlite3") == 0) { have_sqlite3_driver = TRUE; } else if (strcmp (name, "mysql") == 0) { have_mysql_driver = TRUE; } else if (strcmp (name, "pgsql") == 0) { have_pgsql_driver = TRUE; } } } while (driver != nullptr); } if (have_sqlite3_driver) { const char* name = "GnuCash Libdbi (SQLITE3) Backend"; auto prov = QofBackendProvider_ptr(new QofDbiBackendProvider<DbType::DBI_SQLITE>{name, FILE_URI_TYPE}); qof_backend_register_provider(std::move(prov)); prov = QofBackendProvider_ptr(new QofDbiBackendProvider<DbType::DBI_SQLITE>{name, SQLITE3_URI_TYPE}); qof_backend_register_provider(std::move(prov)); } if (have_mysql_driver) { const char *name = "GnuCash Libdbi (MYSQL) Backend"; auto prov = QofBackendProvider_ptr(new QofDbiBackendProvider<DbType::DBI_MYSQL>{name, "mysql"}); qof_backend_register_provider(std::move(prov)); } if (have_pgsql_driver) { const char* name = "GnuCash Libdbi (POSTGRESQL) Backend"; auto prov = QofBackendProvider_ptr(new QofDbiBackendProvider<DbType::DBI_PGSQL>{name, "postgres"}); qof_backend_register_provider(std::move(prov)); } /* If needed, set log level to DEBUG so that SQl statements will be put into the gnucash.trace file. */ /* qof_log_set_level( log_module, QOF_LOG_DEBUG ); */ }
STATUS spacestate_init(void) { configuration *conf = get_modifiable_conf(); GError *error = NULL; struct sigaction sa; dbi_inst dbi_instance = 0; /* Establish SIGCHLD handler. */ sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = child_handler; sigaction(SIGCHLD, &sa, NULL); conf->spacestate_host = g_key_file_get_string(conf->keyfile, "spacestate", "host", &error); if (error) { fprintf(stderr, "No spacestate host supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_name = g_key_file_get_string(conf->keyfile, "spacestate", "name", &error); if (error) { fprintf(stderr, "No spacestate name supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_driver = g_key_file_get_string(conf->keyfile, "spacestate", "driver", &error); if (error) { fprintf(stderr, "No spacestate driver supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_username = g_key_file_get_string(conf->keyfile, "spacestate", "username", &error); if (error) { fprintf(stderr, "No spacestate username supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_password = g_key_file_get_string(conf->keyfile, "spacestate", "password", &error); if (error) { fprintf(stderr, "No spacestate password supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_hook_open = g_key_file_get_string(conf->keyfile, "spacestate", "open script", &error); if (error) { fprintf(stderr, "No spacestate password supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->spacestate_hook_close = g_key_file_get_string(conf->keyfile, "spacestate", "close script", &error); if (error) { fprintf(stderr, "No spacestate password supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1); conf->event_handlers[conf->event_handler_cnt] = spacestate_update; conf->event_handler_cnt++; DEBUG(1, "Setting properties to %s space state database %s at %s as user %s", conf->spacestate_driver, conf->spacestate_name, conf->spacestate_host, conf->spacestate_username); dbi_initialize_r(NULL, &dbi_instance); conn = dbi_conn_new_r(conf->spacestate_driver, &dbi_instance); dbi_conn_set_option(conn, "host", conf->spacestate_host); dbi_conn_set_option(conn, "username", conf->spacestate_username); dbi_conn_set_option(conn, "password", conf->spacestate_password); dbi_conn_set_option(conn, "dbname", conf->spacestate_name); dbi_conn_set_option(conn, "encoding", "UTF-8"); return ST_OK; }