3 @brief As a server, perform database queries as defined in the database itself.
10 #include "opensrf/utils.h"
11 #include "opensrf/log.h"
12 #include "opensrf/osrf_json.h"
13 #include "opensrf/osrf_application.h"
14 #include "openils/oils_utils.h"
15 #include "openils/oils_sql.h"
16 #include "openils/oils_buildq.h"
19 @brief Information about a previously prepared query.
21 We store an osrfHash of CachedQueries in the userData area of the application session,
22 keyed on query token. That way we can fetch what a previous call to the prepare method
30 static dbi_conn dbhandle; /* our db connection */
32 static const char modulename[] = "open-ils.qstore";
34 int doPrepare( osrfMethodContext* ctx );
35 int doExecute( osrfMethodContext* ctx );
36 int doSql( osrfMethodContext* ctx );
38 static const char* save_query(
39 osrfMethodContext* ctx, BuildSQLState* state, StoredQ* query );
40 static void free_cached_query( char* key, void* data );
41 static void userDataFree( void* blob );
42 static CachedQuery* search_token( osrfMethodContext* ctx, const char* token );
45 @brief Disconnect from the database.
47 This function is called when the server drone is about to terminate.
49 void osrfAppChildExit() {
50 osrfLogDebug( OSRF_LOG_MARK, "Child is exiting, disconnecting from database..." );
53 dbi_conn_query( dbhandle, "ROLLBACK;" );
54 dbi_conn_close( dbhandle );
60 @brief Initialize the application.
61 @return Zero if successful, or non-zero if not.
63 Load the IDL file into an internal data structure for future reference. Each non-virtual
64 class in the IDL corresponds to a table or view in the database, or to a subquery defined
65 in the IDL. Ignore all virtual tables and virtual fields.
67 Register the functions for remote procedure calls.
69 This function is called when the registering the application, and is executed by the
70 listener before spawning the drones.
72 int osrfAppInitialize() {
74 osrfLogInfo( OSRF_LOG_MARK, "Initializing the QStore Server..." );
75 osrfLogInfo( OSRF_LOG_MARK, "Finding XML file..." );
77 if ( !oilsIDLInit( osrf_settings_host_value( "/IDL" )))
78 return 1; /* return non-zero to indicate error */
80 // Set the SQL options. Here the second and third parameters are irrelevant, but we need
81 // to set the module name for use in error messages.
82 oilsSetSQLOptions( modulename, 0, 100 );
84 growing_buffer* method_name = buffer_init( 64 );
86 OSRF_BUFFER_ADD( method_name, modulename );
87 OSRF_BUFFER_ADD( method_name, ".prepare" );
88 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
89 "doPrepare", "", 1, 0 );
91 buffer_reset( method_name );
92 OSRF_BUFFER_ADD( method_name, modulename );
93 OSRF_BUFFER_ADD( method_name, ".columns" );
94 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
95 "doColumns", "", 1, 0 );
97 buffer_reset( method_name );
98 OSRF_BUFFER_ADD( method_name, modulename );
99 OSRF_BUFFER_ADD( method_name, ".bind_param" );
100 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
101 "doBindParam", "", 2, 0 );
103 buffer_reset( method_name );
104 OSRF_BUFFER_ADD( method_name, modulename );
105 OSRF_BUFFER_ADD( method_name, ".execute" );
106 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
107 "doExecute", "", 1, OSRF_METHOD_STREAMING );
109 buffer_reset( method_name );
110 OSRF_BUFFER_ADD( method_name, modulename );
111 OSRF_BUFFER_ADD( method_name, ".sql" );
112 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
113 "doSql", "", 1, OSRF_METHOD_STREAMING );
115 buffer_reset( method_name );
116 OSRF_BUFFER_ADD( method_name, modulename );
117 OSRF_BUFFER_ADD( method_name, ".finish" );
118 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
119 "doFinish", "", 1, 0 );
121 buffer_reset( method_name );
122 OSRF_BUFFER_ADD( method_name, modulename );
123 OSRF_BUFFER_ADD( method_name, ".messages" );
124 osrfAppRegisterMethod( modulename, OSRF_BUFFER_C_STR( method_name ),
125 "doMessages", "", 1, 0 );
131 @brief Initialize a server drone.
132 @return Zero if successful, -1 if not.
134 Connect to the database. For each non-virtual class in the IDL, execute a dummy "SELECT * "
135 query to get the datatype of each column. Record the datatypes in the loaded IDL.
137 This function is called by a server drone shortly after it is spawned by the listener.
139 int osrfAppChildInit( void ) {
141 dbhandle = oilsConnectDB( modulename );
145 oilsSetDBConnection( dbhandle );
146 osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename );
148 // Apply datatypes from database to the fields in the IDL
149 //if( oilsExtendIDL() ) {
150 // osrfLogError( OSRF_LOG_MARK, "Error extending the IDL" );
159 @brief Load a specified query from the database query tables.
160 @param ctx Pointer to the current method context.
161 @return Zero if successful, or -1 if not.
164 - query id (key of query.stored_query table)
166 Returns: a character string serving as a token for future references to the query.
168 NB: the method return type is temporary. Eventually this method will return both a token
169 and a list of bind variables.
171 int doPrepare( osrfMethodContext* ctx ) {
172 if(osrfMethodVerifyContext( ctx )) {
173 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
177 // Get the query id from a method parameter
178 const jsonObject* query_id_obj = jsonObjectGetIndex( ctx->params, 0 );
179 if( query_id_obj->type != JSON_NUMBER ) {
180 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
181 ctx->request, "Invalid parameter; query id must be a number" );
185 int query_id = atoi( jsonObjectGetString( query_id_obj ));
186 if( query_id <= 0 ) {
187 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
188 ctx->request, "Invalid parameter: query id must be greater than zero" );
192 osrfLogInfo( OSRF_LOG_MARK, "Loading query for id # %d", query_id );
194 BuildSQLState* state = buildSQLStateNew( dbhandle );
195 state->defaults_usable = 1;
196 state->values_required = 0;
197 StoredQ* query = getStoredQuery( state, query_id );
199 osrfLogWarning( OSRF_LOG_MARK, "Unable to load stored query # %d", query_id );
200 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
201 ctx->request, "Unable to load stored query" );
205 const char* token = save_query( ctx, state, query );
207 osrfLogInfo( OSRF_LOG_MARK, "Token for query id # %d is \"%s\"", query_id, token );
209 osrfAppRespondComplete( ctx, jsonNewObject( token ));
214 @brief Execute an SQL query and return a result set.
215 @param ctx Pointer to the current method context.
216 @return Zero if successful, or -1 if not.
219 - query token, as previously returned by the .prepare method.
221 Returns: An array of column names; unavailable names are represented as nulls.
223 int doColumns( osrfMethodContext* ctx ) {
224 if(osrfMethodVerifyContext( ctx )) {
225 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
229 // Get the query token from a method parameter
230 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
231 if( token_obj->type != JSON_STRING ) {
232 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
233 ctx->request, "Invalid parameter; query token must be a string" );
236 const char* token = jsonObjectGetString( token_obj );
238 // Look up the query token in the session-level userData
239 CachedQuery* query = search_token( ctx, token );
241 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
242 ctx->request, "Invalid query token" );
246 osrfLogInfo( OSRF_LOG_MARK, "Listing column names for token %s", token );
248 jsonObject* col_list = oilsGetColNames( query->state, query->query );
249 if( query->state->error ) {
250 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
251 ctx->request, "Unable to get column names" );
254 osrfAppRespondComplete( ctx, col_list );
259 int doBindParam( osrfMethodContext* ctx ) {
260 if(osrfMethodVerifyContext( ctx )) {
261 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
265 // Get the query token from a method parameter
266 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
267 if( token_obj->type != JSON_STRING ) {
268 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
269 ctx->request, "Invalid parameter; query token must be a string" );
272 const char* token = jsonObjectGetString( token_obj );
274 // Look up the query token in the session-level userData
275 CachedQuery* query = search_token( ctx, token );
277 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
278 ctx->request, "Invalid query token" );
282 osrfLogInfo( OSRF_LOG_MARK, "Binding parameter(s) for token %s", token );
284 osrfAppRespondComplete( ctx, jsonNewObject( "build method not yet implemented" ));
289 @brief Execute an SQL query and return a result set.
290 @param ctx Pointer to the current method context.
291 @return Zero if successful, or -1 if not.
294 - query token, as previously returned by the .prepare method.
296 Returns: A series of responses, each of them a row represented as an array of column values.
298 int doExecute( osrfMethodContext* ctx ) {
299 if(osrfMethodVerifyContext( ctx )) {
300 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
304 // Get the query token
305 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
306 if( token_obj->type != JSON_STRING ) {
307 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
308 ctx->request, "Invalid parameter; query token must be a string" );
311 const char* token = jsonObjectGetString( token_obj );
313 // Look up the query token in the session-level userData
314 CachedQuery* query = search_token( ctx, token );
316 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
317 ctx->request, "Invalid query token" );
321 osrfLogInfo( OSRF_LOG_MARK, "Executing query for token \"%s\"", token );
322 if( query->state->error ) {
323 osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state,
324 "No valid prepared query available for query id # %d", query->query->id ));
325 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
326 ctx->request, "No valid prepared query available" );
328 } else if( buildSQL( query->state, query->query )) {
329 osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state,
330 "Unable to build SQL statement for query id # %d", query->query->id ));
331 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
332 ctx->request, "Unable to build SQL statement" );
336 jsonObject* row = oilsFirstRow( query->state );
338 osrfAppRespond( ctx, row );
339 row = oilsNextRow( query->state );
342 if( query->state->error ) {
343 osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state,
344 "Unable to execute SQL statement for query id # %d", query->query->id ));
345 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
346 ctx->request, "Unable to execute SQL statement" );
350 osrfAppRespondComplete( ctx, NULL );
355 @brief Construct an SQL query, but without executing it.
356 @param ctx Pointer to the current method context.
357 @return Zero if successful, or -1 if not.
360 - query token, as previously returned by the .prepare method.
362 Returns: A string containing an SQL query..
364 int doSql( osrfMethodContext* ctx ) {
365 if(osrfMethodVerifyContext( ctx )) {
366 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
370 // Get the query token
371 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
372 if( token_obj->type != JSON_STRING ) {
373 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
374 ctx->request, "Invalid parameter; query token must be a string" );
377 const char* token = jsonObjectGetString( token_obj );
379 // Look up the query token in the session-level userData
380 CachedQuery* query = search_token( ctx, token );
382 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
383 ctx->request, "Invalid query token" );
387 osrfLogInfo( OSRF_LOG_MARK, "Returning SQL for token \"%s\"", token );
388 if( query->state->error ) {
389 osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state,
390 "No valid prepared query available for query id # %d", query->query->id ));
391 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
392 ctx->request, "No valid prepared query available" );
394 } else if( buildSQL( query->state, query->query )) {
395 osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state,
396 "Unable to build SQL statement for query id # %d", query->query->id ));
397 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
398 ctx->request, "Unable to build SQL statement" );
402 osrfAppRespondComplete( ctx, jsonNewObject( OSRF_BUFFER_C_STR( query->state->sql )));
407 @brief Return a list of previously generated error messages for a specified query.
408 @param ctx Pointer to the current method context.
409 @return Zero if successful, or -1 if not.
412 - query token, as previously returned by the .prepare method.
414 Returns: A (possibly empty) array of strings, each one an error message generated during
415 previous operations in connection with the specified query.
417 int doMessages( osrfMethodContext* ctx ) {
418 if(osrfMethodVerifyContext( ctx )) {
419 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
423 // Get the query token from a method parameter
424 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
425 if( token_obj->type != JSON_STRING ) {
426 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
427 ctx->request, "Invalid parameter; query token must be a string" );
430 const char* token = jsonObjectGetString( token_obj );
432 // Look up the query token in the session-level userData
433 CachedQuery* query = search_token( ctx, token );
435 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
436 ctx->request, "Invalid query token" );
440 osrfLogInfo( OSRF_LOG_MARK, "Returning messages for token %s", token );
442 jsonObject* msgs = jsonNewObjectType( JSON_ARRAY );
443 const osrfStringArray* error_msgs = query->state->error_msgs;
445 for( i = 0; i < error_msgs->size; ++i ) {
446 jsonObject* msg = jsonNewObject( osrfStringArrayGetString( error_msgs, i ));
447 jsonObjectPush( msgs, msg );
450 osrfAppRespondComplete( ctx, msgs );
455 @brief Discard a previously stored query, as identified by a token.
456 @param ctx Pointer to the current method context.
457 @return Zero if successful, or -1 if not.
460 - query token, as previously returned by the .prepare method.
464 int doFinish( osrfMethodContext* ctx ) {
465 if(osrfMethodVerifyContext( ctx )) {
466 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
470 // Get the query token.
471 const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 );
472 if( token_obj->type != JSON_STRING ) {
473 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
474 ctx->request, "Invalid parameter; query token must be a string" );
477 const char* token = jsonObjectGetString( token_obj );
479 // Delete the corresponding entry from the cache. If there is no cache, or no such entry,
480 // just ignore the problem and report success.
481 osrfHash* cache = ctx->session->userData;
483 osrfHashRemove( cache, token );
485 osrfAppRespondComplete( ctx, NULL );
490 @brief Save a query in session-level userData for reference in future method calls.
491 @param ctx Pointer to the current method context.
492 @param state Pointer to the state of the query.
493 @param query Pointer to the abstract representation of the query.
494 @return Pointer to an identifying token to be returned to the client.
496 static const char* save_query(
497 osrfMethodContext* ctx, BuildSQLState* state, StoredQ* query ) {
499 CachedQuery* cached_query = safe_malloc( sizeof( CachedQuery ));
500 cached_query->state = state;
501 cached_query->query = query;
503 // Get the cache. If we don't have one yet, make one.
504 osrfHash* cache = ctx->session->userData;
506 cache = osrfNewHash();
507 osrfHashSetCallback( cache, free_cached_query );
508 ctx->session->userData = cache;
509 ctx->session->userDataFree = userDataFree; // arrange to free it at end of session
512 // Create a token string to be used as a key
513 static unsigned int token_count = 0;
514 char* token = va_list_to_string(
515 "%u_%ld_%ld", ++token_count, (long) time( NULL ), (long) getpid() );
517 osrfHashSet( cache, cached_query, token );
522 @brief Free a CachedQuery
523 @param Pointer to the CachedQuery to be freed.
525 static void free_cached_query( char* key, void* data ) {
527 CachedQuery* cached_query = data;
528 buildSQLStateFree( cached_query->state );
529 storedQFree( cached_query->query );
534 @brief Callback for freeing session-level userData.
535 @param blob Opaque pointer t userData.
537 static void userDataFree( void* blob ) {
538 osrfHashFree( (osrfHash*) blob );
542 @brief Search for the cached query corresponding to a given token.
543 @param ctx Pointer to the current method context.
544 @param token Token string from a previous call to the prepare method.
545 @return A pointer to the cached query, if found, or NULL if not.
547 static CachedQuery* search_token( osrfMethodContext* ctx, const char* token ) {
548 if( ctx && ctx->session->userData && token ) {
549 osrfHash* cache = ctx->session->userData;
550 return osrfHashGet( cache, token );