]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/c-apps/oils_buildq.c
Use the oilsConnectDB function to connect to the database, instead
[working/Evergreen.git] / Open-ILS / src / c-apps / oils_buildq.c
1 /**
2         @file buildquery.c
3         @brief Routines for maintaining a BuildSQLState.
4
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.
7 */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <dbi/dbi.h>
12 #include "opensrf/utils.h"
13 #include "opensrf/log.h"
14 #include "opensrf/string_array.h"
15 #include "openils/oils_buildq.h"
16
17 /**
18         @brief Construct a new BuildSQLState.
19         @param dbhandle Handle for the database connection.
20         @return Pointer to the newly constructed BuildSQLState.
21
22         The calling code is responsible for freeing the BuildSQLState by calling BuildSQLStateFree().
23 */
24 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle ) {
25
26         BuildSQLState* state = safe_malloc( sizeof( BuildSQLState ) );
27         state->dbhandle    = dbhandle;
28         state->error       = 0;
29         state->error_msgs  = osrfNewStringArray( 16 );
30         state->sql         = buffer_init( 128 );
31         state->query_stack = NULL;
32         state->expr_stack  = NULL;
33         state->indent      = 0;
34
35         return state;
36 }
37
38 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... ) {
39         if( !state || ! state->error_msgs )
40                 return "";
41
42         VA_LIST_TO_STRING( msg );
43         osrfStringArrayAdd( state->error_msgs, VA_BUF );
44         return osrfStringArrayGetString( state->error_msgs, state->error_msgs->size - 1 );
45 }
46
47 /**
48         @brief Free a BuildSQLState.
49         @param state Pointer to the BuildSQLState to be freed.
50
51         We do @em not close the database connection.
52 */
53 void buildSQLStateFree( BuildSQLState* state ){
54         
55         if( state ) {
56                 osrfStringArrayFree( state->error_msgs );
57                 buffer_free( state->sql );
58                 while( state->query_stack )
59                         pop_id( &state->query_stack );
60                 while( state->expr_stack )
61                         pop_id( &state->expr_stack );
62                 while( state->from_stack )
63                         pop_id( &state->from_stack );
64                 free( state );
65         }
66 }
67
68 /**
69         @brief Free up any resources held by the BuildSQL module.
70 */
71 void buildSQLCleanup( void ) {
72         storedQCleanup();
73 }