3 @brief Routines for maintaining a BuildSQLState.
5 A BuildSQLState shuttles information from the routines that load an abstract representation
6 of a query to the routines that build an SQL statement.
12 #include "opensrf/utils.h"
13 #include "opensrf/log.h"
14 #include "opensrf/string_array.h"
15 #include "openils/oils_buildq.h"
18 @brief Construct a new BuildSQLState.
19 @param dbhandle Handle for the database connection.
20 @return Pointer to the newly constructed BuildSQLState.
22 The calling code is responsible for freeing the BuildSQLState by calling BuildSQLStateFree().
24 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle ) {
26 BuildSQLState* state = safe_malloc( sizeof( BuildSQLState ) );
27 state->dbhandle = dbhandle;
30 state->error_msgs = osrfNewStringArray( 16 );
31 state->sql = buffer_init( 128 );
32 state->bindvar_list = NULL; // Don't build it until we need it
33 state->query_stack = NULL;
34 state->expr_stack = NULL;
36 state->defaults_usable = 0;
37 state->values_required = 0;
43 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... ) {
44 if( !state || ! state->error_msgs )
47 VA_LIST_TO_STRING( msg );
48 osrfStringArrayAdd( state->error_msgs, VA_BUF );
49 return osrfStringArrayGetString( state->error_msgs, state->error_msgs->size - 1 );
53 @brief Free a BuildSQLState.
54 @param state Pointer to the BuildSQLState to be freed.
56 We do @em not close the database connection.
58 void buildSQLStateFree( BuildSQLState* state ){
62 dbi_result_free( state->result );
65 osrfStringArrayFree( state->error_msgs );
66 buffer_free( state->sql );
67 if( state->bindvar_list )
68 osrfHashFree( state->bindvar_list );
69 while( state->query_stack )
70 pop_id( &state->query_stack );
71 while( state->expr_stack )
72 pop_id( &state->expr_stack );
73 while( state->from_stack )
74 pop_id( &state->from_stack );
80 @brief Free up any resources held by the BuildSQL module.
82 void buildSQLCleanup( void ) {