From 41e470ffb6def594739f92963ccf8ebcffc77368 Mon Sep 17 00:00:00 2001 From: scottmk Date: Wed, 28 Apr 2010 21:11:54 +0000 Subject: [PATCH] Create a reusable function for connecting to the database. Use it in qstore (others to follow). M Open-ILS/include/openils/oils_sql.h M Open-ILS/src/c-apps/oils_qstore.c M Open-ILS/src/c-apps/oils_sql.c git-svn-id: svn://svn.open-ils.org/ILS/trunk@16338 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/include/openils/oils_sql.h | 1 + Open-ILS/src/c-apps/oils_qstore.c | 68 ++++++----------------------- Open-ILS/src/c-apps/oils_sql.c | 59 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 54 deletions(-) diff --git a/Open-ILS/include/openils/oils_sql.h b/Open-ILS/include/openils/oils_sql.h index e01426f165..d39a43f4e8 100644 --- a/Open-ILS/include/openils/oils_sql.h +++ b/Open-ILS/include/openils/oils_sql.h @@ -26,6 +26,7 @@ GNU General Public License for more details. extern "C" { #endif +dbi_conn oilsConnectDB( const char* mod_name ); void oilsSetSQLOptions( const char* module_name, int do_pcrud, int flesh_depth ); void oilsSetDBConnection( dbi_conn conn ); int oilsExtendIDL( void ); diff --git a/Open-ILS/src/c-apps/oils_qstore.c b/Open-ILS/src/c-apps/oils_qstore.c index f087aa113e..9329cbfc26 100644 --- a/Open-ILS/src/c-apps/oils_qstore.c +++ b/Open-ILS/src/c-apps/oils_qstore.c @@ -126,63 +126,23 @@ int osrfAppInitialize() { This function is called by a server drone shortly after it is spawned by the listener. */ -int osrfAppChildInit() { +int osrfAppChildInit( void ) { - osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." ); - dbi_initialize( NULL ); - osrfLogDebug( OSRF_LOG_MARK, "... libdbi initialized." ); - - char* driver = osrf_settings_host_value( "/apps/%s/app_settings/driver", modulename ); - char* user = osrf_settings_host_value( "/apps/%s/app_settings/database/user", modulename ); - char* host = osrf_settings_host_value( "/apps/%s/app_settings/database/host", modulename ); - char* port = osrf_settings_host_value( "/apps/%s/app_settings/database/port", modulename ); - char* db = osrf_settings_host_value( "/apps/%s/app_settings/database/db", modulename ); - char* pw = osrf_settings_host_value( "/apps/%s/app_settings/database/pw", modulename ); - - osrfLogDebug( OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver ); - dbhandle = dbi_conn_new( driver ); - - if( !dbhandle ) { - osrfLogError( OSRF_LOG_MARK, "Error loading database driver [%s]", driver ); + dbhandle = oilsConnectDB( modulename ); + if( !dbhandle ) return -1; - } - osrfLogDebug( OSRF_LOG_MARK, "Database driver [%s] seems OK", driver ); - - osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database. host=%s, " - "port=%s, user=%s, db=%s", modulename, host, port, user, db ); - - if( host ) dbi_conn_set_option( dbhandle, "host", host ); - if( port ) dbi_conn_set_option_numeric( dbhandle, "port", atoi( port )); - if( user ) dbi_conn_set_option( dbhandle, "username", user ); - if( pw ) dbi_conn_set_option( dbhandle, "password", pw ); - if( db ) dbi_conn_set_option( dbhandle, "dbname", db ); - - free( user ); - free( host ); - free( port ); - free( db ); - free( pw ); - - const char* err; - if( dbi_conn_connect( dbhandle ) < 0 ) { - sleep( 1 ); - if( dbi_conn_connect( dbhandle ) < 0 ) { - dbi_conn_error( dbhandle, &err ); - osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", err ); - return -1; - } - } - - oilsSetDBConnection( dbhandle ); - osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename ); - - // Add datatypes from database to the fields in the IDL - //if( oilsExtendIDL() ) { - // osrfLogError( OSRF_LOG_MARK, "Error extending the IDL" ); - // return -1; - //} - //else + else { + oilsSetDBConnection( dbhandle ); + osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename ); + + // Apply datatypes from database to the fields in the IDL + //if( oilsExtendIDL() ) { + // osrfLogError( OSRF_LOG_MARK, "Error extending the IDL" ); + // return -1; + //} + //else return 0; + } } /** diff --git a/Open-ILS/src/c-apps/oils_sql.c b/Open-ILS/src/c-apps/oils_sql.c index 2611c1c363..2aa2adff3f 100644 --- a/Open-ILS/src/c-apps/oils_sql.c +++ b/Open-ILS/src/c-apps/oils_sql.c @@ -137,6 +137,65 @@ static int max_flesh_depth = 100; static int enforce_pcrud = 0; // Boolean static char* modulename = NULL; +/** + @brief Connect to the database. + @return A database connection if successful, or NULL if not. + */ +dbi_conn oilsConnectDB( const char* mod_name ) { + + osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." ); + if( dbi_initialize( NULL ) == -1 ) { + osrfLogError( OSRF_LOG_MARK, "Unable to initialize libdbi" ); + return NULL; + } else + osrfLogDebug( OSRF_LOG_MARK, "... libdbi initialized." ); + + char* driver = osrf_settings_host_value( "/apps/%s/app_settings/driver", mod_name ); + char* user = osrf_settings_host_value( "/apps/%s/app_settings/database/user", mod_name ); + char* host = osrf_settings_host_value( "/apps/%s/app_settings/database/host", mod_name ); + char* port = osrf_settings_host_value( "/apps/%s/app_settings/database/port", mod_name ); + char* db = osrf_settings_host_value( "/apps/%s/app_settings/database/db", mod_name ); + char* pw = osrf_settings_host_value( "/apps/%s/app_settings/database/pw", mod_name ); + + osrfLogDebug( OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver ); + dbi_conn handle = dbi_conn_new( driver ); + + if( !handle ) { + osrfLogError( OSRF_LOG_MARK, "Error loading database driver [%s]", driver ); + return NULL; + } + osrfLogDebug( OSRF_LOG_MARK, "Database driver [%s] seems OK", driver ); + + osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database. host=%s, " + "port=%s, user=%s, db=%s", mod_name, host, port, user, db ); + + if( host ) dbi_conn_set_option( handle, "host", host ); + if( port ) dbi_conn_set_option_numeric( handle, "port", atoi( port )); + if( user ) dbi_conn_set_option( handle, "username", user ); + if( pw ) dbi_conn_set_option( handle, "password", pw ); + if( db ) dbi_conn_set_option( handle, "dbname", db ); + + free( user ); + free( host ); + free( port ); + free( db ); + free( pw ); + + if( dbi_conn_connect( handle ) < 0 ) { + sleep( 1 ); + if( dbi_conn_connect( handle ) < 0 ) { + const char* errmsg; + dbi_conn_error( handle, &errmsg ); + osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", errmsg ); + return NULL; + } + } + + osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", mod_name ); + + return handle; +} + /** @brief Select some options. @param module_name: Name of the server. -- 2.43.2