Create a reusable function for connecting to the database.
authorscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Apr 2010 21:11:54 +0000 (21:11 +0000)
committerscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Apr 2010 21:11:54 +0000 (21:11 +0000)
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
Open-ILS/src/c-apps/oils_qstore.c
Open-ILS/src/c-apps/oils_sql.c

index e01426f..d39a43f 100644 (file)
@@ -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 );
index f087aa1..9329cbf 100644 (file)
@@ -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;
+       }
 }
 
 /**
index 2611c1c..2aa2adf 100644 (file)
@@ -138,6 +138,65 @@ 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.
        @param do_pcrud: Boolean.  True if we are to enforce PCRUD permissions.