3 @brief Utility routines for translating JSON into SQL.
11 #include "opensrf/utils.h"
12 #include "opensrf/log.h"
13 #include "opensrf/osrf_application.h"
14 #include "openils/oils_utils.h"
15 #include "openils/oils_sql.h"
17 // The next four macros are OR'd together as needed to form a set
18 // of bitflags. SUBCOMBO enables an extra pair of parentheses when
19 // nesting one UNION, INTERSECT or EXCEPT inside another.
20 // SUBSELECT tells us we're in a subquery, so don't add the
21 // terminal semicolon yet.
24 #define DISABLE_I18N 2
25 #define SELECT_DISTINCT 1
30 struct ClassInfoStruct;
31 typedef struct ClassInfoStruct ClassInfo;
33 #define ALIAS_STORE_SIZE 16
34 #define CLASS_NAME_STORE_SIZE 16
36 struct ClassInfoStruct {
40 osrfHash* class_def; // Points into IDL
41 osrfHash* fields; // Points into IDL
42 osrfHash* links; // Points into IDL
44 // The remaining members are private and internal. Client code should not
45 // access them directly.
47 ClassInfo* next; // Supports linked list of joined classes
48 int in_use; // boolean
50 // We usually store the alias and class name in the following arrays, and
51 // point the corresponding pointers at them. When the string is too big
52 // for the array (which will probably never happen in practice), we strdup it.
54 char alias_store[ ALIAS_STORE_SIZE + 1 ];
55 char class_name_store[ CLASS_NAME_STORE_SIZE + 1 ];
58 struct QueryFrameStruct;
59 typedef struct QueryFrameStruct QueryFrame;
61 struct QueryFrameStruct {
63 ClassInfo* join_list; // linked list of classes joined to the core class
64 QueryFrame* next; // implements stack as linked list
65 int in_use; // boolean
68 static int timeout_needs_resetting;
69 static time_t time_next_reset;
71 static int verifyObjectClass ( osrfMethodContext*, const jsonObject* );
73 static void setXactId( osrfMethodContext* ctx );
74 static inline const char* getXactId( osrfMethodContext* ctx );
75 static inline void clearXactId( osrfMethodContext* ctx );
77 static jsonObject* doFieldmapperSearch ( osrfMethodContext* ctx, osrfHash* class_meta,
78 jsonObject* where_hash, jsonObject* query_hash, int* err );
79 static jsonObject* oilsMakeFieldmapperFromResult( dbi_result, osrfHash* );
80 static jsonObject* oilsMakeJSONFromResult( dbi_result );
82 static char* searchSimplePredicate ( const char* op, const char* class_alias,
83 osrfHash* field, const jsonObject* node );
84 static char* searchFunctionPredicate ( const char*, osrfHash*, const jsonObject*, const char* );
85 static char* searchFieldTransform ( const char*, osrfHash*, const jsonObject* );
86 static char* searchFieldTransformPredicate ( const ClassInfo*, osrfHash*, const jsonObject*,
88 static char* searchBETWEENPredicate ( const char*, osrfHash*, const jsonObject* );
89 static char* searchINPredicate ( const char*, osrfHash*,
90 jsonObject*, const char*, osrfMethodContext* );
91 static char* searchPredicate ( const ClassInfo*, osrfHash*, jsonObject*, osrfMethodContext* );
92 static char* searchJOIN ( const jsonObject*, const ClassInfo* left_info );
93 static char* searchWHERE ( const jsonObject* search_hash, const ClassInfo*, int, osrfMethodContext* );
94 static char* buildSELECT( const jsonObject*, jsonObject* rest_of_query,
95 osrfHash* meta, osrfMethodContext* ctx );
96 static char* buildOrderByFromArray( osrfMethodContext* ctx, const jsonObject* order_array );
98 char* buildQuery( osrfMethodContext* ctx, jsonObject* query, int flags );
100 char* SELECT ( osrfMethodContext*, jsonObject*, const jsonObject*, const jsonObject*,
101 const jsonObject*, const jsonObject*, const jsonObject*, const jsonObject*, int );
103 static osrfStringArray* getPermLocationCache( osrfMethodContext*, const char* );
104 static void setPermLocationCache( osrfMethodContext*, const char*, osrfStringArray* );
106 void userDataFree( void* );
107 static void sessionDataFree( char*, void* );
108 static void pcacheFree( char*, void* );
109 static int obj_is_true( const jsonObject* obj );
110 static const char* json_type( int code );
111 static const char* get_primitive( osrfHash* field );
112 static const char* get_datatype( osrfHash* field );
113 static void pop_query_frame( void );
114 static void push_query_frame( void );
115 static int add_query_core( const char* alias, const char* class_name );
116 static inline ClassInfo* search_alias( const char* target );
117 static ClassInfo* search_all_alias( const char* target );
118 static ClassInfo* add_joined_class( const char* alias, const char* classname );
119 static void clear_query_stack( void );
121 static const jsonObject* verifyUserPCRUD( osrfMethodContext* );
122 static int verifyObjectPCRUD( osrfMethodContext*, const jsonObject*, const int );
123 static const char* org_tree_root( osrfMethodContext* ctx );
124 static jsonObject* single_hash( const char* key, const char* value );
126 static int child_initialized = 0; /* boolean */
128 static dbi_conn writehandle; /* our MASTER db connection */
129 static dbi_conn dbhandle; /* our CURRENT db connection */
130 //static osrfHash * readHandles;
132 // The following points to the top of a stack of QueryFrames. It's a little
133 // confusing because the top level of the query is at the bottom of the stack.
134 static QueryFrame* curr_query = NULL;
136 static dbi_conn writehandle; /* our MASTER db connection */
137 static dbi_conn dbhandle; /* our CURRENT db connection */
138 //static osrfHash * readHandles;
140 static int max_flesh_depth = 100;
142 static int perm_at_threshold = 5;
143 static int enforce_pcrud = 0; // Boolean
144 static char* modulename = NULL;
147 @brief Connect to the database.
148 @return A database connection if successful, or NULL if not.
150 dbi_conn oilsConnectDB( const char* mod_name ) {
152 osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." );
153 if( dbi_initialize( NULL ) == -1 ) {
154 osrfLogError( OSRF_LOG_MARK, "Unable to initialize libdbi" );
157 osrfLogDebug( OSRF_LOG_MARK, "... libdbi initialized." );
159 char* driver = osrf_settings_host_value( "/apps/%s/app_settings/driver", mod_name );
160 char* user = osrf_settings_host_value( "/apps/%s/app_settings/database/user", mod_name );
161 char* host = osrf_settings_host_value( "/apps/%s/app_settings/database/host", mod_name );
162 char* port = osrf_settings_host_value( "/apps/%s/app_settings/database/port", mod_name );
163 char* db = osrf_settings_host_value( "/apps/%s/app_settings/database/db", mod_name );
164 char* pw = osrf_settings_host_value( "/apps/%s/app_settings/database/pw", mod_name );
166 osrfLogDebug( OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver );
167 dbi_conn handle = dbi_conn_new( driver );
170 osrfLogError( OSRF_LOG_MARK, "Error loading database driver [%s]", driver );
173 osrfLogDebug( OSRF_LOG_MARK, "Database driver [%s] seems OK", driver );
175 osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database. host=%s, "
176 "port=%s, user=%s, db=%s", mod_name, host, port, user, db );
178 if( host ) dbi_conn_set_option( handle, "host", host );
179 if( port ) dbi_conn_set_option_numeric( handle, "port", atoi( port ));
180 if( user ) dbi_conn_set_option( handle, "username", user );
181 if( pw ) dbi_conn_set_option( handle, "password", pw );
182 if( db ) dbi_conn_set_option( handle, "dbname", db );
190 if( dbi_conn_connect( handle ) < 0 ) {
192 if( dbi_conn_connect( handle ) < 0 ) {
194 dbi_conn_error( handle, &msg );
195 osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s",
196 msg ? msg : "(No description available)" );
201 osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", mod_name );
207 @brief Select some options.
208 @param module_name: Name of the server.
209 @param do_pcrud: Boolean. True if we are to enforce PCRUD permissions.
211 This source file is used (at this writing) to implement three different servers:
212 - open-ils.reporter-store
216 These servers behave mostly the same, but they implement different combinations of
217 methods, and open-ils.pcrud enforces a permissions scheme that the other two don't.
219 Here we use the server name in messages to identify which kind of server issued them.
220 We use do_crud as a boolean to control whether or not to enforce the permissions scheme.
222 void oilsSetSQLOptions( const char* module_name, int do_pcrud, int flesh_depth ) {
224 module_name = "open-ils.cstore"; // bulletproofing with a default
229 modulename = strdup( module_name );
230 enforce_pcrud = do_pcrud;
231 max_flesh_depth = flesh_depth;
235 @brief Install a database connection.
236 @param conn Pointer to a database connection.
238 In some contexts, @a conn may merely provide a driver so that we can process strings
239 properly, without providing an open database connection.
241 void oilsSetDBConnection( dbi_conn conn ) {
242 dbhandle = writehandle = conn;
246 @brief Determine whether a database connection is alive.
247 @param handle Handle for a database connection.
248 @return 1 if the connection is alive, or zero if it isn't.
250 int oilsIsDBConnected( dbi_conn handle ) {
251 // Do an innocuous SELECT. If it succeeds, the database connection is still good.
252 dbi_result result = dbi_conn_query( handle, "SELECT 1;" );
254 dbi_result_free( result );
257 // This is a terrible, horrible, no good, very bad kludge.
258 // Sometimes the SELECT 1 query fails, not because the database connection is dead,
259 // but because (due to a previous error) the database is ignoring all commands,
260 // even innocuous SELECTs, until the current transaction is rolled back. The only
261 // known way to detect this condition via the dbi library is by looking at the error
262 // message. This approach will break if the language or wording of the message ever
264 // Note: the dbi_conn_ping function purports to determine whether the database
265 // connection is live, but at this writing this function is unreliable and useless.
266 static const char* ok_msg = "ERROR: current transaction is aborted, commands "
267 "ignored until end of transaction block\n";
269 dbi_conn_error( handle, &msg );
270 if( strcmp( msg, ok_msg )) {
271 osrfLogError( OSRF_LOG_MARK, "Database connection isn't working" );
274 return 1; // ignoring SELECT due to previous error; that's okay
279 @brief Get a table name, view name, or subquery for use in a FROM clause.
280 @param class Pointer to the IDL class entry.
281 @return A table name, a view name, or a subquery in parentheses.
283 In some cases the IDL defines a class, not with a table name or a view name, but with
284 a SELECT statement, which may be used as a subquery.
286 char* oilsGetRelation( osrfHash* classdef ) {
288 char* source_def = NULL;
289 const char* tabledef = osrfHashGet( classdef, "tablename" );
292 source_def = strdup( tabledef ); // Return the name of a table or view
294 tabledef = osrfHashGet( classdef, "source_definition" );
296 // Return a subquery, enclosed in parentheses
297 source_def = safe_malloc( strlen( tabledef ) + 3 );
298 source_def[ 0 ] = '(';
299 strcpy( source_def + 1, tabledef );
300 strcat( source_def, ")" );
302 // Not found: return an error
303 const char* classname = osrfHashGet( classdef, "classname" );
308 "%s ERROR No tablename or source_definition for class \"%s\"",
319 @brief Add datatypes from the database to the fields in the IDL.
320 @param handle Handle for a database connection
321 @return Zero if successful, or 1 upon error.
323 For each relevant class in the IDL: ask the database for the datatype of every field.
324 In particular, determine which fields are text fields and which fields are numeric
325 fields, so that we know whether to enclose their values in quotes.
327 int oilsExtendIDL( dbi_conn handle ) {
328 osrfHashIterator* class_itr = osrfNewHashIterator( oilsIDL() );
329 osrfHash* class = NULL;
330 growing_buffer* query_buf = buffer_init( 64 );
331 int results_found = 0; // boolean
333 // For each class in the IDL...
334 while( (class = osrfHashIteratorNext( class_itr ) ) ) {
335 const char* classname = osrfHashIteratorKey( class_itr );
336 osrfHash* fields = osrfHashGet( class, "fields" );
338 // If the class is virtual, ignore it
339 if( str_is_true( osrfHashGet(class, "virtual") ) ) {
340 osrfLogDebug(OSRF_LOG_MARK, "Class %s is virtual, skipping", classname );
344 char* tabledef = oilsGetRelation( class );
346 continue; // No such relation -- a query of it would be doomed to failure
348 buffer_reset( query_buf );
349 buffer_fadd( query_buf, "SELECT * FROM %s AS x WHERE 1=0;", tabledef );
353 osrfLogDebug( OSRF_LOG_MARK, "%s Investigatory SQL = %s",
354 modulename, OSRF_BUFFER_C_STR( query_buf ) );
356 dbi_result result = dbi_conn_query( handle, OSRF_BUFFER_C_STR( query_buf ) );
361 const char* columnName;
362 while( (columnName = dbi_result_get_field_name(result, columnIndex)) ) {
364 osrfLogInternal( OSRF_LOG_MARK, "Looking for column named [%s]...",
367 /* fetch the fieldmapper index */
368 osrfHash* _f = osrfHashGet(fields, columnName);
371 osrfLogDebug(OSRF_LOG_MARK, "Found [%s] in IDL hash...", columnName);
373 /* determine the field type and storage attributes */
375 switch( dbi_result_get_field_type_idx( result, columnIndex )) {
377 case DBI_TYPE_INTEGER : {
379 if( !osrfHashGet(_f, "primitive") )
380 osrfHashSet(_f, "number", "primitive");
382 int attr = dbi_result_get_field_attribs_idx( result, columnIndex );
383 if( attr & DBI_INTEGER_SIZE8 )
384 osrfHashSet( _f, "INT8", "datatype" );
386 osrfHashSet( _f, "INT", "datatype" );
389 case DBI_TYPE_DECIMAL :
390 if( !osrfHashGet( _f, "primitive" ))
391 osrfHashSet( _f, "number", "primitive" );
393 osrfHashSet( _f, "NUMERIC", "datatype" );
396 case DBI_TYPE_STRING :
397 if( !osrfHashGet( _f, "primitive" ))
398 osrfHashSet( _f, "string", "primitive" );
400 osrfHashSet( _f,"TEXT", "datatype" );
403 case DBI_TYPE_DATETIME :
404 if( !osrfHashGet( _f, "primitive" ))
405 osrfHashSet( _f, "string", "primitive" );
407 osrfHashSet( _f, "TIMESTAMP", "datatype" );
410 case DBI_TYPE_BINARY :
411 if( !osrfHashGet( _f, "primitive" ))
412 osrfHashSet( _f, "string", "primitive" );
414 osrfHashSet( _f, "BYTEA", "datatype" );
419 "Setting [%s] to primitive [%s] and datatype [%s]...",
421 osrfHashGet( _f, "primitive" ),
422 osrfHashGet( _f, "datatype" )
426 } // end while loop for traversing columns of result
427 dbi_result_free( result );
430 int errnum = dbi_conn_error( handle, &msg );
431 osrfLogDebug( OSRF_LOG_MARK, "No data found for class [%s]: %d, %s", classname,
432 errnum, msg ? msg : "(No description available)" );
433 // We don't check the database connection here. It's routine to get failures at
434 // this point; we routinely try to query tables that don't exist, because they
435 // are defined in the IDL but not in the database.
437 } // end for each class in IDL
439 buffer_free( query_buf );
440 osrfHashIteratorFree( class_itr );
441 child_initialized = 1;
443 if( !results_found ) {
444 osrfLogError( OSRF_LOG_MARK,
445 "No results found for any class -- bad database connection?" );
447 } else if( ! oilsIsDBConnected( handle )) {
448 osrfLogError( OSRF_LOG_MARK,
449 "Unable to extend IDL: database connection isn't working" );
457 @brief Free an osrfHash that stores a transaction ID.
458 @param blob A pointer to the osrfHash to be freed, cast to a void pointer.
460 This function is a callback, to be called by the application session when it ends.
461 The application session stores the osrfHash via an opaque pointer.
463 If the osrfHash contains an entry for the key "xact_id", it means that an
464 uncommitted transaction is pending. Roll it back.
466 void userDataFree( void* blob ) {
467 osrfHash* hash = (osrfHash*) blob;
468 if( osrfHashGet( hash, "xact_id" ) && writehandle ) {
469 if( !dbi_conn_query( writehandle, "ROLLBACK;" )) {
471 int errnum = dbi_conn_error( writehandle, &msg );
472 osrfLogWarning( OSRF_LOG_MARK, "Unable to perform rollback: %d %s",
473 errnum, msg ? msg : "(No description available)" );
477 osrfHashFree( hash );
481 @name Managing session data
482 @brief Maintain data stored via the userData pointer of the application session.
484 Currently, session-level data is stored in an osrfHash. Other arrangements are
485 possible, and some would be more efficient. The application session calls a
486 callback function to free userData before terminating.
488 Currently, the only data we store at the session level is the transaction id. By this
489 means we can ensure that any pending transactions are rolled back before the application
495 @brief Free an item in the application session's userData.
496 @param key The name of a key for an osrfHash.
497 @param item An opaque pointer to the item associated with the key.
499 We store an osrfHash as userData with the application session, and arrange (by
500 installing userDataFree() as a different callback) for the session to free that
501 osrfHash before terminating.
503 This function is a callback for freeing items in the osrfHash. Currently we store
505 - Transaction id of a pending transaction; a character string. Key: "xact_id".
506 - Authkey; a character string. Key: "authkey".
507 - User object from the authentication server; a jsonObject. Key: "user_login".
509 If we ever store anything else in userData, we will need to revisit this function so
510 that it will free whatever else needs freeing.
512 static void sessionDataFree( char* key, void* item ) {
513 if( !strcmp( key, "xact_id" ) || !strcmp( key, "authkey" ) )
515 else if( !strcmp( key, "user_login" ) )
516 jsonObjectFree( (jsonObject*) item );
517 else if( !strcmp( key, "pcache" ) )
518 osrfHashFree( (osrfHash*) item );
521 static void pcacheFree( char* key, void* item ) {
522 osrfStringArrayFree( (osrfStringArray*) item );
526 @brief Save a transaction id.
527 @param ctx Pointer to the method context.
529 Save the session_id of the current application session as a transaction id.
531 static void setXactId( osrfMethodContext* ctx ) {
532 if( ctx && ctx->session ) {
533 osrfAppSession* session = ctx->session;
535 osrfHash* cache = session->userData;
537 // If the session doesn't already have a hash, create one. Make sure
538 // that the application session frees the hash when it terminates.
539 if( NULL == cache ) {
540 session->userData = cache = osrfNewHash();
541 osrfHashSetCallback( cache, &sessionDataFree );
542 ctx->session->userDataFree = &userDataFree;
545 // Save the transaction id in the hash, with the key "xact_id"
546 osrfHashSet( cache, strdup( session->session_id ), "xact_id" );
551 @brief Get the transaction ID for the current transaction, if any.
552 @param ctx Pointer to the method context.
553 @return Pointer to the transaction ID.
555 The return value points to an internal buffer, and will become invalid upon issuing
556 a commit or rollback.
558 static inline const char* getXactId( osrfMethodContext* ctx ) {
559 if( ctx && ctx->session && ctx->session->userData )
560 return osrfHashGet( (osrfHash*) ctx->session->userData, "xact_id" );
566 @brief Clear the current transaction id.
567 @param ctx Pointer to the method context.
569 static inline void clearXactId( osrfMethodContext* ctx ) {
570 if( ctx && ctx->session && ctx->session->userData )
571 osrfHashRemove( ctx->session->userData, "xact_id" );
576 @brief Stash the location for a particular perm in the sessionData cache
577 @param ctx Pointer to the method context.
578 @param perm Name of the permission we're looking at
579 @param array StringArray of perm location ids
581 static void setPermLocationCache( osrfMethodContext* ctx, const char* perm, osrfStringArray* locations ) {
582 if( ctx && ctx->session ) {
583 osrfAppSession* session = ctx->session;
585 osrfHash* cache = session->userData;
587 // If the session doesn't already have a hash, create one. Make sure
588 // that the application session frees the hash when it terminates.
589 if( NULL == cache ) {
590 session->userData = cache = osrfNewHash();
591 osrfHashSetCallback( cache, &sessionDataFree );
592 ctx->session->userDataFree = &userDataFree;
595 osrfHash* pcache = osrfHashGet(cache, "pcache");
597 if( NULL == pcache ) {
598 pcache = osrfNewHash();
599 osrfHashSetCallback( pcache, &pcacheFree );
600 osrfHashSet( cache, pcache, "pcache" );
603 if( perm && locations )
604 osrfHashSet( pcache, locations, strdup(perm) );
609 @brief Grab stashed location for a particular perm in the sessionData cache
610 @param ctx Pointer to the method context.
611 @param perm Name of the permission we're looking at
613 static osrfStringArray* getPermLocationCache( osrfMethodContext* ctx, const char* perm ) {
614 if( ctx && ctx->session ) {
615 osrfAppSession* session = ctx->session;
616 osrfHash* cache = session->userData;
618 osrfHash* pcache = osrfHashGet(cache, "pcache");
620 return osrfHashGet( pcache, perm );
629 @brief Save the user's login in the userData for the current application session.
630 @param ctx Pointer to the method context.
631 @param user_login Pointer to the user login object to be cached (we cache the original,
634 If @a user_login is NULL, remove the user login if one is already cached.
636 static void setUserLogin( osrfMethodContext* ctx, jsonObject* user_login ) {
637 if( ctx && ctx->session ) {
638 osrfAppSession* session = ctx->session;
640 osrfHash* cache = session->userData;
642 // If the session doesn't already have a hash, create one. Make sure
643 // that the application session frees the hash when it terminates.
644 if( NULL == cache ) {
645 session->userData = cache = osrfNewHash();
646 osrfHashSetCallback( cache, &sessionDataFree );
647 ctx->session->userDataFree = &userDataFree;
651 osrfHashSet( cache, user_login, "user_login" );
653 osrfHashRemove( cache, "user_login" );
658 @brief Get the user login object for the current application session, if any.
659 @param ctx Pointer to the method context.
660 @return Pointer to the user login object if found; otherwise NULL.
662 The user login object was returned from the authentication server, and then cached so
663 we don't have to call the authentication server again for the same user.
665 static const jsonObject* getUserLogin( osrfMethodContext* ctx ) {
666 if( ctx && ctx->session && ctx->session->userData )
667 return osrfHashGet( (osrfHash*) ctx->session->userData, "user_login" );
673 @brief Save a copy of an authkey in the userData of the current application session.
674 @param ctx Pointer to the method context.
675 @param authkey The authkey to be saved.
677 If @a authkey is NULL, remove the authkey if one is already cached.
679 static void setAuthkey( osrfMethodContext* ctx, const char* authkey ) {
680 if( ctx && ctx->session && authkey ) {
681 osrfAppSession* session = ctx->session;
682 osrfHash* cache = session->userData;
684 // If the session doesn't already have a hash, create one. Make sure
685 // that the application session frees the hash when it terminates.
686 if( NULL == cache ) {
687 session->userData = cache = osrfNewHash();
688 osrfHashSetCallback( cache, &sessionDataFree );
689 ctx->session->userDataFree = &userDataFree;
692 // Save the transaction id in the hash, with the key "xact_id"
693 if( authkey && *authkey )
694 osrfHashSet( cache, strdup( authkey ), "authkey" );
696 osrfHashRemove( cache, "authkey" );
701 @brief Reset the login timeout.
702 @param authkey The authentication key for the current login session.
703 @param now The current time.
704 @return Zero if successful, or 1 if not.
706 Tell the authentication server to reset the timeout so that the login session won't
707 expire for a while longer.
709 We could dispense with the @a now parameter by calling time(). But we just called
710 time() in order to decide whether to reset the timeout, so we might as well reuse
711 the result instead of calling time() again.
713 static int reset_timeout( const char* authkey, time_t now ) {
714 jsonObject* auth_object = jsonNewObject( authkey );
716 // Ask the authentication server to reset the timeout. It returns an event
717 // indicating success or failure.
718 jsonObject* result = oilsUtilsQuickReq( "open-ils.auth",
719 "open-ils.auth.session.reset_timeout", auth_object );
720 jsonObjectFree( auth_object );
722 if( !result || result->type != JSON_HASH ) {
723 osrfLogError( OSRF_LOG_MARK,
724 "Unexpected object type receieved from open-ils.auth.session.reset_timeout" );
725 jsonObjectFree( result );
726 return 1; // Not the right sort of object returned
729 const jsonObject* ilsevent = jsonObjectGetKeyConst( result, "ilsevent" );
730 if( !ilsevent || ilsevent->type != JSON_NUMBER ) {
731 osrfLogError( OSRF_LOG_MARK, "ilsevent is absent or malformed" );
732 jsonObjectFree( result );
733 return 1; // Return code from method not available
736 if( jsonObjectGetNumber( ilsevent ) != 0.0 ) {
737 const char* desc = jsonObjectGetString( jsonObjectGetKeyConst( result, "desc" ));
739 desc = "(No reason available)"; // failsafe; shouldn't happen
740 osrfLogInfo( OSRF_LOG_MARK, "Failure to reset timeout: %s", desc );
741 jsonObjectFree( result );
745 // Revise our local proxy for the timeout deadline
746 // by a smallish fraction of the timeout interval
747 const char* timeout = jsonObjectGetString( jsonObjectGetKeyConst( result, "payload" ));
749 timeout = "1"; // failsafe; shouldn't happen
750 time_next_reset = now + atoi( timeout ) / 15;
752 jsonObjectFree( result );
753 return 0; // Successfully reset timeout
757 @brief Get the authkey string for the current application session, if any.
758 @param ctx Pointer to the method context.
759 @return Pointer to the cached authkey if found; otherwise NULL.
761 If present, the authkey string was cached from a previous method call.
763 static const char* getAuthkey( osrfMethodContext* ctx ) {
764 if( ctx && ctx->session && ctx->session->userData ) {
765 const char* authkey = osrfHashGet( (osrfHash*) ctx->session->userData, "authkey" );
767 // Possibly reset the authentication timeout to keep the login alive. We do so
768 // no more than once per method call, and not at all if it has been only a short
769 // time since the last reset.
771 // Here we reset explicitly, if at all. We also implicitly reset the timeout
772 // whenever we call the "open-ils.auth.session.retrieve" method.
773 if( timeout_needs_resetting ) {
774 time_t now = time( NULL );
775 if( now >= time_next_reset && reset_timeout( authkey, now ) )
776 authkey = NULL; // timeout has apparently expired already
779 timeout_needs_resetting = 0;
787 @brief Implement the transaction.begin method.
788 @param ctx Pointer to the method context.
789 @return Zero if successful, or -1 upon error.
791 Start a transaction. Save a transaction ID for future reference.
794 - authkey (PCRUD only)
796 Return to client: Transaction ID
798 int beginTransaction( osrfMethodContext* ctx ) {
799 if(osrfMethodVerifyContext( ctx )) {
800 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
804 if( enforce_pcrud ) {
805 timeout_needs_resetting = 1;
806 const jsonObject* user = verifyUserPCRUD( ctx );
811 dbi_result result = dbi_conn_query( writehandle, "START TRANSACTION;" );
814 int errnum = dbi_conn_error( writehandle, &msg );
815 osrfLogError( OSRF_LOG_MARK, "%s: Error starting transaction: %d %s",
816 modulename, errnum, msg ? msg : "(No description available)" );
817 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
818 "osrfMethodException", ctx->request, "Error starting transaction" );
819 if( !oilsIsDBConnected( writehandle ))
820 osrfAppSessionPanic( ctx->session );
823 dbi_result_free( result );
825 jsonObject* ret = jsonNewObject( getXactId( ctx ) );
826 osrfAppRespondComplete( ctx, ret );
827 jsonObjectFree( ret );
833 @brief Implement the savepoint.set method.
834 @param ctx Pointer to the method context.
835 @return Zero if successful, or -1 if not.
837 Issue a SAVEPOINT to the database server.
840 - authkey (PCRUD only)
843 Return to client: Savepoint name
845 int setSavepoint( osrfMethodContext* ctx ) {
846 if(osrfMethodVerifyContext( ctx )) {
847 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
852 if( enforce_pcrud ) {
854 timeout_needs_resetting = 1;
855 const jsonObject* user = verifyUserPCRUD( ctx );
860 // Verify that a transaction is pending
861 const char* trans_id = getXactId( ctx );
862 if( NULL == trans_id ) {
863 osrfAppSessionStatus(
865 OSRF_STATUS_INTERNALSERVERERROR,
866 "osrfMethodException",
868 "No active transaction -- required for savepoints"
873 // Get the savepoint name from the method params
874 const char* spName = jsonObjectGetString( jsonObjectGetIndex(ctx->params, spNamePos) );
876 dbi_result result = dbi_conn_queryf( writehandle, "SAVEPOINT \"%s\";", spName );
879 int errnum = dbi_conn_error( writehandle, &msg );
882 "%s: Error creating savepoint %s in transaction %s: %d %s",
887 msg ? msg : "(No description available)"
889 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
890 "osrfMethodException", ctx->request, "Error creating savepoint" );
891 if( !oilsIsDBConnected( writehandle ))
892 osrfAppSessionPanic( ctx->session );
895 dbi_result_free( result );
896 jsonObject* ret = jsonNewObject( spName );
897 osrfAppRespondComplete( ctx, ret );
898 jsonObjectFree( ret );
904 @brief Implement the savepoint.release method.
905 @param ctx Pointer to the method context.
906 @return Zero if successful, or -1 if not.
908 Issue a RELEASE SAVEPOINT to the database server.
911 - authkey (PCRUD only)
914 Return to client: Savepoint name
916 int releaseSavepoint( osrfMethodContext* ctx ) {
917 if(osrfMethodVerifyContext( ctx )) {
918 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
923 if( enforce_pcrud ) {
925 timeout_needs_resetting = 1;
926 const jsonObject* user = verifyUserPCRUD( ctx );
931 // Verify that a transaction is pending
932 const char* trans_id = getXactId( ctx );
933 if( NULL == trans_id ) {
934 osrfAppSessionStatus(
936 OSRF_STATUS_INTERNALSERVERERROR,
937 "osrfMethodException",
939 "No active transaction -- required for savepoints"
944 // Get the savepoint name from the method params
945 const char* spName = jsonObjectGetString( jsonObjectGetIndex(ctx->params, spNamePos) );
947 dbi_result result = dbi_conn_queryf( writehandle, "RELEASE SAVEPOINT \"%s\";", spName );
950 int errnum = dbi_conn_error( writehandle, &msg );
953 "%s: Error releasing savepoint %s in transaction %s: %d %s",
958 msg ? msg : "(No description available)"
960 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
961 "osrfMethodException", ctx->request, "Error releasing savepoint" );
962 if( !oilsIsDBConnected( writehandle ))
963 osrfAppSessionPanic( ctx->session );
966 dbi_result_free( result );
967 jsonObject* ret = jsonNewObject( spName );
968 osrfAppRespondComplete( ctx, ret );
969 jsonObjectFree( ret );
975 @brief Implement the savepoint.rollback method.
976 @param ctx Pointer to the method context.
977 @return Zero if successful, or -1 if not.
979 Issue a ROLLBACK TO SAVEPOINT to the database server.
982 - authkey (PCRUD only)
985 Return to client: Savepoint name
987 int rollbackSavepoint( osrfMethodContext* ctx ) {
988 if(osrfMethodVerifyContext( ctx )) {
989 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
994 if( enforce_pcrud ) {
996 timeout_needs_resetting = 1;
997 const jsonObject* user = verifyUserPCRUD( ctx );
1002 // Verify that a transaction is pending
1003 const char* trans_id = getXactId( ctx );
1004 if( NULL == trans_id ) {
1005 osrfAppSessionStatus(
1007 OSRF_STATUS_INTERNALSERVERERROR,
1008 "osrfMethodException",
1010 "No active transaction -- required for savepoints"
1015 // Get the savepoint name from the method params
1016 const char* spName = jsonObjectGetString( jsonObjectGetIndex(ctx->params, spNamePos) );
1018 dbi_result result = dbi_conn_queryf( writehandle, "ROLLBACK TO SAVEPOINT \"%s\";", spName );
1021 int errnum = dbi_conn_error( writehandle, &msg );
1024 "%s: Error rolling back savepoint %s in transaction %s: %d %s",
1029 msg ? msg : "(No description available)"
1031 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
1032 "osrfMethodException", ctx->request, "Error rolling back savepoint" );
1033 if( !oilsIsDBConnected( writehandle ))
1034 osrfAppSessionPanic( ctx->session );
1037 dbi_result_free( result );
1038 jsonObject* ret = jsonNewObject( spName );
1039 osrfAppRespondComplete( ctx, ret );
1040 jsonObjectFree( ret );
1046 @brief Implement the transaction.commit method.
1047 @param ctx Pointer to the method context.
1048 @return Zero if successful, or -1 if not.
1050 Issue a COMMIT to the database server.
1053 - authkey (PCRUD only)
1055 Return to client: Transaction ID.
1057 int commitTransaction( osrfMethodContext* ctx ) {
1058 if(osrfMethodVerifyContext( ctx )) {
1059 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
1063 if( enforce_pcrud ) {
1064 timeout_needs_resetting = 1;
1065 const jsonObject* user = verifyUserPCRUD( ctx );
1070 // Verify that a transaction is pending
1071 const char* trans_id = getXactId( ctx );
1072 if( NULL == trans_id ) {
1073 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
1074 "osrfMethodException", ctx->request, "No active transaction to commit" );
1078 dbi_result result = dbi_conn_query( writehandle, "COMMIT;" );
1081 int errnum = dbi_conn_error( writehandle, &msg );
1082 osrfLogError( OSRF_LOG_MARK, "%s: Error committing transaction: %d %s",
1083 modulename, errnum, msg ? msg : "(No description available)" );
1084 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
1085 "osrfMethodException", ctx->request, "Error committing transaction" );
1086 if( !oilsIsDBConnected( writehandle ))
1087 osrfAppSessionPanic( ctx->session );
1090 dbi_result_free( result );
1091 jsonObject* ret = jsonNewObject( trans_id );
1092 osrfAppRespondComplete( ctx, ret );
1093 jsonObjectFree( ret );
1100 @brief Implement the transaction.rollback method.
1101 @param ctx Pointer to the method context.
1102 @return Zero if successful, or -1 if not.
1104 Issue a ROLLBACK to the database server.
1107 - authkey (PCRUD only)
1109 Return to client: Transaction ID
1111 int rollbackTransaction( osrfMethodContext* ctx ) {
1112 if( osrfMethodVerifyContext( ctx )) {
1113 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
1117 if( enforce_pcrud ) {
1118 timeout_needs_resetting = 1;
1119 const jsonObject* user = verifyUserPCRUD( ctx );
1124 // Verify that a transaction is pending
1125 const char* trans_id = getXactId( ctx );
1126 if( NULL == trans_id ) {
1127 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
1128 "osrfMethodException", ctx->request, "No active transaction to roll back" );
1132 dbi_result result = dbi_conn_query( writehandle, "ROLLBACK;" );
1135 int errnum = dbi_conn_error( writehandle, &msg );
1136 osrfLogError( OSRF_LOG_MARK, "%s: Error rolling back transaction: %d %s",
1137 modulename, errnum, msg ? msg : "(No description available)" );
1138 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
1139 "osrfMethodException", ctx->request, "Error rolling back transaction" );
1140 if( !oilsIsDBConnected( writehandle ))
1141 osrfAppSessionPanic( ctx->session );
1144 dbi_result_free( result );
1145 jsonObject* ret = jsonNewObject( trans_id );
1146 osrfAppRespondComplete( ctx, ret );
1147 jsonObjectFree( ret );
1154 @brief Implement the "search" method.
1155 @param ctx Pointer to the method context.
1156 @return Zero if successful, or -1 if not.
1159 - authkey (PCRUD only)
1160 - WHERE clause, as jsonObject
1161 - Other SQL clause(s), as a JSON_HASH: joins, SELECT list, LIMIT, etc.
1163 Return to client: rows of the specified class that satisfy a specified WHERE clause.
1164 Optionally flesh linked fields.
1166 int doSearch( osrfMethodContext* ctx ) {
1167 if( osrfMethodVerifyContext( ctx )) {
1168 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
1173 timeout_needs_resetting = 1;
1175 jsonObject* where_clause;
1176 jsonObject* rest_of_query;
1178 if( enforce_pcrud ) {
1179 where_clause = jsonObjectGetIndex( ctx->params, 1 );
1180 rest_of_query = jsonObjectGetIndex( ctx->params, 2 );
1182 where_clause = jsonObjectGetIndex( ctx->params, 0 );
1183 rest_of_query = jsonObjectGetIndex( ctx->params, 1 );
1186 if( !where_clause ) {
1187 osrfLogError( OSRF_LOG_MARK, "No WHERE clause parameter supplied" );
1191 // Get the class metadata
1192 osrfHash* method_meta = (osrfHash*) ctx->method->userData;
1193 osrfHash* class_meta = osrfHashGet( method_meta, "class" );
1197 jsonObject* obj = doFieldmapperSearch( ctx, class_meta, where_clause, rest_of_query, &err );
1199 osrfAppRespondComplete( ctx, NULL );
1203 // Return each row to the client (except that some may be suppressed by PCRUD)
1204 jsonObject* cur = 0;
1205 unsigned long res_idx = 0;
1206 while((cur = jsonObjectGetIndex( obj, res_idx++ ) )) {
1207 if( enforce_pcrud && !verifyObjectPCRUD( ctx, cur, obj->size ))
1209 osrfAppRespond( ctx, cur );
1211 jsonObjectFree( obj );
1213 osrfAppRespondComplete( ctx, NULL );
1218 @brief Implement the "id_list" method.
1219 @param ctx Pointer to the method context.
1220 @param err Pointer through which to return an error code.
1221 @return Zero if successful, or -1 if not.
1224 - authkey (PCRUD only)
1225 - WHERE clause, as jsonObject
1226 - Other SQL clause(s), as a JSON_HASH: joins, LIMIT, etc.
1228 Return to client: The primary key values for all rows of the relevant class that
1229 satisfy a specified WHERE clause.
1231 This method relies on the assumption that every class has a primary key consisting of
1234 int doIdList( osrfMethodContext* ctx ) {
1235 if( osrfMethodVerifyContext( ctx )) {
1236 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
1241 timeout_needs_resetting = 1;
1243 jsonObject* where_clause;
1244 jsonObject* rest_of_query;
1246 // We use the where clause without change. But we need to massage the rest of the
1247 // query, so we work with a copy of it instead of modifying the original.
1249 if( enforce_pcrud ) {
1250 where_clause = jsonObjectGetIndex( ctx->params, 1 );
1251 rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 2 ) );
1253 where_clause = jsonObjectGetIndex( ctx->params, 0 );
1254 rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 1 ) );
1257 if( !where_clause ) {
1258 osrfLogError( OSRF_LOG_MARK, "No WHERE clause parameter supplied" );
1262 // Eliminate certain SQL clauses, if present.
1263 if( rest_of_query ) {
1264 jsonObjectRemoveKey( rest_of_query, "select" );
1265 jsonObjectRemoveKey( rest_of_query, "no_i18n" );
1266 jsonObjectRemoveKey( rest_of_query, "flesh" );
1267 jsonObjectRemoveKey( rest_of_query, "flesh_fields" );
1269 rest_of_query = jsonNewObjectType( JSON_HASH );
1272 jsonObjectSetKey( rest_of_query, "no_i18n", jsonNewBoolObject( 1 ) );
1274 // Get the class metadata
1275 osrfHash* method_meta = (osrfHash*) ctx->method->userData;
1276 osrfHash* class_meta = osrfHashGet( method_meta, "class" );
1278 // Build a SELECT list containing just the primary key,
1279 // i.e. like { "classname":["keyname"] }
1280 jsonObject* col_list_obj = jsonNewObjectType( JSON_ARRAY );
1282 // Load array with name of primary key
1283 jsonObjectPush( col_list_obj, jsonNewObject( osrfHashGet( class_meta, "primarykey" ) ) );
1284 jsonObject* select_clause = jsonNewObjectType( JSON_HASH );
1285 jsonObjectSetKey( select_clause, osrfHashGet( class_meta, "classname" ), col_list_obj );
1287 jsonObjectSetKey( rest_of_query, "select", select_clause );
1292 doFieldmapperSearch( ctx, class_meta, where_clause, rest_of_query, &err );
1294 jsonObjectFree( rest_of_query );
1296 osrfAppRespondComplete( ctx, NULL );
1300 // Return each primary key value to the client
1302 unsigned long res_idx = 0;
1303 while((cur = jsonObjectGetIndex( obj, res_idx++ ) )) {
1304 if( enforce_pcrud && !verifyObjectPCRUD( ctx, cur, obj->size ))
1305 continue; // Suppress due to lack of permission
1307 osrfAppRespond( ctx,
1308 oilsFMGetObject( cur, osrfHashGet( class_meta, "primarykey" ) ) );
1311 jsonObjectFree( obj );
1312 osrfAppRespondComplete( ctx, NULL );
1317 @brief Verify that we have a valid class reference.
1318 @param ctx Pointer to the method context.
1319 @param param Pointer to the method parameters.
1320 @return 1 if the class reference is valid, or zero if it isn't.
1322 The class of the method params must match the class to which the method id devoted.
1323 For PCRUD there are additional restrictions.
1325 static int verifyObjectClass ( osrfMethodContext* ctx, const jsonObject* param ) {
1327 osrfHash* method_meta = (osrfHash*) ctx->method->userData;
1328 osrfHash* class = osrfHashGet( method_meta, "class" );
1330 // Compare the method's class to the parameters' class
1331 if( !param->classname || (strcmp( osrfHashGet(class, "classname"), param->classname ))) {
1333 // Oops -- they don't match. Complain.
1334 growing_buffer* msg = buffer_init( 128 );
1337 "%s: %s method for type %s was passed a %s",
1339 osrfHashGet( method_meta, "methodtype" ),
1340 osrfHashGet( class, "classname" ),
1341 param->classname ? param->classname : "(null)"
1344 char* m = buffer_release( msg );
1345 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException",
1353 return verifyObjectPCRUD( ctx, param, 1 );
1359 @brief (PCRUD only) Verify that the user is properly logged in.
1360 @param ctx Pointer to the method context.
1361 @return If the user is logged in, a pointer to the user object from the authentication
1362 server; otherwise NULL.
1364 static const jsonObject* verifyUserPCRUD( osrfMethodContext* ctx ) {
1366 // Get the authkey (the first method parameter)
1367 const char* auth = jsonObjectGetString( jsonObjectGetIndex( ctx->params, 0 ) );
1369 // See if we have the same authkey, and a user object,
1370 // locally cached from a previous call
1371 const char* cached_authkey = getAuthkey( ctx );
1372 if( cached_authkey && !strcmp( cached_authkey, auth ) ) {
1373 const jsonObject* cached_user = getUserLogin( ctx );
1378 // We have no matching authentication data in the cache. Authenticate from scratch.
1379 jsonObject* auth_object = jsonNewObject( auth );
1381 // Fetch the user object from the authentication server
1382 jsonObject* user = oilsUtilsQuickReq( "open-ils.auth", "open-ils.auth.session.retrieve",
1384 jsonObjectFree( auth_object );
1386 if( !user->classname || strcmp(user->classname, "au" )) {
1388 growing_buffer* msg = buffer_init( 128 );
1391 "%s: permacrud received a bad auth token: %s",
1396 char* m = buffer_release( msg );
1397 osrfAppSessionStatus( ctx->session, OSRF_STATUS_UNAUTHORIZED, "osrfMethodException",
1401 jsonObjectFree( user );
1405 setUserLogin( ctx, user );
1406 setAuthkey( ctx, auth );
1408 // Allow ourselves up to a second before we have to reset the login timeout.
1409 // It would be nice to use some fraction of the timeout interval enforced by the
1410 // authentication server, but that value is not readily available at this point.
1411 // Instead, we use a conservative default interval.
1412 time_next_reset = time( NULL ) + 1;
1418 @brief For PCRUD: Determine whether the current user may access the current row.
1419 @param ctx Pointer to the method context.
1420 @param obj Pointer to the row being potentially accessed.
1421 @return 1 if access is permitted, or 0 if it isn't.
1423 The @a obj parameter points to a JSON_HASH of column values, keyed on column name.
1425 static int verifyObjectPCRUD ( osrfMethodContext* ctx, const jsonObject* obj, const int rs_size ) {
1427 dbhandle = writehandle;
1429 // Figure out what class and method are involved
1430 osrfHash* method_metadata = (osrfHash*) ctx->method->userData;
1431 osrfHash* class = osrfHashGet( method_metadata, "class" );
1432 const char* method_type = osrfHashGet( method_metadata, "methodtype" );
1434 // Set fetch to 1 in all cases except for inserts, meaning that for local or foreign
1435 // contexts we will do another lookup of the current row, even if we already have a
1436 // previously fetched row image, because the row image in hand may not include the
1437 // foreign key(s) that we need.
1439 // This is a quick fix with a bludgeon. There are ways to avoid the extra lookup,
1440 // but they aren't implemented yet.
1443 if( *method_type == 's' || *method_type == 'i' ) {
1444 method_type = "retrieve"; // search and id_list are equivalent to retrieve for this
1446 } else if( *method_type == 'u' || *method_type == 'd' ) {
1447 fetch = 1; // MUST go to the db for the object for update and delete
1450 // Get the appropriate permacrud entry from the IDL, depending on method type
1451 osrfHash* pcrud = osrfHashGet( osrfHashGet( class, "permacrud" ), method_type );
1453 // No permacrud for this method type on this class
1455 growing_buffer* msg = buffer_init( 128 );
1458 "%s: %s on class %s has no permacrud IDL entry",
1460 osrfHashGet( method_metadata, "methodtype" ),
1461 osrfHashGet( class, "classname" )
1464 char* m = buffer_release( msg );
1465 osrfAppSessionStatus( ctx->session, OSRF_STATUS_FORBIDDEN,
1466 "osrfMethodException", ctx->request, m );
1473 // Get the user id, and make sure the user is logged in
1474 const jsonObject* user = verifyUserPCRUD( ctx );
1476 return 0; // Not logged in? No access.
1478 int userid = atoi( oilsFMGetStringConst( user, "id" ) );
1480 // Get a list of permissions from the permacrud entry.
1481 osrfStringArray* permission = osrfHashGet( pcrud, "permission" );
1482 if( permission->size == 0 ) {
1483 osrfLogDebug( OSRF_LOG_MARK, "No permissions required for this action, passing through" );
1487 // Build a list of org units that own the row. This is fairly convoluted because there
1488 // are several different ways that an org unit may own the row, as defined by the
1491 // Local context means that the row includes a foreign key pointing to actor.org_unit,
1492 // identifying an owning org_unit..
1493 osrfStringArray* local_context = osrfHashGet( pcrud, "local_context" );
1495 // Foreign context adds a layer of indirection. The row points to some other row that
1496 // an org unit may own. The "jump" attribute, if present, adds another layer of
1498 osrfHash* foreign_context = osrfHashGet( pcrud, "foreign_context" );
1500 // The following string array stores the list of org units. (We don't have a thingie
1501 // for storing lists of integers, so we fake it with a list of strings.)
1502 osrfStringArray* context_org_array = osrfNewStringArray( 1 );
1505 const char* pkey_value = NULL;
1506 if( str_is_true( osrfHashGet(pcrud, "global_required") ) ) {
1507 // If the global_required attribute is present and true, then the only owning
1508 // org unit is the root org unit, i.e. the one with no parent.
1509 osrfLogDebug( OSRF_LOG_MARK,
1510 "global-level permissions required, fetching top of the org tree" );
1512 // check for perm at top of org tree
1513 const char* org_tree_root_id = org_tree_root( ctx );
1514 if( org_tree_root_id ) {
1515 osrfStringArrayAdd( context_org_array, org_tree_root_id );
1516 osrfLogDebug( OSRF_LOG_MARK, "top of the org tree is %s", org_tree_root_id );
1518 osrfStringArrayFree( context_org_array );
1523 // If the global_required attribute is absent or false, then we look for
1524 // local and/or foreign context. In order to find the relevant foreign
1525 // keys, we must either read the relevant row from the database, or look at
1526 // the image of the row that we already have in memory.
1528 // Even if we have an image of the row in memory, that image may not include the
1529 // foreign key column(s) that we need. So whenever possible, we do a fresh read
1530 // of the row to make sure that we have what we need.
1532 osrfLogDebug( OSRF_LOG_MARK, "global-level permissions not required, "
1533 "fetching context org ids" );
1534 const char* pkey = osrfHashGet( class, "primarykey" );
1535 jsonObject *param = NULL;
1538 // There is no primary key, so we can't do a fresh lookup. Use the row
1539 // image that we already have. If it doesn't have everything we need, too bad.
1541 param = jsonObjectClone( obj );
1542 osrfLogDebug( OSRF_LOG_MARK, "No primary key; using clone of object" );
1543 } else if( obj->classname ) {
1544 pkey_value = oilsFMGetStringConst( obj, pkey );
1546 param = jsonObjectClone( obj );
1547 osrfLogDebug( OSRF_LOG_MARK, "Object supplied, using primary key value of %s",
1550 pkey_value = jsonObjectGetString( obj );
1552 osrfLogDebug( OSRF_LOG_MARK, "Object not supplied, using primary key value "
1553 "of %s and retrieving from the database", pkey_value );
1557 // Fetch the row so that we can look at the foreign key(s)
1558 jsonObject* _tmp_params = single_hash( pkey, pkey_value );
1559 jsonObject* _list = doFieldmapperSearch( ctx, class, _tmp_params, NULL, &err );
1560 jsonObjectFree( _tmp_params );
1562 param = jsonObjectExtractIndex( _list, 0 );
1563 jsonObjectFree( _list );
1567 // The row doesn't exist. Complain, and deny access.
1568 osrfLogDebug( OSRF_LOG_MARK,
1569 "Object not found in the database with primary key %s of %s",
1572 growing_buffer* msg = buffer_init( 128 );
1575 "%s: no object found with primary key %s of %s",
1581 char* m = buffer_release( msg );
1582 osrfAppSessionStatus(
1584 OSRF_STATUS_INTERNALSERVERERROR,
1585 "osrfMethodException",
1594 if( local_context && local_context->size > 0 ) {
1595 // The IDL provides a list of column names for the foreign keys denoting
1596 // local context, i.e. columns identifying owing org units directly. Look up
1597 // the value of each one, and if it isn't null, add it to the list of org units.
1598 osrfLogDebug( OSRF_LOG_MARK, "%d class-local context field(s) specified",
1599 local_context->size );
1601 const char* lcontext = NULL;
1602 while ( (lcontext = osrfStringArrayGetString(local_context, i++)) ) {
1603 const char* fkey_value = oilsFMGetStringConst( param, lcontext );
1604 if( fkey_value ) { // if not null
1605 osrfStringArrayAdd( context_org_array, fkey_value );
1608 "adding class-local field %s (value: %s) to the context org list",
1610 osrfStringArrayGetString( context_org_array, context_org_array->size - 1 )
1616 if( foreign_context ) {
1617 unsigned long class_count = osrfHashGetCount( foreign_context );
1618 osrfLogDebug( OSRF_LOG_MARK, "%d foreign context classes(s) specified", class_count );
1620 if( class_count > 0 ) {
1622 // The IDL provides a list of foreign key columns pointing to rows that
1623 // an org unit may own. Follow each link, identify the owning org unit,
1624 // and add it to the list.
1625 osrfHash* fcontext = NULL;
1626 osrfHashIterator* class_itr = osrfNewHashIterator( foreign_context );
1627 while( (fcontext = osrfHashIteratorNext( class_itr )) ) {
1628 // For each class to which a foreign key points:
1629 const char* class_name = osrfHashIteratorKey( class_itr );
1630 osrfHash* fcontext = osrfHashGet( foreign_context, class_name );
1634 "%d foreign context fields(s) specified for class %s",
1635 ((osrfStringArray*)osrfHashGet(fcontext,"context"))->size,
1639 // Get the name of the key field in the foreign table
1640 const char* foreign_pkey = osrfHashGet( fcontext, "field" );
1642 // Get the value of the foreign key pointing to the foreign table
1643 char* foreign_pkey_value =
1644 oilsFMGetString( param, osrfHashGet( fcontext, "fkey" ));
1645 if( !foreign_pkey_value )
1646 continue; // Foreign key value is null; skip it
1648 // Look up the row to which the foreign key points
1649 jsonObject* _tmp_params = single_hash( foreign_pkey, foreign_pkey_value );
1650 jsonObject* _list = doFieldmapperSearch(
1651 ctx, osrfHashGet( oilsIDL(), class_name ), _tmp_params, NULL, &err );
1653 jsonObject* _fparam = NULL;
1654 if( _list && JSON_ARRAY == _list->type && _list->size > 0 )
1655 _fparam = jsonObjectExtractIndex( _list, 0 );
1657 jsonObjectFree( _tmp_params );
1658 jsonObjectFree( _list );
1660 // At this point _fparam either points to the row identified by the
1661 // foreign key, or it's NULL (no such row found).
1663 osrfStringArray* jump_list = osrfHashGet( fcontext, "jump" );
1665 const char* bad_class = NULL; // For noting failed lookups
1667 bad_class = class_name; // Referenced row not found
1668 else if( jump_list ) {
1669 // Follow a chain of rows, linked by foreign keys, to find an owner
1670 const char* flink = NULL;
1672 while ( (flink = osrfStringArrayGetString(jump_list, k++)) && _fparam ) {
1673 // For each entry in the jump list. Each entry (i.e. flink) is
1674 // the name of a foreign key column in the current row.
1676 // From the IDL, get the linkage information for the next jump
1677 osrfHash* foreign_link_hash =
1678 oilsIDLFindPath( "/%s/links/%s", _fparam->classname, flink );
1680 // Get the class metadata for the class
1681 // to which the foreign key points
1682 osrfHash* foreign_class_meta = osrfHashGet( oilsIDL(),
1683 osrfHashGet( foreign_link_hash, "class" ));
1685 // Get the name of the referenced key of that class
1686 foreign_pkey = osrfHashGet( foreign_link_hash, "key" );
1688 // Get the value of the foreign key pointing to that class
1689 free( foreign_pkey_value );
1690 foreign_pkey_value = oilsFMGetString( _fparam, flink );
1691 if( !foreign_pkey_value )
1692 break; // Foreign key is null; quit looking
1694 // Build a WHERE clause for the lookup
1695 _tmp_params = single_hash( foreign_pkey, foreign_pkey_value );
1698 _list = doFieldmapperSearch( ctx, foreign_class_meta,
1699 _tmp_params, NULL, &err );
1701 // Get the resulting row
1702 jsonObjectFree( _fparam );
1703 if( _list && JSON_ARRAY == _list->type && _list->size > 0 )
1704 _fparam = jsonObjectExtractIndex( _list, 0 );
1706 // Referenced row not found
1708 bad_class = osrfHashGet( foreign_link_hash, "class" );
1711 jsonObjectFree( _tmp_params );
1712 jsonObjectFree( _list );
1718 // We had a foreign key pointing to such-and-such a row, but then
1719 // we couldn't fetch that row. The data in the database are in an
1720 // inconsistent state; the database itself may even be corrupted.
1721 growing_buffer* msg = buffer_init( 128 );
1724 "%s: no object of class %s found with primary key %s of %s",
1728 foreign_pkey_value ? foreign_pkey_value : "(null)"
1731 char* m = buffer_release( msg );
1732 osrfAppSessionStatus(
1734 OSRF_STATUS_INTERNALSERVERERROR,
1735 "osrfMethodException",
1741 osrfHashIteratorFree( class_itr );
1742 free( foreign_pkey_value );
1743 jsonObjectFree( param );
1748 free( foreign_pkey_value );
1751 // Examine each context column of the foreign row,
1752 // and add its value to the list of org units.
1754 const char* foreign_field = NULL;
1755 osrfStringArray* ctx_array = osrfHashGet( fcontext, "context" );
1756 while ( (foreign_field = osrfStringArrayGetString( ctx_array, j++ )) ) {
1757 osrfStringArrayAdd( context_org_array,
1758 oilsFMGetStringConst( _fparam, foreign_field ));
1759 osrfLogDebug( OSRF_LOG_MARK,
1760 "adding foreign class %s field %s (value: %s) "
1761 "to the context org list",
1764 osrfStringArrayGetString(
1765 context_org_array, context_org_array->size - 1 )
1769 jsonObjectFree( _fparam );
1773 osrfHashIteratorFree( class_itr );
1777 jsonObjectFree( param );
1780 const char* context_org = NULL;
1781 const char* perm = NULL;
1784 // For every combination of permission and context org unit: call a stored procedure
1785 // to determine if the user has this permission in the context of this org unit.
1786 // If the answer is yes at any point, then we're done, and the user has permission.
1787 // In other words permissions are additive.
1789 while( (perm = osrfStringArrayGetString(permission, i++)) ) {
1792 osrfStringArray* pcache = NULL;
1793 if (rs_size > perm_at_threshold) { // grab and cache locations of user perms
1794 pcache = getPermLocationCache(ctx, perm);
1797 pcache = osrfNewStringArray(0);
1799 result = dbi_conn_queryf(
1801 "SELECT permission.usr_has_perm_at_all(%d, '%s') AS at;",
1809 "Received a result for permission [%s] for user %d",
1814 if( dbi_result_first_row( result )) {
1816 jsonObject* return_val = oilsMakeJSONFromResult( result );
1817 osrfStringArrayAdd( pcache, jsonObjectGetString( jsonObjectGetKeyConst( return_val, "at" ) ) );
1818 jsonObjectFree( return_val );
1819 } while( dbi_result_next_row( result ));
1821 setPermLocationCache(ctx, perm, pcache);
1824 dbi_result_free( result );
1830 while( (context_org = osrfStringArrayGetString( context_org_array, j++ )) ) {
1832 if (rs_size > perm_at_threshold) {
1833 if (osrfStringArrayContains( pcache, context_org )) {
1842 "Checking object permission [%s] for user %d "
1843 "on object %s (class %s) at org %d",
1847 osrfHashGet( class, "classname" ),
1851 result = dbi_conn_queryf(
1853 "SELECT permission.usr_has_object_perm(%d, '%s', '%s', '%s', %d) AS has_perm;",
1856 osrfHashGet( class, "classname" ),
1864 "Received a result for object permission [%s] "
1865 "for user %d on object %s (class %s) at org %d",
1869 osrfHashGet( class, "classname" ),
1873 if( dbi_result_first_row( result )) {
1874 jsonObject* return_val = oilsMakeJSONFromResult( result );
1875 const char* has_perm = jsonObjectGetString(
1876 jsonObjectGetKeyConst( return_val, "has_perm" ));
1880 "Status of object permission [%s] for user %d "
1881 "on object %s (class %s) at org %d is %s",
1885 osrfHashGet(class, "classname"),
1890 if( *has_perm == 't' )
1892 jsonObjectFree( return_val );
1895 dbi_result_free( result );
1900 int errnum = dbi_conn_error( writehandle, &msg );
1901 osrfLogWarning( OSRF_LOG_MARK,
1902 "Unable to call check object permissions: %d, %s",
1903 errnum, msg ? msg : "(No description available)" );
1904 if( !oilsIsDBConnected( writehandle ))
1905 osrfAppSessionPanic( ctx->session );
1909 if (rs_size > perm_at_threshold) break;
1911 osrfLogDebug( OSRF_LOG_MARK,
1912 "Checking non-object permission [%s] for user %d at org %d",
1913 perm, userid, atoi(context_org) );
1914 result = dbi_conn_queryf(
1916 "SELECT permission.usr_has_perm(%d, '%s', %d) AS has_perm;",
1923 osrfLogDebug( OSRF_LOG_MARK,
1924 "Received a result for permission [%s] for user %d at org %d",
1925 perm, userid, atoi( context_org ));
1926 if( dbi_result_first_row( result )) {
1927 jsonObject* return_val = oilsMakeJSONFromResult( result );
1928 const char* has_perm = jsonObjectGetString(
1929 jsonObjectGetKeyConst( return_val, "has_perm" ));
1930 osrfLogDebug( OSRF_LOG_MARK,
1931 "Status of permission [%s] for user %d at org %d is [%s]",
1932 perm, userid, atoi( context_org ), has_perm );
1933 if( *has_perm == 't' )
1935 jsonObjectFree( return_val );
1938 dbi_result_free( result );
1943 int errnum = dbi_conn_error( writehandle, &msg );
1944 osrfLogWarning( OSRF_LOG_MARK, "Unable to call user object permissions: %d, %s",
1945 errnum, msg ? msg : "(No description available)" );
1946 if( !oilsIsDBConnected( writehandle ))
1947 osrfAppSessionPanic( ctx->session );
1956 osrfStringArrayFree( context_org_array );
1962 @brief Look up the root of the org_unit tree.
1963 @param ctx Pointer to the method context.
1964 @return The id of the root org unit, as a character string.
1966 Query actor.org_unit where parent_ou is null, and return the id as a string.
1968 This function assumes that there is only one root org unit, i.e. that we
1969 have a single tree, not a forest.
1971 The calling code is responsible for freeing the returned string.
1973 static const char* org_tree_root( osrfMethodContext* ctx ) {
1975 static char cached_root_id[ 32 ] = ""; // extravagantly large buffer
1976 static time_t last_lookup_time = 0;
1977 time_t current_time = time( NULL );
1979 if( cached_root_id[ 0 ] && ( current_time - last_lookup_time < 3600 ) ) {
1980 // We successfully looked this up less than an hour ago.
1981 // It's not likely to have changed since then.
1982 return strdup( cached_root_id );
1984 last_lookup_time = current_time;
1987 jsonObject* where_clause = single_hash( "parent_ou", NULL );
1988 jsonObject* result = doFieldmapperSearch(
1989 ctx, osrfHashGet( oilsIDL(), "aou" ), where_clause, NULL, &err );
1990 jsonObjectFree( where_clause );
1992 jsonObject* tree_top = jsonObjectGetIndex( result, 0 );
1995 jsonObjectFree( result );
1997 growing_buffer* msg = buffer_init( 128 );
1998 OSRF_BUFFER_ADD( msg, modulename );
1999 OSRF_BUFFER_ADD( msg,
2000 ": Internal error, could not find the top of the org tree (parent_ou = NULL)" );
2002 char* m = buffer_release( msg );
2003 osrfAppSessionStatus( ctx->session,
2004 OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, m );
2007 cached_root_id[ 0 ] = '\0';
2011 const char* root_org_unit_id = oilsFMGetStringConst( tree_top, "id" );
2012 osrfLogDebug( OSRF_LOG_MARK, "Top of the org tree is %s", root_org_unit_id );
2014 strcpy( cached_root_id, root_org_unit_id );
2015 jsonObjectFree( result );
2016 return cached_root_id;
2020 @brief Create a JSON_HASH with a single key/value pair.
2021 @param key The key of the key/value pair.
2022 @param value the value of the key/value pair.
2023 @return Pointer to a newly created jsonObject of type JSON_HASH.
2025 The value of the key/value is either a string or (if @a value is NULL) a null.
2027 static jsonObject* single_hash( const char* key, const char* value ) {
2029 if( ! key ) key = "";
2031 jsonObject* hash = jsonNewObjectType( JSON_HASH );
2032 jsonObjectSetKey( hash, key, jsonNewObject( value ) );
2037 int doCreate( osrfMethodContext* ctx ) {
2038 if(osrfMethodVerifyContext( ctx )) {
2039 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
2044 timeout_needs_resetting = 1;
2046 osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
2047 jsonObject* target = NULL;
2048 jsonObject* options = NULL;
2050 if( enforce_pcrud ) {
2051 target = jsonObjectGetIndex( ctx->params, 1 );
2052 options = jsonObjectGetIndex( ctx->params, 2 );
2054 target = jsonObjectGetIndex( ctx->params, 0 );
2055 options = jsonObjectGetIndex( ctx->params, 1 );
2058 if( !verifyObjectClass( ctx, target )) {
2059 osrfAppRespondComplete( ctx, NULL );
2063 osrfLogDebug( OSRF_LOG_MARK, "Object seems to be of the correct type" );
2065 const char* trans_id = getXactId( ctx );
2067 osrfLogError( OSRF_LOG_MARK, "No active transaction -- required for CREATE" );
2069 osrfAppSessionStatus(
2071 OSRF_STATUS_BADREQUEST,
2072 "osrfMethodException",
2074 "No active transaction -- required for CREATE"
2076 osrfAppRespondComplete( ctx, NULL );
2080 // The following test is harmless but redundant. If a class is
2081 // readonly, we don't register a create method for it.
2082 if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
2083 osrfAppSessionStatus(
2085 OSRF_STATUS_BADREQUEST,
2086 "osrfMethodException",
2088 "Cannot INSERT readonly class"
2090 osrfAppRespondComplete( ctx, NULL );
2094 // Set the last_xact_id
2095 int index = oilsIDL_ntop( target->classname, "last_xact_id" );
2097 osrfLogDebug(OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d",
2098 trans_id, target->classname, index);
2099 jsonObjectSetIndex( target, index, jsonNewObject( trans_id ));
2102 osrfLogDebug( OSRF_LOG_MARK, "There is a transaction running..." );
2104 dbhandle = writehandle;
2106 osrfHash* fields = osrfHashGet( meta, "fields" );
2107 char* pkey = osrfHashGet( meta, "primarykey" );
2108 char* seq = osrfHashGet( meta, "sequence" );
2110 growing_buffer* table_buf = buffer_init( 128 );
2111 growing_buffer* col_buf = buffer_init( 128 );
2112 growing_buffer* val_buf = buffer_init( 128 );
2114 OSRF_BUFFER_ADD( table_buf, "INSERT INTO " );
2115 OSRF_BUFFER_ADD( table_buf, osrfHashGet( meta, "tablename" ));
2116 OSRF_BUFFER_ADD_CHAR( col_buf, '(' );
2117 buffer_add( val_buf,"VALUES (" );
2121 osrfHash* field = NULL;
2122 osrfHashIterator* field_itr = osrfNewHashIterator( fields );
2123 while( (field = osrfHashIteratorNext( field_itr ) ) ) {
2125 const char* field_name = osrfHashIteratorKey( field_itr );
2127 if( str_is_true( osrfHashGet( field, "virtual" ) ) )
2130 const jsonObject* field_object = oilsFMGetObject( target, field_name );
2133 if( field_object && field_object->classname ) {
2134 value = oilsFMGetString(
2136 (char*)oilsIDLFindPath( "/%s/primarykey", field_object->classname )
2138 } else if( field_object && JSON_BOOL == field_object->type ) {
2139 if( jsonBoolIsTrue( field_object ) )
2140 value = strdup( "t" );
2142 value = strdup( "f" );
2144 value = jsonObjectToSimpleString( field_object );
2150 OSRF_BUFFER_ADD_CHAR( col_buf, ',' );
2151 OSRF_BUFFER_ADD_CHAR( val_buf, ',' );
2154 buffer_add( col_buf, field_name );
2156 if( !field_object || field_object->type == JSON_NULL ) {
2157 buffer_add( val_buf, "DEFAULT" );
2159 } else if( !strcmp( get_primitive( field ), "number" )) {
2160 const char* numtype = get_datatype( field );
2161 if( !strcmp( numtype, "INT8" )) {
2162 buffer_fadd( val_buf, "%lld", atoll( value ));
2164 } else if( !strcmp( numtype, "INT" )) {
2165 buffer_fadd( val_buf, "%d", atoi( value ));
2167 } else if( !strcmp( numtype, "NUMERIC" )) {
2168 buffer_fadd( val_buf, "%f", atof( value ));
2171 if( dbi_conn_quote_string( writehandle, &value )) {
2172 OSRF_BUFFER_ADD( val_buf, value );
2175 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting string [%s]", modulename, value );
2176 osrfAppSessionStatus(
2178 OSRF_STATUS_INTERNALSERVERERROR,
2179 "osrfMethodException",
2181 "Error quoting string -- please see the error log for more details"
2184 buffer_free( table_buf );
2185 buffer_free( col_buf );
2186 buffer_free( val_buf );
2187 osrfAppRespondComplete( ctx, NULL );
2195 osrfHashIteratorFree( field_itr );
2197 OSRF_BUFFER_ADD_CHAR( col_buf, ')' );
2198 OSRF_BUFFER_ADD_CHAR( val_buf, ')' );
2200 char* table_str = buffer_release( table_buf );
2201 char* col_str = buffer_release( col_buf );
2202 char* val_str = buffer_release( val_buf );
2203 growing_buffer* sql = buffer_init( 128 );
2204 buffer_fadd( sql, "%s %s %s;", table_str, col_str, val_str );
2209 char* query = buffer_release( sql );
2211 osrfLogDebug( OSRF_LOG_MARK, "%s: Insert SQL [%s]", modulename, query );
2213 jsonObject* obj = NULL;
2216 dbi_result result = dbi_conn_query( writehandle, query );
2218 obj = jsonNewObject( NULL );
2220 int errnum = dbi_conn_error( writehandle, &msg );
2223 "%s ERROR inserting %s object using query [%s]: %d %s",
2225 osrfHashGet(meta, "fieldmapper"),
2228 msg ? msg : "(No description available)"
2230 osrfAppSessionStatus(
2232 OSRF_STATUS_INTERNALSERVERERROR,
2233 "osrfMethodException",
2235 "INSERT error -- please see the error log for more details"
2237 if( !oilsIsDBConnected( writehandle ))
2238 osrfAppSessionPanic( ctx->session );
2241 dbi_result_free( result );
2243 char* id = oilsFMGetString( target, pkey );
2245 unsigned long long new_id = dbi_conn_sequence_last( writehandle, seq );
2246 growing_buffer* _id = buffer_init( 10 );
2247 buffer_fadd( _id, "%lld", new_id );
2248 id = buffer_release( _id );
2251 // Find quietness specification, if present
2252 const char* quiet_str = NULL;
2254 const jsonObject* quiet_obj = jsonObjectGetKeyConst( options, "quiet" );
2256 quiet_str = jsonObjectGetString( quiet_obj );
2259 if( str_is_true( quiet_str )) { // if quietness is specified
2260 obj = jsonNewObject( id );
2264 // Fetch the row that we just inserted, so that we can return it to the client
2265 jsonObject* where_clause = jsonNewObjectType( JSON_HASH );
2266 jsonObjectSetKey( where_clause, pkey, jsonNewObject( id ));
2269 jsonObject* list = doFieldmapperSearch( ctx, meta, where_clause, NULL, &err );
2273 obj = jsonObjectClone( jsonObjectGetIndex( list, 0 ));
2275 jsonObjectFree( list );
2276 jsonObjectFree( where_clause );
2283 osrfAppRespondComplete( ctx, obj );
2284 jsonObjectFree( obj );
2289 @brief Implement the retrieve method.
2290 @param ctx Pointer to the method context.
2291 @param err Pointer through which to return an error code.
2292 @return If successful, a pointer to the result to be returned to the client;
2295 From the method's class, fetch a row with a specified value in the primary key. This
2296 method relies on the database design convention that a primary key consists of a single
2300 - authkey (PCRUD only)
2301 - value of the primary key for the desired row, for building the WHERE clause
2302 - a JSON_HASH containing any other SQL clauses: select, join, etc.
2304 Return to client: One row from the query.
2306 int doRetrieve( osrfMethodContext* ctx ) {
2307 if(osrfMethodVerifyContext( ctx )) {
2308 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
2313 timeout_needs_resetting = 1;
2318 if( enforce_pcrud ) {
2323 // Get the class metadata
2324 osrfHash* class_def = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
2326 // Get the value of the primary key, from a method parameter
2327 const jsonObject* id_obj = jsonObjectGetIndex( ctx->params, id_pos );
2331 "%s retrieving %s object with primary key value of %s",
2333 osrfHashGet( class_def, "fieldmapper" ),
2334 jsonObjectGetString( id_obj )
2337 // Build a WHERE clause based on the key value
2338 jsonObject* where_clause = jsonNewObjectType( JSON_HASH );
2341 osrfHashGet( class_def, "primarykey" ), // name of key column
2342 jsonObjectClone( id_obj ) // value of key column
2345 jsonObject* rest_of_query = jsonObjectGetIndex( ctx->params, order_pos );
2349 jsonObject* list = doFieldmapperSearch( ctx, class_def, where_clause, rest_of_query, &err );
2351 jsonObjectFree( where_clause );
2353 osrfAppRespondComplete( ctx, NULL );
2357 jsonObject* obj = jsonObjectExtractIndex( list, 0 );
2358 jsonObjectFree( list );
2360 if( enforce_pcrud ) {
2361 // no result, skip this entirely
2362 if(NULL != obj && !verifyObjectPCRUD( ctx, obj, 1 )) {
2363 jsonObjectFree( obj );
2365 growing_buffer* msg = buffer_init( 128 );
2366 OSRF_BUFFER_ADD( msg, modulename );
2367 OSRF_BUFFER_ADD( msg, ": Insufficient permissions to retrieve object" );
2369 char* m = buffer_release( msg );
2370 osrfAppSessionStatus( ctx->session, OSRF_STATUS_NOTALLOWED, "osrfMethodException",
2374 osrfAppRespondComplete( ctx, NULL );
2379 osrfAppRespondComplete( ctx, obj );
2380 jsonObjectFree( obj );
2385 @brief Translate a numeric value to a string representation for the database.
2386 @param field Pointer to the IDL field definition.
2387 @param value Pointer to a jsonObject holding the value of a field.
2388 @return Pointer to a newly allocated string.
2390 The input object is typically a JSON_NUMBER, but it may be a JSON_STRING as long as
2391 its contents are numeric. A non-numeric string is likely to result in invalid SQL,
2392 or (what is worse) valid SQL that is wrong.
2394 If the datatype of the receiving field is not numeric, wrap the value in quotes.
2396 The calling code is responsible for freeing the resulting string by calling free().
2398 static char* jsonNumberToDBString( osrfHash* field, const jsonObject* value ) {
2399 growing_buffer* val_buf = buffer_init( 32 );
2400 const char* numtype = get_datatype( field );
2402 // For historical reasons the following contains cruft that could be cleaned up.
2403 if( !strncmp( numtype, "INT", 3 ) ) {
2404 if( value->type == JSON_NUMBER )
2405 //buffer_fadd( val_buf, "%ld", (long)jsonObjectGetNumber(value) );
2406 buffer_fadd( val_buf, jsonObjectGetString( value ) );
2408 buffer_fadd( val_buf, jsonObjectGetString( value ) );
2411 } else if( !strcmp( numtype, "NUMERIC" )) {
2412 if( value->type == JSON_NUMBER )
2413 buffer_fadd( val_buf, jsonObjectGetString( value ));
2415 buffer_fadd( val_buf, jsonObjectGetString( value ));
2419 // Presumably this was really intended to be a string, so quote it
2420 char* str = jsonObjectToSimpleString( value );
2421 if( dbi_conn_quote_string( dbhandle, &str )) {
2422 OSRF_BUFFER_ADD( val_buf, str );
2425 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key string [%s]", modulename, str );
2427 buffer_free( val_buf );
2432 return buffer_release( val_buf );
2435 static char* searchINPredicate( const char* class_alias, osrfHash* field,
2436 jsonObject* node, const char* op, osrfMethodContext* ctx ) {
2437 growing_buffer* sql_buf = buffer_init( 32 );
2443 osrfHashGet( field, "name" )
2447 buffer_add( sql_buf, "IN (" );
2448 } else if( !strcasecmp( op,"not in" )) {
2449 buffer_add( sql_buf, "NOT IN (" );
2451 buffer_add( sql_buf, "IN (" );
2454 if( node->type == JSON_HASH ) {
2455 // subquery predicate
2456 char* subpred = buildQuery( ctx, node, SUBSELECT );
2458 buffer_free( sql_buf );
2462 buffer_add( sql_buf, subpred );
2465 } else if( node->type == JSON_ARRAY ) {
2466 // literal value list
2467 int in_item_index = 0;
2468 int in_item_first = 1;
2469 const jsonObject* in_item;
2470 while( (in_item = jsonObjectGetIndex( node, in_item_index++ )) ) {
2475 buffer_add( sql_buf, ", " );
2478 if( in_item->type != JSON_STRING && in_item->type != JSON_NUMBER ) {
2479 osrfLogError( OSRF_LOG_MARK,
2480 "%s: Expected string or number within IN list; found %s",
2481 modulename, json_type( in_item->type ) );
2482 buffer_free( sql_buf );
2486 // Append the literal value -- quoted if not a number
2487 if( JSON_NUMBER == in_item->type ) {
2488 char* val = jsonNumberToDBString( field, in_item );
2489 OSRF_BUFFER_ADD( sql_buf, val );
2492 } else if( !strcmp( get_primitive( field ), "number" )) {
2493 char* val = jsonNumberToDBString( field, in_item );
2494 OSRF_BUFFER_ADD( sql_buf, val );
2498 char* key_string = jsonObjectToSimpleString( in_item );
2499 if( dbi_conn_quote_string( dbhandle, &key_string )) {
2500 OSRF_BUFFER_ADD( sql_buf, key_string );
2503 osrfLogError( OSRF_LOG_MARK,
2504 "%s: Error quoting key string [%s]", modulename, key_string );
2506 buffer_free( sql_buf );
2512 if( in_item_first ) {
2513 osrfLogError(OSRF_LOG_MARK, "%s: Empty IN list", modulename );
2514 buffer_free( sql_buf );
2518 osrfLogError( OSRF_LOG_MARK, "%s: Expected object or array for IN clause; found %s",
2519 modulename, json_type( node->type ));
2520 buffer_free( sql_buf );
2524 OSRF_BUFFER_ADD_CHAR( sql_buf, ')' );
2526 return buffer_release( sql_buf );
2529 // Receive a JSON_ARRAY representing a function call. The first
2530 // entry in the array is the function name. The rest are parameters.
2531 static char* searchValueTransform( const jsonObject* array ) {
2533 if( array->size < 1 ) {
2534 osrfLogError( OSRF_LOG_MARK, "%s: Empty array for value transform", modulename );
2538 // Get the function name
2539 jsonObject* func_item = jsonObjectGetIndex( array, 0 );
2540 if( func_item->type != JSON_STRING ) {
2541 osrfLogError( OSRF_LOG_MARK, "%s: Error: expected function name, found %s",
2542 modulename, json_type( func_item->type ));
2546 growing_buffer* sql_buf = buffer_init( 32 );
2548 OSRF_BUFFER_ADD( sql_buf, jsonObjectGetString( func_item ) );
2549 OSRF_BUFFER_ADD( sql_buf, "( " );
2551 // Get the parameters
2552 int func_item_index = 1; // We already grabbed the zeroth entry
2553 while( (func_item = jsonObjectGetIndex( array, func_item_index++ )) ) {
2555 // Add a separator comma, if we need one
2556 if( func_item_index > 2 )
2557 buffer_add( sql_buf, ", " );
2559 // Add the current parameter
2560 if( func_item->type == JSON_NULL ) {
2561 buffer_add( sql_buf, "NULL" );
2563 char* val = jsonObjectToSimpleString( func_item );
2564 if( dbi_conn_quote_string( dbhandle, &val )) {
2565 OSRF_BUFFER_ADD( sql_buf, val );
2568 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key string [%s]",
2570 buffer_free( sql_buf );
2577 buffer_add( sql_buf, " )" );
2579 return buffer_release( sql_buf );
2582 static char* searchFunctionPredicate( const char* class_alias, osrfHash* field,
2583 const jsonObject* node, const char* op ) {
2585 if( ! is_good_operator( op ) ) {
2586 osrfLogError( OSRF_LOG_MARK, "%s: Invalid operator [%s]", modulename, op );
2590 char* val = searchValueTransform( node );
2594 growing_buffer* sql_buf = buffer_init( 32 );
2599 osrfHashGet( field, "name" ),
2606 return buffer_release( sql_buf );
2609 // class_alias is a class name or other table alias
2610 // field is a field definition as stored in the IDL
2611 // node comes from the method parameter, and may represent an entry in the SELECT list
2612 static char* searchFieldTransform( const char* class_alias, osrfHash* field,
2613 const jsonObject* node ) {
2614 growing_buffer* sql_buf = buffer_init( 32 );
2616 const char* field_transform = jsonObjectGetString(
2617 jsonObjectGetKeyConst( node, "transform" ) );
2618 const char* transform_subcolumn = jsonObjectGetString(
2619 jsonObjectGetKeyConst( node, "result_field" ) );
2621 if( transform_subcolumn ) {
2622 if( ! is_identifier( transform_subcolumn ) ) {
2623 osrfLogError( OSRF_LOG_MARK, "%s: Invalid subfield name: \"%s\"\n",
2624 modulename, transform_subcolumn );
2625 buffer_free( sql_buf );
2628 OSRF_BUFFER_ADD_CHAR( sql_buf, '(' ); // enclose transform in parentheses
2631 if( field_transform ) {
2633 if( ! is_identifier( field_transform ) ) {
2634 osrfLogError( OSRF_LOG_MARK, "%s: Expected function name, found \"%s\"\n",
2635 modulename, field_transform );
2636 buffer_free( sql_buf );
2640 if( obj_is_true( jsonObjectGetKeyConst( node, "distinct" ) ) ) {
2641 buffer_fadd( sql_buf, "%s(DISTINCT \"%s\".%s",
2642 field_transform, class_alias, osrfHashGet( field, "name" ));
2644 buffer_fadd( sql_buf, "%s(\"%s\".%s",
2645 field_transform, class_alias, osrfHashGet( field, "name" ));
2648 const jsonObject* array = jsonObjectGetKeyConst( node, "params" );
2651 if( array->type != JSON_ARRAY ) {
2652 osrfLogError( OSRF_LOG_MARK,
2653 "%s: Expected JSON_ARRAY for function params; found %s",
2654 modulename, json_type( array->type ) );
2655 buffer_free( sql_buf );
2658 int func_item_index = 0;
2659 jsonObject* func_item;
2660 while( (func_item = jsonObjectGetIndex( array, func_item_index++ ))) {
2662 char* val = jsonObjectToSimpleString( func_item );
2665 buffer_add( sql_buf, ",NULL" );
2666 } else if( dbi_conn_quote_string( dbhandle, &val )) {
2667 OSRF_BUFFER_ADD_CHAR( sql_buf, ',' );
2668 OSRF_BUFFER_ADD( sql_buf, val );
2670 osrfLogError( OSRF_LOG_MARK,
2671 "%s: Error quoting key string [%s]", modulename, val );
2673 buffer_free( sql_buf );
2680 buffer_add( sql_buf, " )" );
2683 buffer_fadd( sql_buf, "\"%s\".%s", class_alias, osrfHashGet( field, "name" ));
2686 if( transform_subcolumn )
2687 buffer_fadd( sql_buf, ").\"%s\"", transform_subcolumn );
2689 return buffer_release( sql_buf );
2692 static char* searchFieldTransformPredicate( const ClassInfo* class_info, osrfHash* field,
2693 const jsonObject* node, const char* op ) {
2695 if( ! is_good_operator( op ) ) {
2696 osrfLogError( OSRF_LOG_MARK, "%s: Error: Invalid operator %s", modulename, op );
2700 char* field_transform = searchFieldTransform( class_info->alias, field, node );
2701 if( ! field_transform )
2704 int extra_parens = 0; // boolean
2706 const jsonObject* value_obj = jsonObjectGetKeyConst( node, "value" );
2708 value = searchWHERE( node, class_info, AND_OP_JOIN, NULL );
2710 osrfLogError( OSRF_LOG_MARK, "%s: Error building condition for field transform",
2712 free( field_transform );
2716 } else if( value_obj->type == JSON_ARRAY ) {
2717 value = searchValueTransform( value_obj );
2719 osrfLogError( OSRF_LOG_MARK,
2720 "%s: Error building value transform for field transform", modulename );
2721 free( field_transform );
2724 } else if( value_obj->type == JSON_HASH ) {
2725 value = searchWHERE( value_obj, class_info, AND_OP_JOIN, NULL );
2727 osrfLogError( OSRF_LOG_MARK, "%s: Error building predicate for field transform",
2729 free( field_transform );
2733 } else if( value_obj->type == JSON_NUMBER ) {
2734 value = jsonNumberToDBString( field, value_obj );
2735 } else if( value_obj->type == JSON_NULL ) {
2736 osrfLogError( OSRF_LOG_MARK,
2737 "%s: Error building predicate for field transform: null value", modulename );
2738 free( field_transform );
2740 } else if( value_obj->type == JSON_BOOL ) {
2741 osrfLogError( OSRF_LOG_MARK,
2742 "%s: Error building predicate for field transform: boolean value", modulename );
2743 free( field_transform );
2746 if( !strcmp( get_primitive( field ), "number") ) {
2747 value = jsonNumberToDBString( field, value_obj );
2749 value = jsonObjectToSimpleString( value_obj );
2750 if( !dbi_conn_quote_string( dbhandle, &value )) {
2751 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key string [%s]",
2752 modulename, value );
2754 free( field_transform );
2760 const char* left_parens = "";
2761 const char* right_parens = "";
2763 if( extra_parens ) {
2768 const char* right_percent = "";
2769 const char* real_op = op;
2771 if( !strcasecmp( op, "startwith") ) {
2773 right_percent = "|| '%'";
2776 growing_buffer* sql_buf = buffer_init( 32 );
2780 "%s%s %s %s %s%s %s%s",
2792 free( field_transform );
2794 return buffer_release( sql_buf );
2797 static char* searchSimplePredicate( const char* op, const char* class_alias,
2798 osrfHash* field, const jsonObject* node ) {
2800 if( ! is_good_operator( op ) ) {
2801 osrfLogError( OSRF_LOG_MARK, "%s: Invalid operator [%s]", modulename, op );
2807 // Get the value to which we are comparing the specified column
2808 if( node->type != JSON_NULL ) {
2809 if( node->type == JSON_NUMBER ) {
2810 val = jsonNumberToDBString( field, node );
2811 } else if( !strcmp( get_primitive( field ), "number" ) ) {
2812 val = jsonNumberToDBString( field, node );
2814 val = jsonObjectToSimpleString( node );
2819 if( JSON_NUMBER != node->type && strcmp( get_primitive( field ), "number") ) {
2820 // Value is not numeric; enclose it in quotes
2821 if( !dbi_conn_quote_string( dbhandle, &val ) ) {
2822 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key string [%s]",
2829 // Compare to a null value
2830 val = strdup( "NULL" );
2831 if( strcmp( op, "=" ))
2837 growing_buffer* sql_buf = buffer_init( 32 );
2838 buffer_fadd( sql_buf, "\"%s\".%s %s %s", class_alias, osrfHashGet(field, "name"), op, val );
2839 char* pred = buffer_release( sql_buf );
2846 static char* searchBETWEENPredicate( const char* class_alias,
2847 osrfHash* field, const jsonObject* node ) {
2849 const jsonObject* x_node = jsonObjectGetIndex( node, 0 );
2850 const jsonObject* y_node = jsonObjectGetIndex( node, 1 );
2852 if( NULL == y_node ) {
2853 osrfLogError( OSRF_LOG_MARK, "%s: Not enough operands for BETWEEN operator", modulename );
2856 else if( NULL != jsonObjectGetIndex( node, 2 ) ) {
2857 osrfLogError( OSRF_LOG_MARK, "%s: Too many operands for BETWEEN operator", modulename );
2864 if( !strcmp( get_primitive( field ), "number") ) {
2865 x_string = jsonNumberToDBString( field, x_node );
2866 y_string = jsonNumberToDBString( field, y_node );
2869 x_string = jsonObjectToSimpleString( x_node );
2870 y_string = jsonObjectToSimpleString( y_node );
2871 if( !(dbi_conn_quote_string( dbhandle, &x_string )
2872 && dbi_conn_quote_string( dbhandle, &y_string )) ) {
2873 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key strings [%s] and [%s]",
2874 modulename, x_string, y_string );
2881 growing_buffer* sql_buf = buffer_init( 32 );
2882 buffer_fadd( sql_buf, "\"%s\".%s BETWEEN %s AND %s",
2883 class_alias, osrfHashGet( field, "name" ), x_string, y_string );
2887 return buffer_release( sql_buf );
2890 static char* searchPredicate( const ClassInfo* class_info, osrfHash* field,
2891 jsonObject* node, osrfMethodContext* ctx ) {
2894 if( node->type == JSON_ARRAY ) { // equality IN search
2895 pred = searchINPredicate( class_info->alias, field, node, NULL, ctx );
2896 } else if( node->type == JSON_HASH ) { // other search
2897 jsonIterator* pred_itr = jsonNewIterator( node );
2898 if( !jsonIteratorHasNext( pred_itr ) ) {
2899 osrfLogError( OSRF_LOG_MARK, "%s: Empty predicate for field \"%s\"",
2900 modulename, osrfHashGet(field, "name" ));
2902 jsonObject* pred_node = jsonIteratorNext( pred_itr );
2904 // Verify that there are no additional predicates
2905 if( jsonIteratorHasNext( pred_itr ) ) {
2906 osrfLogError( OSRF_LOG_MARK, "%s: Multiple predicates for field \"%s\"",
2907 modulename, osrfHashGet(field, "name" ));
2908 } else if( !(strcasecmp( pred_itr->key,"between" )) )
2909 pred = searchBETWEENPredicate( class_info->alias, field, pred_node );
2910 else if( !(strcasecmp( pred_itr->key,"in" ))
2911 || !(strcasecmp( pred_itr->key,"not in" )) )
2912 pred = searchINPredicate(
2913 class_info->alias, field, pred_node, pred_itr->key, ctx );
2914 else if( pred_node->type == JSON_ARRAY )
2915 pred = searchFunctionPredicate(
2916 class_info->alias, field, pred_node, pred_itr->key );
2917 else if( pred_node->type == JSON_HASH )
2918 pred = searchFieldTransformPredicate(
2919 class_info, field, pred_node, pred_itr->key );
2921 pred = searchSimplePredicate( pred_itr->key, class_info->alias, field, pred_node );
2923 jsonIteratorFree( pred_itr );
2925 } else if( node->type == JSON_NULL ) { // IS NULL search
2926 growing_buffer* _p = buffer_init( 64 );
2929 "\"%s\".%s IS NULL",
2930 class_info->class_name,
2931 osrfHashGet( field, "name" )
2933 pred = buffer_release( _p );
2934 } else { // equality search
2935 pred = searchSimplePredicate( "=", class_info->alias, field, node );
2954 field : call_number,
2970 static char* searchJOIN( const jsonObject* join_hash, const ClassInfo* left_info ) {
2972 const jsonObject* working_hash;
2973 jsonObject* freeable_hash = NULL;
2975 if( join_hash->type == JSON_HASH ) {
2976 working_hash = join_hash;
2977 } else if( join_hash->type == JSON_STRING ) {
2978 // turn it into a JSON_HASH by creating a wrapper
2979 // around a copy of the original
2980 const char* _tmp = jsonObjectGetString( join_hash );
2981 freeable_hash = jsonNewObjectType( JSON_HASH );
2982 jsonObjectSetKey( freeable_hash, _tmp, NULL );
2983 working_hash = freeable_hash;
2987 "%s: JOIN failed; expected JSON object type not found",
2993 growing_buffer* join_buf = buffer_init( 128 );
2994 const char* leftclass = left_info->class_name;
2996 jsonObject* snode = NULL;
2997 jsonIterator* search_itr = jsonNewIterator( working_hash );
2999 while ( (snode = jsonIteratorNext( search_itr )) ) {
3000 const char* right_alias = search_itr->key;
3002 jsonObjectGetString( jsonObjectGetKeyConst( snode, "class" ) );
3004 class = right_alias;
3006 const ClassInfo* right_info = add_joined_class( right_alias, class );
3010 "%s: JOIN failed. Class \"%s\" not resolved in IDL",
3014 jsonIteratorFree( search_itr );
3015 buffer_free( join_buf );
3017 jsonObjectFree( freeable_hash );
3020 osrfHash* links = right_info->links;
3021 const char* table = right_info->source_def;
3023 const char* fkey = jsonObjectGetString( jsonObjectGetKeyConst( snode, "fkey" ) );
3024 const char* field = jsonObjectGetString( jsonObjectGetKeyConst( snode, "field" ) );
3026 if( field && !fkey ) {
3027 // Look up the corresponding join column in the IDL.
3028 // The link must be defined in the child table,
3029 // and point to the right parent table.
3030 osrfHash* idl_link = (osrfHash*) osrfHashGet( links, field );
3031 const char* reltype = NULL;
3032 const char* other_class = NULL;
3033 reltype = osrfHashGet( idl_link, "reltype" );
3034 if( reltype && strcmp( reltype, "has_many" ) )
3035 other_class = osrfHashGet( idl_link, "class" );
3036 if( other_class && !strcmp( other_class, leftclass ) )
3037 fkey = osrfHashGet( idl_link, "key" );
3041 "%s: JOIN failed. No link defined from %s.%s to %s",
3047 buffer_free( join_buf );
3049 jsonObjectFree( freeable_hash );
3050 jsonIteratorFree( search_itr );
3054 } else if( !field && fkey ) {
3055 // Look up the corresponding join column in the IDL.
3056 // The link must be defined in the child table,
3057 // and point to the right parent table.
3058 osrfHash* left_links = left_info->links;
3059 osrfHash* idl_link = (osrfHash*) osrfHashGet( left_links, fkey );
3060 const char* reltype = NULL;
3061 const char* other_class = NULL;
3062 reltype = osrfHashGet( idl_link, "reltype" );
3063 if( reltype && strcmp( reltype, "has_many" ) )
3064 other_class = osrfHashGet( idl_link, "class" );
3065 if( other_class && !strcmp( other_class, class ) )
3066 field = osrfHashGet( idl_link, "key" );
3070 "%s: JOIN failed. No link defined from %s.%s to %s",
3076 buffer_free( join_buf );
3078 jsonObjectFree( freeable_hash );
3079 jsonIteratorFree( search_itr );
3083 } else if( !field && !fkey ) {
3084 osrfHash* left_links = left_info->links;
3086 // For each link defined for the left class:
3087 // see if the link references the joined class
3088 osrfHashIterator* itr = osrfNewHashIterator( left_links );
3089 osrfHash* curr_link = NULL;
3090 while( (curr_link = osrfHashIteratorNext( itr ) ) ) {
3091 const char* other_class = osrfHashGet( curr_link, "class" );
3092 if( other_class && !strcmp( other_class, class ) ) {
3094 // In the IDL, the parent class doesn't always know then names of the child
3095 // columns that are pointing to it, so don't use that end of the link
3096 const char* reltype = osrfHashGet( curr_link, "reltype" );
3097 if( reltype && strcmp( reltype, "has_many" ) ) {
3098 // Found a link between the classes
3099 fkey = osrfHashIteratorKey( itr );
3100 field = osrfHashGet( curr_link, "key" );
3105 osrfHashIteratorFree( itr );
3107 if( !field || !fkey ) {
3108 // Do another such search, with the classes reversed
3110 // For each link defined for the joined class:
3111 // see if the link references the left class
3112 osrfHashIterator* itr = osrfNewHashIterator( links );
3113 osrfHash* curr_link = NULL;
3114 while( (curr_link = osrfHashIteratorNext( itr ) ) ) {
3115 const char* other_class = osrfHashGet( curr_link, "class" );
3116 if( other_class && !strcmp( other_class, leftclass ) ) {
3118 // In the IDL, the parent class doesn't know then names of the child
3119 // columns that are pointing to it, so don't use that end of the link
3120 const char* reltype = osrfHashGet( curr_link, "reltype" );
3121 if( reltype && strcmp( reltype, "has_many" ) ) {
3122 // Found a link between the classes
3123 field = osrfHashIteratorKey( itr );
3124 fkey = osrfHashGet( curr_link, "key" );
3129 osrfHashIteratorFree( itr );
3132 if( !field || !fkey ) {
3135 "%s: JOIN failed. No link defined between %s and %s",
3140 buffer_free( join_buf );
3142 jsonObjectFree( freeable_hash );
3143 jsonIteratorFree( search_itr );
3148 const char* type = jsonObjectGetString( jsonObjectGetKeyConst( snode, "type" ) );
3150 if( !strcasecmp( type,"left" )) {
3151 buffer_add( join_buf, " LEFT JOIN" );
3152 } else if( !strcasecmp( type,"right" )) {
3153 buffer_add( join_buf, " RIGHT JOIN" );
3154 } else if( !strcasecmp( type,"full" )) {
3155 buffer_add( join_buf, " FULL JOIN" );
3157 buffer_add( join_buf, " INNER JOIN" );
3160 buffer_add( join_buf, " INNER JOIN" );
3163 buffer_fadd( join_buf, " %s AS \"%s\" ON ( \"%s\".%s = \"%s\".%s",
3164 table, right_alias, right_alias, field, left_info->alias, fkey );
3166 // Add any other join conditions as specified by "filter"
3167 const jsonObject* filter = jsonObjectGetKeyConst( snode, "filter" );
3169 const char* filter_op = jsonObjectGetString(
3170 jsonObjectGetKeyConst( snode, "filter_op" ) );
3171 if( filter_op && !strcasecmp( "or",filter_op )) {
3172 buffer_add( join_buf, " OR " );
3174 buffer_add( join_buf, " AND " );
3177 char* jpred = searchWHERE( filter, right_info, AND_OP_JOIN, NULL );
3179 OSRF_BUFFER_ADD_CHAR( join_buf, ' ' );
3180 OSRF_BUFFER_ADD( join_buf, jpred );
3185 "%s: JOIN failed. Invalid conditional expression.",
3188 jsonIteratorFree( search_itr );
3189 buffer_free( join_buf );
3191 jsonObjectFree( freeable_hash );
3196 buffer_add( join_buf, " ) " );
3198 // Recursively add a nested join, if one is present
3199 const jsonObject* join_filter = jsonObjectGetKeyConst( snode, "join" );
3201 char* jpred = searchJOIN( join_filter, right_info );
3203 OSRF_BUFFER_ADD_CHAR( join_buf, ' ' );
3204 OSRF_BUFFER_ADD( join_buf, jpred );
3207 osrfLogError( OSRF_LOG_MARK, "%s: Invalid nested join.", modulename );
3208 jsonIteratorFree( search_itr );
3209 buffer_free( join_buf );
3211 jsonObjectFree( freeable_hash );
3218 jsonObjectFree( freeable_hash );
3219 jsonIteratorFree( search_itr );
3221 return buffer_release( join_buf );
3226 { +class : { -or|-and : { field : { op : value }, ... } ... }, ... }
3227 { +class : { -or|-and : [ { field : { op : value }, ... }, ...] ... }, ... }
3228 [ { +class : { -or|-and : [ { field : { op : value }, ... }, ...] ... }, ... }, ... ]
3230 Generate code to express a set of conditions, as for a WHERE clause. Parameters:
3232 search_hash is the JSON expression of the conditions.
3233 meta is the class definition from the IDL, for the relevant table.
3234 opjoin_type indicates whether multiple conditions, if present, should be
3235 connected by AND or OR.
3236 osrfMethodContext is loaded with all sorts of stuff, but all we do with it here is
3237 to pass it to other functions -- and all they do with it is to use the session
3238 and request members to send error messages back to the client.
3242 static char* searchWHERE( const jsonObject* search_hash, const ClassInfo* class_info,
3243 int opjoin_type, osrfMethodContext* ctx ) {
3247 "%s: Entering searchWHERE; search_hash addr = %p, meta addr = %p, "
3248 "opjoin_type = %d, ctx addr = %p",
3251 class_info->class_def,
3256 growing_buffer* sql_buf = buffer_init( 128 );
3258 jsonObject* node = NULL;
3261 if( search_hash->type == JSON_ARRAY ) {
3262 if( 0 == search_hash->size ) {
3265 "%s: Invalid predicate structure: empty JSON array",
3268 buffer_free( sql_buf );
3272 unsigned long i = 0;
3273 while(( node = jsonObjectGetIndex( search_hash, i++ ) )) {
3277 if( opjoin_type == OR_OP_JOIN )
3278 buffer_add( sql_buf, " OR " );
3280 buffer_add( sql_buf, " AND " );
3283 char* subpred = searchWHERE( node, class_info, opjoin_type, ctx );
3285 buffer_free( sql_buf );
3289 buffer_fadd( sql_buf, "( %s )", subpred );
3293 } else if( search_hash->type == JSON_HASH ) {
3294 osrfLogDebug( OSRF_LOG_MARK,
3295 "%s: In WHERE clause, condition type is JSON_HASH", modulename );
3296 jsonIterator* search_itr = jsonNewIterator( search_hash );
3297 if( !jsonIteratorHasNext( search_itr ) ) {
3300 "%s: Invalid predicate structure: empty JSON object",
3303 jsonIteratorFree( search_itr );
3304 buffer_free( sql_buf );
3308 while( (node = jsonIteratorNext( search_itr )) ) {
3313 if( opjoin_type == OR_OP_JOIN )
3314 buffer_add( sql_buf, " OR " );
3316 buffer_add( sql_buf, " AND " );
3319 if( '+' == search_itr->key[ 0 ] ) {
3321 // This plus sign prefixes a class name or other table alias;
3322 // make sure the table alias is in scope
3323 ClassInfo* alias_info = search_all_alias( search_itr->key + 1 );
3324 if( ! alias_info ) {
3327 "%s: Invalid table alias \"%s\" in WHERE clause",
3331 jsonIteratorFree( search_itr );
3332 buffer_free( sql_buf );
3336 if( node->type == JSON_STRING ) {
3337 // It's the name of a column; make sure it belongs to the class
3338 const char* fieldname = jsonObjectGetString( node );
3339 if( ! osrfHashGet( alias_info->fields, fieldname ) ) {
3342 "%s: Invalid column name \"%s\" in WHERE clause "
3343 "for table alias \"%s\"",
3348 jsonIteratorFree( search_itr );
3349 buffer_free( sql_buf );
3353 buffer_fadd( sql_buf, " \"%s\".%s ", alias_info->alias, fieldname );
3355 // It's something more complicated
3356 char* subpred = searchWHERE( node, alias_info, AND_OP_JOIN, ctx );
3358 jsonIteratorFree( search_itr );
3359 buffer_free( sql_buf );
3363 buffer_fadd( sql_buf, "( %s )", subpred );
3366 } else if( '-' == search_itr->key[ 0 ] ) {
3367 if( !strcasecmp( "-or", search_itr->key )) {
3368 char* subpred = searchWHERE( node, class_info, OR_OP_JOIN, ctx );
3370 jsonIteratorFree( search_itr );
3371 buffer_free( sql_buf );
3375 buffer_fadd( sql_buf, "( %s )", subpred );
3377 } else if( !strcasecmp( "-and", search_itr->key )) {
3378 char* subpred = searchWHERE( node, class_info, AND_OP_JOIN, ctx );
3380 jsonIteratorFree( search_itr );
3381 buffer_free( sql_buf );
3385 buffer_fadd( sql_buf, "( %s )", subpred );
3387 } else if( !strcasecmp("-not",search_itr->key) ) {
3388 char* subpred = searchWHERE( node, class_info, AND_OP_JOIN, ctx );
3390 jsonIteratorFree( search_itr );
3391 buffer_free( sql_buf );
3395 buffer_fadd( sql_buf, " NOT ( %s )", subpred );
3397 } else if( !strcasecmp( "-exists", search_itr->key )) {
3398 char* subpred = buildQuery( ctx, node, SUBSELECT );
3400 jsonIteratorFree( search_itr );
3401 buffer_free( sql_buf );
3405 buffer_fadd( sql_buf, "EXISTS ( %s )", subpred );
3407 } else if( !strcasecmp("-not-exists", search_itr->key )) {
3408 char* subpred = buildQuery( ctx, node, SUBSELECT );
3410 jsonIteratorFree( search_itr );
3411 buffer_free( sql_buf );
3415 buffer_fadd( sql_buf, "NOT EXISTS ( %s )", subpred );
3417 } else { // Invalid "minus" operator
3420 "%s: Invalid operator \"%s\" in WHERE clause",
3424 jsonIteratorFree( search_itr );
3425 buffer_free( sql_buf );
3431 const char* class = class_info->class_name;
3432 osrfHash* fields = class_info->fields;
3433 osrfHash* field = osrfHashGet( fields, search_itr->key );
3436 const char* table = class_info->source_def;
3439 "%s: Attempt to reference non-existent column \"%s\" on %s (%s)",
3442 table ? table : "?",
3445 jsonIteratorFree( search_itr );
3446 buffer_free( sql_buf );
3450 char* subpred = searchPredicate( class_info, field, node, ctx );
3452 buffer_free( sql_buf );
3453 jsonIteratorFree( search_itr );
3457 buffer_add( sql_buf, subpred );
3461 jsonIteratorFree( search_itr );
3464 // ERROR ... only hash and array allowed at this level
3465 char* predicate_string = jsonObjectToJSON( search_hash );
3468 "%s: Invalid predicate structure: %s",
3472 buffer_free( sql_buf );
3473 free( predicate_string );
3477 return buffer_release( sql_buf );
3480 /* Build a JSON_ARRAY of field names for a given table alias
3482 static jsonObject* defaultSelectList( const char* table_alias ) {
3487 ClassInfo* class_info = search_all_alias( table_alias );
3488 if( ! class_info ) {
3491 "%s: Can't build default SELECT clause for \"%s\"; no such table alias",
3498 jsonObject* array = jsonNewObjectType( JSON_ARRAY );
3499 osrfHash* field_def = NULL;
3500 osrfHashIterator* field_itr = osrfNewHashIterator( class_info->fields );
3501 while( ( field_def = osrfHashIteratorNext( field_itr ) ) ) {
3502 const char* field_name = osrfHashIteratorKey( field_itr );
3503 if( ! str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3504 jsonObjectPush( array, jsonNewObject( field_name ) );
3507 osrfHashIteratorFree( field_itr );
3512 // Translate a jsonObject into a UNION, INTERSECT, or EXCEPT query.
3513 // The jsonObject must be a JSON_HASH with an single entry for "union",
3514 // "intersect", or "except". The data associated with this key must be an
3515 // array of hashes, each hash being a query.
3516 // Also allowed but currently ignored: entries for "order_by" and "alias".
3517 static char* doCombo( osrfMethodContext* ctx, jsonObject* combo, int flags ) {
3519 if( ! combo || combo->type != JSON_HASH )
3520 return NULL; // should be impossible; validated by caller
3522 const jsonObject* query_array = NULL; // array of subordinate queries
3523 const char* op = NULL; // name of operator, e.g. UNION
3524 const char* alias = NULL; // alias for the query (needed for ORDER BY)
3525 int op_count = 0; // for detecting conflicting operators
3526 int excepting = 0; // boolean
3527 int all = 0; // boolean
3528 jsonObject* order_obj = NULL;
3530 // Identify the elements in the hash
3531 jsonIterator* query_itr = jsonNewIterator( combo );
3532 jsonObject* curr_obj = NULL;
3533 while( (curr_obj = jsonIteratorNext( query_itr ) ) ) {
3534 if( ! strcmp( "union", query_itr->key ) ) {
3537 query_array = curr_obj;
3538 } else if( ! strcmp( "intersect", query_itr->key ) ) {
3541 query_array = curr_obj;
3542 } else if( ! strcmp( "except", query_itr->key ) ) {
3546 query_array = curr_obj;
3547 } else if( ! strcmp( "order_by", query_itr->key ) ) {
3550 "%s: ORDER BY not supported for UNION, INTERSECT, or EXCEPT",
3553 order_obj = curr_obj;
3554 } else if( ! strcmp( "alias", query_itr->key ) ) {
3555 if( curr_obj->type != JSON_STRING ) {
3556 jsonIteratorFree( query_itr );
3559 alias = jsonObjectGetString( curr_obj );
3560 } else if( ! strcmp( "all", query_itr->key ) ) {
3561 if( obj_is_true( curr_obj ) )
3565 osrfAppSessionStatus(
3567 OSRF_STATUS_INTERNALSERVERERROR,
3568 "osrfMethodException",
3570 "Malformed query; unexpected entry in query object"
3574 "%s: Unexpected entry for \"%s\" in%squery",
3579 jsonIteratorFree( query_itr );
3583 jsonIteratorFree( query_itr );
3585 // More sanity checks
3586 if( ! query_array ) {
3588 osrfAppSessionStatus(
3590 OSRF_STATUS_INTERNALSERVERERROR,
3591 "osrfMethodException",
3593 "Expected UNION, INTERSECT, or EXCEPT operator not found"
3597 "%s: Expected UNION, INTERSECT, or EXCEPT operator not found",
3600 return NULL; // should be impossible...
3601 } else if( op_count > 1 ) {
3603 osrfAppSessionStatus(
3605 OSRF_STATUS_INTERNALSERVERERROR,
3606 "osrfMethodException",
3608 "Found more than one of UNION, INTERSECT, and EXCEPT in same query"
3612 "%s: Found more than one of UNION, INTERSECT, and EXCEPT in same query",
3616 } if( query_array->type != JSON_ARRAY ) {
3618 osrfAppSessionStatus(
3620 OSRF_STATUS_INTERNALSERVERERROR,
3621 "osrfMethodException",
3623 "Malformed query: expected array of queries under UNION, INTERSECT or EXCEPT"
3627 "%s: Expected JSON_ARRAY of queries for%soperator; found %s",
3630 json_type( query_array->type )
3633 } if( query_array->size < 2 ) {
3635 osrfAppSessionStatus(
3637 OSRF_STATUS_INTERNALSERVERERROR,
3638 "osrfMethodException",
3640 "UNION, INTERSECT or EXCEPT requires multiple queries as operands"
3644 "%s:%srequires multiple queries as operands",
3649 } else if( excepting && query_array->size > 2 ) {
3651 osrfAppSessionStatus(
3653 OSRF_STATUS_INTERNALSERVERERROR,
3654 "osrfMethodException",
3656 "EXCEPT operator has too many queries as operands"
3660 "%s:EXCEPT operator has too many queries as operands",
3664 } else if( order_obj && ! alias ) {
3666 osrfAppSessionStatus(
3668 OSRF_STATUS_INTERNALSERVERERROR,
3669 "osrfMethodException",
3671 "ORDER BY requires an alias for a UNION, INTERSECT, or EXCEPT"
3675 "%s:ORDER BY requires an alias for a UNION, INTERSECT, or EXCEPT",
3681 // So far so good. Now build the SQL.
3682 growing_buffer* sql = buffer_init( 256 );
3684 // If we nested inside another UNION, INTERSECT, or EXCEPT,
3685 // Add a layer of parentheses
3686 if( flags & SUBCOMBO )
3687 OSRF_BUFFER_ADD( sql, "( " );
3689 // Traverse the query array. Each entry should be a hash.
3690 int first = 1; // boolean
3692 jsonObject* query = NULL;
3693 while( (query = jsonObjectGetIndex( query_array, i++ )) ) {
3694 if( query->type != JSON_HASH ) {
3696 osrfAppSessionStatus(
3698 OSRF_STATUS_INTERNALSERVERERROR,
3699 "osrfMethodException",
3701 "Malformed query under UNION, INTERSECT or EXCEPT"
3705 "%s: Malformed query under%s -- expected JSON_HASH, found %s",
3708 json_type( query->type )
3717 OSRF_BUFFER_ADD( sql, op );
3719 OSRF_BUFFER_ADD( sql, "ALL " );
3722 char* query_str = buildQuery( ctx, query, SUBSELECT | SUBCOMBO );
3726 "%s: Error building query under%s",
3734 OSRF_BUFFER_ADD( sql, query_str );
3737 if( flags & SUBCOMBO )
3738 OSRF_BUFFER_ADD_CHAR( sql, ')' );
3740 if( !(flags & SUBSELECT) )
3741 OSRF_BUFFER_ADD_CHAR( sql, ';' );
3743 return buffer_release( sql );
3746 // Translate a jsonObject into a SELECT, UNION, INTERSECT, or EXCEPT query.
3747 // The jsonObject must be a JSON_HASH with an entry for "from", "union", "intersect",
3748 // or "except" to indicate the type of query.
3749 char* buildQuery( osrfMethodContext* ctx, jsonObject* query, int flags ) {
3753 osrfAppSessionStatus(
3755 OSRF_STATUS_INTERNALSERVERERROR,
3756 "osrfMethodException",
3758 "Malformed query; no query object"
3760 osrfLogError( OSRF_LOG_MARK, "%s: Null pointer to query object", modulename );
3762 } else if( query->type != JSON_HASH ) {
3764 osrfAppSessionStatus(
3766 OSRF_STATUS_INTERNALSERVERERROR,
3767 "osrfMethodException",
3769 "Malformed query object"
3773 "%s: Query object is %s instead of JSON_HASH",
3775 json_type( query->type )
3780 // Determine what kind of query it purports to be, and dispatch accordingly.
3781 if( jsonObjectGetKeyConst( query, "union" ) ||
3782 jsonObjectGetKeyConst( query, "intersect" ) ||
3783 jsonObjectGetKeyConst( query, "except" )) {
3784 return doCombo( ctx, query, flags );
3786 // It is presumably a SELECT query
3788 // Push a node onto the stack for the current query. Every level of
3789 // subquery gets its own QueryFrame on the Stack.
3792 // Build an SQL SELECT statement
3795 jsonObjectGetKey( query, "select" ),
3796 jsonObjectGetKeyConst( query, "from" ),
3797 jsonObjectGetKeyConst( query, "where" ),
3798 jsonObjectGetKeyConst( query, "having" ),
3799 jsonObjectGetKeyConst( query, "order_by" ),
3800 jsonObjectGetKeyConst( query, "limit" ),
3801 jsonObjectGetKeyConst( query, "offset" ),
3810 /* method context */ osrfMethodContext* ctx,
3812 /* SELECT */ jsonObject* selhash,
3813 /* FROM */ const jsonObject* join_hash,
3814 /* WHERE */ const jsonObject* search_hash,
3815 /* HAVING */ const jsonObject* having_hash,
3816 /* ORDER BY */ const jsonObject* order_hash,
3817 /* LIMIT */ const jsonObject* limit,
3818 /* OFFSET */ const jsonObject* offset,
3819 /* flags */ int flags
3821 const char* locale = osrf_message_get_last_locale();
3823 // general tmp objects
3824 const jsonObject* tmp_const;
3825 jsonObject* selclass = NULL;
3826 jsonObject* snode = NULL;
3827 jsonObject* onode = NULL;
3829 char* string = NULL;
3830 int from_function = 0;
3835 osrfLogDebug(OSRF_LOG_MARK, "cstore SELECT locale: %s", locale ? locale : "(none)" );
3837 // punt if there's no FROM clause
3838 if( !join_hash || ( join_hash->type == JSON_HASH && !join_hash->size )) {
3841 "%s: FROM clause is missing or empty",
3845 osrfAppSessionStatus(
3847 OSRF_STATUS_INTERNALSERVERERROR,
3848 "osrfMethodException",
3850 "FROM clause is missing or empty in JSON query"
3855 // the core search class
3856 const char* core_class = NULL;
3858 // get the core class -- the only key of the top level FROM clause, or a string
3859 if( join_hash->type == JSON_HASH ) {
3860 jsonIterator* tmp_itr = jsonNewIterator( join_hash );
3861 snode = jsonIteratorNext( tmp_itr );
3863 // Populate the current QueryFrame with information
3864 // about the core class
3865 if( add_query_core( NULL, tmp_itr->key ) ) {
3867 osrfAppSessionStatus(
3869 OSRF_STATUS_INTERNALSERVERERROR,
3870 "osrfMethodException",
3872 "Unable to look up core class"
3876 core_class = curr_query->core.class_name;
3879 jsonObject* extra = jsonIteratorNext( tmp_itr );
3881 jsonIteratorFree( tmp_itr );
3884 // There shouldn't be more than one entry in join_hash
3888 "%s: Malformed FROM clause: extra entry in JSON_HASH",
3892 osrfAppSessionStatus(
3894 OSRF_STATUS_INTERNALSERVERERROR,
3895 "osrfMethodException",
3897 "Malformed FROM clause in JSON query"
3899 return NULL; // Malformed join_hash; extra entry
3901 } else if( join_hash->type == JSON_ARRAY ) {
3902 // We're selecting from a function, not from a table
3904 core_class = jsonObjectGetString( jsonObjectGetIndex( join_hash, 0 ));
3907 } else if( join_hash->type == JSON_STRING ) {
3908 // Populate the current QueryFrame with information
3909 // about the core class
3910 core_class = jsonObjectGetString( join_hash );
3912 if( add_query_core( NULL, core_class ) ) {
3914 osrfAppSessionStatus(
3916 OSRF_STATUS_INTERNALSERVERERROR,
3917 "osrfMethodException",
3919 "Unable to look up core class"
3927 "%s: FROM clause is unexpected JSON type: %s",
3929 json_type( join_hash->type )
3932 osrfAppSessionStatus(
3934 OSRF_STATUS_INTERNALSERVERERROR,
3935 "osrfMethodException",
3937 "Ill-formed FROM clause in JSON query"
3942 // Build the join clause, if any, while filling out the list
3943 // of joined classes in the current QueryFrame.
3944 char* join_clause = NULL;
3945 if( join_hash && ! from_function ) {
3947 join_clause = searchJOIN( join_hash, &curr_query->core );
3948 if( ! join_clause ) {
3950 osrfAppSessionStatus(
3952 OSRF_STATUS_INTERNALSERVERERROR,
3953 "osrfMethodException",
3955 "Unable to construct JOIN clause(s)"
3961 // For in case we don't get a select list
3962 jsonObject* defaultselhash = NULL;
3964 // if there is no select list, build a default select list ...
3965 if( !selhash && !from_function ) {
3966 jsonObject* default_list = defaultSelectList( core_class );
3967 if( ! default_list ) {
3969 osrfAppSessionStatus(
3971 OSRF_STATUS_INTERNALSERVERERROR,
3972 "osrfMethodException",
3974 "Unable to build default SELECT clause in JSON query"
3976 free( join_clause );
3981 selhash = defaultselhash = jsonNewObjectType( JSON_HASH );
3982 jsonObjectSetKey( selhash, core_class, default_list );
3985 // The SELECT clause can be encoded only by a hash
3986 if( !from_function && selhash->type != JSON_HASH ) {
3989 "%s: Expected JSON_HASH for SELECT clause; found %s",
3991 json_type( selhash->type )
3995 osrfAppSessionStatus(
3997 OSRF_STATUS_INTERNALSERVERERROR,
3998 "osrfMethodException",
4000 "Malformed SELECT clause in JSON query"
4002 free( join_clause );
4006 // If you see a null or wild card specifier for the core class, or an
4007 // empty array, replace it with a default SELECT list
4008 tmp_const = jsonObjectGetKeyConst( selhash, core_class );
4010 int default_needed = 0; // boolean
4011 if( JSON_STRING == tmp_const->type
4012 && !strcmp( "*", jsonObjectGetString( tmp_const ) ))
4014 else if( JSON_NULL == tmp_const->type )
4017 if( default_needed ) {
4018 // Build a default SELECT list
4019 jsonObject* default_list = defaultSelectList( core_class );
4020 if( ! default_list ) {
4022 osrfAppSessionStatus(
4024 OSRF_STATUS_INTERNALSERVERERROR,
4025 "osrfMethodException",
4027 "Can't build default SELECT clause in JSON query"
4029 free( join_clause );
4034 jsonObjectSetKey( selhash, core_class, default_list );
4038 // temp buffers for the SELECT list and GROUP BY clause
4039 growing_buffer* select_buf = buffer_init( 128 );
4040 growing_buffer* group_buf = buffer_init( 128 );
4042 int aggregate_found = 0; // boolean
4044 // Build a select list
4045 if( from_function ) // From a function we select everything
4046 OSRF_BUFFER_ADD_CHAR( select_buf, '*' );
4049 // Build the SELECT list as SQL
4053 jsonIterator* selclass_itr = jsonNewIterator( selhash );
4054 while ( (selclass = jsonIteratorNext( selclass_itr )) ) { // For each class
4056 const char* cname = selclass_itr->key;
4058 // Make sure the target relation is in the FROM clause.
4060 // At this point join_hash is a step down from the join_hash we
4061 // received as a parameter. If the original was a JSON_STRING,
4062 // then json_hash is now NULL. If the original was a JSON_HASH,
4063 // then json_hash is now the first (and only) entry in it,
4064 // denoting the core class. We've already excluded the
4065 // possibility that the original was a JSON_ARRAY, because in
4066 // that case from_function would be non-NULL, and we wouldn't
4069 // If the current table alias isn't in scope, bail out
4070 ClassInfo* class_info = search_alias( cname );
4071 if( ! class_info ) {
4074 "%s: SELECT clause references class not in FROM clause: \"%s\"",
4079 osrfAppSessionStatus(
4081 OSRF_STATUS_INTERNALSERVERERROR,
4082 "osrfMethodException",
4084 "Selected class not in FROM clause in JSON query"
4086 jsonIteratorFree( selclass_itr );
4087 buffer_free( select_buf );
4088 buffer_free( group_buf );
4089 if( defaultselhash )
4090 jsonObjectFree( defaultselhash );
4091 free( join_clause );
4095 if( selclass->type != JSON_ARRAY ) {
4098 "%s: Malformed SELECT list for class \"%s\"; not an array",
4103 osrfAppSessionStatus(
4105 OSRF_STATUS_INTERNALSERVERERROR,
4106 "osrfMethodException",
4108 "Selected class not in FROM clause in JSON query"
4111 jsonIteratorFree( selclass_itr );
4112 buffer_free( select_buf );
4113 buffer_free( group_buf );
4114 if( defaultselhash )
4115 jsonObjectFree( defaultselhash );
4116 free( join_clause );
4120 // Look up some attributes of the current class
4121 osrfHash* idlClass = class_info->class_def;
4122 osrfHash* class_field_set = class_info->fields;
4123 const char* class_pkey = osrfHashGet( idlClass, "primarykey" );
4124 const char* class_tname = osrfHashGet( idlClass, "tablename" );
4126 if( 0 == selclass->size ) {
4129 "%s: No columns selected from \"%s\"",
4135 // stitch together the column list for the current table alias...
4136 unsigned long field_idx = 0;
4137 jsonObject* selfield = NULL;
4138 while(( selfield = jsonObjectGetIndex( selclass, field_idx++ ) )) {
4140 // If we need a separator comma, add one
4144 OSRF_BUFFER_ADD_CHAR( select_buf, ',' );
4147 // if the field specification is a string, add it to the list
4148 if( selfield->type == JSON_STRING ) {
4150 // Look up the field in the IDL
4151 const char* col_name = jsonObjectGetString( selfield );
4152 osrfHash* field_def = osrfHashGet( class_field_set, col_name );
4154 // No such field in current class
4157 "%s: Selected column \"%s\" not defined in IDL for class \"%s\"",
4163 osrfAppSessionStatus(
4165 OSRF_STATUS_INTERNALSERVERERROR,
4166 "osrfMethodException",
4168 "Selected column not defined in JSON query"
4170 jsonIteratorFree( selclass_itr );
4171 buffer_free( select_buf );
4172 buffer_free( group_buf );
4173 if( defaultselhash )
4174 jsonObjectFree( defaultselhash );
4175 free( join_clause );
4177 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
4178 // Virtual field not allowed
4181 "%s: Selected column \"%s\" for class \"%s\" is virtual",
4187 osrfAppSessionStatus(
4189 OSRF_STATUS_INTERNALSERVERERROR,
4190 "osrfMethodException",
4192 "Selected column may not be virtual in JSON query"
4194 jsonIteratorFree( selclass_itr );
4195 buffer_free( select_buf );
4196 buffer_free( group_buf );
4197 if( defaultselhash )
4198 jsonObjectFree( defaultselhash );
4199 free( join_clause );
4205 if( flags & DISABLE_I18N )
4208 i18n = osrfHashGet( field_def, "i18n" );
4210 if( str_is_true( i18n ) ) {
4211 buffer_fadd( select_buf, " oils_i18n_xlate('%s', '%s', '%s', "
4212 "'%s', \"%s\".%s::TEXT, '%s') AS \"%s\"",
4213 class_tname, cname, col_name, class_pkey,
4214 cname, class_pkey, locale, col_name );
4216 buffer_fadd( select_buf, " \"%s\".%s AS \"%s\"",
4217 cname, col_name, col_name );
4220 buffer_fadd( select_buf, " \"%s\".%s AS \"%s\"",
4221 cname, col_name, col_name );
4224 // ... but it could be an object, in which case we check for a Field Transform
4225 } else if( selfield->type == JSON_HASH ) {
4227 const char* col_name = jsonObjectGetString(
4228 jsonObjectGetKeyConst( selfield, "column" ) );
4230 // Get the field definition from the IDL
4231 osrfHash* field_def = osrfHashGet( class_field_set, col_name );
4233 // No such field in current class
4236 "%s: Selected column \"%s\" is not defined in IDL for class \"%s\"",
4242 osrfAppSessionStatus(
4244 OSRF_STATUS_INTERNALSERVERERROR,
4245 "osrfMethodException",
4247 "Selected column is not defined in JSON query"
4249 jsonIteratorFree( selclass_itr );
4250 buffer_free( select_buf );
4251 buffer_free( group_buf );
4252 if( defaultselhash )
4253 jsonObjectFree( defaultselhash );
4254 free( join_clause );
4256 } else if( str_is_true( osrfHashGet( field_def, "virtual" ))) {
4257 // No such field in current class
4260 "%s: Selected column \"%s\" is virtual for class \"%s\"",
4266 osrfAppSessionStatus(
4268 OSRF_STATUS_INTERNALSERVERERROR,
4269 "osrfMethodException",
4271 "Selected column is virtual in JSON query"
4273 jsonIteratorFree( selclass_itr );
4274 buffer_free( select_buf );
4275 buffer_free( group_buf );
4276 if( defaultselhash )
4277 jsonObjectFree( defaultselhash );
4278 free( join_clause );
4282 // Decide what to use as a column alias
4284 if((tmp_const = jsonObjectGetKeyConst( selfield, "alias" ))) {
4285 _alias = jsonObjectGetString( tmp_const );
4286 } else if((tmp_const = jsonObjectGetKeyConst( selfield, "result_field" ))) { // Use result_field name as the alias
4287 _alias = jsonObjectGetString( tmp_const );
4288 } else { // Use field name as the alias
4292 if( jsonObjectGetKeyConst( selfield, "transform" )) {
4293 char* transform_str = searchFieldTransform(
4294 class_info->alias, field_def, selfield );
4295 if( transform_str ) {
4296 buffer_fadd( select_buf, " %s AS \"%s\"", transform_str, _alias );
4297 free( transform_str );
4300 osrfAppSessionStatus(
4302 OSRF_STATUS_INTERNALSERVERERROR,
4303 "osrfMethodException",
4305 "Unable to generate transform function in JSON query"
4307 jsonIteratorFree( selclass_itr );
4308 buffer_free( select_buf );
4309 buffer_free( group_buf );
4310 if( defaultselhash )
4311 jsonObjectFree( defaultselhash );
4312 free( join_clause );
4319 if( flags & DISABLE_I18N )
4322 i18n = osrfHashGet( field_def, "i18n" );
4324 if( str_is_true( i18n ) ) {
4325 buffer_fadd( select_buf,
4326 " oils_i18n_xlate('%s', '%s', '%s', '%s', "
4327 "\"%s\".%s::TEXT, '%s') AS \"%s\"",
4328 class_tname, cname, col_name, class_pkey, cname,
4329 class_pkey, locale, _alias );
4331 buffer_fadd( select_buf, " \"%s\".%s AS \"%s\"",
4332 cname, col_name, _alias );
4335 buffer_fadd( select_buf, " \"%s\".%s AS \"%s\"",
4336 cname, col_name, _alias );
4343 "%s: Selected item is unexpected JSON type: %s",
4345 json_type( selfield->type )
4348 osrfAppSessionStatus(
4350 OSRF_STATUS_INTERNALSERVERERROR,
4351 "osrfMethodException",
4353 "Ill-formed SELECT item in JSON query"
4355 jsonIteratorFree( selclass_itr );
4356 buffer_free( select_buf );
4357 buffer_free( group_buf );
4358 if( defaultselhash )
4359 jsonObjectFree( defaultselhash );
4360 free( join_clause );
4364 const jsonObject* agg_obj = jsonObjectGetKeyConst( selfield, "aggregate" );
4365 if( obj_is_true( agg_obj ) )
4366 aggregate_found = 1;
4368 // Append a comma (except for the first one)
4369 // and add the column to a GROUP BY clause
4373 OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
4375 buffer_fadd( group_buf, " %d", sel_pos );
4379 if (is_agg->size || (flags & SELECT_DISTINCT)) {
4381 const jsonObject* aggregate_obj = jsonObjectGetKeyConst( elfield, "aggregate");
4382 if ( ! obj_is_true( aggregate_obj ) ) {
4386 OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
4389 buffer_fadd(group_buf, " %d", sel_pos);
4392 } else if (is_agg = jsonObjectGetKeyConst( selfield, "having" )) {
4396 OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
4399 _column = searchFieldTransform(class_info->alias, field, selfield);
4400 OSRF_BUFFER_ADD_CHAR(group_buf, ' ');
4401 OSRF_BUFFER_ADD(group_buf, _column);
4402 _column = searchFieldTransform(class_info->alias, field, selfield);
4409 } // end while -- iterating across SELECT columns
4411 } // end while -- iterating across classes
4413 jsonIteratorFree( selclass_itr );
4416 char* col_list = buffer_release( select_buf );
4418 // Make sure the SELECT list isn't empty. This can happen, for example,
4419 // if we try to build a default SELECT clause from a non-core table.
4422 osrfLogError( OSRF_LOG_MARK, "%s: SELECT clause is empty", modulename );
4424 osrfAppSessionStatus(
4426 OSRF_STATUS_INTERNALSERVERERROR,
4427 "osrfMethodException",
4429 "SELECT list is empty"
4432 buffer_free( group_buf );
4433 if( defaultselhash )
4434 jsonObjectFree( defaultselhash );
4435 free( join_clause );
4441 table = searchValueTransform( join_hash );
4443 table = strdup( curr_query->core.source_def );
4447 osrfAppSessionStatus(
4449 OSRF_STATUS_INTERNALSERVERERROR,
4450 "osrfMethodException",
4452 "Unable to identify table for core class"
4455 buffer_free( group_buf );
4456 if( defaultselhash )
4457 jsonObjectFree( defaultselhash );
4458 free( join_clause );
4462 // Put it all together
4463 growing_buffer* sql_buf = buffer_init( 128 );
4464 buffer_fadd(sql_buf, "SELECT %s FROM %s AS \"%s\" ", col_list, table, core_class );
4468 // Append the join clause, if any
4470 buffer_add(sql_buf, join_clause );
4471 free( join_clause );
4474 char* order_by_list = NULL;
4475 char* having_buf = NULL;
4477 if( !from_function ) {
4479 // Build a WHERE clause, if there is one
4481 buffer_add( sql_buf, " WHERE " );
4483 // and it's on the WHERE clause
4484 char* pred = searchWHERE( search_hash, &curr_query->core, AND_OP_JOIN, ctx );
4487 osrfAppSessionStatus(
4489 OSRF_STATUS_INTERNALSERVERERROR,
4490 "osrfMethodException",
4492 "Severe query error in WHERE predicate -- see error log for more details"
4495 buffer_free( group_buf );
4496 buffer_free( sql_buf );
4497 if( defaultselhash )
4498 jsonObjectFree( defaultselhash );
4502 buffer_add( sql_buf, pred );
4506 // Build a HAVING clause, if there is one
4509 // and it's on the the WHERE clause
4510 having_buf = searchWHERE( having_hash, &curr_query->core, AND_OP_JOIN, ctx );
4512 if( ! having_buf ) {
4514 osrfAppSessionStatus(
4516 OSRF_STATUS_INTERNALSERVERERROR,
4517 "osrfMethodException",
4519 "Severe query error in HAVING predicate -- see error log for more details"
4522 buffer_free( group_buf );
4523 buffer_free( sql_buf );
4524 if( defaultselhash )
4525 jsonObjectFree( defaultselhash );
4530 // Build an ORDER BY clause, if there is one
4531 if( NULL == order_hash )
4532 ; // No ORDER BY? do nothing
4533 else if( JSON_ARRAY == order_hash->type ) {
4534 order_by_list = buildOrderByFromArray( ctx, order_hash );
4535 if( !order_by_list ) {
4537 buffer_free( group_buf );
4538 buffer_free( sql_buf );
4539 if( defaultselhash )
4540 jsonObjectFree( defaultselhash );
4543 } else if( JSON_HASH == order_hash->type ) {
4544 // This hash is keyed on class alias. Each class has either
4545 // an array of field names or a hash keyed on field name.
4546 growing_buffer* order_buf = NULL; // to collect ORDER BY list
4547 jsonIterator* class_itr = jsonNewIterator( order_hash );
4548 while( (snode = jsonIteratorNext( class_itr )) ) {
4550 ClassInfo* order_class_info = search_alias( class_itr->key );
4551 if( ! order_class_info ) {
4552 osrfLogError( OSRF_LOG_MARK,
4553 "%s: Invalid class \"%s\" referenced in ORDER BY clause",
4554 modulename, class_itr->key );
4556 osrfAppSessionStatus(
4558 OSRF_STATUS_INTERNALSERVERERROR,
4559 "osrfMethodException",
4561 "Invalid class referenced in ORDER BY clause -- "
4562 "see error log for more details"
4564 jsonIteratorFree( class_itr );
4565 buffer_free( order_buf );
4567 buffer_free( group_buf );
4568 buffer_free( sql_buf );
4569 if( defaultselhash )
4570 jsonObjectFree( defaultselhash );
4574 osrfHash* field_list_def = order_class_info->fields;
4576 if( snode->type == JSON_HASH ) {
4578 // Hash is keyed on field names from the current class. For each field
4579 // there is another layer of hash to define the sorting details, if any,
4580 // or a string to indicate direction of sorting.
4581 jsonIterator* order_itr = jsonNewIterator( snode );
4582 while( (onode = jsonIteratorNext( order_itr )) ) {
4584 osrfHash* field_def = osrfHashGet( field_list_def, order_itr->key );
4586 osrfLogError( OSRF_LOG_MARK,
4587 "%s: Invalid field \"%s\" in ORDER BY clause",
4588 modulename, order_itr->key );
4590 osrfAppSessionStatus(
4592 OSRF_STATUS_INTERNALSERVERERROR,
4593 "osrfMethodException",
4595 "Invalid field in ORDER BY clause -- "
4596 "see error log for more details"
4598 jsonIteratorFree( order_itr );
4599 jsonIteratorFree( class_itr );
4600 buffer_free( order_buf );
4602 buffer_free( group_buf );
4603 buffer_free( sql_buf );
4604 if( defaultselhash )
4605 jsonObjectFree( defaultselhash );
4607 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
4608 osrfLogError( OSRF_LOG_MARK,
4609 "%s: Virtual field \"%s\" in ORDER BY clause",
4610 modulename, order_itr->key );
4612 osrfAppSessionStatus(
4614 OSRF_STATUS_INTERNALSERVERERROR,
4615 "osrfMethodException",
4617 "Virtual field in ORDER BY clause -- "
4618 "see error log for more details"
4620 jsonIteratorFree( order_itr );
4621 jsonIteratorFree( class_itr );
4622 buffer_free( order_buf );
4624 buffer_free( group_buf );
4625 buffer_free( sql_buf );
4626 if( defaultselhash )
4627 jsonObjectFree( defaultselhash );
4631 const char* direction = NULL;
4632 if( onode->type == JSON_HASH ) {
4633 if( jsonObjectGetKeyConst( onode, "transform" ) ) {
4634 string = searchFieldTransform(
4636 osrfHashGet( field_list_def, order_itr->key ),
4640 if( ctx ) osrfAppSessionStatus(
4642 OSRF_STATUS_INTERNALSERVERERROR,
4643 "osrfMethodException",
4645 "Severe query error in ORDER BY clause -- "
4646 "see error log for more details"
4648 jsonIteratorFree( order_itr );
4649 jsonIteratorFree( class_itr );
4651 buffer_free( group_buf );
4652 buffer_free( order_buf);
4653 buffer_free( sql_buf );
4654 if( defaultselhash )
4655 jsonObjectFree( defaultselhash );
4659 growing_buffer* field_buf = buffer_init( 16 );
4660 buffer_fadd( field_buf, "\"%s\".%s",
4661 class_itr->key, order_itr->key );
4662 string = buffer_release( field_buf );
4665 if( (tmp_const = jsonObjectGetKeyConst( onode, "direction" )) ) {
4666 const char* dir = jsonObjectGetString( tmp_const );
4667 if(!strncasecmp( dir, "d", 1 )) {
4668 direction = " DESC";
4674 } else if( JSON_NULL == onode->type || JSON_ARRAY == onode->type ) {
4675 osrfLogError( OSRF_LOG_MARK,
4676 "%s: Expected JSON_STRING in ORDER BY clause; found %s",
4677 modulename, json_type( onode->type ) );
4679 osrfAppSessionStatus(
4681 OSRF_STATUS_INTERNALSERVERERROR,
4682 "osrfMethodException",
4684 "Malformed ORDER BY clause -- see error log for more details"
4686 jsonIteratorFree( order_itr );
4687 jsonIteratorFree( class_itr );
4689 buffer_free( group_buf );
4690 buffer_free( order_buf );
4691 buffer_free( sql_buf );
4692 if( defaultselhash )
4693 jsonObjectFree( defaultselhash );
4697 string = strdup( order_itr->key );
4698 const char* dir = jsonObjectGetString( onode );
4699 if( !strncasecmp( dir, "d", 1 )) {
4700 direction = " DESC";
4707 OSRF_BUFFER_ADD( order_buf, ", " );
4709 order_buf = buffer_init( 128 );
4711 OSRF_BUFFER_ADD( order_buf, string );
4715 OSRF_BUFFER_ADD( order_buf, direction );
4719 jsonIteratorFree( order_itr );
4721 } else if( snode->type == JSON_ARRAY ) {
4723 // Array is a list of fields from the current class
4724 unsigned long order_idx = 0;
4725 while(( onode = jsonObjectGetIndex( snode, order_idx++ ) )) {
4727 const char* _f = jsonObjectGetString( onode );
4729 osrfHash* field_def = osrfHashGet( field_list_def, _f );
4731 osrfLogError( OSRF_LOG_MARK,
4732 "%s: Invalid field \"%s\" in ORDER BY clause",
4735 osrfAppSessionStatus(
4737 OSRF_STATUS_INTERNALSERVERERROR,
4738 "osrfMethodException",
4740 "Invalid field in ORDER BY clause -- "
4741 "see error log for more details"
4743 jsonIteratorFree( class_itr );
4744 buffer_free( order_buf );
4746 buffer_free( group_buf );
4747 buffer_free( sql_buf );
4748 if( defaultselhash )
4749 jsonObjectFree( defaultselhash );
4751 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
4752 osrfLogError( OSRF_LOG_MARK,
4753 "%s: Virtual field \"%s\" in ORDER BY clause",
4756 osrfAppSessionStatus(
4758 OSRF_STATUS_INTERNALSERVERERROR,
4759 "osrfMethodException",
4761 "Virtual field in ORDER BY clause -- "
4762 "see error log for more details"
4764 jsonIteratorFree( class_itr );
4765 buffer_free( order_buf );
4767 buffer_free( group_buf );
4768 buffer_free( sql_buf );
4769 if( defaultselhash )
4770 jsonObjectFree( defaultselhash );
4775 OSRF_BUFFER_ADD( order_buf, ", " );
4777 order_buf = buffer_init( 128 );
4779 buffer_fadd( order_buf, "\"%s\".%s", class_itr->key, _f );
4783 // IT'S THE OOOOOOOOOOOLD STYLE!
4785 osrfLogError( OSRF_LOG_MARK,
4786 "%s: Possible SQL injection attempt; direct order by is not allowed",
4789 osrfAppSessionStatus(
4791 OSRF_STATUS_INTERNALSERVERERROR,
4792 "osrfMethodException",
4794 "Severe query error -- see error log for more details"
4799 buffer_free( group_buf );
4800 buffer_free( order_buf );
4801 buffer_free( sql_buf );
4802 if( defaultselhash )
4803 jsonObjectFree( defaultselhash );
4804 jsonIteratorFree( class_itr );
4808 jsonIteratorFree( class_itr );
4810 order_by_list = buffer_release( order_buf );
4812 osrfLogError( OSRF_LOG_MARK,
4813 "%s: Malformed ORDER BY clause; expected JSON_HASH or JSON_ARRAY, found %s",
4814 modulename, json_type( order_hash->type ) );
4816 osrfAppSessionStatus(
4818 OSRF_STATUS_INTERNALSERVERERROR,
4819 "osrfMethodException",
4821 "Malformed ORDER BY clause -- see error log for more details"
4824 buffer_free( group_buf );
4825 buffer_free( sql_buf );
4826 if( defaultselhash )
4827 jsonObjectFree( defaultselhash );
4832 string = buffer_release( group_buf );
4834 if( *string && ( aggregate_found || (flags & SELECT_DISTINCT) ) ) {
4835 OSRF_BUFFER_ADD( sql_buf, " GROUP BY " );
4836 OSRF_BUFFER_ADD( sql_buf, string );
4841 if( having_buf && *having_buf ) {
4842 OSRF_BUFFER_ADD( sql_buf, " HAVING " );
4843 OSRF_BUFFER_ADD( sql_buf, having_buf );
4847 if( order_by_list ) {
4849 if( *order_by_list ) {
4850 OSRF_BUFFER_ADD( sql_buf, " ORDER BY " );
4851 OSRF_BUFFER_ADD( sql_buf, order_by_list );
4854 free( order_by_list );
4858 const char* str = jsonObjectGetString( limit );
4859 buffer_fadd( sql_buf, " LIMIT %d", atoi( str ));
4863 const char* str = jsonObjectGetString( offset );
4864 buffer_fadd( sql_buf, " OFFSET %d", atoi( str ));
4867 if( !(flags & SUBSELECT) )
4868 OSRF_BUFFER_ADD_CHAR( sql_buf, ';' );
4870 if( defaultselhash )
4871 jsonObjectFree( defaultselhash );
4873 return buffer_release( sql_buf );
4875 } // end of SELECT()
4878 @brief Build a list of ORDER BY expressions.
4879 @param ctx Pointer to the method context.
4880 @param order_array Pointer to a JSON_ARRAY of field specifications.
4881 @return Pointer to a string containing a comma-separated list of ORDER BY expressions.
4882 Each expression may be either a column reference or a function call whose first parameter
4883 is a column reference.
4885 Each entry in @a order_array must be a JSON_HASH with values for "class" and "field".
4886 It may optionally include entries for "direction" and/or "transform".
4888 The calling code is responsible for freeing the returned string.
4890 static char* buildOrderByFromArray( osrfMethodContext* ctx, const jsonObject* order_array ) {
4891 if( ! order_array ) {
4892 osrfLogError( OSRF_LOG_MARK, "%s: Logic error: NULL pointer for ORDER BY clause",
4895 osrfAppSessionStatus(
4897 OSRF_STATUS_INTERNALSERVERERROR,
4898 "osrfMethodException",
4900 "Logic error: ORDER BY clause expected, not found; "
4901 "see error log for more details"
4904 } else if( order_array->type != JSON_ARRAY ) {
4905 osrfLogError( OSRF_LOG_MARK,
4906 "%s: Logic error: Expected JSON_ARRAY for ORDER BY clause, not found", modulename );
4908 osrfAppSessionStatus(
4910 OSRF_STATUS_INTERNALSERVERERROR,
4911 "osrfMethodException",
4913 "Logic error: Unexpected format for ORDER BY clause; see error log for more details" );
4917 growing_buffer* order_buf = buffer_init( 128 );
4918 int first = 1; // boolean
4920 jsonObject* order_spec;
4921 while( (order_spec = jsonObjectGetIndex( order_array, order_idx++ ))) {
4923 if( JSON_HASH != order_spec->type ) {
4924 osrfLogError( OSRF_LOG_MARK,
4925 "%s: Malformed field specification in ORDER BY clause; "
4926 "expected JSON_HASH, found %s",
4927 modulename, json_type( order_spec->type ) );
4929 osrfAppSessionStatus(
4931 OSRF_STATUS_INTERNALSERVERERROR,
4932 "osrfMethodException",
4934 "Malformed ORDER BY clause -- see error log for more details"
4936 buffer_free( order_buf );
4940 const char* class_alias =
4941 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "class" ));
4943 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "field" ));
4945 if( !field || !class_alias ) {
4946 osrfLogError( OSRF_LOG_MARK,
4947 "%s: Missing class or field name in field specification of ORDER BY clause",
4950 osrfAppSessionStatus(
4952 OSRF_STATUS_INTERNALSERVERERROR,
4953 "osrfMethodException",
4955 "Malformed ORDER BY clause -- see error log for more details"
4957 buffer_free( order_buf );
4961 const ClassInfo* order_class_info = search_alias( class_alias );
4962 if( ! order_class_info ) {
4963 osrfLogInternal( OSRF_LOG_MARK, "%s: ORDER BY clause references class \"%s\" "
4964 "not in FROM clause, skipping it", modulename, class_alias );
4968 // Add a separating comma, except at the beginning
4972 OSRF_BUFFER_ADD( order_buf, ", " );
4974 osrfHash* field_def = osrfHashGet( order_class_info->fields, field );
4976 osrfLogError( OSRF_LOG_MARK,
4977 "%s: Invalid field \"%s\".%s referenced in ORDER BY clause",
4978 modulename, class_alias, field );
4980 osrfAppSessionStatus(
4982 OSRF_STATUS_INTERNALSERVERERROR,
4983 "osrfMethodException",
4985 "Invalid field referenced in ORDER BY clause -- "
4986 "see error log for more details"
4990 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
4991 osrfLogError( OSRF_LOG_MARK, "%s: Virtual field \"%s\" in ORDER BY clause",
4992 modulename, field );
4994 osrfAppSessionStatus(
4996 OSRF_STATUS_INTERNALSERVERERROR,
4997 "osrfMethodException",
4999 "Virtual field in ORDER BY clause -- see error log for more details"
5001 buffer_free( order_buf );
5005 if( jsonObjectGetKeyConst( order_spec, "transform" )) {
5006 char* transform_str = searchFieldTransform( class_alias, field_def, order_spec );
5007 if( ! transform_str ) {
5009 osrfAppSessionStatus(
5011 OSRF_STATUS_INTERNALSERVERERROR,
5012 "osrfMethodException",
5014 "Severe query error in ORDER BY clause -- "
5015 "see error log for more details"
5017 buffer_free( order_buf );
5021 OSRF_BUFFER_ADD( order_buf, transform_str );
5022 free( transform_str );
5025 buffer_fadd( order_buf, "\"%s\".%s", class_alias, field );
5027 const char* direction =
5028 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "direction" ) );
5030 if( direction[ 0 ] || 'D' == direction[ 0 ] )
5031 OSRF_BUFFER_ADD( order_buf, " DESC" );
5033 OSRF_BUFFER_ADD( order_buf, " ASC" );
5037 return buffer_release( order_buf );
5041 @brief Build a SELECT statement.
5042 @param search_hash Pointer to a JSON_HASH or JSON_ARRAY encoding the WHERE clause.
5043 @param rest_of_query Pointer to a JSON_HASH containing any other SQL clauses.
5044 @param meta Pointer to the class metadata for the core class.
5045 @param ctx Pointer to the method context.
5046 @return Pointer to a character string containing the WHERE clause; or NULL upon error.
5048 Within the rest_of_query hash, the meaningful keys are "join", "select", "no_i18n",
5049 "order_by", "limit", and "offset".
5051 The SELECT statements built here are distinct from those built for the json_query method.
5053 static char* buildSELECT ( const jsonObject* search_hash, jsonObject* rest_of_query,
5054 osrfHash* meta, osrfMethodContext* ctx ) {
5056 const char* locale = osrf_message_get_last_locale();
5058 osrfHash* fields = osrfHashGet( meta, "fields" );
5059 const char* core_class = osrfHashGet( meta, "classname" );
5061 const jsonObject* join_hash = jsonObjectGetKeyConst( rest_of_query, "join" );
5063 jsonObject* selhash = NULL;
5064 jsonObject* defaultselhash = NULL;
5066 growing_buffer* sql_buf = buffer_init( 128 );
5067 growing_buffer* select_buf = buffer_init( 128 );
5069 if( !(selhash = jsonObjectGetKey( rest_of_query, "select" )) ) {
5070 defaultselhash = jsonNewObjectType( JSON_HASH );
5071 selhash = defaultselhash;
5074 // If there's no SELECT list for the core class, build one
5075 if( !jsonObjectGetKeyConst( selhash, core_class ) ) {
5076 jsonObject* field_list = jsonNewObjectType( JSON_ARRAY );
5078 // Add every non-virtual field to the field list
5079 osrfHash* field_def = NULL;
5080 osrfHashIterator* field_itr = osrfNewHashIterator( fields );
5081 while( ( field_def = osrfHashIteratorNext( field_itr ) ) ) {
5082 if( ! str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
5083 const char* field = osrfHashIteratorKey( field_itr );
5084 jsonObjectPush( field_list, jsonNewObject( field ) );
5087 osrfHashIteratorFree( field_itr );
5088 jsonObjectSetKey( selhash, core_class, field_list );
5091 // Build a list of columns for the SELECT clause
5093 const jsonObject* snode = NULL;
5094 jsonIterator* class_itr = jsonNewIterator( selhash );
5095 while( (snode = jsonIteratorNext( class_itr )) ) { // For each class
5097 // If the class isn't in the IDL, ignore it
5098 const char* cname = class_itr->key;
5099 osrfHash* idlClass = osrfHashGet( oilsIDL(), cname );
5103 // If the class isn't the core class, and isn't in the JOIN clause, ignore it
5104 if( strcmp( core_class, class_itr->key )) {
5108 jsonObject* found = jsonObjectFindPath( join_hash, "//%s", class_itr->key );
5109 if( !found->size ) {
5110 jsonObjectFree( found );
5114 jsonObjectFree( found );
5117 const jsonObject* node = NULL;
5118 jsonIterator* select_itr = jsonNewIterator( snode );
5119 while( (node = jsonIteratorNext( select_itr )) ) {
5120 const char* item_str = jsonObjectGetString( node );
5121 osrfHash* field = osrfHashGet( osrfHashGet( idlClass, "fields" ), item_str );
5122 char* fname = osrfHashGet( field, "name" );
5130 OSRF_BUFFER_ADD_CHAR( select_buf, ',' );
5135 const jsonObject* no_i18n_obj = jsonObjectGetKeyConst( rest_of_query, "no_i18n" );
5136 if( obj_is_true( no_i18n_obj ) ) // Suppress internationalization?
5139 i18n = osrfHashGet( field, "i18n" );
5141 if( str_is_true( i18n ) ) {
5142 char* pkey = osrfHashGet( idlClass, "primarykey" );
5143 char* tname = osrfHashGet( idlClass, "tablename" );
5145 buffer_fadd( select_buf, " oils_i18n_xlate('%s', '%s', '%s', "
5146 "'%s', \"%s\".%s::TEXT, '%s') AS \"%s\"",
5147 tname, cname, fname, pkey, cname, pkey, locale, fname );
5149 buffer_fadd( select_buf, " \"%s\".%s", cname, fname );
5152 buffer_fadd( select_buf, " \"%s\".%s", cname, fname );
5156 jsonIteratorFree( select_itr );
5159 jsonIteratorFree( class_itr );
5161 char* col_list = buffer_release( select_buf );
5162 char* table = oilsGetRelation( meta );
5164 table = strdup( "(null)" );
5166 buffer_fadd( sql_buf, "SELECT %s FROM %s AS \"%s\"", col_list, table, core_class );
5170 // Clear the query stack (as a fail-safe precaution against possible
5171 // leftover garbage); then push the first query frame onto the stack.
5172 clear_query_stack();
5174 if( add_query_core( NULL, core_class ) ) {
5176 osrfAppSessionStatus(
5178 OSRF_STATUS_INTERNALSERVERERROR,
5179 "osrfMethodException",
5181 "Unable to build query frame for core class"
5183 buffer_free( sql_buf );
5184 if( defaultselhash )
5185 jsonObjectFree( defaultselhash );
5189 // Add the JOIN clauses, if any
5191 char* join_clause = searchJOIN( join_hash, &curr_query->core );
5192 OSRF_BUFFER_ADD_CHAR( sql_buf, ' ' );
5193 OSRF_BUFFER_ADD( sql_buf, join_clause );
5194 free( join_clause );
5197 osrfLogDebug( OSRF_LOG_MARK, "%s pre-predicate SQL = %s",
5198 modulename, OSRF_BUFFER_C_STR( sql_buf ));
5200 OSRF_BUFFER_ADD( sql_buf, " WHERE " );
5202 // Add the conditions in the WHERE clause
5203 char* pred = searchWHERE( search_hash, &curr_query->core, AND_OP_JOIN, ctx );
5205 osrfAppSessionStatus(
5207 OSRF_STATUS_INTERNALSERVERERROR,
5208 "osrfMethodException",
5210 "Severe query error -- see error log for more details"
5212 buffer_free( sql_buf );
5213 if( defaultselhash )
5214 jsonObjectFree( defaultselhash );
5215 clear_query_stack();
5218 buffer_add( sql_buf, pred );
5222 // Add the ORDER BY, LIMIT, and/or OFFSET clauses, if present
5223 if( rest_of_query ) {
5224 const jsonObject* order_by = NULL;
5225 if( ( order_by = jsonObjectGetKeyConst( rest_of_query, "order_by" )) ){
5227 char* order_by_list = NULL;
5229 if( JSON_ARRAY == order_by->type ) {
5230 order_by_list = buildOrderByFromArray( ctx, order_by );
5231 if( !order_by_list ) {
5232 buffer_free( sql_buf );
5233 if( defaultselhash )
5234 jsonObjectFree( defaultselhash );
5235 clear_query_stack();
5238 } else if( JSON_HASH == order_by->type ) {
5239 // We expect order_by to be a JSON_HASH keyed on class names. Traverse it
5240 // and build a list of ORDER BY expressions.
5241 growing_buffer* order_buf = buffer_init( 128 );
5243 jsonIterator* class_itr = jsonNewIterator( order_by );
5244 while( (snode = jsonIteratorNext( class_itr )) ) { // For each class:
5246 ClassInfo* order_class_info = search_alias( class_itr->key );
5247 if( ! order_class_info )
5248 continue; // class not referenced by FROM clause? Ignore it.
5250 if( JSON_HASH == snode->type ) {
5252 // If the data for the current class is a JSON_HASH, then it is
5253 // keyed on field name.
5255 const jsonObject* onode = NULL;
5256 jsonIterator* order_itr = jsonNewIterator( snode );
5257 while( (onode = jsonIteratorNext( order_itr )) ) { // For each field
5259 osrfHash* field_def = osrfHashGet(
5260 order_class_info->fields, order_itr->key );
5262 continue; // Field not defined in IDL? Ignore it.
5263 if( str_is_true( osrfHashGet( field_def, "virtual")))
5264 continue; // Field is virtual? Ignore it.
5266 char* field_str = NULL;
5267 char* direction = NULL;
5268 if( onode->type == JSON_HASH ) {
5269 if( jsonObjectGetKeyConst( onode, "transform" ) ) {
5270 field_str = searchFieldTransform(
5271 class_itr->key, field_def, onode );
5273 osrfAppSessionStatus(
5275 OSRF_STATUS_INTERNALSERVERERROR,
5276 "osrfMethodException",
5278 "Severe query error in ORDER BY clause -- "
5279 "see error log for more details"
5281 jsonIteratorFree( order_itr );
5282 jsonIteratorFree( class_itr );
5283 buffer_free( order_buf );
5284 buffer_free( sql_buf );
5285 if( defaultselhash )
5286 jsonObjectFree( defaultselhash );
5287 clear_query_stack();
5291 growing_buffer* field_buf = buffer_init( 16 );
5292 buffer_fadd( field_buf, "\"%s\".%s",
5293 class_itr->key, order_itr->key );
5294 field_str = buffer_release( field_buf );
5297 if( ( order_by = jsonObjectGetKeyConst( onode, "direction" )) ) {
5298 const char* dir = jsonObjectGetString( order_by );
5299 if(!strncasecmp( dir, "d", 1 )) {
5300 direction = " DESC";
5304 field_str = strdup( order_itr->key );
5305 const char* dir = jsonObjectGetString( onode );
5306 if( !strncasecmp( dir, "d", 1 )) {
5307 direction = " DESC";
5316 buffer_add( order_buf, ", " );
5319 buffer_add( order_buf, field_str );
5323 buffer_add( order_buf, direction );
5325 } // end while; looping over ORDER BY expressions
5327 jsonIteratorFree( order_itr );
5329 } else if( JSON_STRING == snode->type ) {
5330 // We expect a comma-separated list of sort fields.
5331 const char* str = jsonObjectGetString( snode );
5332 if( strchr( str, ';' )) {
5333 // No semicolons allowed. It is theoretically possible for a
5334 // legitimate semicolon to occur within quotes, but it's not likely
5335 // to occur in practice in the context of an ORDER BY list.
5336 osrfLogError( OSRF_LOG_MARK, "%s: Possible attempt at SOL injection -- "
5337 "semicolon found in ORDER BY list: \"%s\"", modulename, str );
5339 osrfAppSessionStatus(
5341 OSRF_STATUS_INTERNALSERVERERROR,
5342 "osrfMethodException",
5344 "Possible attempt at SOL injection -- "
5345 "semicolon found in ORDER BY list"
5348 jsonIteratorFree( class_itr );
5349 buffer_free( order_buf );
5350 buffer_free( sql_buf );
5351 if( defaultselhash )
5352 jsonObjectFree( defaultselhash );
5353 clear_query_stack();
5356 buffer_add( order_buf, str );
5360 } // end while; looping over order_by classes
5362 jsonIteratorFree( class_itr );
5363 order_by_list = buffer_release( order_buf );
5366 osrfLogWarning( OSRF_LOG_MARK,
5367 "\"order_by\" object in a query is not a JSON_HASH or JSON_ARRAY;"
5368 "no ORDER BY generated" );
5371 if( order_by_list && *order_by_list ) {
5372 OSRF_BUFFER_ADD( sql_buf, " ORDER BY " );
5373 OSRF_BUFFER_ADD( sql_buf, order_by_list );
5376 free( order_by_list );
5379 const jsonObject* limit = jsonObjectGetKeyConst( rest_of_query, "limit" );
5381 const char* str = jsonObjectGetString( limit );
5389 const jsonObject* offset = jsonObjectGetKeyConst( rest_of_query, "offset" );
5391 const char* str = jsonObjectGetString( offset );
5400 if( defaultselhash )
5401 jsonObjectFree( defaultselhash );
5402 clear_query_stack();
5404 OSRF_BUFFER_ADD_CHAR( sql_buf, ';' );
5405 return buffer_release( sql_buf );
5408 int doJSONSearch ( osrfMethodContext* ctx ) {
5409 if(osrfMethodVerifyContext( ctx )) {
5410 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
5414 osrfLogDebug( OSRF_LOG_MARK, "Received query request" );
5418 jsonObject* hash = jsonObjectGetIndex( ctx->params, 0 );
5422 if( obj_is_true( jsonObjectGetKeyConst( hash, "distinct" )))
5423 flags |= SELECT_DISTINCT;
5425 if( obj_is_true( jsonObjectGetKeyConst( hash, "no_i18n" )))
5426 flags |= DISABLE_I18N;
5428 osrfLogDebug( OSRF_LOG_MARK, "Building SQL ..." );
5429 clear_query_stack(); // a possibly needless precaution
5430 char* sql = buildQuery( ctx, hash, flags );
5431 clear_query_stack();
5438 osrfLogDebug( OSRF_LOG_MARK, "%s SQL = %s", modulename, sql );
5441 dbhandle = writehandle;
5443 dbi_result result = dbi_conn_query( dbhandle, sql );
5446 osrfLogDebug( OSRF_LOG_MARK, "Query returned with no errors" );
5448 if( dbi_result_first_row( result )) {
5449 /* JSONify the result */
5450 osrfLogDebug( OSRF_LOG_MARK, "Query returned at least one row" );
5453 jsonObject* return_val = oilsMakeJSONFromResult( result );
5454 osrfAppRespond( ctx, return_val );
5455 jsonObjectFree( return_val );
5456 } while( dbi_result_next_row( result ));
5459 osrfLogDebug( OSRF_LOG_MARK, "%s returned no results for query %s", modulename, sql );
5462 osrfAppRespondComplete( ctx, NULL );
5464 /* clean up the query */
5465 dbi_result_free( result );
5470 int errnum = dbi_conn_error( dbhandle, &msg );
5471 osrfLogError( OSRF_LOG_MARK, "%s: Error with query [%s]: %d %s",
5472 modulename, sql, errnum, msg ? msg : "(No description available)" );
5473 osrfAppSessionStatus(
5475 OSRF_STATUS_INTERNALSERVERERROR,
5476 "osrfMethodException",
5478 "Severe query error -- see error log for more details"
5480 if( !oilsIsDBConnected( dbhandle ))
5481 osrfAppSessionPanic( ctx->session );
5488 // The last parameter, err, is used to report an error condition by updating an int owned by
5489 // the calling code.
5491 // In case of an error, we set *err to -1. If there is no error, *err is left unchanged.
5492 // It is the responsibility of the calling code to initialize *err before the
5493 // call, so that it will be able to make sense of the result.
5495 // Note also that we return NULL if and only if we set *err to -1. So the err parameter is
5496 // redundant anyway.
5497 static jsonObject* doFieldmapperSearch( osrfMethodContext* ctx, osrfHash* class_meta,
5498 jsonObject* where_hash, jsonObject* query_hash, int* err ) {
5501 dbhandle = writehandle;
5503 char* core_class = osrfHashGet( class_meta, "classname" );
5504 char* pkey = osrfHashGet( class_meta, "primarykey" );
5506 const jsonObject* _tmp;
5508 char* sql = buildSELECT( where_hash, query_hash, class_meta, ctx );
5510 osrfLogDebug( OSRF_LOG_MARK, "Problem building query, returning NULL" );
5515 osrfLogDebug( OSRF_LOG_MARK, "%s SQL = %s", modulename, sql );
5517 dbi_result result = dbi_conn_query( dbhandle, sql );
5518 if( NULL == result ) {
5520 int errnum = dbi_conn_error( dbhandle, &msg );
5521 osrfLogError(OSRF_LOG_MARK, "%s: Error retrieving %s with query [%s]: %d %s",
5522 modulename, osrfHashGet( class_meta, "fieldmapper" ), sql, errnum,
5523 msg ? msg : "(No description available)" );
5524 if( !oilsIsDBConnected( dbhandle ))
5525 osrfAppSessionPanic( ctx->session );
5526 osrfAppSessionStatus(
5528 OSRF_STATUS_INTERNALSERVERERROR,
5529 "osrfMethodException",
5531 "Severe query error -- see error log for more details"
5538 osrfLogDebug( OSRF_LOG_MARK, "Query returned with no errors" );
5541 jsonObject* res_list = jsonNewObjectType( JSON_ARRAY );
5542 jsonObject* row_obj = NULL;
5544 if( dbi_result_first_row( result )) {
5546 // Convert each row to a JSON_ARRAY of column values, and enclose those objects
5547 // in a JSON_ARRAY of rows. If two or more rows have the same key value, then
5548 // eliminate the duplicates.
5549 osrfLogDebug( OSRF_LOG_MARK, "Query returned at least one row" );
5550 osrfHash* dedup = osrfNewHash();
5552 row_obj = oilsMakeFieldmapperFromResult( result, class_meta );
5553 char* pkey_val = oilsFMGetString( row_obj, pkey );
5554 if( osrfHashGet( dedup, pkey_val ) ) {
5555 jsonObjectFree( row_obj );
5558 osrfHashSet( dedup, pkey_val, pkey_val );
5559 jsonObjectPush( res_list, row_obj );
5561 } while( dbi_result_next_row( result ));
5562 osrfHashFree( dedup );
5565 osrfLogDebug( OSRF_LOG_MARK, "%s returned no results for query %s",
5569 /* clean up the query */
5570 dbi_result_free( result );
5573 // If we're asked to flesh, and there's anything to flesh, then flesh it
5574 // (but not for PCRUD, lest the user to bypass permissions by fleshing
5575 // something that he has no permission to look at).
5576 if( res_list->size && query_hash && ! enforce_pcrud ) {
5577 _tmp = jsonObjectGetKeyConst( query_hash, "flesh" );
5579 // Get the flesh depth
5580 int flesh_depth = (int) jsonObjectGetNumber( _tmp );
5581 if( flesh_depth == -1 || flesh_depth > max_flesh_depth )
5582 flesh_depth = max_flesh_depth;
5584 // We need a non-zero flesh depth, and a list of fields to flesh
5585 const jsonObject* temp_blob = jsonObjectGetKeyConst( query_hash, "flesh_fields" );
5586 if( temp_blob && flesh_depth > 0 ) {
5588 jsonObject* flesh_blob = jsonObjectClone( temp_blob );
5589 const jsonObject* flesh_fields = jsonObjectGetKeyConst( flesh_blob, core_class );
5591 osrfStringArray* link_fields = NULL;
5592 osrfHash* links = osrfHashGet( class_meta, "links" );
5594 // Make an osrfStringArray of the names of fields to be fleshed
5595 if( flesh_fields ) {
5596 if( flesh_fields->size == 1 ) {
5597 const char* _t = jsonObjectGetString(
5598 jsonObjectGetIndex( flesh_fields, 0 ) );
5599 if( !strcmp( _t, "*" ))
5600 link_fields = osrfHashKeys( links );
5603 if( !link_fields ) {
5605 link_fields = osrfNewStringArray( 1 );
5606 jsonIterator* _i = jsonNewIterator( flesh_fields );
5607 while ((_f = jsonIteratorNext( _i ))) {
5608 osrfStringArrayAdd( link_fields, jsonObjectGetString( _f ) );
5610 jsonIteratorFree( _i );
5614 osrfHash* fields = osrfHashGet( class_meta, "fields" );
5616 // Iterate over the JSON_ARRAY of rows
5618 unsigned long res_idx = 0;
5619 while((cur = jsonObjectGetIndex( res_list, res_idx++ ) )) {
5622 const char* link_field;
5624 // Iterate over the list of fleshable fields
5625 while( (link_field = osrfStringArrayGetString(link_fields, i++)) ) {
5627 osrfLogDebug( OSRF_LOG_MARK, "Starting to flesh %s", link_field );
5629 osrfHash* kid_link = osrfHashGet( links, link_field );
5631 continue; // Not a link field; skip it
5633 osrfHash* field = osrfHashGet( fields, link_field );
5635 continue; // Not a field at all; skip it (IDL is ill-formed)
5637 osrfHash* kid_idl = osrfHashGet( oilsIDL(),
5638 osrfHashGet( kid_link, "class" ));
5640 continue; // The class it links to doesn't exist; skip it
5642 const char* reltype = osrfHashGet( kid_link, "reltype" );
5644 continue; // No reltype; skip it (IDL is ill-formed)
5646 osrfHash* value_field = field;
5648 if( !strcmp( reltype, "has_many" )
5649 || !strcmp( reltype, "might_have" ) ) { // has_many or might_have
5650 value_field = osrfHashGet(
5651 fields, osrfHashGet( class_meta, "primarykey" ) );
5654 osrfStringArray* link_map = osrfHashGet( kid_link, "map" );
5656 if( link_map->size > 0 ) {
5657 jsonObject* _kid_key = jsonNewObjectType( JSON_ARRAY );
5660 jsonNewObject( osrfStringArrayGetString( link_map, 0 ) )
5665 osrfHashGet( kid_link, "class" ),
5672 "Link field: %s, remote class: %s, fkey: %s, reltype: %s",
5673 osrfHashGet( kid_link, "field" ),
5674 osrfHashGet( kid_link, "class" ),
5675 osrfHashGet( kid_link, "key" ),
5676 osrfHashGet( kid_link, "reltype" )
5679 const char* search_key = jsonObjectGetString(
5680 jsonObjectGetIndex( cur,
5681 atoi( osrfHashGet( value_field, "array_position" ) )
5686 osrfLogDebug( OSRF_LOG_MARK, "Nothing to search for!" );
5690 osrfLogDebug( OSRF_LOG_MARK, "Creating param objects..." );
5692 // construct WHERE clause
5693 jsonObject* where_clause = jsonNewObjectType( JSON_HASH );
5696 osrfHashGet( kid_link, "key" ),
5697 jsonNewObject( search_key )
5700 // construct the rest of the query, mostly
5701 // by copying pieces of the previous level of query
5702 jsonObject* rest_of_query = jsonNewObjectType( JSON_HASH );
5703 jsonObjectSetKey( rest_of_query, "flesh",
5704 jsonNewNumberObject( flesh_depth - 1 + link_map->size )
5708 jsonObjectSetKey( rest_of_query, "flesh_fields",
5709 jsonObjectClone( flesh_blob ));
5711 if( jsonObjectGetKeyConst( query_hash, "order_by" )) {
5712 jsonObjectSetKey( rest_of_query, "order_by",
5713 jsonObjectClone( jsonObjectGetKeyConst( query_hash, "order_by" ))
5717 if( jsonObjectGetKeyConst( query_hash, "select" )) {
5718 jsonObjectSetKey( rest_of_query, "select",
5719 jsonObjectClone( jsonObjectGetKeyConst( query_hash, "select" ))
5723 // do the query, recursively, to expand the fleshable field
5724 jsonObject* kids = doFieldmapperSearch( ctx, kid_idl,
5725 where_clause, rest_of_query, err );
5727 jsonObjectFree( where_clause );
5728 jsonObjectFree( rest_of_query );
5731 osrfStringArrayFree( link_fields );
5732 jsonObjectFree( res_list );
5733 jsonObjectFree( flesh_blob );
5737 osrfLogDebug( OSRF_LOG_MARK, "Search for %s return %d linked objects",
5738 osrfHashGet( kid_link, "class" ), kids->size );
5740 // Traverse the result set
5741 jsonObject* X = NULL;
5742 if( link_map->size > 0 && kids->size > 0 ) {
5744 kids = jsonNewObjectType( JSON_ARRAY );
5746 jsonObject* _k_node;
5747 unsigned long res_idx = 0;
5748 while((_k_node = jsonObjectGetIndex( X, res_idx++ ) )) {
5754 (unsigned long) atoi(
5760 osrfHashGet( kid_link, "class" )
5764 osrfStringArrayGetString( link_map, 0 )
5772 } // end while loop traversing X
5775 if( !strcmp( osrfHashGet( kid_link, "reltype" ), "has_a" )
5776 || !strcmp( osrfHashGet( kid_link, "reltype" ), "might_have" )) {
5777 osrfLogDebug(OSRF_LOG_MARK, "Storing fleshed objects in %s",
5778 osrfHashGet( kid_link, "field" ));
5781 (unsigned long) atoi( osrfHashGet( field, "array_position" ) ),
5782 jsonObjectClone( jsonObjectGetIndex( kids, 0 ))
5786 if( !strcmp( osrfHashGet( kid_link, "reltype" ), "has_many" )) {
5788 osrfLogDebug( OSRF_LOG_MARK, "Storing fleshed objects in %s",
5789 osrfHashGet( kid_link, "field" ) );
5792 (unsigned long) atoi( osrfHashGet( field, "array_position" ) ),
5793 jsonObjectClone( kids )
5798 jsonObjectFree( kids );
5802 jsonObjectFree( kids );
5804 osrfLogDebug( OSRF_LOG_MARK, "Fleshing of %s complete",
5805 osrfHashGet( kid_link, "field" ) );
5806 osrfLogDebug( OSRF_LOG_MARK, "%s", jsonObjectToJSON( cur ));
5808 } // end while loop traversing list of fleshable fields
5809 } // end while loop traversing res_list
5810 jsonObjectFree( flesh_blob );
5811 osrfStringArrayFree( link_fields );
5820 int doUpdate( osrfMethodContext* ctx ) {
5821 if( osrfMethodVerifyContext( ctx )) {
5822 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
5827 timeout_needs_resetting = 1;
5829 osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
5831 jsonObject* target = NULL;
5833 target = jsonObjectGetIndex( ctx->params, 1 );
5835 target = jsonObjectGetIndex( ctx->params, 0 );
5837 if(!verifyObjectClass( ctx, target )) {
5838 osrfAppRespondComplete( ctx, NULL );
5842 if( getXactId( ctx ) == NULL ) {
5843 osrfAppSessionStatus(
5845 OSRF_STATUS_BADREQUEST,
5846 "osrfMethodException",
5848 "No active transaction -- required for UPDATE"
5850 osrfAppRespondComplete( ctx, NULL );
5854 // The following test is harmless but redundant. If a class is
5855 // readonly, we don't register an update method for it.
5856 if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
5857 osrfAppSessionStatus(
5859 OSRF_STATUS_BADREQUEST,
5860 "osrfMethodException",
5862 "Cannot UPDATE readonly class"
5864 osrfAppRespondComplete( ctx, NULL );
5868 const char* trans_id = getXactId( ctx );
5870 // Set the last_xact_id
5871 int index = oilsIDL_ntop( target->classname, "last_xact_id" );
5873 osrfLogDebug( OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d",
5874 trans_id, target->classname, index );
5875 jsonObjectSetIndex( target, index, jsonNewObject( trans_id ));
5878 char* pkey = osrfHashGet( meta, "primarykey" );
5879 osrfHash* fields = osrfHashGet( meta, "fields" );
5881 char* id = oilsFMGetString( target, pkey );
5885 "%s updating %s object with %s = %s",
5887 osrfHashGet( meta, "fieldmapper" ),
5892 dbhandle = writehandle;
5893 growing_buffer* sql = buffer_init( 128 );
5894 buffer_fadd( sql,"UPDATE %s SET", osrfHashGet( meta, "tablename" ));
5897 osrfHash* field_def = NULL;
5898 osrfHashIterator* field_itr = osrfNewHashIterator( fields );
5899 while( ( field_def = osrfHashIteratorNext( field_itr ) ) ) {
5901 // Skip virtual fields, and the primary key
5902 if( str_is_true( osrfHashGet( field_def, "virtual") ) )
5905 const char* field_name = osrfHashIteratorKey( field_itr );
5906 if( ! strcmp( field_name, pkey ) )
5909 const jsonObject* field_object = oilsFMGetObject( target, field_name );
5911 int value_is_numeric = 0; // boolean
5913 if( field_object && field_object->classname ) {
5914 value = oilsFMGetString(
5916 (char*) oilsIDLFindPath( "/%s/primarykey", field_object->classname )
5918 } else if( field_object && JSON_BOOL == field_object->type ) {
5919 if( jsonBoolIsTrue( field_object ) )
5920 value = strdup( "t" );
5922 value = strdup( "f" );
5924 value = jsonObjectToSimpleString( field_object );
5925 if( field_object && JSON_NUMBER == field_object->type )
5926 value_is_numeric = 1;
5929 osrfLogDebug( OSRF_LOG_MARK, "Updating %s object with %s = %s",
5930 osrfHashGet( meta, "fieldmapper" ), field_name, value);
5932 if( !field_object || field_object->type == JSON_NULL ) {
5933 if( !( !( strcmp( osrfHashGet( meta, "classname" ), "au" ) )
5934 && !( strcmp( field_name, "passwd" ) )) ) { // arg at the special case!
5938 OSRF_BUFFER_ADD_CHAR( sql, ',' );
5939 buffer_fadd( sql, " %s = NULL", field_name );
5942 } else if( value_is_numeric || !strcmp( get_primitive( field_def ), "number") ) {
5946 OSRF_BUFFER_ADD_CHAR( sql, ',' );
5948 const char* numtype = get_datatype( field_def );
5949 if( !strncmp( numtype, "INT", 3 ) ) {
5950 buffer_fadd( sql, " %s = %ld", field_name, atol( value ) );
5951 } else if( !strcmp( numtype, "NUMERIC" ) ) {
5952 buffer_fadd( sql, " %s = %f", field_name, atof( value ) );
5954 // Must really be intended as a string, so quote it
5955 if( dbi_conn_quote_string( dbhandle, &value )) {
5956 buffer_fadd( sql, " %s = %s", field_name, value );
5958 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting string [%s]",
5959 modulename, value );
5960 osrfAppSessionStatus(
5962 OSRF_STATUS_INTERNALSERVERERROR,
5963 "osrfMethodException",
5965 "Error quoting string -- please see the error log for more details"
5969 osrfHashIteratorFree( field_itr );
5971 osrfAppRespondComplete( ctx, NULL );
5976 osrfLogDebug( OSRF_LOG_MARK, "%s is of type %s", field_name, numtype );
5979 if( dbi_conn_quote_string( dbhandle, &value ) ) {
5983 OSRF_BUFFER_ADD_CHAR( sql, ',' );
5984 buffer_fadd( sql, " %s = %s", field_name, value );
5986 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting string [%s]", modulename, value );
5987 osrfAppSessionStatus(
5989 OSRF_STATUS_INTERNALSERVERERROR,
5990 "osrfMethodException",
5992 "Error quoting string -- please see the error log for more details"
5996 osrfHashIteratorFree( field_itr );
5998 osrfAppRespondComplete( ctx, NULL );
6007 osrfHashIteratorFree( field_itr );
6009 jsonObject* obj = jsonNewObject( id );
6011 if( strcmp( get_primitive( osrfHashGet( osrfHashGet(meta, "fields"), pkey )), "number" ))
6012 dbi_conn_quote_string( dbhandle, &id );
6014 buffer_fadd( sql, " WHERE %s = %s;", pkey, id );
6016 char* query = buffer_release( sql );
6017 osrfLogDebug( OSRF_LOG_MARK, "%s: Update SQL [%s]", modulename, query );
6019 dbi_result result = dbi_conn_query( dbhandle, query );
6024 jsonObjectFree( obj );
6025 obj = jsonNewObject( NULL );
6027 int errnum = dbi_conn_error( dbhandle, &msg );
6030 "%s ERROR updating %s object with %s = %s: %d %s",
6032 osrfHashGet( meta, "fieldmapper" ),
6036 msg ? msg : "(No description available)"
6038 osrfAppSessionStatus(
6040 OSRF_STATUS_INTERNALSERVERERROR,
6041 "osrfMethodException",
6043 "Error in updating a row -- please see the error log for more details"
6045 if( !oilsIsDBConnected( dbhandle ))
6046 osrfAppSessionPanic( ctx->session );
6049 dbi_result_free( result );
6052 osrfAppRespondComplete( ctx, obj );
6053 jsonObjectFree( obj );
6057 int doDelete( osrfMethodContext* ctx ) {
6058 if( osrfMethodVerifyContext( ctx )) {
6059 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
6064 timeout_needs_resetting = 1;
6066 osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
6068 if( getXactId( ctx ) == NULL ) {
6069 osrfAppSessionStatus(
6071 OSRF_STATUS_BADREQUEST,
6072 "osrfMethodException",
6074 "No active transaction -- required for DELETE"
6076 osrfAppRespondComplete( ctx, NULL );
6080 // The following test is harmless but redundant. If a class is
6081 // readonly, we don't register a delete method for it.
6082 if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
6083 osrfAppSessionStatus(
6085 OSRF_STATUS_BADREQUEST,
6086 "osrfMethodException",
6088 "Cannot DELETE readonly class"
6090 osrfAppRespondComplete( ctx, NULL );
6094 dbhandle = writehandle;
6096 char* pkey = osrfHashGet( meta, "primarykey" );
6103 if( jsonObjectGetIndex( ctx->params, _obj_pos )->classname ) {
6104 if( !verifyObjectClass( ctx, jsonObjectGetIndex( ctx->params, _obj_pos ))) {
6105 osrfAppRespondComplete( ctx, NULL );
6109 id = oilsFMGetString( jsonObjectGetIndex(ctx->params, _obj_pos), pkey );
6111 if( enforce_pcrud && !verifyObjectPCRUD( ctx, NULL, 1 )) {
6112 osrfAppRespondComplete( ctx, NULL );
6115 id = jsonObjectToSimpleString( jsonObjectGetIndex( ctx->params, _obj_pos ));
6120 "%s deleting %s object with %s = %s",
6122 osrfHashGet( meta, "fieldmapper" ),
6127 jsonObject* obj = jsonNewObject( id );
6129 if( strcmp( get_primitive( osrfHashGet( osrfHashGet(meta, "fields"), pkey ) ), "number" ) )
6130 dbi_conn_quote_string( writehandle, &id );
6132 dbi_result result = dbi_conn_queryf( writehandle, "DELETE FROM %s WHERE %s = %s;",
6133 osrfHashGet( meta, "tablename" ), pkey, id );
6138 jsonObjectFree( obj );
6139 obj = jsonNewObject( NULL );
6141 int errnum = dbi_conn_error( writehandle, &msg );
6144 "%s ERROR deleting %s object with %s = %s: %d %s",
6146 osrfHashGet( meta, "fieldmapper" ),
6150 msg ? msg : "(No description available)"
6152 osrfAppSessionStatus(
6154 OSRF_STATUS_INTERNALSERVERERROR,
6155 "osrfMethodException",
6157 "Error in deleting a row -- please see the error log for more details"
6159 if( !oilsIsDBConnected( writehandle ))
6160 osrfAppSessionPanic( ctx->session );
6162 dbi_result_free( result );
6166 osrfAppRespondComplete( ctx, obj );
6167 jsonObjectFree( obj );
6172 @brief Translate a row returned from the database into a jsonObject of type JSON_ARRAY.
6173 @param result An iterator for a result set; we only look at the current row.
6174 @param @meta Pointer to the class metadata for the core class.
6175 @return Pointer to the resulting jsonObject if successful; otherwise NULL.
6177 If a column is not defined in the IDL, or if it has no array_position defined for it in
6178 the IDL, or if it is defined as virtual, ignore it.
6180 Otherwise, translate the column value into a jsonObject of type JSON_NULL, JSON_NUMBER,
6181 or JSON_STRING. Then insert this jsonObject into the JSON_ARRAY according to its
6182 array_position in the IDL.
6184 A field defined in the IDL but not represented in the returned row will leave a hole
6185 in the JSON_ARRAY. In effect it will be treated as a null value.
6187 In the resulting JSON_ARRAY, the field values appear in the sequence defined by the IDL,
6188 regardless of their sequence in the SELECT statement. The JSON_ARRAY is assigned the
6189 classname corresponding to the @a meta argument.
6191 The calling code is responsible for freeing the the resulting jsonObject by calling
6194 static jsonObject* oilsMakeFieldmapperFromResult( dbi_result result, osrfHash* meta) {
6195 if( !( result && meta )) return NULL;
6197 jsonObject* object = jsonNewObjectType( JSON_ARRAY );
6198 jsonObjectSetClass( object, osrfHashGet( meta, "classname" ));
6199 osrfLogInternal( OSRF_LOG_MARK, "Setting object class to %s ", object->classname );
6201 osrfHash* fields = osrfHashGet( meta, "fields" );
6203 int columnIndex = 1;
6204 const char* columnName;
6206 /* cycle through the columns in the row returned from the database */
6207 while( (columnName = dbi_result_get_field_name( result, columnIndex )) ) {
6209 osrfLogInternal( OSRF_LOG_MARK, "Looking for column named [%s]...", (char*) columnName );
6211 int fmIndex = -1; // Will be set to the IDL's sequence number for this field
6213 /* determine the field type and storage attributes */
6214 unsigned short type = dbi_result_get_field_type_idx( result, columnIndex );
6215 int attr = dbi_result_get_field_attribs_idx( result, columnIndex );
6217 // Fetch the IDL's sequence number for the field. If the field isn't in the IDL,
6218 // or if it has no sequence number there, or if it's virtual, skip it.
6219 osrfHash* _f = osrfHashGet( fields, (char*) columnName );
6222 if( str_is_true( osrfHashGet( _f, "virtual" )))
6223 continue; // skip this column: IDL says it's virtual
6225 const char* pos = (char*) osrfHashGet( _f, "array_position" );
6226 if( !pos ) // IDL has no sequence number for it. This shouldn't happen,
6227 continue; // since we assign sequence numbers dynamically as we load the IDL.
6229 fmIndex = atoi( pos );
6230 osrfLogInternal( OSRF_LOG_MARK, "... Found column at position [%s]...", pos );
6232 continue; // This field is not defined in the IDL
6235 // Stuff the column value into a slot in the JSON_ARRAY, indexed according to the
6236 // sequence number from the IDL (which is likely to be different from the sequence
6237 // of columns in the SELECT clause).
6238 if( dbi_result_field_is_null_idx( result, columnIndex )) {
6239 jsonObjectSetIndex( object, fmIndex, jsonNewObject( NULL ));
6244 case DBI_TYPE_INTEGER :
6246 if( attr & DBI_INTEGER_SIZE8 )
6247 jsonObjectSetIndex( object, fmIndex,
6248 jsonNewNumberObject(
6249 dbi_result_get_longlong_idx( result, columnIndex )));
6251 jsonObjectSetIndex( object, fmIndex,
6252 jsonNewNumberObject( dbi_result_get_int_idx( result, columnIndex )));
6256 case DBI_TYPE_DECIMAL :
6257 jsonObjectSetIndex( object, fmIndex,
6258 jsonNewNumberObject( dbi_result_get_double_idx(result, columnIndex )));
6261 case DBI_TYPE_STRING :
6266 jsonNewObject( dbi_result_get_string_idx( result, columnIndex ))
6271 case DBI_TYPE_DATETIME : {
6273 char dt_string[ 256 ] = "";
6276 // Fetch the date column as a time_t
6277 time_t _tmp_dt = dbi_result_get_datetime_idx( result, columnIndex );
6279 // Translate the time_t to a human-readable string
6280 if( !( attr & DBI_DATETIME_DATE )) {
6281 gmtime_r( &_tmp_dt, &gmdt );
6282 strftime( dt_string, sizeof( dt_string ), "%T", &gmdt );
6283 } else if( !( attr & DBI_DATETIME_TIME )) {
6284 localtime_r( &_tmp_dt, &gmdt );
6285 strftime( dt_string, sizeof( dt_string ), "%F", &gmdt );
6287 localtime_r( &_tmp_dt, &gmdt );
6288 strftime( dt_string, sizeof( dt_string ), "%FT%T%z", &gmdt );
6291 jsonObjectSetIndex( object, fmIndex, jsonNewObject( dt_string ));
6295 case DBI_TYPE_BINARY :
6296 osrfLogError( OSRF_LOG_MARK,
6297 "Can't do binary at column %s : index %d", columnName, columnIndex );
6306 static jsonObject* oilsMakeJSONFromResult( dbi_result result ) {
6307 if( !result ) return NULL;
6309 jsonObject* object = jsonNewObject( NULL );
6312 char dt_string[ 256 ];
6316 int columnIndex = 1;
6318 unsigned short type;
6319 const char* columnName;
6321 /* cycle through the column list */
6322 while(( columnName = dbi_result_get_field_name( result, columnIndex ))) {
6324 osrfLogInternal( OSRF_LOG_MARK, "Looking for column named [%s]...", (char*) columnName );
6326 fmIndex = -1; // reset the position
6328 /* determine the field type and storage attributes */
6329 type = dbi_result_get_field_type_idx( result, columnIndex );
6330 attr = dbi_result_get_field_attribs_idx( result, columnIndex );
6332 if( dbi_result_field_is_null_idx( result, columnIndex )) {
6333 jsonObjectSetKey( object, columnName, jsonNewObject( NULL ));
6338 case DBI_TYPE_INTEGER :
6340 if( attr & DBI_INTEGER_SIZE8 )
6341 jsonObjectSetKey( object, columnName,
6342 jsonNewNumberObject( dbi_result_get_longlong_idx(
6343 result, columnIndex )) );
6345 jsonObjectSetKey( object, columnName, jsonNewNumberObject(
6346 dbi_result_get_int_idx( result, columnIndex )) );
6349 case DBI_TYPE_DECIMAL :
6350 jsonObjectSetKey( object, columnName, jsonNewNumberObject(
6351 dbi_result_get_double_idx( result, columnIndex )) );
6354 case DBI_TYPE_STRING :
6355 jsonObjectSetKey( object, columnName,
6356 jsonNewObject( dbi_result_get_string_idx( result, columnIndex )));
6359 case DBI_TYPE_DATETIME :
6361 memset( dt_string, '\0', sizeof( dt_string ));
6362 memset( &gmdt, '\0', sizeof( gmdt ));
6364 _tmp_dt = dbi_result_get_datetime_idx( result, columnIndex );
6366 if( !( attr & DBI_DATETIME_DATE )) {
6367 gmtime_r( &_tmp_dt, &gmdt );
6368 strftime( dt_string, sizeof( dt_string ), "%T", &gmdt );
6369 } else if( !( attr & DBI_DATETIME_TIME )) {
6370 localtime_r( &_tmp_dt, &gmdt );
6371 strftime( dt_string, sizeof( dt_string ), "%F", &gmdt );
6373 localtime_r( &_tmp_dt, &gmdt );
6374 strftime( dt_string, sizeof( dt_string ), "%FT%T%z", &gmdt );
6377 jsonObjectSetKey( object, columnName, jsonNewObject( dt_string ));
6380 case DBI_TYPE_BINARY :
6381 osrfLogError( OSRF_LOG_MARK,
6382 "Can't do binary at column %s : index %d", columnName, columnIndex );
6386 } // end while loop traversing result
6391 // Interpret a string as true or false
6392 int str_is_true( const char* str ) {
6393 if( NULL == str || strcasecmp( str, "true" ) )
6399 // Interpret a jsonObject as true or false
6400 static int obj_is_true( const jsonObject* obj ) {
6403 else switch( obj->type )
6411 if( strcasecmp( obj->value.s, "true" ) )
6415 case JSON_NUMBER : // Support 1/0 for perl's sake
6416 if( jsonObjectGetNumber( obj ) == 1.0 )
6425 // Translate a numeric code into a text string identifying a type of
6426 // jsonObject. To be used for building error messages.
6427 static const char* json_type( int code ) {
6433 return "JSON_ARRAY";
6435 return "JSON_STRING";
6437 return "JSON_NUMBER";
6443 return "(unrecognized)";
6447 // Extract the "primitive" attribute from an IDL field definition.
6448 // If we haven't initialized the app, then we must be running in
6449 // some kind of testbed. In that case, default to "string".
6450 static const char* get_primitive( osrfHash* field ) {
6451 const char* s = osrfHashGet( field, "primitive" );
6453 if( child_initialized )
6456 "%s ERROR No \"datatype\" attribute for field \"%s\"",
6458 osrfHashGet( field, "name" )
6466 // Extract the "datatype" attribute from an IDL field definition.
6467 // If we haven't initialized the app, then we must be running in
6468 // some kind of testbed. In that case, default to to NUMERIC,
6469 // since we look at the datatype only for numbers.
6470 static const char* get_datatype( osrfHash* field ) {
6471 const char* s = osrfHashGet( field, "datatype" );
6473 if( child_initialized )
6476 "%s ERROR No \"datatype\" attribute for field \"%s\"",
6478 osrfHashGet( field, "name" )
6487 @brief Determine whether a string is potentially a valid SQL identifier.
6488 @param s The identifier to be tested.
6489 @return 1 if the input string is potentially a valid SQL identifier, or 0 if not.
6491 Purpose: to prevent certain kinds of SQL injection. To that end we don't necessarily
6492 need to follow all the rules exactly, such as requiring that the first character not
6495 We allow leading and trailing white space. In between, we do not allow punctuation
6496 (except for underscores and dollar signs), control characters, or embedded white space.
6498 More pedantically we should allow quoted identifiers containing arbitrary characters, but
6499 for the foreseeable future such quoted identifiers are not likely to be an issue.
6501 int is_identifier( const char* s) {
6505 // Skip leading white space
6506 while( isspace( (unsigned char) *s ) )
6510 return 0; // Nothing but white space? Not okay.
6512 // Check each character until we reach white space or
6513 // end-of-string. Letters, digits, underscores, and
6514 // dollar signs are okay. With the exception of periods
6515 // (as in schema.identifier), control characters and other
6516 // punctuation characters are not okay. Anything else
6517 // is okay -- it could for example be part of a multibyte
6518 // UTF8 character such as a letter with diacritical marks,
6519 // and those are allowed.
6521 if( isalnum( (unsigned char) *s )
6525 ; // Fine; keep going
6526 else if( ispunct( (unsigned char) *s )
6527 || iscntrl( (unsigned char) *s ) )
6530 } while( *s && ! isspace( (unsigned char) *s ) );
6532 // If we found any white space in the above loop,
6533 // the rest had better be all white space.
6535 while( isspace( (unsigned char) *s ) )
6539 return 0; // White space was embedded within non-white space
6545 @brief Determine whether to accept a character string as a comparison operator.
6546 @param op The candidate comparison operator.
6547 @return 1 if the string is acceptable as a comparison operator, or 0 if not.
6549 We don't validate the operator for real. We just make sure that it doesn't contain
6550 any semicolons or white space (with special exceptions for a few specific operators).
6551 The idea is to block certain kinds of SQL injection. If it has no semicolons or white
6552 space but it's still not a valid operator, then the database will complain.
6554 Another approach would be to compare the string against a short list of approved operators.
6555 We don't do that because we want to allow custom operators like ">100*", which at this
6556 writing would be difficult or impossible to express otherwise in a JSON query.
6558 int is_good_operator( const char* op ) {
6559 if( !op ) return 0; // Sanity check
6563 if( isspace( (unsigned char) *s ) ) {
6564 // Special exceptions for SIMILAR TO, IS DISTINCT FROM,
6565 // and IS NOT DISTINCT FROM.
6566 if( !strcasecmp( op, "similar to" ) )
6568 else if( !strcasecmp( op, "is distinct from" ) )
6570 else if( !strcasecmp( op, "is not distinct from" ) )
6575 else if( ';' == *s )
6583 @name Query Frame Management
6585 The following machinery supports a stack of query frames for use by SELECT().
6587 A query frame caches information about one level of a SELECT query. When we enter
6588 a subquery, we push another query frame onto the stack, and pop it off when we leave.
6590 The query frame stores information about the core class, and about any joined classes
6593 The main purpose is to map table aliases to classes and tables, so that a query can
6594 join to the same table more than once. A secondary goal is to reduce the number of
6595 lookups in the IDL by caching the results.
6599 #define STATIC_CLASS_INFO_COUNT 3
6601 static ClassInfo static_class_info[ STATIC_CLASS_INFO_COUNT ];
6604 @brief Allocate a ClassInfo as raw memory.
6605 @return Pointer to the newly allocated ClassInfo.
6607 Except for the in_use flag, which is used only by the allocation and deallocation
6608 logic, we don't initialize the ClassInfo here.
6610 static ClassInfo* allocate_class_info( void ) {
6611 // In order to reduce the number of mallocs and frees, we return a static
6612 // instance of ClassInfo, if we can find one that we're not already using.
6613 // We rely on the fact that the compiler will implicitly initialize the
6614 // static instances so that in_use == 0.
6617 for( i = 0; i < STATIC_CLASS_INFO_COUNT; ++i ) {
6618 if( ! static_class_info[ i ].in_use ) {
6619 static_class_info[ i ].in_use = 1;
6620 return static_class_info + i;
6624 // The static ones are all in use. Malloc one.
6626 return safe_malloc( sizeof( ClassInfo ) );
6630 @brief Free any malloc'd memory owned by a ClassInfo, returning it to a pristine state.
6631 @param info Pointer to the ClassInfo to be cleared.
6633 static void clear_class_info( ClassInfo* info ) {
6638 // Free any malloc'd strings
6640 if( info->alias != info->alias_store )
6641 free( info->alias );
6643 if( info->class_name != info->class_name_store )
6644 free( info->class_name );
6646 free( info->source_def );
6648 info->alias = info->class_name = info->source_def = NULL;
6653 @brief Free a ClassInfo and everything it owns.
6654 @param info Pointer to the ClassInfo to be freed.
6656 static void free_class_info( ClassInfo* info ) {
6661 clear_class_info( info );
6663 // If it's one of the static instances, just mark it as not in use
6666 for( i = 0; i < STATIC_CLASS_INFO_COUNT; ++i ) {
6667 if( info == static_class_info + i ) {
6668 static_class_info[ i ].in_use = 0;
6673 // Otherwise it must have been malloc'd, so free it
6679 @brief Populate an already-allocated ClassInfo.
6680 @param info Pointer to the ClassInfo to be populated.
6681 @param alias Alias for the class. If it is NULL, or an empty string, use the class
6683 @param class Name of the class.
6684 @return Zero if successful, or 1 if not.
6686 Populate the ClassInfo with copies of the alias and class name, and with pointers to
6687 the relevant portions of the IDL for the specified class.
6689 static int build_class_info( ClassInfo* info, const char* alias, const char* class ) {
6692 osrfLogError( OSRF_LOG_MARK,
6693 "%s ERROR: No ClassInfo available to populate", modulename );
6694 info->alias = info->class_name = info->source_def = NULL;
6695 info->class_def = info->fields = info->links = NULL;
6700 osrfLogError( OSRF_LOG_MARK,
6701 "%s ERROR: No class name provided for lookup", modulename );
6702 info->alias = info->class_name = info->source_def = NULL;
6703 info->class_def = info->fields = info->links = NULL;
6707 // Alias defaults to class name if not supplied
6708 if( ! alias || ! alias[ 0 ] )
6711 // Look up class info in the IDL
6712 osrfHash* class_def = osrfHashGet( oilsIDL(), class );
6714 osrfLogError( OSRF_LOG_MARK,
6715 "%s ERROR: Class %s not defined in IDL", modulename, class );
6716 info->alias = info->class_name = info->source_def = NULL;
6717 info->class_def = info->fields = info->links = NULL;
6719 } else if( str_is_true( osrfHashGet( class_def, "virtual" ) ) ) {
6720 osrfLogError( OSRF_LOG_MARK,
6721 "%s ERROR: Class %s is defined as virtual", modulename, class );
6722 info->alias = info->class_name = info->source_def = NULL;
6723 info->class_def = info->fields = info->links = NULL;
6727 osrfHash* links = osrfHashGet( class_def, "links" );
6729 osrfLogError( OSRF_LOG_MARK,
6730 "%s ERROR: No links defined in IDL for class %s", modulename, class );
6731 info->alias = info->class_name = info->source_def = NULL;
6732 info->class_def = info->fields = info->links = NULL;
6736 osrfHash* fields = osrfHashGet( class_def, "fields" );
6738 osrfLogError( OSRF_LOG_MARK,
6739 "%s ERROR: No fields defined in IDL for class %s", modulename, class );
6740 info->alias = info->class_name = info->source_def = NULL;
6741 info->class_def = info->fields = info->links = NULL;
6745 char* source_def = oilsGetRelation( class_def );
6749 // We got everything we need, so populate the ClassInfo
6750 if( strlen( alias ) > ALIAS_STORE_SIZE )
6751 info->alias = strdup( alias );
6753 strcpy( info->alias_store, alias );
6754 info->alias = info->alias_store;
6757 if( strlen( class ) > CLASS_NAME_STORE_SIZE )
6758 info->class_name = strdup( class );
6760 strcpy( info->class_name_store, class );
6761 info->class_name = info->class_name_store;
6764 info->source_def = source_def;
6766 info->class_def = class_def;
6767 info->links = links;
6768 info->fields = fields;
6773 #define STATIC_FRAME_COUNT 3
6775 static QueryFrame static_frame[ STATIC_FRAME_COUNT ];
6778 @brief Allocate a QueryFrame as raw memory.
6779 @return Pointer to the newly allocated QueryFrame.
6781 Except for the in_use flag, which is used only by the allocation and deallocation
6782 logic, we don't initialize the QueryFrame here.
6784 static QueryFrame* allocate_frame( void ) {
6785 // In order to reduce the number of mallocs and frees, we return a static
6786 // instance of QueryFrame, if we can find one that we're not already using.
6787 // We rely on the fact that the compiler will implicitly initialize the
6788 // static instances so that in_use == 0.
6791 for( i = 0; i < STATIC_FRAME_COUNT; ++i ) {
6792 if( ! static_frame[ i ].in_use ) {
6793 static_frame[ i ].in_use = 1;
6794 return static_frame + i;
6798 // The static ones are all in use. Malloc one.
6800 return safe_malloc( sizeof( QueryFrame ) );
6804 @brief Free a QueryFrame, and all the memory it owns.
6805 @param frame Pointer to the QueryFrame to be freed.
6807 static void free_query_frame( QueryFrame* frame ) {
6812 clear_class_info( &frame->core );
6814 // Free the join list
6816 ClassInfo* info = frame->join_list;
6819 free_class_info( info );
6823 frame->join_list = NULL;
6826 // If the frame is a static instance, just mark it as unused
6828 for( i = 0; i < STATIC_FRAME_COUNT; ++i ) {
6829 if( frame == static_frame + i ) {
6830 static_frame[ i ].in_use = 0;
6835 // Otherwise it must have been malloc'd, so free it
6841 @brief Search a given QueryFrame for a specified alias.
6842 @param frame Pointer to the QueryFrame to be searched.
6843 @param target The alias for which to search.
6844 @return Pointer to the ClassInfo for the specified alias, if found; otherwise NULL.
6846 static ClassInfo* search_alias_in_frame( QueryFrame* frame, const char* target ) {
6847 if( ! frame || ! target ) {
6851 ClassInfo* found_class = NULL;
6853 if( !strcmp( target, frame->core.alias ) )
6854 return &(frame->core);
6856 ClassInfo* curr_class = frame->join_list;
6857 while( curr_class ) {
6858 if( strcmp( target, curr_class->alias ) )
6859 curr_class = curr_class->next;
6861 found_class = curr_class;
6871 @brief Push a new (blank) QueryFrame onto the stack.
6873 static void push_query_frame( void ) {
6874 QueryFrame* frame = allocate_frame();
6875 frame->join_list = NULL;
6876 frame->next = curr_query;
6878 // Initialize the ClassInfo for the core class
6879 ClassInfo* core = &frame->core;
6880 core->alias = core->class_name = core->source_def = NULL;
6881 core->class_def = core->fields = core->links = NULL;
6887 @brief Pop a QueryFrame off the stack and destroy it.
6889 static void pop_query_frame( void ) {
6894 QueryFrame* popped = curr_query;
6895 curr_query = popped->next;
6897 free_query_frame( popped );
6901 @brief Populate the ClassInfo for the core class.
6902 @param alias Alias for the core class. If it is NULL or an empty string, we use the
6903 class name as an alias.
6904 @param class_name Name of the core class.
6905 @return Zero if successful, or 1 if not.
6907 Populate the ClassInfo of the core class with copies of the alias and class name, and
6908 with pointers to the relevant portions of the IDL for the core class.
6910 static int add_query_core( const char* alias, const char* class_name ) {
6913 if( ! curr_query ) {
6914 osrfLogError( OSRF_LOG_MARK,
6915 "%s ERROR: No QueryFrame available for class %s", modulename, class_name );
6917 } else if( curr_query->core.alias ) {
6918 osrfLogError( OSRF_LOG_MARK,
6919 "%s ERROR: Core class %s already populated as %s",
6920 modulename, curr_query->core.class_name, curr_query->core.alias );
6924 build_class_info( &curr_query->core, alias, class_name );
6925 if( curr_query->core.alias )
6928 osrfLogError( OSRF_LOG_MARK,
6929 "%s ERROR: Unable to look up core class %s", modulename, class_name );
6935 @brief Search the current QueryFrame for a specified alias.
6936 @param target The alias for which to search.
6937 @return A pointer to the corresponding ClassInfo, if found; otherwise NULL.
6939 static inline ClassInfo* search_alias( const char* target ) {
6940 return search_alias_in_frame( curr_query, target );
6944 @brief Search all levels of query for a specified alias, starting with the current query.
6945 @param target The alias for which to search.
6946 @return A pointer to the corresponding ClassInfo, if found; otherwise NULL.
6948 static ClassInfo* search_all_alias( const char* target ) {
6949 ClassInfo* found_class = NULL;
6950 QueryFrame* curr_frame = curr_query;
6952 while( curr_frame ) {
6953 if(( found_class = search_alias_in_frame( curr_frame, target ) ))
6956 curr_frame = curr_frame->next;
6963 @brief Add a class to the list of classes joined to the current query.
6964 @param alias Alias of the class to be added. If it is NULL or an empty string, we use
6965 the class name as an alias.
6966 @param classname The name of the class to be added.
6967 @return A pointer to the ClassInfo for the added class, if successful; otherwise NULL.
6969 static ClassInfo* add_joined_class( const char* alias, const char* classname ) {
6971 if( ! classname || ! *classname ) { // sanity check
6972 osrfLogError( OSRF_LOG_MARK, "Can't join a class with no class name" );
6979 const ClassInfo* conflict = search_alias( alias );
6981 osrfLogError( OSRF_LOG_MARK,
6982 "%s ERROR: Table alias \"%s\" conflicts with class \"%s\"",
6983 modulename, alias, conflict->class_name );
6987 ClassInfo* info = allocate_class_info();
6989 if( build_class_info( info, alias, classname ) ) {
6990 free_class_info( info );
6994 // Add the new ClassInfo to the join list of the current QueryFrame
6995 info->next = curr_query->join_list;
6996 curr_query->join_list = info;
7002 @brief Destroy all nodes on the query stack.
7004 static void clear_query_stack( void ) {