From 6a723a1ad288703840470bf9629b89ecb0c4b970 Mon Sep 17 00:00:00 2001 From: scottmk Date: Wed, 19 May 2010 14:11:29 +0000 Subject: [PATCH] Make sure that all bind variables have been assigned values before trying to execute a query. M Open-ILS/src/c-apps/oils_execsql.c git-svn-id: svn://svn.open-ils.org/ILS/trunk@16451 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/c-apps/oils_execsql.c | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Open-ILS/src/c-apps/oils_execsql.c b/Open-ILS/src/c-apps/oils_execsql.c index c4bfbbd81e..0f55209fcf 100644 --- a/Open-ILS/src/c-apps/oils_execsql.c +++ b/Open-ILS/src/c-apps/oils_execsql.c @@ -14,6 +14,7 @@ static jsonObject* get_row( BuildSQLState* state ); static jsonObject* get_date_column( dbi_result result, int col_idx ); +static int values_missing( BuildSQLState* state ); /** @brief Execute the current SQL statement and return the first row. @@ -29,6 +30,14 @@ jsonObject* oilsFirstRow( BuildSQLState* state ) { if( !state ) return NULL; + // Make sure all the bind variables have values for them + if( !state->values_required && values_missing( state )) { + state->error = 1; + osrfLogError( OSRF_LOG_MARK, sqlAddMsg( state, + "Unable to execute query: values not available for all bind variables\n" )); + return NULL; + } + if( state->result ) dbi_result_free( state->result ); @@ -162,3 +171,28 @@ static jsonObject* get_date_column( dbi_result result, int col_idx ) { return jsonNewObject( timestring ); } + +/** + @brief Determine whether all bind variables have values supplied for them. + @param state Pointer to the query-building context. + @return The number of bind variables with no available value. +*/ +static int values_missing( BuildSQLState* state ) { + if( !state->bindvar_list || osrfHashGetCount( state->bindvar_list ) == 0 ) + return 0; // Nothing to count + + int count = 0; + osrfHashIterator* iter = osrfNewHashIterator( state->bindvar_list ); + + BindVar* bind = NULL; + while(( bind = osrfHashIteratorNext( iter ))) { + if( !bind->actual_value && !bind->default_value ) { + sqlAddMsg( state, "No value for bind value \"%s\", with label \"%s\"", + bind->name, bind->label ); + ++count; + } + } + + osrfHashIteratorFree( iter ); + return count; +} -- 2.43.2