[open-ils-commits] r16338 - in trunk/Open-ILS: include/openils src/c-apps (scottmk)
svn at svn.open-ils.org
svn at svn.open-ils.org
Wed Apr 28 17:11:59 EDT 2010
Author: scottmk
Date: 2010-04-28 17:11:54 -0400 (Wed, 28 Apr 2010)
New Revision: 16338
Modified:
trunk/Open-ILS/include/openils/oils_sql.h
trunk/Open-ILS/src/c-apps/oils_qstore.c
trunk/Open-ILS/src/c-apps/oils_sql.c
Log:
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
Modified: trunk/Open-ILS/include/openils/oils_sql.h
===================================================================
--- trunk/Open-ILS/include/openils/oils_sql.h 2010-04-28 19:28:45 UTC (rev 16337)
+++ trunk/Open-ILS/include/openils/oils_sql.h 2010-04-28 21:11:54 UTC (rev 16338)
@@ -26,6 +26,7 @@
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 );
Modified: trunk/Open-ILS/src/c-apps/oils_qstore.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_qstore.c 2010-04-28 19:28:45 UTC (rev 16337)
+++ trunk/Open-ILS/src/c-apps/oils_qstore.c 2010-04-28 21:11:54 UTC (rev 16338)
@@ -126,63 +126,23 @@
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 );
+ else {
+ oilsSetDBConnection( dbhandle );
+ osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename );
- 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
+ // 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;
+ }
}
/**
Modified: trunk/Open-ILS/src/c-apps/oils_sql.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_sql.c 2010-04-28 19:28:45 UTC (rev 16337)
+++ trunk/Open-ILS/src/c-apps/oils_sql.c 2010-04-28 21:11:54 UTC (rev 16338)
@@ -138,6 +138,65 @@
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.
More information about the open-ils-commits
mailing list