database *open_database(const char *dbtype, const char *dbhost, int dbport, const char *dbname, const char *dbuser, const char *dbpasswd) { database *db = calloc(1, sizeof(database)); static initialized = FALSE; strncpy(db->driver, dbtype, sizeof(db->driver)); strncpy(db->host, dbhost, sizeof(db->host)); sprintf(db->port, "%d", dbport); strncpy(db->name, dbname, sizeof(db->name)); strncpy(db->username, dbuser, sizeof(db->username)); strncpy(db->password, dbpasswd, sizeof(db->password)); if (!initialized) { int c = dbi_initialize(NULL); if (c < 0) { LOG(LOG_ERR, "Unable to initialize libdbi! Make sure you specified a valid driver directory.\n"); dbi_shutdown(); return NULL; } else if (c == 0) { LOG(LOG_ERR, "Initialized libdbi, but no drivers were found!\n"); dbi_shutdown(); return NULL; } } initialized = TRUE; return(db); }
static char query_handler(char *query_name, char *myq) { if(myq == NULL) { myq = "SELECT NULL"; } if(query_name == NULL) { query_name = "noname"; } if(query_name == "") { query_name = "noname"; } dbi_initialize(NULL); conn = dbi_conn_new("mysql"); dbi_conn_set_option(conn, "host", getenv("DB_HOSTNAME")); dbi_conn_set_option(conn, "username", getenv("DB_USERNAME")); dbi_conn_set_option(conn, "password", getenv("DB_PASSWORD")); dbi_conn_set_option(conn, "dbname", getenv("DB_DATABASE")); dbi_conn_set_option(conn, "encoding", "UTF-8"); if (dbi_conn_connect(conn) < 0) { printf("Could not connect. Please check the option settings and if the" \ "specific driver is available\n"); } else { result = dbi_conn_queryf(conn, myq, threshold); if (result) { xmlAddChild(sql_node,query_doc(result,query_name)); dbi_result_free(result); } dbi_conn_close(conn); } return 0; }
IoObject *IoDBI_initWithDriversPath(IoDBI *self, IoObject *locals, IoMessage *m) { /*doc DBI initWithDriversPath Initialize the DBI environment with the specified libdbi driver path. */ IoObject *dir = IoMessage_locals_valueArgAt_(m, locals, 0); if (ISSYMBOL(dir)) { DATA(self)->driverCount = dbi_initialize(CSTRING(dir)); } else { IoState_error_(IOSTATE, m, "argument 0 to method '%s' must be a Symbol, not a '%s'\n", CSTRING(IoMessage_name(m)), IoObject_name(dir)); } if (DATA(self)->driverCount == -1) { IoState_error_(IOSTATE, m, "*** IoDBI error during dbi_initialize\n"); } else { DATA(self)->didInit = 1; } return IONUMBER(DATA(self)->driverCount); }
int main (int argc, char *argv[]) { dbi_conn conn; dbi_result result; const char *err_msg; int err_code; if (argc < 2) { fprintf (stderr, "%s: Need a libdir as argument!\n", argv[0]); return 1; } dbi_initialize (argv[1]); conn = dbi_conn_new ("null"); dbi_conn_connect (conn); /* * Queries */ result = dbi_conn_query (conn, "COMMIT"); assert (result != NULL); dbi_result_free (result); dbi_conn_set_option_numeric (conn, "null.error.commit", 1); result = dbi_conn_query (conn, "COMMIT"); assert (result == NULL); dbi_conn_set_option_numeric (conn, "null.error.commit", 0); result = dbi_conn_query (conn, "COMMIT"); assert (result != NULL); dbi_result_free (result); /* * Normal queries */ result = dbi_conn_query (conn, "SELECT * FROM DUAL"); assert (result != NULL); dbi_result_free (result); dbi_conn_set_option_numeric (conn, "null.error.query", 1); result = dbi_conn_query (conn, "SELECT * FROM DUAL"); assert (result == NULL); dbi_conn_set_option_numeric (conn, "null.error.query", 0); result = dbi_conn_query (conn, "SELECT * FROM DUAL"); assert (result != NULL); dbi_result_free (result); /* * Cleanup */ dbi_conn_close (conn); dbi_shutdown (); return 0; }
static int _sql_setparam(struct sql_table_helper* th,char* key, char* value) { char* dbi_errstr=NULL; dbi_driver driver; /* if not connected */ if (! th->conn) { /* initialize some stuff */ th->table_next=th->table_start; th->result=NULL; th->connected=0; /* initialize db */ if (getenv("RRDDEBUGSQL")) { fprintf(stderr,"RRDDEBUGSQL: %li: initialize libDBI\n",time(NULL) ); } dbi_initialize(NULL); /* load the driver */ driver=dbi_driver_open(th->dbdriver); if (! driver) { rrd_set_error( "libdbi - no such driver: %s (possibly a dynamic link problem of the driver being linked without -ldbi)",th->dbdriver); return -1; } /* and connect to driver */ th->conn=dbi_conn_open(driver); /* and handle errors */ if (! th->conn) { rrd_set_error( "libdbi - could not open connection to driver %s",th->dbdriver); dbi_shutdown(); return -1; } } if (th->connected) { rrd_set_error( "we are already connected - can not set parameter %s=%s",key,value); _sql_close(th); return -1; } if (getenv("RRDDEBUGSQL")) { fprintf(stderr,"RRDDEBUGSQL: %li: setting option %s to %s\n",time(NULL),key,value ); } if (strcmp(key, "port") == 0) { if (dbi_conn_set_option_numeric(th->conn,key,atoi(value))) { dbi_conn_error(th->conn,(const char**)&dbi_errstr); rrd_set_error( "libdbi: problems setting %s to %d - %s",key,value,dbi_errstr); _sql_close(th); return -1; } } else { if (dbi_conn_set_option(th->conn,key,value)) { dbi_conn_error(th->conn,(const char**)&dbi_errstr); rrd_set_error( "libdbi: problems setting %s to %s - %s",key,value,dbi_errstr); _sql_close(th); return -1; } } return 0; }
/* The following function is responsible for initializing a connection */ static rsRetVal initConn(instanceData *pData, int bSilent) { DEFiRet; int iDrvrsLoaded; ASSERT(pData != NULL); ASSERT(pData->conn == NULL); if(bDbiInitialized == 0) { /* we need to init libdbi first */ # ifdef HAVE_DBI_R iDrvrsLoaded = dbi_initialize_r((char*) pData->dbiDrvrDir, &dbiInst); # else iDrvrsLoaded = dbi_initialize((char*) pData->dbiDrvrDir); # endif if(iDrvrsLoaded == 0) { errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi or libdbi drivers not present on this system - suspending."); ABORT_FINALIZE(RS_RET_SUSPENDED); } else if(iDrvrsLoaded < 0) { errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi could not be " "initialized (do you have any dbi drivers installed?) - suspending."); ABORT_FINALIZE(RS_RET_SUSPENDED); } bDbiInitialized = 1; /* we are done for the rest of our existence... */ } # ifdef HAVE_DBI_R pData->conn = dbi_conn_new_r((char*)pData->drvrName, dbiInst); # else pData->conn = dbi_conn_new((char*)pData->drvrName); # endif if(pData->conn == NULL) { errmsg.LogError(0, RS_RET_SUSPENDED, "can not initialize libdbi connection"); ABORT_FINALIZE(RS_RET_SUSPENDED); } else { /* we could get the handle, now on with work... */ /* Connect to database */ dbi_conn_set_option(pData->conn, "host", (char*) pData->host); dbi_conn_set_option(pData->conn, "username", (char*) pData->usrName); dbi_conn_set_option(pData->conn, "dbname", (char*) pData->dbName); if(pData->pwd != NULL) dbi_conn_set_option(pData->conn, "password", (char*) pData->pwd); if(dbi_conn_connect(pData->conn) < 0) { reportDBError(pData, bSilent); closeConn(pData); /* ignore any error we may get */ ABORT_FINALIZE(RS_RET_SUSPENDED); } pData->txSupport = dbi_conn_cap_get(pData->conn, "transaction_support"); } finalize_it: RETiRet; }
IoObject *IoDBI_init(IoDBI *self, IoObject *locals, IoMessage *m) { //doc DBI init Initialize the DBI environment with the default driver path. DATA(self)->driverCount = dbi_initialize(NULL); if (DATA(self)->driverCount == -1) { IoState_error_(IOSTATE, m, "*** IoDBI error during dbi_initialize\n"); } else { DATA(self)->didInit = 1; } return IONUMBER(DATA(self)->driverCount); }
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); } #ifdef HAVE_LIBDBI_R status = dbi_initialize_r (NULL, &inst); #else status = dbi_initialize (NULL); #endif if (status < 0) { ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.", status); return (-1); } else if (status == 0) { ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please " "install at least one `DBD' or check your installation."); return (-1); } DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.", status, (status == 1) ? "" : "s"); return (0); } /* }}} int cdbi_init */
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; }
int main() { dbi_conn conn; dbi_result result; int v; dbi_initialize(NULL); conn = dbi_conn_new("pgsql"); dbi_conn_set_option(conn, "host", "localhost"); dbi_conn_set_option(conn, "username", "mud"); dbi_conn_set_option(conn, "dbname", "mud"); if ( (v = dbi_conn_connect(conn)) < 0 ) { const char* p; dbi_conn_error(conn, &p); printf(" :: %s\n", p); return 0; } else printf("%d\n", v); result = dbi_conn_query(conn, "select * from account"); }
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"); } }
dbi_conn open_database(const char *dbtype, const char *dbhost, const char *dbport, const char *dbname, const char *dbuser, const char *dbpasswd) { dbi_conn conn; dbi_initialize(NULL); conn = dbi_conn_new(dbtype); if ( ! conn ) { LOG(LOG_ERR, "Cannot start a connection with driver %s", dbtype); } dbi_conn_set_option(conn, "host", dbhost); dbi_conn_set_option(conn, "port", dbport); dbi_conn_set_option(conn, "username", dbuser); dbi_conn_set_option(conn, "password", dbpasswd); dbi_conn_set_option(conn, "dbname", dbname); dbi_conn_set_option(conn, "encoding", "UTF-8"); if (dbi_conn_connect(conn) < 0) { const char *errmsg; dbi_conn_error(conn, &errmsg); LOG(LOG_ERR, "%s dbhost=%s,dbport=%d,dbname=%s,dbuser=%s,dbpasswd=%s", errmsg, dbhost, dbport, dbname, dbuser, dbpasswd); return(NULL); } return(conn); }
static apr_status_t init_ftpd_dbi(apr_pool_t * p, apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s) { apr_status_t rv = APR_SUCCESS; int rval; dbi_driver dbi_driver; void *data; apr_hash_index_t *idx; char *key; ftpd_dbi_config *val; apr_ssize_t len; const char *userdata_key = "mod_ftpd_dbi_init"; /* dbi_config *conf = ap_get_module_config(s->module_config, * &ftpd_dbi_module); */ apr_pool_userdata_get(&data, userdata_key, s->process->pool); ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, plog, "[mod_ftpd_dbi.c] init."); if (!data) { apr_pool_userdata_set((const void *) 1, userdata_key, apr_pool_cleanup_null, s->process->pool); return OK; } ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, "[mod_ftpd_dbi.c] Running DBI init Code"); if ((rval = dbi_initialize(dbi_global_config.driverdir)) > 0) { if (dbi_global_config.driverdir == NULL) { ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, plog, "[mod_ftpd_dbi.c] Initialization of libdbi found %d drivers in default driver directory", rval); } else { ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, plog, "[mod_ftpd_dbi.c] Initialization of libdbi found %d drivers in directory %s", rval, dbi_global_config.driverdir); } if (s->loglevel >= APLOG_DEBUG) { dbi_driver = NULL; while ((dbi_driver = dbi_driver_list(dbi_driver)) != NULL) { ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, plog, "[mod_ftpd_dbi.c] Driver '%s' was loaded.", dbi_driver_get_name(dbi_driver)); } } } else { /* An error was returned or libdbi found 0 drivers */ if (dbi_global_config.driverdir == NULL) { ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "[mod_ftpd_dbi.c] - Initlialization of libdbi with default driver directory failed"); } else { ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "[mod_ftpd_dbi.c] - Initlialization of libdbi with FtpDbiDriverDir %s failed", dbi_global_config.driverdir); } return !APR_SUCCESS; } /* loop the hashed config stuff... */ for (idx = apr_hash_first(p, ftpd_dbi_config_hash); idx; idx = apr_hash_next(idx)) { apr_hash_this(idx, (void *) &key, &len, (void *) &val); apr_reslist_create(&val->pool, val->rec.conn_min, /* hard minimum */ val->rec.conn_soft, /* soft maximum */ val->rec.conn_max, /* hard maximum */ val->rec.conn_ttl, /* Time to live -- dbi server might override/disconnect! */ safe_dbi_new_conn, /* Make a New Connection */ safe_dbi_kill_conn, /* Kill Old Connection */ (void *) &val->rec, p); apr_hash_set(ftpd_dbi_config_hash, key, APR_HASH_KEY_STRING, val); } apr_pool_cleanup_register(p, p, kill_dbi, apr_pool_cleanup_null); ap_add_version_component(p, "mod_ftpd_dbi/" MOD_FTPD_DBI_VERSION); return rv; }
int main( int argc, char* argv[] ) { // Parse command line const char* idl_file_name = NULL; const char* query_file_name = NULL; int verbose = 0; // boolean int opt; opterr = 0; const char optstring[] = ":f:i:v"; while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) { switch( opt ) { case 'f' : // get file name of query if( query_file_name ) { fprintf( stderr, "Multiple input files not allowed\n" ); return EXIT_FAILURE; } else query_file_name = optarg; break; case 'i' : // get name of IDL file if( idl_file_name ) { fprintf( stderr, "Multiple IDL file names not allowed\n" ); return EXIT_FAILURE; } else idl_file_name = optarg; break; case 'v' : // Verbose verbose = 1; break; case '?' : // Invalid option fprintf( stderr, "Invalid option '-%c' on command line\n", (char) optopt ); return EXIT_FAILURE; default : // Huh? fprintf( stderr, "Internal error: unexpected value '%c'" "for optopt", (char) optopt ); return EXIT_FAILURE; } } // If the command line doesn't specify an IDL file, get it // from an environmental variable, or apply a default if( NULL == idl_file_name ) { idl_file_name = getenv( "OILS_IDL_FILENAME" ); if( NULL == idl_file_name ) idl_file_name = "/openils/conf/fm_IDL.xml"; } if( verbose ) printf( "IDL file: %s\n", idl_file_name ); char* loaded_json = NULL; const char* json_query = NULL; // Get the JSON query into a string if( query_file_name ) { // Got a file? Load it if( optind < argc ) fprintf( stderr, "Extra parameter(s) ignored\n" ); loaded_json = load_query( query_file_name ); if( !loaded_json ) return EXIT_FAILURE; json_query = loaded_json; } else { // No file? Use command line parameter if ( optind == argc ) { fprintf( stderr, "No JSON query specified\n" ); return EXIT_FAILURE; } else json_query = argv[ optind ]; } if( verbose ) printf( "JSON query: %s\n", json_query ); osrfLogSetLevel( OSRF_LOG_WARNING ); // Suppress informational messages (void) oilsIDLInit( idl_file_name ); // Load IDL into memory // Load a database driver, connect to it, and install the connection in // the cstore module. We don't actually connect to a database, but we // need the driver to process quoted strings correctly. if( dbi_initialize( NULL ) < 0 ) { printf( "Unable to load database driver\n" ); return EXIT_FAILURE; }; dbi_conn conn = dbi_conn_new( "pgsql" ); // change string if ever necessary if( !conn ) { printf( "Unable to establish dbi connection\n" ); dbi_shutdown(); return EXIT_FAILURE; } oilsSetDBConnection( conn ); // The foregoing is an inelegant kludge. The true, proper, and uniquely // correct thing to do is to load the system settings and then call // osrfAppInitialize() and osrfAppChildInit(). Maybe we'll actually // do that some day, but this will do for now. // Translate the JSON into SQL int rc = test_json_query( json_query ); dbi_conn_close( conn ); dbi_shutdown(); if( loaded_json ) free( loaded_json ); return rc ? EXIT_FAILURE : EXIT_SUCCESS; }
/** * 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); }
/* Connects to database */ static GSM_Error SMSDDBI_Connect(GSM_SMSDConfig * Config) { int rc; struct GSM_SMSDdbobj *db = Config->db; rc = dbi_initialize(Config->driverspath); if (rc == 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI did not find any drivers, try using DriversPath option"); dbi_shutdown(); return ERR_DB_DRIVER; } else if (rc < 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to initialize!"); return ERR_DB_DRIVER; } Config->conn.dbi = dbi_conn_new(Config->driver); if (Config->conn.dbi == NULL) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to init %s driver!", Config->driver); dbi_shutdown(); return ERR_DB_DRIVER; } else { SMSD_Log(DEBUG_SQL, Config, "Using DBI driver '%s'", dbi_driver_get_name(dbi_conn_get_driver(Config->conn.dbi))); } dbi_conn_error_handler(Config->conn.dbi, SMSDDBI_Callback, Config); if (dbi_conn_set_option(Config->conn.dbi, "sqlite_dbdir", Config->dbdir) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite_dbdir!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "sqlite3_dbdir", Config->dbdir) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite3_dbdir!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "host", Config->host) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set host!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "username", Config->user) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set username!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "password", Config->password) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set password!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "dbname", Config->database) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set dbname!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_set_option(Config->conn.dbi, "encoding", "UTF-8") != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set encoding!"); SMSDDBI_Free(Config); return ERR_DB_CONFIG; } if (dbi_conn_connect(Config->conn.dbi) != 0) { SMSD_Log(DEBUG_ERROR, Config, "DBI failed to connect!"); SMSDDBI_Free(Config); return ERR_DB_CONNECT; } Config->db = db; return ERR_NONE; }
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 ); */ }