]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_cstore.c
In SELECT(): Eliminate a sanity check and error message because
[Evergreen.git] / Open-ILS / src / c-apps / oils_cstore.c
1 #include <ctype.h>
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_settings.h"
4 #include "opensrf/osrf_message.h"
5 #include "opensrf/utils.h"
6 #include "opensrf/osrf_json.h"
7 #include "opensrf/log.h"
8 #include "openils/oils_utils.h"
9 #include <dbi/dbi.h>
10
11 #include <time.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #ifdef RSTORE
17 #  define MODULENAME "open-ils.reporter-store"
18 #else
19 #  ifdef PCRUD
20 #    define MODULENAME "open-ils.pcrud"
21 #  else
22 #    define MODULENAME "open-ils.cstore"
23 #  endif
24 #endif
25
26 #define SUBSELECT       4
27 #define DISABLE_I18N    2
28 #define SELECT_DISTINCT 1
29 #define AND_OP_JOIN     0
30 #define OR_OP_JOIN      1
31
32 int osrfAppChildInit();
33 int osrfAppInitialize();
34 void osrfAppChildExit();
35
36 static int verifyObjectClass ( osrfMethodContext*, const jsonObject* );
37
38 int beginTransaction ( osrfMethodContext* );
39 int commitTransaction ( osrfMethodContext* );
40 int rollbackTransaction ( osrfMethodContext* );
41
42 int setSavepoint ( osrfMethodContext* );
43 int releaseSavepoint ( osrfMethodContext* );
44 int rollbackSavepoint ( osrfMethodContext* );
45
46 int doJSONSearch ( osrfMethodContext* );
47
48 int dispatchCRUDMethod ( osrfMethodContext* );
49 static jsonObject* doCreate ( osrfMethodContext*, int* );
50 static jsonObject* doRetrieve ( osrfMethodContext*, int* );
51 static jsonObject* doUpdate ( osrfMethodContext*, int* );
52 static jsonObject* doDelete ( osrfMethodContext*, int* );
53 static jsonObject* doFieldmapperSearch ( osrfMethodContext*, osrfHash*,
54         const jsonObject*, int* );
55 static jsonObject* oilsMakeFieldmapperFromResult( dbi_result, osrfHash* );
56 static jsonObject* oilsMakeJSONFromResult( dbi_result );
57
58 static char* searchSimplePredicate ( const char* op, const char* class, 
59                                 osrfHash* field, const jsonObject* node );
60 static char* searchFunctionPredicate ( const char*, osrfHash*, const jsonObject*, const char* );
61 static char* searchFieldTransform ( const char*, osrfHash*, const jsonObject*);
62 static char* searchFieldTransformPredicate ( const char*, osrfHash*, const jsonObject*, const char* );
63 static char* searchBETWEENPredicate ( const char*, osrfHash*, const jsonObject* );
64 static char* searchINPredicate ( const char*, osrfHash*,
65                                                                  jsonObject*, const char*, osrfMethodContext* );
66 static char* searchPredicate ( const char*, osrfHash*, jsonObject*, osrfMethodContext* );
67 static char* searchJOIN ( const jsonObject*, osrfHash* );
68 static char* searchWHERE ( const jsonObject*, osrfHash*, int, osrfMethodContext* );
69 static char* buildSELECT ( jsonObject*, jsonObject*, osrfHash*, osrfMethodContext* );
70
71 char* SELECT ( osrfMethodContext*, jsonObject*, jsonObject*, jsonObject*, jsonObject*, jsonObject*, jsonObject*, jsonObject*, int );
72
73 void userDataFree( void* );
74 static void sessionDataFree( char*, void* );
75 static char* getSourceDefinition( osrfHash* );
76 static int str_is_true( const char* str );
77 static int obj_is_true( const jsonObject* obj );
78 static const char* json_type( int code );
79 static const char* get_primitive( osrfHash* field );
80 static const char* get_datatype( osrfHash* field );
81 static int is_identifier( const char* s);
82 static int is_good_operator( const char* op );
83
84 #ifdef PCRUD
85 static jsonObject* verifyUserPCRUD( osrfMethodContext* );
86 static int verifyObjectPCRUD( osrfMethodContext*, const jsonObject* );
87 #endif
88
89 static int child_initialized = 0;   /* boolean */
90
91 static dbi_conn writehandle; /* our MASTER db connection */
92 static dbi_conn dbhandle; /* our CURRENT db connection */
93 //static osrfHash * readHandles;
94 static jsonObject* jsonNULL = NULL; // 
95 static int max_flesh_depth = 100;
96
97 /* called when this process is about to exit */
98 void osrfAppChildExit() {
99     osrfLogDebug(OSRF_LOG_MARK, "Child is exiting, disconnecting from database...");
100
101     int same = 0;
102     if (writehandle == dbhandle) same = 1;
103     if (writehandle) {
104         dbi_conn_query(writehandle, "ROLLBACK;");
105         dbi_conn_close(writehandle);
106         writehandle = NULL;
107     }
108     if (dbhandle && !same)
109         dbi_conn_close(dbhandle);
110
111     // XXX add cleanup of readHandles whenever that gets used
112
113     return;
114 }
115
116 int osrfAppInitialize() {
117
118     osrfLogInfo(OSRF_LOG_MARK, "Initializing the CStore Server...");
119     osrfLogInfo(OSRF_LOG_MARK, "Finding XML file...");
120
121     if (!oilsIDLInit( osrf_settings_host_value("/IDL") )) return 1; /* return non-zero to indicate error */
122
123     growing_buffer* method_name = buffer_init(64);
124 #ifndef PCRUD
125     // Generic search thingy
126     buffer_add(method_name, MODULENAME);
127         buffer_add(method_name, ".json_query");
128         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
129                                                    "doJSONSearch", "", 1, OSRF_METHOD_STREAMING );
130 #endif
131
132     // first we register all the transaction and savepoint methods
133     buffer_reset(method_name);
134         OSRF_BUFFER_ADD(method_name, MODULENAME);
135         OSRF_BUFFER_ADD(method_name, ".transaction.begin");
136         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
137                                                    "beginTransaction", "", 0, 0 );
138
139     buffer_reset(method_name);
140         OSRF_BUFFER_ADD(method_name, MODULENAME);
141         OSRF_BUFFER_ADD(method_name, ".transaction.commit");
142         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
143                                                    "commitTransaction", "", 0, 0 );
144
145     buffer_reset(method_name);
146         OSRF_BUFFER_ADD(method_name, MODULENAME);
147         OSRF_BUFFER_ADD(method_name, ".transaction.rollback");
148         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
149                                                    "rollbackTransaction", "", 0, 0 );
150
151     buffer_reset(method_name);
152         OSRF_BUFFER_ADD(method_name, MODULENAME);
153         OSRF_BUFFER_ADD(method_name, ".savepoint.set");
154         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
155                                                    "setSavepoint", "", 1, 0 );
156
157     buffer_reset(method_name);
158         OSRF_BUFFER_ADD(method_name, MODULENAME);
159         OSRF_BUFFER_ADD(method_name, ".savepoint.release");
160         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
161                                                    "releaseSavepoint", "", 1, 0 );
162
163     buffer_reset(method_name);
164         OSRF_BUFFER_ADD(method_name, MODULENAME);
165         OSRF_BUFFER_ADD(method_name, ".savepoint.rollback");
166         osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
167                                                    "rollbackSavepoint", "", 1, 0 );
168
169     buffer_free(method_name);
170
171         static const char* global_method[] = {
172                 "create",
173                 "retrieve",
174                 "update",
175                 "delete",
176                 "search",
177                 "id_list"
178         };
179         const int global_method_count
180                 = sizeof( global_method ) / sizeof ( global_method[0] );
181         
182     int c_index = 0; 
183     char* classname;
184     osrfStringArray* classes = osrfHashKeys( oilsIDL() );
185     osrfLogDebug(OSRF_LOG_MARK, "%d classes loaded", classes->size );
186     osrfLogDebug(OSRF_LOG_MARK,
187                 "At least %d methods will be generated", classes->size * global_method_count);
188
189     while ( (classname = osrfStringArrayGetString(classes, c_index++)) ) {
190         osrfLogInfo(OSRF_LOG_MARK, "Generating class methods for %s", classname);
191
192         osrfHash* idlClass = osrfHashGet(oilsIDL(), classname);
193
194         if (!osrfStringArrayContains( osrfHashGet(idlClass, "controller"), MODULENAME )) {
195             osrfLogInfo(OSRF_LOG_MARK, "%s is not listed as a controller for %s, moving on", MODULENAME, classname);
196             continue;
197         }
198
199                 if ( str_is_true( osrfHashGet(idlClass, "virtual") ) ) {
200                         osrfLogDebug(OSRF_LOG_MARK, "Class %s is virtual, skipping", classname );
201                         continue;
202                 }
203
204         // Look up some other attributes of the current class
205         const char* idlClass_fieldmapper = osrfHashGet(idlClass, "fieldmapper");
206                 const char* readonly = osrfHashGet(idlClass, "readonly");
207 #ifdef PCRUD
208         osrfHash* idlClass_permacrud = osrfHashGet(idlClass, "permacrud");
209 #endif
210
211         int i;
212         for( i = 0; i < global_method_count; ++i ) {
213             const char* method_type = global_method[ i ];
214             osrfLogDebug(OSRF_LOG_MARK,
215                 "Using files to build %s class methods for %s", method_type, classname);
216
217             if (!idlClass_fieldmapper) continue;
218
219 #ifdef PCRUD
220             if (!idlClass_permacrud) continue;
221
222             const char* tmp_method = method_type;
223             if ( *tmp_method == 'i' || *tmp_method == 's') {
224                 tmp_method = "retrieve";
225             }
226             if (!osrfHashGet( idlClass_permacrud, tmp_method )) continue;
227 #endif
228
229             if (    str_is_true( readonly ) &&
230                     ( *method_type == 'c' || *method_type == 'u' || *method_type == 'd')
231                ) continue;
232
233             osrfHash* method_meta = osrfNewHash();
234             osrfHashSet(method_meta, idlClass, "class");
235
236             method_name =  buffer_init(64);
237 #ifdef PCRUD
238             buffer_fadd(method_name, "%s.%s.%s", MODULENAME, method_type, classname);
239 #else
240             char* st_tmp = NULL;
241             char* part = NULL;
242             char* _fm = strdup( idlClass_fieldmapper );
243             part = strtok_r(_fm, ":", &st_tmp);
244
245             buffer_fadd(method_name, "%s.direct.%s", MODULENAME, part);
246
247             while ((part = strtok_r(NULL, ":", &st_tmp))) {
248                                 OSRF_BUFFER_ADD_CHAR(method_name, '.');
249                                 OSRF_BUFFER_ADD(method_name, part);
250             }
251                         OSRF_BUFFER_ADD_CHAR(method_name, '.');
252                         OSRF_BUFFER_ADD(method_name, method_type);
253             free(_fm);
254 #endif
255
256             char* method = buffer_release(method_name);
257
258             osrfHashSet( method_meta, method, "methodname" );
259             osrfHashSet( method_meta, strdup(method_type), "methodtype" );
260
261             int flags = 0;
262             if (*method_type == 'i' || *method_type == 's') {
263                 flags = flags | OSRF_METHOD_STREAMING;
264             }
265
266             osrfAppRegisterExtendedMethod(
267                     MODULENAME,
268                     method,
269                     "dispatchCRUDMethod",
270                     "",
271                     1,
272                     flags,
273                     (void*)method_meta
274                     );
275
276             free(method);
277         }
278     }
279
280     return 0;
281 }
282
283 static char* getSourceDefinition( osrfHash* class ) {
284
285         char* tabledef = osrfHashGet(class, "tablename");
286
287         if (tabledef) {
288                 tabledef = strdup(tabledef);
289         } else {
290                 tabledef = osrfHashGet(class, "source_definition");
291                 if( tabledef ) {
292                         growing_buffer* tablebuf = buffer_init(128);
293                         buffer_fadd( tablebuf, "(%s)", tabledef );
294                         tabledef = buffer_release(tablebuf);
295                 } else {
296                         const char* classname = osrfHashGet( class, "classname" );
297                         if( !classname )
298                                 classname = "???";
299                         osrfLogError(
300                                 OSRF_LOG_MARK,
301                                 "%s ERROR No tablename or source_definition for class \"%s\"",
302                                 MODULENAME,
303                                 classname
304                         );
305                 }
306         }
307
308         return tabledef;
309 }
310
311 /**
312  * Connects to the database 
313  */
314 int osrfAppChildInit() {
315
316     osrfLogDebug(OSRF_LOG_MARK, "Attempting to initialize libdbi...");
317     dbi_initialize(NULL);
318     osrfLogDebug(OSRF_LOG_MARK, "... libdbi initialized.");
319
320     char* driver        = osrf_settings_host_value("/apps/%s/app_settings/driver", MODULENAME);
321     char* user  = osrf_settings_host_value("/apps/%s/app_settings/database/user", MODULENAME);
322     char* host  = osrf_settings_host_value("/apps/%s/app_settings/database/host", MODULENAME);
323     char* port  = osrf_settings_host_value("/apps/%s/app_settings/database/port", MODULENAME);
324     char* db    = osrf_settings_host_value("/apps/%s/app_settings/database/db", MODULENAME);
325     char* pw    = osrf_settings_host_value("/apps/%s/app_settings/database/pw", MODULENAME);
326     char* md    = osrf_settings_host_value("/apps/%s/app_settings/max_query_recursion", MODULENAME);
327
328     osrfLogDebug(OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver);
329     writehandle = dbi_conn_new(driver);
330
331     if(!writehandle) {
332         osrfLogError(OSRF_LOG_MARK, "Error loading database driver [%s]", driver);
333         return -1;
334     }
335     osrfLogDebug(OSRF_LOG_MARK, "Database driver [%s] seems OK", driver);
336
337     osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database.  host=%s, "
338             "port=%s, user=%s, pw=%s, db=%s", MODULENAME, host, port, user, pw, db );
339
340     if(host) dbi_conn_set_option(writehandle, "host", host );
341     if(port) dbi_conn_set_option_numeric( writehandle, "port", atoi(port) );
342     if(user) dbi_conn_set_option(writehandle, "username", user);
343     if(pw) dbi_conn_set_option(writehandle, "password", pw );
344     if(db) dbi_conn_set_option(writehandle, "dbname", db );
345
346     if(md) max_flesh_depth = atoi(md);
347     if(max_flesh_depth < 0) max_flesh_depth = 1;
348     if(max_flesh_depth > 1000) max_flesh_depth = 1000;
349
350     free(user);
351     free(host);
352     free(port);
353     free(db);
354     free(pw);
355
356     const char* err;
357     if (dbi_conn_connect(writehandle) < 0) {
358         sleep(1);
359         if (dbi_conn_connect(writehandle) < 0) {
360             dbi_conn_error(writehandle, &err);
361             osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", err);
362             return -1;
363         }
364     }
365
366     osrfLogInfo(OSRF_LOG_MARK, "%s successfully connected to the database", MODULENAME);
367
368     unsigned short type;
369     int i = 0; 
370     char* classname;
371     osrfStringArray* classes = osrfHashKeys( oilsIDL() );
372
373     while ( (classname = osrfStringArrayGetString(classes, i++)) ) {
374         osrfHash* class = osrfHashGet( oilsIDL(), classname );
375         osrfHash* fields = osrfHashGet( class, "fields" );
376
377                 if( str_is_true( osrfHashGet(class, "virtual") ) ) {
378                         osrfLogDebug(OSRF_LOG_MARK, "Class %s is virtual, skipping", classname );
379                         continue;
380                 }
381
382         char* tabledef = getSourceDefinition(class);
383                 if( !tabledef )
384                         tabledef = strdup( "(null)" );
385
386         growing_buffer* sql_buf = buffer_init(32);
387         buffer_fadd( sql_buf, "SELECT * FROM %s AS x WHERE 1=0;", tabledef );
388
389         free(tabledef);
390
391         char* sql = buffer_release(sql_buf);
392         osrfLogDebug(OSRF_LOG_MARK, "%s Investigatory SQL = %s", MODULENAME, sql);
393
394         dbi_result result = dbi_conn_query(writehandle, sql);
395         free(sql);
396
397         if (result) {
398
399             int columnIndex = 1;
400             const char* columnName;
401             osrfHash* _f;
402             while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
403
404                 osrfLogInternal(OSRF_LOG_MARK, "Looking for column named [%s]...", (char*)columnName);
405
406                 /* fetch the fieldmapper index */
407                 if( (_f = osrfHashGet(fields, (char*)columnName)) ) {
408
409                     osrfLogDebug(OSRF_LOG_MARK, "Found [%s] in IDL hash...", (char*)columnName);
410
411                     /* determine the field type and storage attributes */
412                     type = dbi_result_get_field_type(result, columnName);
413
414                     switch( type ) {
415
416                                                 case DBI_TYPE_INTEGER : {
417
418                                                         if ( !osrfHashGet(_f, "primitive") )
419                                                                 osrfHashSet(_f,"number", "primitive");
420
421                                                         int attr = dbi_result_get_field_attribs(result, columnName);
422                                                         if( attr & DBI_INTEGER_SIZE8 ) 
423                                                                 osrfHashSet(_f,"INT8", "datatype");
424                                                         else 
425                                                                 osrfHashSet(_f,"INT", "datatype");
426                                                         break;
427                                                 }
428                         case DBI_TYPE_DECIMAL :
429                             if ( !osrfHashGet(_f, "primitive") )
430                                 osrfHashSet(_f,"number", "primitive");
431
432                             osrfHashSet(_f,"NUMERIC", "datatype");
433                             break;
434
435                         case DBI_TYPE_STRING :
436                             if ( !osrfHashGet(_f, "primitive") )
437                                 osrfHashSet(_f,"string", "primitive");
438                             osrfHashSet(_f,"TEXT", "datatype");
439                             break;
440
441                         case DBI_TYPE_DATETIME :
442                             if ( !osrfHashGet(_f, "primitive") )
443                                 osrfHashSet(_f,"string", "primitive");
444
445                             osrfHashSet(_f,"TIMESTAMP", "datatype");
446                             break;
447
448                         case DBI_TYPE_BINARY :
449                             if ( !osrfHashGet(_f, "primitive") )
450                                 osrfHashSet(_f,"string", "primitive");
451
452                             osrfHashSet(_f,"BYTEA", "datatype");
453                     }
454
455                     osrfLogDebug(
456                             OSRF_LOG_MARK,
457                             "Setting [%s] to primitive [%s] and datatype [%s]...",
458                             (char*)columnName,
459                             osrfHashGet(_f, "primitive"),
460                             osrfHashGet(_f, "datatype")
461                             );
462                 }
463             }
464             dbi_result_free(result);
465         } else {
466             osrfLogDebug(OSRF_LOG_MARK, "No data found for class [%s]...", (char*)classname);
467         }
468     }
469
470     osrfStringArrayFree(classes);
471
472         child_initialized = 1;
473     return 0;
474 }
475
476 /*
477   This function is a sleazy hack intended *only* for testing and
478   debugging.  Any real server process should initialize the 
479   database connection by calling osrfAppChildInit().
480 */
481 void set_cstore_dbi_conn( dbi_conn conn ) {
482         dbhandle = writehandle = conn;
483 }
484
485 void userDataFree( void* blob ) {
486     osrfHashFree( (osrfHash*)blob );
487     return;
488 }
489
490 static void sessionDataFree( char* key, void* item ) {
491     if (!(strcmp(key,"xact_id"))) {
492         if (writehandle)
493             dbi_conn_query(writehandle, "ROLLBACK;");
494         free(item);
495     }
496
497     return;
498 }
499
500 int beginTransaction ( osrfMethodContext* ctx ) {
501         if(osrfMethodVerifyContext( ctx )) {
502                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
503                 return -1;
504         }
505
506 #ifdef PCRUD
507     jsonObject* user = verifyUserPCRUD( ctx );
508     if (!user) return -1;
509     jsonObjectFree(user);
510 #endif
511
512     dbi_result result = dbi_conn_query(writehandle, "START TRANSACTION;");
513     if (!result) {
514         osrfLogError(OSRF_LOG_MARK, "%s: Error starting transaction", MODULENAME );
515         osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error starting transaction" );
516         return -1;
517     } else {
518         jsonObject* ret = jsonNewObject(ctx->session->session_id);
519         osrfAppRespondComplete( ctx, ret );
520         jsonObjectFree(ret);
521
522         if (!ctx->session->userData) {
523             ctx->session->userData = osrfNewHash();
524             osrfHashSetCallback((osrfHash*)ctx->session->userData, &sessionDataFree);
525         }
526
527         osrfHashSet( (osrfHash*)ctx->session->userData, strdup( ctx->session->session_id ), "xact_id" );
528         ctx->session->userDataFree = &userDataFree;
529
530     }
531     return 0;
532 }
533
534 int setSavepoint ( osrfMethodContext* ctx ) {
535         if(osrfMethodVerifyContext( ctx )) {
536                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
537                 return -1;
538         }
539
540     int spNamePos = 0;
541 #ifdef PCRUD
542     spNamePos = 1;
543     jsonObject* user = verifyUserPCRUD( ctx );
544     if (!user) return -1;
545     jsonObjectFree(user);
546 #endif
547
548     if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
549         osrfAppSessionStatus(
550                 ctx->session,
551                 OSRF_STATUS_INTERNALSERVERERROR,
552                 "osrfMethodException",
553                 ctx->request,
554                 "No active transaction -- required for savepoints"
555                 );
556         return -1;
557     }
558
559         const char* spName = jsonObjectGetString(jsonObjectGetIndex(ctx->params, spNamePos));
560
561         dbi_result result = dbi_conn_queryf(writehandle, "SAVEPOINT \"%s\";", spName);
562         if (!result) {
563                 osrfLogError(
564                         OSRF_LOG_MARK,
565                         "%s: Error creating savepoint %s in transaction %s",
566                         MODULENAME,
567                         spName,
568                         osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
569                 );
570                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, 
571                                 "osrfMethodException", ctx->request, "Error creating savepoint" );
572                 return -1;
573         } else {
574                 jsonObject* ret = jsonNewObject(spName);
575                 osrfAppRespondComplete( ctx, ret );
576                 jsonObjectFree(ret);
577         }
578         return 0;
579 }
580
581 int releaseSavepoint ( osrfMethodContext* ctx ) {
582         if(osrfMethodVerifyContext( ctx )) {
583                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
584                 return -1;
585         }
586
587         int spNamePos = 0;
588 #ifdef PCRUD
589     spNamePos = 1;
590     jsonObject* user = verifyUserPCRUD( ctx );
591     if (!user) return -1;
592     jsonObjectFree(user);
593 #endif
594
595     if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
596         osrfAppSessionStatus(
597                 ctx->session,
598                 OSRF_STATUS_INTERNALSERVERERROR,
599                 "osrfMethodException",
600                 ctx->request,
601                 "No active transaction -- required for savepoints"
602                 );
603         return -1;
604     }
605
606         const char* spName = jsonObjectGetString( jsonObjectGetIndex(ctx->params, spNamePos) );
607
608     dbi_result result = dbi_conn_queryf(writehandle, "RELEASE SAVEPOINT \"%s\";", spName);
609     if (!result) {
610         osrfLogError(
611                 OSRF_LOG_MARK,
612                 "%s: Error releasing savepoint %s in transaction %s",
613                 MODULENAME,
614                 spName,
615                 osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
616                 );
617                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR,
618                                 "osrfMethodException", ctx->request, "Error releasing savepoint" );
619         return -1;
620     } else {
621         jsonObject* ret = jsonNewObject(spName);
622         osrfAppRespondComplete( ctx, ret );
623         jsonObjectFree(ret);
624     }
625     return 0;
626 }
627
628 int rollbackSavepoint ( osrfMethodContext* ctx ) {
629         if(osrfMethodVerifyContext( ctx )) {
630                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
631                 return -1;
632         }
633
634         int spNamePos = 0;
635 #ifdef PCRUD
636     spNamePos = 1;
637     jsonObject* user = verifyUserPCRUD( ctx );
638     if (!user) return -1;
639     jsonObjectFree(user);
640 #endif
641
642     if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
643         osrfAppSessionStatus(
644                 ctx->session,
645                 OSRF_STATUS_INTERNALSERVERERROR,
646                 "osrfMethodException",
647                 ctx->request,
648                 "No active transaction -- required for savepoints"
649                 );
650         return -1;
651     }
652
653         const char* spName = jsonObjectGetString( jsonObjectGetIndex(ctx->params, spNamePos) );
654
655     dbi_result result = dbi_conn_queryf(writehandle, "ROLLBACK TO SAVEPOINT \"%s\";", spName);
656     if (!result) {
657         osrfLogError(
658                 OSRF_LOG_MARK,
659                 "%s: Error rolling back savepoint %s in transaction %s",
660                 MODULENAME,
661                 spName,
662                 osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
663                 );
664                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, 
665                                 "osrfMethodException", ctx->request, "Error rolling back savepoint" );
666         return -1;
667     } else {
668         jsonObject* ret = jsonNewObject(spName);
669         osrfAppRespondComplete( ctx, ret );
670         jsonObjectFree(ret);
671     }
672     return 0;
673 }
674
675 int commitTransaction ( osrfMethodContext* ctx ) {
676         if(osrfMethodVerifyContext( ctx )) {
677                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
678                 return -1;
679         }
680
681 #ifdef PCRUD
682     jsonObject* user = verifyUserPCRUD( ctx );
683     if (!user) return -1;
684     jsonObjectFree(user);
685 #endif
686
687     if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
688         osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "No active transaction to commit" );
689         return -1;
690     }
691
692     dbi_result result = dbi_conn_query(writehandle, "COMMIT;");
693     if (!result) {
694         osrfLogError(OSRF_LOG_MARK, "%s: Error committing transaction", MODULENAME );
695         osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error committing transaction" );
696         return -1;
697     } else {
698         osrfHashRemove(ctx->session->userData, "xact_id");
699         jsonObject* ret = jsonNewObject(ctx->session->session_id);
700         osrfAppRespondComplete( ctx, ret );
701         jsonObjectFree(ret);
702     }
703     return 0;
704 }
705
706 int rollbackTransaction ( osrfMethodContext* ctx ) {
707         if(osrfMethodVerifyContext( ctx )) {
708                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
709                 return -1;
710         }
711
712 #ifdef PCRUD
713     jsonObject* user = verifyUserPCRUD( ctx );
714     if (!user) return -1;
715     jsonObjectFree(user);
716 #endif
717
718     if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
719         osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "No active transaction to roll back" );
720         return -1;
721     }
722
723     dbi_result result = dbi_conn_query(writehandle, "ROLLBACK;");
724     if (!result) {
725         osrfLogError(OSRF_LOG_MARK, "%s: Error rolling back transaction", MODULENAME );
726         osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error rolling back transaction" );
727         return -1;
728     } else {
729         osrfHashRemove(ctx->session->userData, "xact_id");
730         jsonObject* ret = jsonNewObject(ctx->session->session_id);
731         osrfAppRespondComplete( ctx, ret );
732         jsonObjectFree(ret);
733     }
734     return 0;
735 }
736
737 int dispatchCRUDMethod ( osrfMethodContext* ctx ) {
738         if(osrfMethodVerifyContext( ctx )) {
739                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
740                 return -1;
741         }
742
743         osrfHash* meta = (osrfHash*) ctx->method->userData;
744     osrfHash* class_obj = osrfHashGet( meta, "class" );
745
746     int err = 0;
747
748     const char* methodtype = osrfHashGet(meta, "methodtype");
749     jsonObject * obj = NULL;
750
751     if (!strcmp(methodtype, "create")) {
752         obj = doCreate(ctx, &err);
753         osrfAppRespondComplete( ctx, obj );
754     }
755     else if (!strcmp(methodtype, "retrieve")) {
756         obj = doRetrieve(ctx, &err);
757         osrfAppRespondComplete( ctx, obj );
758     }
759     else if (!strcmp(methodtype, "update")) {
760         obj = doUpdate(ctx, &err);
761         osrfAppRespondComplete( ctx, obj );
762     }
763     else if (!strcmp(methodtype, "delete")) {
764         obj = doDelete(ctx, &err);
765         osrfAppRespondComplete( ctx, obj );
766     }
767     else if (!strcmp(methodtype, "search")) {
768
769         jsonObject* _p = jsonObjectClone( ctx->params );
770 #ifdef PCRUD
771         jsonObjectFree(_p);
772         _p = jsonParseString("[]");
773         jsonObjectPush(_p, jsonObjectClone(jsonObjectGetIndex(ctx->params, 1)));
774         jsonObjectPush(_p, jsonObjectClone(jsonObjectGetIndex(ctx->params, 2)));
775 #endif
776
777         obj = doFieldmapperSearch(ctx, class_obj, _p, &err);
778
779         jsonObjectFree(_p);
780         if(err) return err;
781
782         jsonObject* cur;
783         jsonIterator* itr = jsonNewIterator( obj );
784         while ((cur = jsonIteratorNext( itr ))) {
785 #ifdef PCRUD
786             if(!verifyObjectPCRUD(ctx, cur)) continue;
787 #endif
788             osrfAppRespond( ctx, cur );
789         }
790         jsonIteratorFree(itr);
791         osrfAppRespondComplete( ctx, NULL );
792
793     } else if (!strcmp(methodtype, "id_list")) {
794
795         jsonObject* _p = jsonObjectClone( ctx->params );
796 #ifdef PCRUD
797         jsonObjectFree(_p);
798         _p = jsonParseString("[]");
799         jsonObjectPush(_p, jsonObjectClone(jsonObjectGetIndex(ctx->params, 1)));
800         jsonObjectPush(_p, jsonObjectClone(jsonObjectGetIndex(ctx->params, 2)));
801 #endif
802
803         if (jsonObjectGetIndex( _p, 1 )) {
804             jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "select" );
805             jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "no_i18n" );
806             jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "flesh" );
807             jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "flesh_columns" );
808         } else {
809             jsonObjectSetIndex( _p, 1, jsonNewObjectType(JSON_HASH) );
810         }
811
812                 jsonObjectSetKey( jsonObjectGetIndex( _p, 1 ), "no_i18n", jsonNewBoolObject( 1 ) );
813
814         jsonObjectSetKey(
815             jsonObjectGetIndex( _p, 1 ),
816             "select",
817             jsonParseStringFmt(
818                 "{ \"%s\":[\"%s\"] }",
819                 osrfHashGet( class_obj, "classname" ),
820                 osrfHashGet( class_obj, "primarykey" )
821             )
822         );
823
824         obj = doFieldmapperSearch(ctx, class_obj, _p, &err);
825
826         jsonObjectFree(_p);
827         if(err) return err;
828
829         jsonObject* cur;
830         jsonIterator* itr = jsonNewIterator( obj );
831         while ((cur = jsonIteratorNext( itr ))) {
832 #ifdef PCRUD
833             if(!verifyObjectPCRUD(ctx, cur)) continue;
834 #endif
835             osrfAppRespond(
836                     ctx,
837                     oilsFMGetObject( cur, osrfHashGet( class_obj, "primarykey" ) )
838                     );
839         }
840         jsonIteratorFree(itr);
841         osrfAppRespondComplete( ctx, NULL );
842
843     } else {
844         osrfAppRespondComplete( ctx, obj );
845     }
846
847     jsonObjectFree(obj);
848
849     return err;
850 }
851
852 static int verifyObjectClass ( osrfMethodContext* ctx, const jsonObject* param ) {
853
854     int ret = 1;
855     osrfHash* meta = (osrfHash*) ctx->method->userData;
856     osrfHash* class = osrfHashGet( meta, "class" );
857
858     if (!param->classname || (strcmp( osrfHashGet(class, "classname"), param->classname ))) {
859
860         growing_buffer* msg = buffer_init(128);
861         buffer_fadd(
862                 msg,
863                 "%s: %s method for type %s was passed a %s",
864                 MODULENAME,
865                 osrfHashGet(meta, "methodtype"),
866                 osrfHashGet(class, "classname"),
867                 param->classname
868                 );
869
870         char* m = buffer_release(msg);
871         osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, m );
872
873         free(m);
874
875         return 0;
876     }
877
878 #ifdef PCRUD
879     ret = verifyObjectPCRUD( ctx, param );
880 #endif
881
882     return ret;
883 }
884
885 #ifdef PCRUD
886
887 static jsonObject* verifyUserPCRUD( osrfMethodContext* ctx ) {
888         const char* auth = jsonObjectGetString( jsonObjectGetIndex( ctx->params, 0 ) );
889     jsonObject* auth_object = jsonNewObject(auth);
890     jsonObject* user = oilsUtilsQuickReq("open-ils.auth","open-ils.auth.session.retrieve", auth_object);
891     jsonObjectFree(auth_object);
892
893     if (!user->classname || strcmp(user->classname, "au")) {
894
895         growing_buffer* msg = buffer_init(128);
896         buffer_fadd(
897             msg,
898             "%s: permacrud received a bad auth token: %s",
899             MODULENAME,
900             auth
901         );
902
903         char* m = buffer_release(msg);
904         osrfAppSessionStatus( ctx->session, OSRF_STATUS_UNAUTHORIZED, "osrfMethodException", ctx->request, m );
905
906         free(m);
907         jsonObjectFree(user);
908         user = jsonNULL;
909     }
910
911     return user;
912
913 }
914
915 static int verifyObjectPCRUD (  osrfMethodContext* ctx, const jsonObject* obj ) {
916
917     dbhandle = writehandle;
918
919     osrfHash* meta = (osrfHash*) ctx->method->userData;
920     osrfHash* class = osrfHashGet( meta, "class" );
921     char* method_type = strdup( osrfHashGet(meta, "methodtype") );
922     int fetch = 0;
923
924     if ( ( *method_type == 's' || *method_type == 'i' ) ) {
925         free(method_type);
926         method_type = strdup("retrieve"); // search and id_list are equivelant to retrieve for this
927     } else if ( *method_type == 'u' || *method_type == 'd' ) {
928         fetch = 1; // MUST go to the db for the object for update and delete
929     }
930
931     osrfHash* pcrud = osrfHashGet( osrfHashGet(class, "permacrud"), method_type );
932     free(method_type);
933
934     if (!pcrud) {
935         // No permacrud for this method type on this class
936
937         growing_buffer* msg = buffer_init(128);
938         buffer_fadd(
939             msg,
940             "%s: %s on class %s has no permacrud IDL entry",
941             MODULENAME,
942             osrfHashGet(meta, "methodtype"),
943             osrfHashGet(class, "classname")
944         );
945
946         char* m = buffer_release(msg);
947         osrfAppSessionStatus( ctx->session, OSRF_STATUS_FORBIDDEN, "osrfMethodException", ctx->request, m );
948
949         free(m);
950
951         return 0;
952     }
953
954     jsonObject* user = verifyUserPCRUD( ctx );
955     if (!user) return 0;
956
957     int userid = atoi( oilsFMGetString( user, "id" ) );
958     jsonObjectFree(user);
959
960     osrfStringArray* permission = osrfHashGet(pcrud, "permission");
961     osrfStringArray* local_context = osrfHashGet(pcrud, "local_context");
962     osrfHash* foreign_context = osrfHashGet(pcrud, "foreign_context");
963
964     osrfStringArray* context_org_array = osrfNewStringArray(1);
965
966     int err = 0;
967     char* pkey_value = NULL;
968         if ( str_is_true( osrfHashGet(pcrud, "global_required") ) ) {
969             osrfLogDebug( OSRF_LOG_MARK, "global-level permissions required, fetching top of the org tree" );
970
971         // check for perm at top of org tree
972         jsonObject* _tmp_params = jsonParseString("[{\"parent_ou\":null}]");
973                 jsonObject* _list = doFieldmapperSearch(ctx, osrfHashGet( oilsIDL(), "aou" ), _tmp_params, &err);
974
975         jsonObject* _tree_top = jsonObjectGetIndex(_list, 0);
976
977         if (!_tree_top) {
978             jsonObjectFree(_tmp_params);
979             jsonObjectFree(_list);
980     
981             growing_buffer* msg = buffer_init(128);
982                         OSRF_BUFFER_ADD( msg, MODULENAME );
983                         OSRF_BUFFER_ADD( msg,
984                                 ": Internal error, could not find the top of the org tree (parent_ou = NULL)" );
985     
986             char* m = buffer_release(msg);
987             osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, m );
988             free(m);
989
990             return 0;
991         }
992
993         osrfStringArrayAdd( context_org_array, oilsFMGetString( _tree_top, "id" ) );
994             osrfLogDebug( OSRF_LOG_MARK, "top of the org tree is %s", osrfStringArrayGetString(context_org_array, 0) );
995
996         jsonObjectFree(_tmp_params);
997         jsonObjectFree(_list);
998
999     } else {
1000             osrfLogDebug( OSRF_LOG_MARK, "global-level permissions not required, fetching context org ids" );
1001             char* pkey = osrfHashGet(class, "primarykey");
1002         jsonObject *param = NULL;
1003
1004         if (obj->classname) {
1005             pkey_value = oilsFMGetString( obj, pkey );
1006             if (!fetch) param = jsonObjectClone(obj);
1007                 osrfLogDebug( OSRF_LOG_MARK, "Object supplied, using primary key value of %s", pkey_value );
1008         } else {
1009             pkey_value = jsonObjectToSimpleString( obj );
1010             fetch = 1;
1011                 osrfLogDebug( OSRF_LOG_MARK, "Object not supplied, using primary key value of %s and retrieving from the database", pkey_value );
1012         }
1013
1014         if (fetch) {
1015             jsonObject* _tmp_params = jsonParseStringFmt("[{\"%s\":\"%s\"}]", pkey, pkey_value);
1016                 jsonObject* _list = doFieldmapperSearch(
1017                 ctx,
1018                 class,
1019                 _tmp_params,
1020                 &err
1021             );
1022     
1023             param = jsonObjectClone(jsonObjectGetIndex(_list, 0));
1024     
1025             jsonObjectFree(_tmp_params);
1026             jsonObjectFree(_list);
1027         }
1028
1029         if (!param) {
1030             osrfLogDebug( OSRF_LOG_MARK, "Object not found in the database with primary key %s of %s", pkey, pkey_value );
1031
1032             growing_buffer* msg = buffer_init(128);
1033             buffer_fadd(
1034                 msg,
1035                 "%s: no object found with primary key %s of %s",
1036                 MODULENAME,
1037                 pkey,
1038                 pkey_value
1039             );
1040         
1041             char* m = buffer_release(msg);
1042             osrfAppSessionStatus(
1043                 ctx->session,
1044                 OSRF_STATUS_INTERNALSERVERERROR,
1045                 "osrfMethodException",
1046                 ctx->request,
1047                 m
1048             );
1049         
1050             free(m);
1051             if (pkey_value) free(pkey_value);
1052
1053             return 0;
1054         }
1055
1056         if (local_context->size > 0) {
1057                 osrfLogDebug( OSRF_LOG_MARK, "%d class-local context field(s) specified", local_context->size);
1058             int i = 0;
1059             char* lcontext = NULL;
1060             while ( (lcontext = osrfStringArrayGetString(local_context, i++)) ) {
1061                 osrfStringArrayAdd( context_org_array, oilsFMGetString( param, lcontext ) );
1062                     osrfLogDebug(
1063                     OSRF_LOG_MARK,
1064                     "adding class-local field %s (value: %s) to the context org list",
1065                     lcontext,
1066                     osrfStringArrayGetString(context_org_array, context_org_array->size - 1)
1067                 );
1068             }
1069         }
1070
1071         osrfStringArray* class_list;
1072
1073         if (foreign_context) {
1074             class_list = osrfHashKeys( foreign_context );
1075                 osrfLogDebug( OSRF_LOG_MARK, "%d foreign context classes(s) specified", class_list->size);
1076
1077             if (class_list->size > 0) {
1078     
1079                 int i = 0;
1080                 char* class_name = NULL;
1081                 while ( (class_name = osrfStringArrayGetString(class_list, i++)) ) {
1082                     osrfHash* fcontext = osrfHashGet(foreign_context, class_name);
1083
1084                         osrfLogDebug(
1085                         OSRF_LOG_MARK,
1086                         "%d foreign context fields(s) specified for class %s",
1087                         ((osrfStringArray*)osrfHashGet(fcontext,"context"))->size,
1088                         class_name
1089                     );
1090     
1091                     char* foreign_pkey = osrfHashGet(fcontext, "field");
1092                     char* foreign_pkey_value = oilsFMGetString(param, osrfHashGet(fcontext, "fkey"));
1093
1094                     jsonObject* _tmp_params = jsonParseStringFmt(
1095                         "[{\"%s\":\"%s\"}]",
1096                         foreign_pkey,
1097                         foreign_pkey_value
1098                     );
1099     
1100                         jsonObject* _list = doFieldmapperSearch(
1101                         ctx,
1102                         osrfHashGet( oilsIDL(), class_name ),
1103                         _tmp_params,
1104                         &err
1105                     );
1106
1107                     jsonObject* _fparam = jsonObjectClone(jsonObjectGetIndex(_list, 0));
1108                     jsonObjectFree(_tmp_params);
1109                     jsonObjectFree(_list);
1110  
1111                     osrfStringArray* jump_list = osrfHashGet(fcontext, "jump");
1112
1113                     if (_fparam && jump_list) {
1114                         char* flink = NULL;
1115                         int k = 0;
1116                         while ( (flink = osrfStringArrayGetString(jump_list, k++)) && _fparam ) {
1117                             free(foreign_pkey_value);
1118
1119                             osrfHash* foreign_link_hash = oilsIDLFindPath( "/%s/links/%s", _fparam->classname, flink );
1120
1121                             foreign_pkey_value = oilsFMGetString(_fparam, flink);
1122                             foreign_pkey = osrfHashGet( foreign_link_hash, "key" );
1123
1124                             _tmp_params = jsonParseStringFmt(
1125                                 "[{\"%s\":\"%s\"}]",
1126                                 foreign_pkey,
1127                                 foreign_pkey_value
1128                             );
1129
1130                                 _list = doFieldmapperSearch(
1131                                 ctx,
1132                                 osrfHashGet( oilsIDL(), osrfHashGet( foreign_link_hash, "class" ) ),
1133                                 _tmp_params,
1134                                 &err
1135                             );
1136
1137                             _fparam = jsonObjectClone(jsonObjectGetIndex(_list, 0));
1138                             jsonObjectFree(_tmp_params);
1139                             jsonObjectFree(_list);
1140                         }
1141                     }
1142
1143            
1144                     if (!_fparam) {
1145
1146                         growing_buffer* msg = buffer_init(128);
1147                         buffer_fadd(
1148                             msg,
1149                             "%s: no object found with primary key %s of %s",
1150                             MODULENAME,
1151                             foreign_pkey,
1152                             foreign_pkey_value
1153                         );
1154                 
1155                         char* m = buffer_release(msg);
1156                         osrfAppSessionStatus(
1157                             ctx->session,
1158                             OSRF_STATUS_INTERNALSERVERERROR,
1159                             "osrfMethodException",
1160                             ctx->request,
1161                             m
1162                         );
1163
1164                         free(m);
1165                         osrfStringArrayFree(class_list);
1166                         free(foreign_pkey_value);
1167                         jsonObjectFree(param);
1168
1169                         return 0;
1170                     }
1171         
1172                     free(foreign_pkey_value);
1173     
1174                     int j = 0;
1175                     char* foreign_field = NULL;
1176                     while ( (foreign_field = osrfStringArrayGetString(osrfHashGet(fcontext,"context"), j++)) ) {
1177                         osrfStringArrayAdd( context_org_array, oilsFMGetString( _fparam, foreign_field ) );
1178                             osrfLogDebug(
1179                             OSRF_LOG_MARK,
1180                             "adding foreign class %s field %s (value: %s) to the context org list",
1181                             class_name,
1182                             foreign_field,
1183                             osrfStringArrayGetString(context_org_array, context_org_array->size - 1)
1184                         );
1185                     }
1186
1187                     jsonObjectFree(_fparam);
1188                 }
1189     
1190                 osrfStringArrayFree(class_list);
1191             }
1192         }
1193
1194         jsonObjectFree(param);
1195     }
1196
1197     char* context_org = NULL;
1198     char* perm = NULL;
1199     int OK = 0;
1200
1201     if (permission->size == 0) {
1202             osrfLogDebug( OSRF_LOG_MARK, "No permission specified for this action, passing through" );
1203         OK = 1;
1204     }
1205     
1206     int i = 0;
1207     while ( (perm = osrfStringArrayGetString(permission, i++)) ) {
1208         int j = 0;
1209         while ( (context_org = osrfStringArrayGetString(context_org_array, j++)) ) {
1210             dbi_result result;
1211
1212             if (pkey_value) {
1213                     osrfLogDebug(
1214                     OSRF_LOG_MARK,
1215                     "Checking object permission [%s] for user %d on object %s (class %s) at org %d",
1216                     perm,
1217                     userid,
1218                     pkey_value,
1219                     osrfHashGet(class, "classname"),
1220                     atoi(context_org)
1221                 );
1222
1223                 result = dbi_conn_queryf(
1224                     writehandle,
1225                     "SELECT permission.usr_has_object_perm(%d, '%s', '%s', '%s', %d) AS has_perm;",
1226                     userid,
1227                     perm,
1228                     osrfHashGet(class, "classname"),
1229                     pkey_value,
1230                     atoi(context_org)
1231                 );
1232
1233                 if (result) {
1234                     osrfLogDebug(
1235                         OSRF_LOG_MARK,
1236                         "Recieved a result for object permission [%s] for user %d on object %s (class %s) at org %d",
1237                         perm,
1238                         userid,
1239                         pkey_value,
1240                         osrfHashGet(class, "classname"),
1241                         atoi(context_org)
1242                     );
1243
1244                     if (dbi_result_first_row(result)) {
1245                         jsonObject* return_val = oilsMakeJSONFromResult( result );
1246                                                 const char* has_perm = jsonObjectGetString( jsonObjectGetKeyConst(return_val, "has_perm") );
1247
1248                             osrfLogDebug(
1249                             OSRF_LOG_MARK,
1250                             "Status of object permission [%s] for user %d on object %s (class %s) at org %d is %s",
1251                             perm,
1252                             userid,
1253                             pkey_value,
1254                             osrfHashGet(class, "classname"),
1255                             atoi(context_org),
1256                             has_perm
1257                         );
1258
1259                         if ( *has_perm == 't' ) OK = 1;
1260                         jsonObjectFree(return_val);
1261                     }
1262
1263                     dbi_result_free(result); 
1264                     if (OK) break;
1265                 }
1266             }
1267
1268                 osrfLogDebug( OSRF_LOG_MARK, "Checking non-object permission [%s] for user %d at org %d", perm, userid, atoi(context_org) );
1269             result = dbi_conn_queryf(
1270                 writehandle,
1271                 "SELECT permission.usr_has_perm(%d, '%s', %d) AS has_perm;",
1272                 userid,
1273                 perm,
1274                 atoi(context_org)
1275             );
1276
1277                         if (result) {
1278                                 osrfLogDebug( OSRF_LOG_MARK, "Received a result for permission [%s] for user %d at org %d",
1279                                                 perm, userid, atoi(context_org) );
1280                                 if ( dbi_result_first_row(result) ) {
1281                                         jsonObject* return_val = oilsMakeJSONFromResult( result );
1282                                         const char* has_perm = jsonObjectGetString( jsonObjectGetKeyConst(return_val, "has_perm") );
1283                                         osrfLogDebug( OSRF_LOG_MARK, "Status of permission [%s] for user %d at org %d is [%s]",
1284                                                         perm, userid, atoi(context_org), has_perm );
1285                                         if ( *has_perm == 't' ) OK = 1;
1286                                         jsonObjectFree(return_val);
1287                                 }
1288
1289                                 dbi_result_free(result); 
1290                                 if (OK) break;
1291                         }
1292
1293         }
1294         if (OK) break;
1295     }
1296
1297     if (pkey_value) free(pkey_value);
1298     osrfStringArrayFree(context_org_array);
1299
1300     return OK;
1301 }
1302 #endif
1303
1304
1305 static jsonObject* doCreate(osrfMethodContext* ctx, int* err ) {
1306
1307         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
1308 #ifdef PCRUD
1309         jsonObject* target = jsonObjectGetIndex( ctx->params, 1 );
1310         jsonObject* options = jsonObjectGetIndex( ctx->params, 2 );
1311 #else
1312         jsonObject* target = jsonObjectGetIndex( ctx->params, 0 );
1313         jsonObject* options = jsonObjectGetIndex( ctx->params, 1 );
1314 #endif
1315
1316         if (!verifyObjectClass(ctx, target)) {
1317                 *err = -1;
1318                 return jsonNULL;
1319         }
1320
1321         osrfLogDebug( OSRF_LOG_MARK, "Object seems to be of the correct type" );
1322
1323         char* trans_id = NULL;
1324         if( ctx->session && ctx->session->userData )
1325                 trans_id = osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" );
1326
1327         if ( !trans_id ) {
1328                 osrfLogError( OSRF_LOG_MARK, "No active transaction -- required for CREATE" );
1329
1330                 osrfAppSessionStatus(
1331                         ctx->session,
1332                         OSRF_STATUS_BADREQUEST,
1333                         "osrfMethodException",
1334                         ctx->request,
1335                         "No active transaction -- required for CREATE"
1336                 );
1337                 *err = -1;
1338                 return jsonNULL;
1339         }
1340
1341         // The following test is harmless but redundant.  If a class is
1342         // readonly, we don't register a create method for it.
1343         if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
1344                 osrfAppSessionStatus(
1345                         ctx->session,
1346                         OSRF_STATUS_BADREQUEST,
1347                         "osrfMethodException",
1348                         ctx->request,
1349                         "Cannot INSERT readonly class"
1350                 );
1351                 *err = -1;
1352                 return jsonNULL;
1353         }
1354
1355         // Set the last_xact_id
1356         int index = oilsIDL_ntop( target->classname, "last_xact_id" );
1357         if (index > -1) {
1358                 osrfLogDebug(OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d", trans_id, target->classname, index);
1359                 jsonObjectSetIndex(target, index, jsonNewObject(trans_id));
1360         }       
1361
1362         osrfLogDebug( OSRF_LOG_MARK, "There is a transaction running..." );
1363
1364         dbhandle = writehandle;
1365
1366         osrfHash* fields = osrfHashGet(meta, "fields");
1367         char* pkey = osrfHashGet(meta, "primarykey");
1368         char* seq = osrfHashGet(meta, "sequence");
1369
1370         growing_buffer* table_buf = buffer_init(128);
1371         growing_buffer* col_buf = buffer_init(128);
1372         growing_buffer* val_buf = buffer_init(128);
1373
1374         OSRF_BUFFER_ADD(table_buf, "INSERT INTO ");
1375         OSRF_BUFFER_ADD(table_buf, osrfHashGet(meta, "tablename"));
1376         OSRF_BUFFER_ADD_CHAR( col_buf, '(' );
1377         buffer_add(val_buf,"VALUES (");
1378
1379
1380         int i = 0;
1381         int first = 1;
1382         char* field_name;
1383         osrfStringArray* field_list = osrfHashKeys( fields );
1384         while ( (field_name = osrfStringArrayGetString(field_list, i++)) ) {
1385
1386                 osrfHash* field = osrfHashGet( fields, field_name );
1387
1388                 if( str_is_true( osrfHashGet( field, "virtual" ) ) )
1389                         continue;
1390
1391                 const jsonObject* field_object = oilsFMGetObject( target, field_name );
1392
1393                 char* value;
1394                 if (field_object && field_object->classname) {
1395                         value = oilsFMGetString(
1396                                 field_object,
1397                                 (char*)oilsIDLFindPath("/%s/primarykey", field_object->classname)
1398                         );
1399                 } else {
1400                         value = jsonObjectToSimpleString( field_object );
1401                 }
1402
1403
1404                 if (first) {
1405                         first = 0;
1406                 } else {
1407                         OSRF_BUFFER_ADD_CHAR( col_buf, ',' );
1408                         OSRF_BUFFER_ADD_CHAR( val_buf, ',' );
1409                 }
1410
1411                 buffer_add(col_buf, field_name);
1412
1413                 if (!field_object || field_object->type == JSON_NULL) {
1414                         buffer_add( val_buf, "DEFAULT" );
1415                         
1416                 } else if ( !strcmp(get_primitive( field ), "number") ) {
1417                         const char* numtype = get_datatype( field );
1418                         if ( !strcmp( numtype, "INT8") ) {
1419                                 buffer_fadd( val_buf, "%lld", atoll(value) );
1420                                 
1421                         } else if ( !strcmp( numtype, "INT") ) {
1422                                 buffer_fadd( val_buf, "%d", atoi(value) );
1423                                 
1424                         } else if ( !strcmp( numtype, "NUMERIC") ) {
1425                                 buffer_fadd( val_buf, "%f", atof(value) );
1426                         }
1427                 } else {
1428                         if ( dbi_conn_quote_string(writehandle, &value) ) {
1429                                 OSRF_BUFFER_ADD( val_buf, value );
1430
1431                         } else {
1432                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting string [%s]", MODULENAME, value);
1433                                 osrfAppSessionStatus(
1434                                         ctx->session,
1435                                         OSRF_STATUS_INTERNALSERVERERROR,
1436                                         "osrfMethodException",
1437                                         ctx->request,
1438                                         "Error quoting string -- please see the error log for more details"
1439                                 );
1440                                 free(value);
1441                                 buffer_free(table_buf);
1442                                 buffer_free(col_buf);
1443                                 buffer_free(val_buf);
1444                                 *err = -1;
1445                                 return jsonNULL;
1446                         }
1447                 }
1448
1449                 free(value);
1450                 
1451         }
1452
1453
1454         OSRF_BUFFER_ADD_CHAR( col_buf, ')' );
1455         OSRF_BUFFER_ADD_CHAR( val_buf, ')' );
1456
1457         char* table_str = buffer_release(table_buf);
1458         char* col_str   = buffer_release(col_buf);
1459         char* val_str   = buffer_release(val_buf);
1460         growing_buffer* sql = buffer_init(128);
1461         buffer_fadd( sql, "%s %s %s;", table_str, col_str, val_str );
1462         free(table_str);
1463         free(col_str);
1464         free(val_str);
1465
1466         char* query = buffer_release(sql);
1467
1468         osrfLogDebug(OSRF_LOG_MARK, "%s: Insert SQL [%s]", MODULENAME, query);
1469
1470         
1471         dbi_result result = dbi_conn_query(writehandle, query);
1472
1473         jsonObject* obj = NULL;
1474
1475         if (!result) {
1476                 obj = jsonNewObject(NULL);
1477                 osrfLogError(
1478                         OSRF_LOG_MARK,
1479                         "%s ERROR inserting %s object using query [%s]",
1480                         MODULENAME,
1481                         osrfHashGet(meta, "fieldmapper"),
1482                         query
1483                 );
1484                 osrfAppSessionStatus(
1485                         ctx->session,
1486                         OSRF_STATUS_INTERNALSERVERERROR,
1487                         "osrfMethodException",
1488                         ctx->request,
1489                         "INSERT error -- please see the error log for more details"
1490                 );
1491                 *err = -1;
1492         } else {
1493
1494                 char* id = oilsFMGetString(target, pkey);
1495                 if (!id) {
1496                         unsigned long long new_id = dbi_conn_sequence_last(writehandle, seq);
1497                         growing_buffer* _id = buffer_init(10);
1498                         buffer_fadd(_id, "%lld", new_id);
1499                         id = buffer_release(_id);
1500                 }
1501
1502                 // Find quietness specification, if present
1503                 const char* quiet_str = NULL;
1504                 if ( options ) {
1505                         const jsonObject* quiet_obj = jsonObjectGetKeyConst( options, "quiet" );
1506                         if( quiet_obj )
1507                                 quiet_str = jsonObjectGetString( quiet_obj );
1508                 }
1509
1510                 if( str_is_true( quiet_str ) ) {  // if quietness is specified
1511                         obj = jsonNewObject(id);
1512                 }
1513                 else {
1514
1515                         jsonObject* fake_params = jsonNewObjectType(JSON_ARRAY);
1516                         jsonObjectPush(fake_params, jsonNewObjectType(JSON_HASH));
1517
1518                         jsonObjectSetKey(
1519                                 jsonObjectGetIndex(fake_params, 0),
1520                                 pkey,
1521                                 jsonNewObject(id)
1522                         );
1523
1524                         jsonObject* list = doFieldmapperSearch( ctx,meta, fake_params, err);
1525
1526                         if(*err) {
1527                                 jsonObjectFree( fake_params );
1528                                 obj = jsonNULL;
1529                         } else {
1530                                 obj = jsonObjectClone( jsonObjectGetIndex(list, 0) );
1531                         }
1532
1533                         jsonObjectFree( list );
1534                         jsonObjectFree( fake_params );
1535                 }
1536
1537                 free(id);
1538         }
1539
1540         free(query);
1541
1542         return obj;
1543
1544 }
1545
1546
1547 static jsonObject* doRetrieve(osrfMethodContext* ctx, int* err ) {
1548
1549     int id_pos = 0;
1550     int order_pos = 1;
1551
1552 #ifdef PCRUD
1553     id_pos = 1;
1554     order_pos = 2;
1555 #endif
1556
1557         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
1558
1559         const char* id = jsonObjectGetString(jsonObjectGetIndex(ctx->params, id_pos));
1560         jsonObject* order_hash = jsonObjectGetIndex(ctx->params, order_pos);
1561
1562         osrfLogDebug(
1563                 OSRF_LOG_MARK,
1564                 "%s retrieving %s object with primary key value of %s",
1565                 MODULENAME,
1566                 osrfHashGet(meta, "fieldmapper"),
1567                 id
1568         );
1569
1570         jsonObject* fake_params = jsonNewObjectType(JSON_ARRAY);
1571         jsonObjectPush(fake_params, jsonNewObjectType(JSON_HASH));
1572
1573         jsonObjectSetKey(
1574                 jsonObjectGetIndex(fake_params, 0),
1575                 osrfHashGet(meta, "primarykey"),
1576                 jsonObjectClone(jsonObjectGetIndex(ctx->params, id_pos))
1577         );
1578
1579
1580         if (order_hash) jsonObjectPush(fake_params, jsonObjectClone(order_hash) );
1581
1582         jsonObject* list = doFieldmapperSearch( ctx,meta, fake_params, err);
1583
1584         if(*err) {
1585                 jsonObjectFree( fake_params );
1586                 return jsonNULL;
1587         }
1588
1589         jsonObject* obj = jsonObjectClone( jsonObjectGetIndex(list, 0) );
1590
1591         jsonObjectFree( list );
1592         jsonObjectFree( fake_params );
1593
1594 #ifdef PCRUD
1595         if(!verifyObjectPCRUD(ctx, obj)) {
1596         jsonObjectFree(obj);
1597         *err = -1;
1598
1599         growing_buffer* msg = buffer_init(128);
1600                 OSRF_BUFFER_ADD( msg, MODULENAME );
1601                 OSRF_BUFFER_ADD( msg, ": Insufficient permissions to retrieve object" );
1602
1603         char* m = buffer_release(msg);
1604         osrfAppSessionStatus( ctx->session, OSRF_STATUS_NOTALLOWED, "osrfMethodException", ctx->request, m );
1605
1606         free(m);
1607
1608                 return jsonNULL;
1609         }
1610 #endif
1611
1612         return obj;
1613 }
1614
1615 static char* jsonNumberToDBString ( osrfHash* field, const jsonObject* value ) {
1616         growing_buffer* val_buf = buffer_init(32);
1617         const char* numtype = get_datatype( field );
1618
1619         if ( !strncmp( numtype, "INT", 3 ) ) {
1620                 if (value->type == JSON_NUMBER)
1621                         //buffer_fadd( val_buf, "%ld", (long)jsonObjectGetNumber(value) );
1622                         buffer_fadd( val_buf, jsonObjectGetString( value ) );
1623                 else {
1624                         //const char* val_str = jsonObjectGetString( value );
1625                         //buffer_fadd( val_buf, "%ld", atol(val_str) );
1626                         buffer_fadd( val_buf, jsonObjectGetString( value ) );
1627                 }
1628
1629         } else if ( !strcmp( numtype, "NUMERIC" ) ) {
1630                 if (value->type == JSON_NUMBER)
1631                         //buffer_fadd( val_buf, "%f",  jsonObjectGetNumber(value) );
1632                         buffer_fadd( val_buf, jsonObjectGetString( value ) );
1633                 else {
1634                         //const char* val_str = jsonObjectGetString( value );
1635                         //buffer_fadd( val_buf, "%f", atof(val_str) );
1636                         buffer_fadd( val_buf, jsonObjectGetString( value ) );
1637                 }
1638
1639         } else {
1640                 // Presumably this was really intended ot be a string, so quote it
1641                 char* str = jsonObjectToSimpleString( value );
1642                 if ( dbi_conn_quote_string(dbhandle, &str) ) {
1643                         OSRF_BUFFER_ADD( val_buf, str );
1644                         free(str);
1645                 } else {
1646                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, str);
1647                         free(str);
1648                         buffer_free(val_buf);
1649                         return NULL;
1650                 }
1651         }
1652
1653         return buffer_release(val_buf);
1654 }
1655
1656 static char* searchINPredicate (const char* class, osrfHash* field,
1657                 jsonObject* node, const char* op, osrfMethodContext* ctx ) {
1658         growing_buffer* sql_buf = buffer_init(32);
1659         
1660         buffer_fadd(
1661                 sql_buf,
1662                 "\"%s\".%s ",
1663                 class,
1664                 osrfHashGet(field, "name")
1665         );
1666
1667         if (!op) {
1668                 buffer_add(sql_buf, "IN (");
1669         } else if (!(strcasecmp(op,"not in"))) {
1670                 buffer_add(sql_buf, "NOT IN (");
1671         } else {
1672                 buffer_add(sql_buf, "IN (");
1673         }
1674
1675     if (node->type == JSON_HASH) {
1676         // subquery predicate
1677         char* subpred = SELECT(
1678             ctx,
1679             jsonObjectGetKey( node, "select" ),
1680             jsonObjectGetKey( node, "from" ),
1681             jsonObjectGetKey( node, "where" ),
1682             jsonObjectGetKey( node, "having" ),
1683             jsonObjectGetKey( node, "order_by" ),
1684             jsonObjectGetKey( node, "limit" ),
1685             jsonObjectGetKey( node, "offset" ),
1686             SUBSELECT
1687         );
1688
1689                 if( subpred ) {
1690                         buffer_add(sql_buf, subpred);
1691                         free(subpred);
1692                 } else {
1693                         buffer_free( sql_buf );
1694                         return NULL;
1695                 }
1696
1697     } else if (node->type == JSON_ARRAY) {
1698         // literal value list
1699         int in_item_index = 0;
1700         int in_item_first = 1;
1701         const jsonObject* in_item;
1702         while ( (in_item = jsonObjectGetIndex(node, in_item_index++)) ) {
1703
1704                         if (in_item_first)
1705                                 in_item_first = 0;
1706                         else
1707                                 buffer_add(sql_buf, ", ");
1708
1709                         // Sanity check
1710                         if ( in_item->type != JSON_STRING && in_item->type != JSON_NUMBER ) {
1711                                 osrfLogError(OSRF_LOG_MARK, "%s: Expected string or number within IN list; found %s",
1712                                                 MODULENAME, json_type( in_item->type ) );
1713                                                                         buffer_free(sql_buf);
1714                                 return NULL;
1715                         }
1716                         
1717                         // Append the literal value -- quoted if not a number
1718                         if ( JSON_NUMBER == in_item->type ) {
1719                                 char* val = jsonNumberToDBString( field, in_item );
1720                                 OSRF_BUFFER_ADD( sql_buf, val );
1721                                 free(val);
1722
1723                         } else if ( !strcmp( get_primitive( field ), "number") ) {
1724                                 char* val = jsonNumberToDBString( field, in_item );
1725                                 OSRF_BUFFER_ADD( sql_buf, val );
1726                                 free(val);
1727
1728                         } else {
1729                                 char* key_string = jsonObjectToSimpleString(in_item);
1730                                 if ( dbi_conn_quote_string(dbhandle, &key_string) ) {
1731                                         OSRF_BUFFER_ADD( sql_buf, key_string );
1732                                         free(key_string);
1733                                 } else {
1734                                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, key_string);
1735                                         free(key_string);
1736                                         buffer_free(sql_buf);
1737                                         return NULL;
1738                                 }
1739                         }
1740                 }
1741
1742                 if( in_item_first ) {
1743                         osrfLogError(OSRF_LOG_MARK, "%s: Empty IN list", MODULENAME );
1744                         buffer_free( sql_buf );
1745                         return NULL;
1746                 }
1747         } else {
1748                 osrfLogError(OSRF_LOG_MARK, "%s: Expected object or array for IN clause; found %s",
1749                         MODULENAME, json_type( node->type ) );
1750                 buffer_free(sql_buf);
1751                 return NULL;
1752         }
1753
1754         OSRF_BUFFER_ADD_CHAR( sql_buf, ')' );
1755
1756         return buffer_release(sql_buf);
1757 }
1758
1759 // Receive a JSON_ARRAY representing a function call.  The first
1760 // entry in the array is the function name.  The rest are parameters.
1761 static char* searchValueTransform( const jsonObject* array ) {
1762         
1763         if( array->size < 1 ) {
1764                 osrfLogError(OSRF_LOG_MARK, "%s: Empty array for value transform", MODULENAME);
1765                 return NULL;
1766         }
1767         
1768         // Get the function name
1769         jsonObject* func_item = jsonObjectGetIndex( array, 0 );
1770         if( func_item->type != JSON_STRING ) {
1771                 osrfLogError(OSRF_LOG_MARK, "%s: Error: expected function name, found %s",
1772                         MODULENAME, json_type( func_item->type ) );
1773                 return NULL;
1774         }
1775         
1776         growing_buffer* sql_buf = buffer_init(32);
1777
1778         OSRF_BUFFER_ADD( sql_buf, jsonObjectGetString( func_item ) );
1779         OSRF_BUFFER_ADD( sql_buf, "( " );
1780         
1781         // Get the parameters
1782         int func_item_index = 1;   // We already grabbed the zeroth entry
1783         while ( (func_item = jsonObjectGetIndex(array, func_item_index++)) ) {
1784
1785                 // Add a separator comma, if we need one
1786                 if( func_item_index > 2 )
1787                         buffer_add( sql_buf, ", " );
1788
1789                 // Add the current parameter
1790                 if (func_item->type == JSON_NULL) {
1791                         buffer_add( sql_buf, "NULL" );
1792                 } else {
1793                         char* val = jsonObjectToSimpleString(func_item);
1794                         if ( dbi_conn_quote_string(dbhandle, &val) ) {
1795                                 OSRF_BUFFER_ADD( sql_buf, val );
1796                                 free(val);
1797                         } else {
1798                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, val);
1799                                 buffer_free(sql_buf);
1800                                 free(val);
1801                                 return NULL;
1802                         }
1803                 }
1804         }
1805
1806         buffer_add( sql_buf, " )" );
1807
1808         return buffer_release(sql_buf);
1809 }
1810
1811 static char* searchFunctionPredicate (const char* class, osrfHash* field,
1812                 const jsonObject* node, const char* op) {
1813
1814         if( ! is_good_operator( op ) ) {
1815                 osrfLogError( OSRF_LOG_MARK, "%s: Invalid operator [%s]", MODULENAME, op );
1816                 return NULL;
1817         }
1818         
1819         char* val = searchValueTransform(node);
1820         if( !val )
1821                 return NULL;
1822         
1823         growing_buffer* sql_buf = buffer_init(32);
1824         buffer_fadd(
1825                 sql_buf,
1826                 "\"%s\".%s %s %s",
1827                 class,
1828                 osrfHashGet(field, "name"),
1829                 op,
1830                 val
1831         );
1832
1833         free(val);
1834
1835         return buffer_release(sql_buf);
1836 }
1837
1838 // class is a class name
1839 // field is a field definition as stored in the IDL
1840 // node comes from the method parameter, and may represent an entry in the SELECT list
1841 static char* searchFieldTransform (const char* class, osrfHash* field, const jsonObject* node) {
1842         growing_buffer* sql_buf = buffer_init(32);
1843
1844         const char* field_transform = jsonObjectGetString( jsonObjectGetKeyConst( node, "transform" ) );
1845         const char* transform_subcolumn = jsonObjectGetString( jsonObjectGetKeyConst( node, "result_field" ) );
1846
1847         if(transform_subcolumn) {
1848                 if( ! is_identifier( transform_subcolumn ) ) {
1849                         osrfLogError( OSRF_LOG_MARK, "%s: Invalid subfield name: \"%s\"\n",
1850                                         MODULENAME, transform_subcolumn );
1851                         buffer_free( sql_buf );
1852                         return NULL;
1853                 }
1854                 OSRF_BUFFER_ADD_CHAR( sql_buf, '(' );    // enclose transform in parentheses
1855         }
1856
1857         if (field_transform) {
1858                 
1859                 if( ! is_identifier( field_transform ) ) {
1860                         osrfLogError( OSRF_LOG_MARK, "%s: Expected function name, found \"%s\"\n",
1861                                         MODULENAME, field_transform );
1862                         buffer_free( sql_buf );
1863                         return NULL;
1864                 }
1865                 
1866                 buffer_fadd( sql_buf, "%s(\"%s\".%s", field_transform, class, osrfHashGet(field, "name"));
1867                 const jsonObject* array = jsonObjectGetKeyConst( node, "params" );
1868
1869                 if (array) {
1870                         if( array->type != JSON_ARRAY ) {
1871                                 osrfLogError( OSRF_LOG_MARK,
1872                                         "%s: Expected JSON_ARRAY for function params; found %s",
1873                                         MODULENAME, json_type( array->type ) );
1874                                 buffer_free( sql_buf );
1875                                 return NULL;
1876                         }
1877                         int func_item_index = 0;
1878                         jsonObject* func_item;
1879                         while ( (func_item = jsonObjectGetIndex(array, func_item_index++)) ) {
1880
1881                                 char* val = jsonObjectToSimpleString(func_item);
1882
1883                                 if ( !val ) {
1884                                         buffer_add( sql_buf, ",NULL" );
1885                                 } else if ( dbi_conn_quote_string(dbhandle, &val) ) {
1886                                         OSRF_BUFFER_ADD_CHAR( sql_buf, ',' );
1887                                         OSRF_BUFFER_ADD( sql_buf, val );
1888                                 } else {
1889                                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, val);
1890                                         free(val);
1891                                         buffer_free(sql_buf);
1892                                         return NULL;
1893                         }
1894                                 free(val);
1895                         }
1896                 }
1897
1898                 buffer_add( sql_buf, " )" );
1899
1900         } else {
1901                 buffer_fadd( sql_buf, "\"%s\".%s", class, osrfHashGet(field, "name"));
1902         }
1903
1904         if (transform_subcolumn)
1905                 buffer_fadd( sql_buf, ").\"%s\"", transform_subcolumn );
1906
1907         return buffer_release(sql_buf);
1908 }
1909
1910 static char* searchFieldTransformPredicate (const char* class, osrfHash* field,
1911                 const jsonObject* node, const char* op ) {
1912
1913         if( ! is_good_operator( op ) ) {
1914                 osrfLogError(OSRF_LOG_MARK, "%s: Error: Invalid operator %s", MODULENAME, op);
1915                 return NULL;
1916         }
1917
1918         char* field_transform = searchFieldTransform( class, field, node );
1919         if( ! field_transform )
1920                 return NULL;
1921         char* value = NULL;
1922         int extra_parens = 0;   // boolean
1923
1924         const jsonObject* value_obj = jsonObjectGetKeyConst( node, "value" );
1925         if ( ! value_obj ) {
1926                 value = searchWHERE( node, osrfHashGet( oilsIDL(), class ), AND_OP_JOIN, NULL );
1927                 if( !value ) {
1928                         osrfLogError(OSRF_LOG_MARK, "%s: Error building condition for field transform", MODULENAME);
1929                         free(field_transform);
1930                         return NULL;
1931                 }
1932                 extra_parens = 1;
1933         } else if ( value_obj->type == JSON_ARRAY ) {
1934                 value = searchValueTransform( value_obj );
1935                 if( !value ) {
1936                         osrfLogError(OSRF_LOG_MARK, "%s: Error building value transform for field transform", MODULENAME);
1937                         free( field_transform );
1938                         return NULL;
1939                 }
1940         } else if ( value_obj->type == JSON_HASH ) {
1941                 value = searchWHERE( value_obj, osrfHashGet( oilsIDL(), class ), AND_OP_JOIN, NULL );
1942                 if( !value ) {
1943                         osrfLogError(OSRF_LOG_MARK, "%s: Error building predicate for field transform", MODULENAME);
1944                         free(field_transform);
1945                         return NULL;
1946                 }
1947                 extra_parens = 1;
1948         } else if ( value_obj->type == JSON_NUMBER ) {
1949                 value = jsonNumberToDBString( field, value_obj );
1950         } else if ( value_obj->type == JSON_NULL ) {
1951                 osrfLogError(OSRF_LOG_MARK, "%s: Error building predicate for field transform: null value", MODULENAME);
1952                 free(field_transform);
1953                 return NULL;
1954         } else if ( value_obj->type == JSON_BOOL ) {
1955                 osrfLogError(OSRF_LOG_MARK, "%s: Error building predicate for field transform: boolean value", MODULENAME);
1956                 free(field_transform);
1957                 return NULL;
1958         } else {
1959                 if ( !strcmp( get_primitive( field ), "number") ) {
1960                         value = jsonNumberToDBString( field, value_obj );
1961                 } else {
1962                         value = jsonObjectToSimpleString( value_obj );
1963                         if ( !dbi_conn_quote_string(dbhandle, &value) ) {
1964                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, value);
1965                                 free(value);
1966                                 free(field_transform);
1967                                 return NULL;
1968                         }
1969                 }
1970         }
1971
1972         const char* left_parens  = "";
1973         const char* right_parens = "";
1974
1975         if( extra_parens ) {
1976                 left_parens  = "(";
1977                 right_parens = ")";
1978         }
1979
1980         growing_buffer* sql_buf = buffer_init(32);
1981
1982         buffer_fadd(
1983                 sql_buf,
1984                 "%s%s %s %s %s %s%s",
1985                 left_parens,
1986                 field_transform,
1987                 op,
1988                 left_parens,
1989                 value,
1990                 right_parens,
1991                 right_parens
1992         );
1993
1994         free(value);
1995         free(field_transform);
1996
1997         return buffer_release(sql_buf);
1998 }
1999
2000 static char* searchSimplePredicate (const char* op, const char* class,
2001                 osrfHash* field, const jsonObject* node) {
2002
2003         if( ! is_good_operator( op ) ) {
2004                 osrfLogError( OSRF_LOG_MARK, "%s: Invalid operator [%s]", MODULENAME, op );
2005                 return NULL;
2006         }
2007
2008         char* val = NULL;
2009
2010         // Get the value to which we are comparing the specified column
2011         if (node->type != JSON_NULL) {
2012                 if ( node->type == JSON_NUMBER ) {
2013                         val = jsonNumberToDBString( field, node );
2014                 } else if ( !strcmp( get_primitive( field ), "number" ) ) {
2015                         val = jsonNumberToDBString( field, node );
2016                 } else {
2017                         val = jsonObjectToSimpleString(node);
2018                 }
2019         }
2020
2021         if( val ) {
2022                 if( JSON_NUMBER != node->type && strcmp( get_primitive( field ), "number") ) {
2023                         // Value is not numeric; enclose it in quotes
2024                         if ( !dbi_conn_quote_string( dbhandle, &val ) ) {
2025                                 osrfLogError( OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, val );
2026                                 free( val );
2027                                 return NULL;
2028                         }
2029                 }
2030         } else {
2031                 // Compare to a null value
2032                 val = strdup( "NULL" );
2033                 if (strcmp( op, "=" ))
2034                         op = "IS NOT";
2035                 else
2036                         op = "IS";
2037         }
2038
2039         growing_buffer* sql_buf = buffer_init(32);
2040         buffer_fadd( sql_buf, "\"%s\".%s %s %s", class, osrfHashGet(field, "name"), op, val );
2041         char* pred = buffer_release( sql_buf );
2042
2043         free(val);
2044
2045         return pred;
2046 }
2047
2048 static char* searchBETWEENPredicate (const char* class, osrfHash* field, const jsonObject* node) {
2049
2050         const jsonObject* x_node = jsonObjectGetIndex( node, 0 );
2051         const jsonObject* y_node = jsonObjectGetIndex( node, 1 );
2052         
2053         if( NULL == y_node ) {
2054                 osrfLogError( OSRF_LOG_MARK, "%s: Not enough operands for BETWEEN operator", MODULENAME );
2055                 return NULL;
2056         }
2057         else if( NULL != jsonObjectGetIndex( node, 2 ) ) {
2058                 osrfLogError( OSRF_LOG_MARK, "%s: Too many operands for BETWEEN operator", MODULENAME );
2059                 return NULL;
2060         }
2061         
2062         char* x_string;
2063         char* y_string;
2064
2065         if ( !strcmp( get_primitive( field ), "number") ) {
2066                 x_string = jsonNumberToDBString(field, x_node);
2067                 y_string = jsonNumberToDBString(field, y_node);
2068
2069         } else {
2070                 x_string = jsonObjectToSimpleString(x_node);
2071                 y_string = jsonObjectToSimpleString(y_node);
2072                 if ( !(dbi_conn_quote_string(dbhandle, &x_string) && dbi_conn_quote_string(dbhandle, &y_string)) ) {
2073                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key strings [%s] and [%s]",
2074                                         MODULENAME, x_string, y_string);
2075                         free(x_string);
2076                         free(y_string);
2077                         return NULL;
2078                 }
2079         }
2080
2081         growing_buffer* sql_buf = buffer_init(32);
2082         buffer_fadd( sql_buf, "%s BETWEEN %s AND %s", osrfHashGet(field, "name"), x_string, y_string );
2083         free(x_string);
2084         free(y_string);
2085
2086         return buffer_release(sql_buf);
2087 }
2088
2089 static char* searchPredicate ( const char* class, osrfHash* field, 
2090                                                            jsonObject* node, osrfMethodContext* ctx ) {
2091
2092         char* pred = NULL;
2093         if (node->type == JSON_ARRAY) { // equality IN search
2094                 pred = searchINPredicate( class, field, node, NULL, ctx );
2095         } else if (node->type == JSON_HASH) { // other search
2096                 jsonIterator* pred_itr = jsonNewIterator( node );
2097                 if( !jsonIteratorHasNext( pred_itr ) ) {
2098                         osrfLogError( OSRF_LOG_MARK, "%s: Empty predicate for field \"%s\"", 
2099                                         MODULENAME, osrfHashGet(field, "name") );
2100                 } else {
2101                         jsonObject* pred_node = jsonIteratorNext( pred_itr );
2102
2103                         // Verify that there are no additional predicates
2104                         if( jsonIteratorHasNext( pred_itr ) ) {
2105                                 osrfLogError( OSRF_LOG_MARK, "%s: Multiple predicates for field \"%s\"", 
2106                                                 MODULENAME, osrfHashGet(field, "name") );
2107                         } else if ( !(strcasecmp( pred_itr->key,"between" )) )
2108                                 pred = searchBETWEENPredicate( class, field, pred_node );
2109                         else if ( !(strcasecmp( pred_itr->key,"in" )) || !(strcasecmp( pred_itr->key,"not in" )) )
2110                                 pred = searchINPredicate( class, field, pred_node, pred_itr->key, ctx );
2111                         else if ( pred_node->type == JSON_ARRAY )
2112                                 pred = searchFunctionPredicate( class, field, pred_node, pred_itr->key );
2113                         else if ( pred_node->type == JSON_HASH )
2114                                 pred = searchFieldTransformPredicate( class, field, pred_node, pred_itr->key );
2115                         else
2116                                 pred = searchSimplePredicate( pred_itr->key, class, field, pred_node );
2117                 }
2118                 jsonIteratorFree(pred_itr);
2119
2120         } else if (node->type == JSON_NULL) { // IS NULL search
2121                 growing_buffer* _p = buffer_init(64);
2122                 buffer_fadd(
2123                         _p,
2124                         "\"%s\".%s IS NULL",
2125                         class,
2126                         osrfHashGet(field, "name")
2127                 );
2128                 pred = buffer_release(_p);
2129         } else { // equality search
2130                 pred = searchSimplePredicate( "=", class, field, node );
2131         }
2132
2133         return pred;
2134
2135 }
2136
2137
2138 /*
2139
2140 join : {
2141         acn : {
2142                 field : record,
2143                 fkey : id
2144                 type : left
2145                 filter_op : or
2146                 filter : { ... },
2147                 join : {
2148                         acp : {
2149                                 field : call_number,
2150                                 fkey : id,
2151                                 filter : { ... },
2152                         },
2153                 },
2154         },
2155         mrd : {
2156                 field : record,
2157                 type : inner
2158                 fkey : id,
2159                 filter : { ... },
2160         }
2161 }
2162
2163 */
2164
2165 static char* searchJOIN ( const jsonObject* join_hash, osrfHash* leftmeta ) {
2166
2167         const jsonObject* working_hash;
2168         jsonObject* freeable_hash = NULL;
2169
2170         if (join_hash->type == JSON_STRING) {
2171                 // create a wrapper around a copy of the original
2172                 const char* _tmp = jsonObjectGetString( join_hash );
2173                 freeable_hash = jsonNewObjectType(JSON_HASH);
2174                 jsonObjectSetKey(freeable_hash, _tmp, NULL);
2175                 working_hash = freeable_hash;
2176         }
2177         else {
2178                 if( join_hash->type != JSON_HASH ) {
2179                         osrfLogError(
2180                                 OSRF_LOG_MARK,
2181                                 "%s: JOIN failed; expected JSON object type not found",
2182                                 MODULENAME
2183                         );
2184                         return NULL;
2185                 }
2186                 working_hash = join_hash;
2187         }
2188
2189         growing_buffer* join_buf = buffer_init(128);
2190         const char* leftclass = osrfHashGet(leftmeta, "classname");
2191
2192         jsonObject* snode = NULL;
2193         jsonIterator* search_itr = jsonNewIterator( working_hash );
2194
2195         while ( (snode = jsonIteratorNext( search_itr )) ) {
2196                 const char* class = search_itr->key;
2197                 osrfHash* idlClass = osrfHashGet( oilsIDL(), class );
2198                 if( !idlClass ) {
2199                         osrfLogError(
2200                                 OSRF_LOG_MARK,
2201                                 "%s: JOIN failed.  No class \"%s\" defined in IDL",
2202                                 MODULENAME,
2203                                 search_itr->key
2204                         );
2205                         jsonIteratorFree( search_itr );
2206                         buffer_free( join_buf );
2207                         if( freeable_hash )
2208                                 jsonObjectFree( freeable_hash );
2209                         return NULL;
2210                 }
2211
2212                 const char* fkey = jsonObjectGetString( jsonObjectGetKeyConst( snode, "fkey" ) );
2213                 const char* field = jsonObjectGetString( jsonObjectGetKeyConst( snode, "field" ) );
2214
2215                 if (field && !fkey) {
2216                         // Look up the corresponding join column in the IDL.
2217                         // The link must be defined in the child table,
2218                         // and point to the right parent table.
2219                         osrfHash* idl_link = (osrfHash*) oilsIDLFindPath( "/%s/links/%s", class, field );
2220                         const char* reltype = NULL;
2221                         const char* other_class = NULL;
2222                         reltype = osrfHashGet( idl_link, "reltype" );
2223                         if( reltype && strcmp( reltype, "has_many" ) )
2224                                 other_class = osrfHashGet( idl_link, "class" );
2225                         if( other_class && !strcmp( other_class, leftclass ) )
2226                                 fkey = osrfHashGet( idl_link, "key" );
2227                         if (!fkey) {
2228                                 osrfLogError(
2229                                         OSRF_LOG_MARK,
2230                                         "%s: JOIN failed.  No link defined from %s.%s to %s",
2231                                         MODULENAME,
2232                                         class,
2233                                         field,
2234                                         leftclass
2235                                 );
2236                                 buffer_free(join_buf);
2237                                 if(freeable_hash)
2238                                         jsonObjectFree(freeable_hash);
2239                                 jsonIteratorFree(search_itr);
2240                                 return NULL;
2241                         }
2242
2243                 } else if (!field && fkey) {
2244                         // Look up the corresponding join column in the IDL.
2245                         // The link must be defined in the child table,
2246                         // and point to the right parent table.
2247                         osrfHash* idl_link = (osrfHash*) oilsIDLFindPath( "/%s/links/%s", leftclass, fkey );
2248                         const char* reltype = NULL;
2249                         const char* other_class = NULL;
2250                         reltype = osrfHashGet( idl_link, "reltype" );
2251                         if( reltype && strcmp( reltype, "has_many" ) )
2252                                 other_class = osrfHashGet( idl_link, "class" );
2253                         if( other_class && !strcmp( other_class, class ) )
2254                                 field = osrfHashGet( idl_link, "key" );
2255                         if (!field) {
2256                                 osrfLogError(
2257                                         OSRF_LOG_MARK,
2258                                         "%s: JOIN failed.  No link defined from %s.%s to %s",
2259                                         MODULENAME,
2260                                         leftclass,
2261                                         fkey,
2262                                         class
2263                                 );
2264                                 buffer_free(join_buf);
2265                                 if(freeable_hash)
2266                                         jsonObjectFree(freeable_hash);
2267                                 jsonIteratorFree(search_itr);
2268                                 return NULL;
2269                         }
2270
2271                 } else if (!field && !fkey) {
2272                         osrfHash* _links = oilsIDL_links( leftclass );
2273
2274                         // For each link defined for the left class:
2275                         // see if the link references the joined class
2276                         osrfHashIterator* itr = osrfNewHashIterator( _links );
2277                         osrfHash* curr_link = NULL;
2278                         while( (curr_link = osrfHashIteratorNext( itr ) ) ) {
2279                                 const char* other_class = osrfHashGet( curr_link, "class" );
2280                                 if( other_class && !strcmp( other_class, class ) ) {
2281
2282                                         // In the IDL, the parent class doesn't know then names of the child
2283                                         // columns that are pointing to it, so don't use that end of the link
2284                                         const char* reltype = osrfHashGet( curr_link, "reltype" );
2285                                         if( reltype && strcmp( reltype, "has_many" ) ) {
2286                                                 // Found a link between the classes
2287                                                 fkey = osrfHashIteratorKey( itr );
2288                                                 field = osrfHashGet( curr_link, "key" );
2289                                                 break;
2290                                         }
2291                                 }
2292                         }
2293                         osrfHashIteratorFree( itr );
2294
2295                         if (!field || !fkey) {
2296                                 // Do another such search, with the classes reversed
2297                                 _links = oilsIDL_links( class );
2298
2299                                 // For each link defined for the joined class:
2300                                 // see if the link references the left class
2301                                 osrfHashIterator* itr = osrfNewHashIterator( _links );
2302                                 osrfHash* curr_link = NULL;
2303                                 while( (curr_link = osrfHashIteratorNext( itr ) ) ) {
2304                                         const char* other_class = osrfHashGet( curr_link, "class" );
2305                                         if( other_class && !strcmp( other_class, leftclass ) ) {
2306
2307                                                 // In the IDL, the parent class doesn't know then names of the child
2308                                                 // columns that are pointing to it, so don't use that end of the link
2309                                                 const char* reltype = osrfHashGet( curr_link, "reltype" );
2310                                                 if( reltype && strcmp( reltype, "has_many" ) ) {
2311                                                         // Found a link between the classes
2312                                                         field = osrfHashIteratorKey( itr );
2313                                                         fkey = osrfHashGet( curr_link, "key" );
2314                                                         break;
2315                                                 }
2316                                         }
2317                                 }
2318                                 osrfHashIteratorFree( itr );
2319                         }
2320
2321                         if (!field || !fkey) {
2322                                 osrfLogError(
2323                                         OSRF_LOG_MARK,
2324                                         "%s: JOIN failed.  No link defined between %s and %s",
2325                                         MODULENAME,
2326                                         leftclass,
2327                                         class
2328                                 );
2329                                 buffer_free(join_buf);
2330                                 if(freeable_hash)
2331                                         jsonObjectFree(freeable_hash);
2332                                 jsonIteratorFree(search_itr);
2333                                 return NULL;
2334                         }
2335
2336                 }
2337
2338                 const char* type = jsonObjectGetString( jsonObjectGetKeyConst( snode, "type" ) );
2339                 if (type) {
2340                         if ( !strcasecmp(type,"left") ) {
2341                                 buffer_add(join_buf, " LEFT JOIN");
2342                         } else if ( !strcasecmp(type,"right") ) {
2343                                 buffer_add(join_buf, " RIGHT JOIN");
2344                         } else if ( !strcasecmp(type,"full") ) {
2345                                 buffer_add(join_buf, " FULL JOIN");
2346                         } else {
2347                                 buffer_add(join_buf, " INNER JOIN");
2348                         }
2349                 } else {
2350                         buffer_add(join_buf, " INNER JOIN");
2351                 }
2352
2353                 char* table = getSourceDefinition(idlClass);
2354                 if( !table ) {
2355                         jsonIteratorFree( search_itr );
2356                         buffer_free( join_buf );
2357                         if( freeable_hash )
2358                                 jsonObjectFree( freeable_hash );
2359                         return NULL;
2360                 }
2361
2362                 buffer_fadd(join_buf, " %s AS \"%s\" ON ( \"%s\".%s = \"%s\".%s",
2363                                         table, class, class, field, leftclass, fkey);
2364                 free(table);
2365
2366                 const jsonObject* filter = jsonObjectGetKeyConst( snode, "filter" );
2367                 if (filter) {
2368                         const char* filter_op = jsonObjectGetString( jsonObjectGetKeyConst( snode, "filter_op" ) );
2369                         if ( filter_op && !strcasecmp("or",filter_op) ) {
2370                                 buffer_add( join_buf, " OR " );
2371                         } else {
2372                                 buffer_add( join_buf, " AND " );
2373                         }
2374
2375                         char* jpred = searchWHERE( filter, idlClass, AND_OP_JOIN, NULL );
2376                         if( jpred ) {
2377                                 OSRF_BUFFER_ADD_CHAR( join_buf, ' ' );
2378                                 OSRF_BUFFER_ADD( join_buf, jpred );
2379                                 free(jpred);
2380                         } else {
2381                                 osrfLogError(
2382                                         OSRF_LOG_MARK,
2383                                         "%s: JOIN failed.  Invalid conditional expression.",
2384                                         MODULENAME
2385                                 );
2386                                 jsonIteratorFree( search_itr );
2387                                 buffer_free( join_buf );
2388                                 if( freeable_hash )
2389                                         jsonObjectFree( freeable_hash );
2390                                 return NULL;
2391                         }
2392                 }
2393
2394                 buffer_add(join_buf, " ) ");
2395                 
2396                 const jsonObject* join_filter = jsonObjectGetKeyConst( snode, "join" );
2397                 if (join_filter) {
2398                         char* jpred = searchJOIN( join_filter, idlClass );
2399                         if( jpred ) {
2400                                 OSRF_BUFFER_ADD_CHAR( join_buf, ' ' );
2401                                 OSRF_BUFFER_ADD( join_buf, jpred );
2402                                 free(jpred);
2403                         } else {
2404                                 osrfLogError(  OSRF_LOG_MARK, "%s: Invalid nested join.", MODULENAME );
2405                                 jsonIteratorFree( search_itr );
2406                                 buffer_free( join_buf );
2407                                 if( freeable_hash )
2408                                         jsonObjectFree( freeable_hash );
2409                                 return NULL;
2410                         }
2411                 }
2412         }
2413
2414         if(freeable_hash)
2415                 jsonObjectFree(freeable_hash);
2416         jsonIteratorFree(search_itr);
2417
2418         return buffer_release(join_buf);
2419 }
2420
2421 /*
2422
2423 { +class : { -or|-and : { field : { op : value }, ... } ... }, ... }
2424 { +class : { -or|-and : [ { field : { op : value }, ... }, ...] ... }, ... }
2425 [ { +class : { -or|-and : [ { field : { op : value }, ... }, ...] ... }, ... }, ... ]
2426
2427 Generate code to express a set of conditions, as for a WHERE clause.  Parameters:
2428
2429 search_hash is the JSON expression of the conditions.
2430 meta is the class definition from the IDL, for the relevant table.
2431 opjoin_type indicates whether multiple conditions, if present, should be
2432         connected by AND or OR.
2433 osrfMethodContext is loaded with all sorts of stuff, but all we do with it here is
2434         to pass it to other functions -- and all they do with it is to use the session
2435         and request members to send error messages back to the client.
2436
2437 */
2438
2439 static char* searchWHERE ( const jsonObject* search_hash, osrfHash* meta, int opjoin_type, osrfMethodContext* ctx ) {
2440
2441         osrfLogDebug(
2442         OSRF_LOG_MARK,
2443         "%s: Entering searchWHERE; search_hash addr = %p, meta addr = %p, opjoin_type = %d, ctx addr = %p",
2444         MODULENAME,
2445         search_hash,
2446         meta,
2447         opjoin_type,
2448         ctx
2449     );
2450
2451         growing_buffer* sql_buf = buffer_init(128);
2452
2453         jsonObject* node = NULL;
2454
2455         int first = 1;
2456         if ( search_hash->type == JSON_ARRAY ) {
2457                 osrfLogDebug(OSRF_LOG_MARK, "%s: In WHERE clause, condition type is JSON_ARRAY", MODULENAME);
2458                 jsonIterator* search_itr = jsonNewIterator( search_hash );
2459                 if( !jsonIteratorHasNext( search_itr ) ) {
2460                         osrfLogError(
2461                                 OSRF_LOG_MARK,
2462                                 "%s: Invalid predicate structure: empty JSON array",
2463                                 MODULENAME
2464                         );
2465                         jsonIteratorFree( search_itr );
2466                         buffer_free( sql_buf );
2467                         return NULL;
2468                 }
2469
2470                 while ( (node = jsonIteratorNext( search_itr )) ) {
2471             if (first) {
2472                 first = 0;
2473             } else {
2474                 if (opjoin_type == OR_OP_JOIN) buffer_add(sql_buf, " OR ");
2475                 else buffer_add(sql_buf, " AND ");
2476             }
2477
2478             char* subpred = searchWHERE( node, meta, opjoin_type, ctx );
2479                         if( subpred ) {
2480                 buffer_fadd(sql_buf, "( %s )", subpred);
2481                 free(subpred);
2482                         } else {
2483                                 jsonIteratorFree( search_itr );
2484                                 buffer_free( sql_buf );
2485                                 return NULL;
2486                         }
2487         }
2488         jsonIteratorFree(search_itr);
2489
2490         } else if ( search_hash->type == JSON_HASH ) {
2491                 osrfLogDebug(OSRF_LOG_MARK, "%s: In WHERE clause, condition type is JSON_HASH", MODULENAME);
2492                 jsonIterator* search_itr = jsonNewIterator( search_hash );
2493                 if( !jsonIteratorHasNext( search_itr ) ) {
2494                         osrfLogError(
2495                                 OSRF_LOG_MARK,
2496                                 "%s: Invalid predicate structure: empty JSON object",
2497                                 MODULENAME
2498                         );
2499                         jsonIteratorFree( search_itr );
2500                         buffer_free( sql_buf );
2501                         return NULL;
2502                 }
2503
2504                 while ( (node = jsonIteratorNext( search_itr )) ) {
2505
2506             if (first) {
2507                 first = 0;
2508             } else {
2509                 if (opjoin_type == OR_OP_JOIN) buffer_add(sql_buf, " OR ");
2510                 else buffer_add(sql_buf, " AND ");
2511             }
2512
2513                         if ( '+' == search_itr->key[ 0 ] ) {
2514                                 if ( node->type == JSON_STRING ) {
2515                                         // Intended purpose; to allow reference to a Boolean column
2516
2517                                         // Verify that the class alias is not empty
2518                                         if( '\0' == search_itr->key[ 1 ] ) {
2519                                                 osrfLogError(
2520                                                         OSRF_LOG_MARK,
2521                                                         "%s: Table alias is empty",
2522                                                         MODULENAME
2523                                                 );
2524                                                 jsonIteratorFree( search_itr );
2525                                                 buffer_free( sql_buf );
2526                                                 return NULL;
2527                                         }
2528
2529                                         // Verify that the string looks like an identifier.
2530                                         const char* subpred = jsonObjectGetString( node );
2531                                         if( ! is_identifier( subpred ) ) {
2532                                                 osrfLogError(
2533                                                          OSRF_LOG_MARK,
2534                                                         "%s: Invalid boolean identifier in WHERE clause: \"%s\"",
2535                                                         MODULENAME,
2536                                                         subpred
2537                                                 );
2538                                                 jsonIteratorFree( search_itr );
2539                                                 buffer_free( sql_buf );
2540                                                 return NULL;
2541                                         }
2542
2543                                         buffer_fadd(sql_buf, " \"%s\".%s ", search_itr->key + 1, subpred);
2544                                 } else {
2545                                         char* subpred = searchWHERE( node, osrfHashGet( oilsIDL(), search_itr->key + 1 ), AND_OP_JOIN, ctx );
2546                                         if( subpred ) {
2547                                                 buffer_fadd(sql_buf, "( %s )", subpred);
2548                                                 free(subpred);
2549                                         } else {
2550                                                 jsonIteratorFree( search_itr );
2551                                                 buffer_free( sql_buf );
2552                                                 return NULL;
2553                                         }
2554                                 }
2555                         } else if ( !strcasecmp("-or",search_itr->key) ) {
2556                                 char* subpred = searchWHERE( node, meta, OR_OP_JOIN, ctx );
2557                                 if( subpred ) {
2558                                         buffer_fadd(sql_buf, "( %s )", subpred);
2559                                         free( subpred );
2560                                 } else {
2561                                         buffer_free( sql_buf );
2562                                         return NULL;
2563                                 }
2564                         } else if ( !strcasecmp("-and",search_itr->key) ) {
2565                                 char* subpred = searchWHERE( node, meta, AND_OP_JOIN, ctx );
2566                                 if( subpred ) {
2567                                         buffer_fadd(sql_buf, "( %s )", subpred);
2568                                         free( subpred );
2569                                 } else {
2570                                         buffer_free( sql_buf );
2571                                         return NULL;
2572                                 }
2573                         } else if ( !strcasecmp("-not",search_itr->key) ) {
2574                                 char* subpred = searchWHERE( node, meta, AND_OP_JOIN, ctx );
2575                                 if( subpred ) {
2576                                         buffer_fadd(sql_buf, " NOT ( %s )", subpred);
2577                                         free( subpred );
2578                                 } else {
2579                                         buffer_free( sql_buf );
2580                                         return NULL;
2581                                 }
2582                         } else if ( !strcasecmp("-exists",search_itr->key) ) {
2583                 char* subpred = SELECT(
2584                     ctx,
2585                     jsonObjectGetKey( node, "select" ),
2586                     jsonObjectGetKey( node, "from" ),
2587                     jsonObjectGetKey( node, "where" ),
2588                     jsonObjectGetKey( node, "having" ),
2589                     jsonObjectGetKey( node, "order_by" ),
2590                     jsonObjectGetKey( node, "limit" ),
2591                     jsonObjectGetKey( node, "offset" ),
2592                     SUBSELECT
2593                 );
2594
2595                                 if( subpred ) {
2596                                         buffer_fadd(sql_buf, "EXISTS ( %s )", subpred);
2597                                         free(subpred);
2598                                 } else {
2599                                         buffer_free( sql_buf );
2600                                         return NULL;
2601                                 }
2602             } else if ( !strcasecmp("-not-exists",search_itr->key) ) {
2603                 char* subpred = SELECT(
2604                     ctx,
2605                     jsonObjectGetKey( node, "select" ),
2606                     jsonObjectGetKey( node, "from" ),
2607                     jsonObjectGetKey( node, "where" ),
2608                     jsonObjectGetKey( node, "having" ),
2609                     jsonObjectGetKey( node, "order_by" ),
2610                     jsonObjectGetKey( node, "limit" ),
2611                     jsonObjectGetKey( node, "offset" ),
2612                     SUBSELECT
2613                 );
2614
2615                                 if( subpred ) {
2616                                         buffer_fadd(sql_buf, "NOT EXISTS ( %s )", subpred);
2617                                         free(subpred);
2618                                 } else {
2619                                         buffer_free( sql_buf );
2620                                         return NULL;
2621                                 }
2622
2623             } else {
2624
2625                 char* class = osrfHashGet(meta, "classname");
2626                 osrfHash* fields = osrfHashGet(meta, "fields");
2627                 osrfHash* field = osrfHashGet( fields, search_itr->key );
2628
2629
2630                 if (!field) {
2631                     char* table = getSourceDefinition(meta);
2632                                         if( !table )
2633                                                 table = strdup( "(?)" );
2634                     osrfLogError(
2635                         OSRF_LOG_MARK,
2636                         "%s: Attempt to reference non-existent column \"%s\" on %s (%s)",
2637                         MODULENAME,
2638                         search_itr->key,
2639                         table,
2640                         class ? class : "?"
2641                     );
2642                     buffer_free(sql_buf);
2643                     free(table);
2644                                         jsonIteratorFree(search_itr);
2645                                         return NULL;
2646                                 }
2647
2648                                 char* subpred = searchPredicate( class, field, node, ctx );
2649                                 if( subpred ) {
2650                                         buffer_add( sql_buf, subpred );
2651                                         free(subpred);
2652                                 } else {
2653                                         buffer_free(sql_buf);
2654                                         jsonIteratorFree(search_itr);
2655                                         return NULL;
2656                                 }
2657                         }
2658                 }
2659                 jsonIteratorFree(search_itr);
2660
2661     } else {
2662         // ERROR ... only hash and array allowed at this level
2663         char* predicate_string = jsonObjectToJSON( search_hash );
2664         osrfLogError(
2665             OSRF_LOG_MARK,
2666             "%s: Invalid predicate structure: %s",
2667             MODULENAME,
2668             predicate_string
2669         );
2670         buffer_free(sql_buf);
2671         free(predicate_string);
2672         return NULL;
2673     }
2674
2675         return buffer_release(sql_buf);
2676 }
2677
2678 // Return 1 if the class is in the FROM clause, or 0 if not
2679 static int is_joined( jsonObject* from_clause, const char* class ) {
2680
2681         if( ! from_clause )
2682                 return 0;
2683         else if( from_clause->type == JSON_STRING ) {
2684                 if( strcmp( class, jsonObjectGetString( from_clause ) ) )
2685                         return 0;
2686                 else
2687                         return 1;
2688         } else if( from_clause->type != JSON_HASH ) {
2689                 return 0;
2690         } else {      // Look at any subjoins
2691                 jsonIterator* class_itr = jsonNewIterator( from_clause );
2692                 jsonObject* curr_class;
2693                 int rc = 0;    // return code
2694                 while ( ( curr_class = jsonIteratorNext( class_itr ) ) ) {
2695                         if( ! strcmp( class_itr->key, class ) ) {
2696                                 rc = 1;
2697                                 break;
2698                         } else {
2699                                 jsonObject* subjoin = jsonObjectGetKey( curr_class, "join" );
2700                                 if( subjoin && is_joined( subjoin, class ) ) {
2701                                         rc = 1;
2702                                         break;
2703                                 }
2704                         }
2705                 }
2706                 jsonIteratorFree( class_itr );
2707
2708                 return rc;
2709         }
2710 }
2711
2712 char* SELECT (
2713                 /* method context */ osrfMethodContext* ctx,
2714                 
2715                 /* SELECT   */ jsonObject* selhash,
2716                 /* FROM     */ jsonObject* join_hash,
2717                 /* WHERE    */ jsonObject* search_hash,
2718                 /* HAVING   */ jsonObject* having_hash,
2719                 /* ORDER BY */ jsonObject* order_hash,
2720                 /* LIMIT    */ jsonObject* limit,
2721                 /* OFFSET   */ jsonObject* offset,
2722                 /* flags    */ int flags
2723 ) {
2724         const char* locale = osrf_message_get_last_locale();
2725
2726         // in case we don't get a select list
2727         jsonObject* defaultselhash = NULL;
2728
2729         // general tmp objects
2730         const jsonObject* tmp_const;
2731         jsonObject* selclass = NULL;
2732         jsonObject* selfield = NULL;
2733         jsonObject* snode = NULL;
2734         jsonObject* onode = NULL;
2735
2736         char* string = NULL;
2737         int from_function = 0;
2738         int first = 1;
2739         int gfirst = 1;
2740         //int hfirst = 1;
2741
2742         // the core search class
2743         char* core_class = NULL;
2744
2745         // metadata about the core search class
2746         osrfHash* core_meta = NULL;
2747
2748         osrfLogDebug(OSRF_LOG_MARK, "cstore SELECT locale: %s", locale);
2749
2750         // punt if there's no core class
2751         if (!join_hash || ( join_hash->type == JSON_HASH && !join_hash->size )) {
2752                 osrfLogError(
2753                         OSRF_LOG_MARK,
2754                         "%s: FROM clause is missing or empty",
2755                         MODULENAME
2756                 );
2757                 if( ctx )
2758                         osrfAppSessionStatus(
2759                                 ctx->session,
2760                                 OSRF_STATUS_INTERNALSERVERERROR,
2761                                 "osrfMethodException",
2762                                 ctx->request,
2763                                 "FROM clause is missing or empty in JSON query"
2764                         );
2765                 return NULL;
2766         }
2767
2768         // get the core class -- the only key of the top level FROM clause, or a string
2769         if (join_hash->type == JSON_HASH) {
2770                 jsonIterator* tmp_itr = jsonNewIterator( join_hash );
2771                 snode = jsonIteratorNext( tmp_itr );
2772                 
2773                 core_class = strdup( tmp_itr->key );
2774                 join_hash = snode;
2775                 
2776                 jsonObject* extra = jsonIteratorNext( tmp_itr );
2777
2778                 jsonIteratorFree( tmp_itr );
2779                 snode = NULL;
2780
2781                 // There shouldn't be more than one entry in join_hash
2782                 if( extra ) {
2783                         osrfLogError(
2784                                 OSRF_LOG_MARK,
2785                                 "%s: Malformed FROM clause: extra entry in JSON_HASH",
2786                                 MODULENAME
2787                         );
2788                         if( ctx )
2789                                 osrfAppSessionStatus(
2790                                         ctx->session,
2791                                         OSRF_STATUS_INTERNALSERVERERROR,
2792                                         "osrfMethodException",
2793                                         ctx->request,
2794                                         "Malformed FROM clause in JSON query"
2795                                 );
2796                         free( core_class );
2797                         return NULL;    // Malformed join_hash; extra entry
2798                 }
2799         } else if (join_hash->type == JSON_ARRAY) {
2800                 from_function = 1;
2801                 core_class = jsonObjectToSimpleString( jsonObjectGetIndex(join_hash, 0) );
2802                 selhash = NULL;
2803
2804         } else if (join_hash->type == JSON_STRING) {
2805                 core_class = jsonObjectToSimpleString( join_hash );
2806                 join_hash = NULL;
2807         }
2808         else {
2809                 osrfLogError(
2810                         OSRF_LOG_MARK,
2811                         "%s: FROM clause is unexpected JSON type: %s",
2812                         MODULENAME,
2813                         json_type( join_hash->type )
2814                 );
2815                 if( ctx )
2816                         osrfAppSessionStatus(
2817                                 ctx->session,
2818                                 OSRF_STATUS_INTERNALSERVERERROR,
2819                                 "osrfMethodException",
2820                                 ctx->request,
2821                                 "Ill-formed FROM clause in JSON query"
2822                         );
2823                 free( core_class );
2824                 return NULL;
2825         }
2826
2827         if (!from_function) {
2828                 // Get the IDL class definition for the core class
2829                 core_meta = osrfHashGet( oilsIDL(), core_class );
2830                 if( !core_meta ) {    // Didn't find it?
2831                         osrfLogError(
2832                                 OSRF_LOG_MARK,
2833                                 "%s: SELECT clause references undefined class: \"%s\"",
2834                                 MODULENAME,
2835                                 core_class
2836                         );
2837                         if( ctx )
2838                                 osrfAppSessionStatus(
2839                                         ctx->session,
2840                                         OSRF_STATUS_INTERNALSERVERERROR,
2841                                         "osrfMethodException",
2842                                         ctx->request,
2843                                         "SELECT clause references undefined class in JSON query"
2844                                 );
2845                         free( core_class );
2846                         return NULL;
2847                 }
2848
2849                 // Make sure the class isn't virtual
2850                 if( str_is_true( osrfHashGet( core_meta, "virtual" ) ) ) {
2851                         osrfLogError(
2852                                 OSRF_LOG_MARK,
2853                                 "%s: Core class is virtual: \"%s\"",
2854                                 MODULENAME,
2855                                 core_class
2856                         );
2857                         if( ctx )
2858                                 osrfAppSessionStatus(
2859                                         ctx->session,
2860                                         OSRF_STATUS_INTERNALSERVERERROR,
2861                                         "osrfMethodException",
2862                                         ctx->request,
2863                                         "FROM clause references virtual class in JSON query"
2864                                 );
2865                         free( core_class );
2866                         return NULL;
2867                 }
2868         }
2869
2870         // if the select list is empty, or the core class field list is '*',
2871         // build the default select list ...
2872         if (!selhash) {
2873                 selhash = defaultselhash = jsonNewObjectType(JSON_HASH);
2874                 jsonObjectSetKey( selhash, core_class, jsonNewObjectType(JSON_ARRAY) );
2875         } else if( selhash->type != JSON_HASH ) {
2876                 osrfLogError(
2877                         OSRF_LOG_MARK,
2878                         "%s: Expected JSON_HASH for SELECT clause; found %s",
2879                         MODULENAME,
2880                         json_type( selhash->type )
2881                 );
2882
2883                 if (ctx)
2884                         osrfAppSessionStatus(
2885                                 ctx->session,
2886                                 OSRF_STATUS_INTERNALSERVERERROR,
2887                                 "osrfMethodException",
2888                                 ctx->request,
2889                                 "Malformed SELECT clause in JSON query"
2890                         );
2891                 free( core_class );
2892                 return NULL;
2893         } else if ( (tmp_const = jsonObjectGetKeyConst( selhash, core_class )) && tmp_const->type == JSON_STRING ) {
2894                 const char* _x = jsonObjectGetString( tmp_const );
2895                 if (!strncmp( "*", _x, 1 )) {
2896                         jsonObjectRemoveKey( selhash, core_class );
2897                         jsonObjectSetKey( selhash, core_class, jsonNewObjectType(JSON_ARRAY) );
2898                 }
2899         }
2900
2901         // the query buffer
2902         growing_buffer* sql_buf = buffer_init(128);
2903
2904         // temp buffers for the SELECT list and GROUP BY clause
2905         growing_buffer* select_buf = buffer_init(128);
2906         growing_buffer* group_buf = buffer_init(128);
2907
2908         int aggregate_found = 0;     // boolean
2909
2910         // Build a select list
2911         if(from_function)   // From a function we select everything
2912                 OSRF_BUFFER_ADD_CHAR( select_buf, '*' );
2913         else {
2914
2915                 // If we need to build a default list, prepare to do so
2916                 jsonObject* _tmp = jsonObjectGetKey( selhash, core_class );
2917                 if ( _tmp && !_tmp->size ) {
2918
2919                         osrfHash* core_fields = osrfHashGet( core_meta, "fields" );
2920
2921                         osrfHashIterator* field_itr = osrfNewHashIterator( core_fields );
2922                         osrfHash* field_def;
2923                         while( ( field_def = osrfHashIteratorNext( field_itr ) ) ) {
2924                                 if( ! str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
2925                                         // This field is not virtual, so add it to the list
2926                                         jsonObjectPush( _tmp, jsonNewObject( osrfHashIteratorKey( field_itr ) ) );
2927                                 }
2928                         }
2929                         osrfHashIteratorFree( field_itr );
2930                 }
2931
2932                 // Now build the actual select list
2933             int sel_pos = 1;
2934             first = 1;
2935             gfirst = 1;
2936             jsonIterator* selclass_itr = jsonNewIterator( selhash );
2937             while ( (selclass = jsonIteratorNext( selclass_itr )) ) {    // For each class
2938
2939                         const char* cname = selclass_itr->key;
2940
2941                         // Make sure the target relation is in the FROM clause.
2942                         
2943                         // At this point join_hash is a step down from the join_hash we
2944                         // received as a parameter.  If the original was a JSON_STRING,
2945                         // then json_hash is now NULL.  If the original was a JSON_HASH,
2946                         // then json_hash is now the first (and only) entry in it,
2947                         // denoting the core class.  We've already excluded the
2948                         // possibility that the original was a JSON_ARRAY, because in
2949                         // that case from_function would be non-NULL, and we wouldn't
2950                         // be here.
2951
2952                         // If the current class isn't the core class
2953                         // and it isn't in the join tree, bail out
2954                         if ( strcmp( core_class, cname ) && ! is_joined( join_hash, cname ) ) {
2955                                 osrfLogError(
2956                                         OSRF_LOG_MARK,
2957                                         "%s: SELECT clause references class not in FROM clause: \"%s\"",
2958                                         MODULENAME,
2959                                         cname
2960                                 );
2961                                 if( ctx )
2962                                         osrfAppSessionStatus(
2963                                                 ctx->session,
2964                                                 OSRF_STATUS_INTERNALSERVERERROR,
2965                                                 "osrfMethodException",
2966                                                 ctx->request,
2967                                                 "Selected class not in FROM clause in JSON query"
2968                                         );
2969                                 jsonIteratorFree( selclass_itr );
2970                                 buffer_free( sql_buf );
2971                                 buffer_free( select_buf );
2972                                 buffer_free( group_buf );
2973                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
2974                                 free( core_class );
2975                                 return NULL;
2976                         }
2977
2978                         // Look up some attributes of the current class, so that we 
2979                         // don't have to look them up again for each field
2980                         osrfHash* idlClass = osrfHashGet( oilsIDL(), cname );
2981                         osrfHash* class_field_set = osrfHashGet( idlClass, "fields" );
2982                         const char* class_pkey = osrfHashGet( idlClass, "primarykey" );
2983                         const char* class_tname = osrfHashGet( idlClass, "tablename" );
2984                         
2985                     // stitch together the column list ...
2986                     jsonIterator* select_itr = jsonNewIterator( selclass );
2987                     while ( (selfield = jsonIteratorNext( select_itr )) ) {   // for each SELECT column
2988
2989                                 // If we need a separator comma, add one
2990                                 if (first) {
2991                                         first = 0;
2992                                 } else {
2993                                         OSRF_BUFFER_ADD_CHAR( select_buf, ',' );
2994                                 }
2995
2996                                 // ... if it's a string, just toss it on the pile
2997                                 if (selfield->type == JSON_STRING) {
2998
2999                                         // Look up the field in the IDL
3000                                         const char* col_name = jsonObjectGetString( selfield );
3001                                         osrfHash* field_def = osrfHashGet( class_field_set, col_name );
3002                                         if ( !field_def ) {
3003                                                 // No such field in current class
3004                                                 osrfLogError(
3005                                                         OSRF_LOG_MARK,
3006                                                         "%s: Selected column \"%s\" not defined in IDL for class \"%s\"",
3007                                                         MODULENAME,
3008                                                         col_name,
3009                                                         cname
3010                                                 );
3011                                                 if( ctx )
3012                                                         osrfAppSessionStatus(
3013                                                                 ctx->session,
3014                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3015                                                                 "osrfMethodException",
3016                                                                 ctx->request,
3017                                                                 "Selected column not defined in JSON query"
3018                                                         );
3019                                                 jsonIteratorFree( select_itr );
3020                                                 jsonIteratorFree( selclass_itr );
3021                                                 buffer_free( sql_buf );
3022                                                 buffer_free( select_buf );
3023                                                 buffer_free( group_buf );
3024                                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3025                                                 free( core_class );
3026                                                 return NULL;
3027                                         } else if ( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3028                                                 // Virtual field not allowed
3029                                                 osrfLogError(
3030                                                         OSRF_LOG_MARK,
3031                                                         "%s: Selected column \"%s\" for class \"%s\" is virtual",
3032                                                         MODULENAME,
3033                                                         col_name,
3034                                                         cname
3035                                                 );
3036                                                 if( ctx )
3037                                                         osrfAppSessionStatus(
3038                                                                 ctx->session,
3039                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3040                                                                 "osrfMethodException",
3041                                                                 ctx->request,
3042                                                                 "Selected column may not be virtual in JSON query"
3043                                                         );
3044                                                 jsonIteratorFree( select_itr );
3045                                                 jsonIteratorFree( selclass_itr );
3046                                                 buffer_free( sql_buf );
3047                                                 buffer_free( select_buf );
3048                                                 buffer_free( group_buf );
3049                                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3050                                                 free( core_class );
3051                                                 return NULL;
3052                                         }
3053
3054                                         if (locale) {
3055                                                 const char* i18n;
3056                                                 if (flags & DISABLE_I18N)
3057                                                         i18n = NULL;
3058                                                 else
3059                                                         i18n = osrfHashGet(field_def, "i18n");
3060
3061                                                 if( str_is_true( i18n ) ) {
3062                             buffer_fadd( select_buf,
3063                                                                 " oils_i18n_xlate('%s', '%s', '%s', '%s', \"%s\".%s::TEXT, '%s') AS \"%s\"",
3064                                                                 class_tname, cname, col_name, class_pkey, cname, class_pkey, locale, col_name );
3065                         } else {
3066                                             buffer_fadd(select_buf, " \"%s\".%s AS \"%s\"", cname, col_name, col_name );
3067                         }
3068                     } else {
3069                                         buffer_fadd(select_buf, " \"%s\".%s AS \"%s\"", cname, col_name, col_name );
3070                     }
3071                                         
3072                                 // ... but it could be an object, in which case we check for a Field Transform
3073                                 } else if (selfield->type == JSON_HASH) {
3074
3075                                         const char* col_name = jsonObjectGetString( jsonObjectGetKeyConst( selfield, "column" ) );
3076
3077                                         // Get the field definition from the IDL
3078                                         osrfHash* field_def = osrfHashGet( class_field_set, col_name );
3079                                         if ( !field_def ) {
3080                                                 // No such field in current class
3081                                                 osrfLogError(
3082                                                         OSRF_LOG_MARK,
3083                                                         "%s: Selected column \"%s\" is not defined in IDL for class \"%s\"",
3084                                                         MODULENAME,
3085                                                         col_name,
3086                                                         cname
3087                                                 );
3088                                                 if( ctx )
3089                                                         osrfAppSessionStatus(
3090                                                                 ctx->session,
3091                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3092                                                                 "osrfMethodException",
3093                                                                 ctx->request,
3094                                                                 "Selected column is not defined in JSON query"
3095                                                         );
3096                                                 jsonIteratorFree( select_itr );
3097                                                 jsonIteratorFree( selclass_itr );
3098                                                 buffer_free( sql_buf );
3099                                                 buffer_free( select_buf );
3100                                                 buffer_free( group_buf );
3101                                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3102                                                 free( core_class );
3103                                                 return NULL;
3104                                         } else if ( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3105                                                 // No such field in current class
3106                                                 osrfLogError(
3107                                                         OSRF_LOG_MARK,
3108                                                         "%s: Selected column \"%s\" is virtual for class \"%s\"",
3109                                                         MODULENAME,
3110                                                         col_name,
3111                                                         cname
3112                                                 );
3113                                                 if( ctx )
3114                                                         osrfAppSessionStatus(
3115                                                                 ctx->session,
3116                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3117                                                                 "osrfMethodException",
3118                                                                 ctx->request,
3119                                                                 "Selected column is virtual in JSON query"
3120                                                         );
3121                                                 jsonIteratorFree( select_itr );
3122                                                 jsonIteratorFree( selclass_itr );
3123                                                 buffer_free( sql_buf );
3124                                                 buffer_free( select_buf );
3125                                                 buffer_free( group_buf );
3126                                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3127                                                 free( core_class );
3128                                                 return NULL;
3129                                         }
3130
3131                                         // Decide what to use as a column alias
3132                                         const char* _alias;
3133                                         if ((tmp_const = jsonObjectGetKeyConst( selfield, "alias" ))) {
3134                                                 _alias = jsonObjectGetString( tmp_const );
3135                                         } else {         // Use field name as the alias
3136                                                 _alias = col_name;
3137                                         }
3138
3139                                         if (jsonObjectGetKeyConst( selfield, "transform" )) {
3140                                                 char* transform_str = searchFieldTransform(cname, field_def, selfield);
3141                                                 if( transform_str ) {
3142                                                         buffer_fadd(select_buf, " %s AS \"%s\"", transform_str, _alias);
3143                                                         free(transform_str);
3144                                                 } else {
3145                                                         if( ctx )
3146                                                                 osrfAppSessionStatus(
3147                                                                         ctx->session,
3148                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3149                                                                         "osrfMethodException",
3150                                                                         ctx->request,
3151                                                                         "Unable to generate transform function in JSON query"
3152                                                                 );
3153                                                         jsonIteratorFree( select_itr );
3154                                                         jsonIteratorFree( selclass_itr );
3155                                                         buffer_free( sql_buf );
3156                                                         buffer_free( select_buf );
3157                                                         buffer_free( group_buf );
3158                                                         if( defaultselhash ) jsonObjectFree( defaultselhash );
3159                                                         free( core_class );
3160                                                         return NULL;
3161                                                 }
3162                                         } else {
3163
3164                                                 if (locale) {
3165                                                         const char* i18n;
3166                                                         if (flags & DISABLE_I18N)
3167                                                                 i18n = NULL;
3168                                                         else
3169                                                                 i18n = osrfHashGet(field_def, "i18n");
3170
3171                                                         if( str_is_true( i18n ) ) {
3172                                                                 buffer_fadd( select_buf,
3173                                                                         " oils_i18n_xlate('%s', '%s', '%s', '%s', \"%s\".%s::TEXT, '%s') AS \"%s\"",
3174                                                                         class_tname, cname, col_name, class_pkey, cname, class_pkey, locale, _alias);
3175                                                         } else {
3176                                                                 buffer_fadd(select_buf, " \"%s\".%s AS \"%s\"", cname, col_name, _alias);
3177                                                         }
3178                                                 } else {
3179                                                         buffer_fadd(select_buf, " \"%s\".%s AS \"%s\"", cname, col_name, _alias);
3180                                                 }
3181                                         }
3182                                 }
3183                                 else {
3184                                         osrfLogError(
3185                                                 OSRF_LOG_MARK,
3186                                                 "%s: Selected item is unexpected JSON type: %s",
3187                                                 MODULENAME,
3188                                                 json_type( selfield->type )
3189                                         );
3190                                         if( ctx )
3191                                                 osrfAppSessionStatus(
3192                                                         ctx->session,
3193                                                         OSRF_STATUS_INTERNALSERVERERROR,
3194                                                         "osrfMethodException",
3195                                                         ctx->request,
3196                                                         "Ill-formed SELECT item in JSON query"
3197                                                 );
3198                                         jsonIteratorFree( select_itr );
3199                                         jsonIteratorFree( selclass_itr );
3200                                         buffer_free( sql_buf );
3201                                         buffer_free( select_buf );
3202                                         buffer_free( group_buf );
3203                                         if( defaultselhash ) jsonObjectFree( defaultselhash );
3204                                         free( core_class );
3205                                         return NULL;
3206                                 }
3207
3208                                 const jsonObject* agg_obj = jsonObjectGetKey( selfield, "aggregate" );
3209                                 if( obj_is_true( agg_obj ) )
3210                                         aggregate_found = 1;
3211                                 else {
3212                                         // Append a comma (except for the first one)
3213                                         // and add the column to a GROUP BY clause
3214                                         if (gfirst)
3215                                                 gfirst = 0;
3216                                         else
3217                                                 OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
3218
3219                                         buffer_fadd(group_buf, " %d", sel_pos);
3220                                 }
3221
3222 #if 0
3223                             if (is_agg->size || (flags & SELECT_DISTINCT)) {
3224
3225                                         const jsonObject* aggregate_obj = jsonObjectGetKey( selfield, "aggregate" );
3226                                     if ( ! obj_is_true( aggregate_obj ) ) {
3227                                             if (gfirst) {
3228                                                     gfirst = 0;
3229                                             } else {
3230                                                         OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
3231                                             }
3232
3233                                             buffer_fadd(group_buf, " %d", sel_pos);
3234
3235                                         /*
3236                                     } else if (is_agg = jsonObjectGetKey( selfield, "having" )) {
3237                                             if (gfirst) {
3238                                                     gfirst = 0;
3239                                             } else {
3240                                                         OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
3241                                             }
3242
3243                                             _column = searchFieldTransform(cname, field, selfield);
3244                                                 OSRF_BUFFER_ADD_CHAR(group_buf, ' ');
3245                                                 OSRF_BUFFER_ADD(group_buf, _column);
3246                                             _column = searchFieldTransform(cname, field, selfield);
3247                                         */
3248                                     }
3249                             }
3250 #endif
3251
3252                             sel_pos++;
3253                     } // end while -- iterating across SELECT columns
3254
3255             jsonIteratorFree(select_itr);
3256             } // end while -- iterating across classes
3257
3258         jsonIteratorFree(selclass_itr);
3259     }
3260
3261
3262         char* col_list = buffer_release(select_buf);
3263         char* table = NULL;
3264         if (from_function) table = searchValueTransform(join_hash);
3265         else table = getSourceDefinition(core_meta);
3266         
3267         if( !table ) {
3268                 if (ctx)
3269                         osrfAppSessionStatus(
3270                                 ctx->session,
3271                                 OSRF_STATUS_INTERNALSERVERERROR,
3272                                 "osrfMethodException",
3273                                 ctx->request,
3274                                 "Unable to identify table for core class"
3275                         );
3276                 free( col_list );
3277                 buffer_free( sql_buf );
3278                 buffer_free( group_buf );
3279                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3280                 free( core_class );
3281                 return NULL;    
3282         }
3283         
3284         // Put it all together
3285         buffer_fadd(sql_buf, "SELECT %s FROM %s AS \"%s\" ", col_list, table, core_class );
3286         free(col_list);
3287         free(table);
3288
3289         char* order_by_list = NULL;
3290         char* having_buf = NULL;
3291
3292         if (!from_function) {
3293
3294                 // Now, walk the join tree and add that clause
3295                 if ( join_hash ) {
3296                         char* join_clause = searchJOIN( join_hash, core_meta );
3297                         if( join_clause ) {
3298                                 buffer_add(sql_buf, join_clause);
3299                                 free(join_clause);
3300                         } else {
3301                                 if (ctx)
3302                                         osrfAppSessionStatus(
3303                                                 ctx->session,
3304                                                 OSRF_STATUS_INTERNALSERVERERROR,
3305                                                 "osrfMethodException",
3306                                                 ctx->request,
3307                                                 "Unable to construct JOIN clause(s)"
3308                                         );
3309                                 buffer_free( sql_buf );
3310                                 buffer_free( group_buf );
3311                                 if( defaultselhash ) jsonObjectFree( defaultselhash );
3312                                 free( core_class );
3313                                 return NULL;
3314                         }
3315             }
3316
3317                 // Build a WHERE clause, if there is one
3318             if ( search_hash ) {
3319                     buffer_add(sql_buf, " WHERE ");
3320
3321                     // and it's on the WHERE clause
3322                     char* pred = searchWHERE( search_hash, core_meta, AND_OP_JOIN, ctx );
3323
3324                     if (pred) {
3325                                 buffer_add(sql_buf, pred);
3326                                 free(pred);
3327                         } else {
3328                                 if (ctx) {
3329                                 osrfAppSessionStatus(
3330                                         ctx->session,
3331                                         OSRF_STATUS_INTERNALSERVERERROR,
3332                                         "osrfMethodException",
3333                                         ctx->request,
3334                                         "Severe query error in WHERE predicate -- see error log for more details"
3335                                 );
3336                             }
3337                             free(core_class);
3338                             buffer_free(group_buf);
3339                             buffer_free(sql_buf);
3340                             if (defaultselhash) jsonObjectFree(defaultselhash);
3341                             return NULL;
3342                         }
3343                 }
3344
3345                 // Build a HAVING clause, if there is one
3346                 if ( having_hash ) {
3347
3348                         // and it's on the the WHERE clause
3349                         having_buf = searchWHERE( having_hash, core_meta, AND_OP_JOIN, ctx );
3350
3351                         if( ! having_buf ) {
3352                                 if (ctx) {
3353                                                 osrfAppSessionStatus(
3354                                                 ctx->session,
3355                                                 OSRF_STATUS_INTERNALSERVERERROR,
3356                                                 "osrfMethodException",
3357                                                 ctx->request,
3358                                                 "Severe query error in HAVING predicate -- see error log for more details"
3359                                         );
3360                                 }
3361                                 free(core_class);
3362                                 buffer_free(group_buf);
3363                                 buffer_free(sql_buf);
3364                                 if (defaultselhash) jsonObjectFree(defaultselhash);
3365                                 return NULL;
3366                         }
3367                 }
3368
3369                 growing_buffer* order_buf = NULL;  // to collect ORDER BY list
3370
3371                 // Build an ORDER BY clause, if there is one
3372                 if( NULL == order_hash )
3373                         ;  // No ORDER BY? do nothing
3374                 else if( JSON_ARRAY == order_hash->type ) {
3375                         // Array of field specifications, each specification being a
3376                         // hash to define the class, field, and other details
3377                         int order_idx = 0;
3378                         jsonObject* order_spec;
3379                         while( (order_spec = jsonObjectGetIndex( order_hash, order_idx++ ) ) ) {
3380
3381                                 if( JSON_HASH != order_spec->type ) {
3382                                         osrfLogError(OSRF_LOG_MARK,
3383                                                  "%s: Malformed field specification in ORDER BY clause; expected JSON_HASH, found %s",
3384                                                 MODULENAME, json_type( order_spec->type ) );
3385                                         if( ctx )
3386                                                 osrfAppSessionStatus(
3387                                                          ctx->session,
3388                                                         OSRF_STATUS_INTERNALSERVERERROR,
3389                                                         "osrfMethodException",
3390                                                         ctx->request,
3391                                                         "Malformed ORDER BY clause -- see error log for more details"
3392                                                 );
3393                                         buffer_free( order_buf );
3394                                         free(core_class);
3395                                         free(having_buf);
3396                                         buffer_free(group_buf);
3397                                         buffer_free(sql_buf);
3398                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3399                                         return NULL;
3400                                 }
3401
3402                                 const char* class =
3403                                                 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "class" ) );
3404                                 const char* field =
3405                                                 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "field" ) );
3406
3407                                 if ( order_buf )
3408                                         OSRF_BUFFER_ADD(order_buf, ", ");
3409                                 else
3410                                         order_buf = buffer_init(128);
3411
3412                                 if( !field || !class ) {
3413                                         osrfLogError(OSRF_LOG_MARK,
3414                                                 "%s: Missing class or field name in field specification of ORDER BY clause",
3415                                                  MODULENAME );
3416                                         if( ctx )
3417                                                 osrfAppSessionStatus(
3418                                                         ctx->session,
3419                                                         OSRF_STATUS_INTERNALSERVERERROR,
3420                                                         "osrfMethodException",
3421                                                         ctx->request,
3422                                                         "Malformed ORDER BY clause -- see error log for more details"
3423                                                 );
3424                                         buffer_free( order_buf );
3425                                         free(core_class);
3426                                         free(having_buf);
3427                                         buffer_free(group_buf);
3428                                         buffer_free(sql_buf);
3429                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3430                                         return NULL;
3431                                 }
3432
3433                                 if (    ! jsonObjectGetKeyConst( selhash, class )
3434                                          && strcmp( core_class, class )
3435                                          && ! is_joined( join_hash, class ) ) {
3436                                         osrfLogError(OSRF_LOG_MARK, "%s: ORDER BY clause references class \"%s\" "
3437                                                         "not in FROM clause", MODULENAME, class );
3438                                         if( ctx )
3439                                                 osrfAppSessionStatus(
3440                                                         ctx->session,
3441                                                         OSRF_STATUS_INTERNALSERVERERROR,
3442                                                         "osrfMethodException",
3443                                                         ctx->request,
3444                                                         "Invalid class referenced in ORDER BY clause -- see error log for more details"
3445                                                 );
3446                                         free(core_class);
3447                                         free(having_buf);
3448                                         buffer_free(group_buf);
3449                                         buffer_free(sql_buf);
3450                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3451                                         return NULL;
3452                                 }
3453
3454                                 osrfHash* field_def = oilsIDLFindPath( "/%s/fields/%s", class, field );
3455                                 if( !field_def ) {
3456                                         osrfLogError(OSRF_LOG_MARK, "%s: Invalid field \"%s\".%s referenced in ORDER BY clause",
3457                                                  MODULENAME, class, field );
3458                                         if( ctx )
3459                                                 osrfAppSessionStatus(
3460                                                         ctx->session,
3461                                                         OSRF_STATUS_INTERNALSERVERERROR,
3462                                                         "osrfMethodException",
3463                                                         ctx->request,
3464                                                         "Invalid field referenced in ORDER BY clause -- see error log for more details"
3465                                                 );
3466                                         free(core_class);
3467                                         free(having_buf);
3468                                         buffer_free(group_buf);
3469                                         buffer_free(sql_buf);
3470                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3471                                         return NULL;
3472                                 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3473                                         osrfLogError(OSRF_LOG_MARK, "%s: Virtual field \"%s\" in ORDER BY clause",
3474                                                                  MODULENAME, field );
3475                                         if( ctx )
3476                                                 osrfAppSessionStatus(
3477                                                         ctx->session,
3478                                                         OSRF_STATUS_INTERNALSERVERERROR,
3479                                                         "osrfMethodException",
3480                                                         ctx->request,
3481                                                         "Virtual field in ORDER BY clause -- see error log for more details"
3482                                                 );
3483                                         buffer_free( order_buf );
3484                                         free(core_class);
3485                                         free(having_buf);
3486                                         buffer_free(group_buf);
3487                                         buffer_free(sql_buf);
3488                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3489                                         return NULL;
3490                                 }
3491
3492                                 if( jsonObjectGetKeyConst( order_spec, "transform" ) ) {
3493                                         char* transform_str = searchFieldTransform( class, field_def, order_spec );
3494                                         if( ! transform_str ) {
3495                                                 if( ctx )
3496                                                         osrfAppSessionStatus(
3497                                                                 ctx->session,
3498                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3499                                                                 "osrfMethodException",
3500                                                                 ctx->request,
3501                                                                 "Severe query error in ORDER BY clause -- see error log for more details"
3502                                                         );
3503                                                 buffer_free( order_buf );
3504                                                 free(core_class);
3505                                                 free(having_buf);
3506                                                 buffer_free(group_buf);
3507                                                 buffer_free(sql_buf);
3508                                                 if (defaultselhash) jsonObjectFree(defaultselhash);
3509                                                 return NULL;
3510                                         }
3511                                         
3512                                         OSRF_BUFFER_ADD( order_buf, transform_str );
3513                                         free( transform_str );
3514                                 }
3515                                 else
3516                                         buffer_fadd( order_buf, "\"%s\".%s", class, field );
3517
3518                                 const char* direction =
3519                                                 jsonObjectGetString( jsonObjectGetKeyConst( order_spec, "direction" ) );
3520                                 if( direction ) {
3521                                         if( direction[ 0 ] || 'D' == direction[ 0 ] )
3522                                                 OSRF_BUFFER_ADD( order_buf, " DESC" );
3523                                         else
3524                                                 OSRF_BUFFER_ADD( order_buf, " ASC" );
3525                                 }
3526                         }
3527                 } else if( JSON_HASH == order_hash->type ) {
3528                         // This hash is keyed on class name.  Each class has either
3529                         // an array of field names or a hash keyed on field name.
3530                         jsonIterator* class_itr = jsonNewIterator( order_hash );
3531                         while ( (snode = jsonIteratorNext( class_itr )) ) {
3532
3533                                 if (   ! jsonObjectGetKeyConst( selhash,class_itr->key )
3534                                         && strcmp( core_class, class_itr->key )
3535                                         && ! is_joined( join_hash, class_itr->key ) ) {
3536                                         osrfLogError(OSRF_LOG_MARK, "%s: Invalid class \"%s\" referenced in ORDER BY clause",
3537                                                                  MODULENAME, class_itr->key );
3538                                         if( ctx )
3539                                                 osrfAppSessionStatus(
3540                                                         ctx->session,
3541                                                         OSRF_STATUS_INTERNALSERVERERROR,
3542                                                         "osrfMethodException",
3543                                                         ctx->request,
3544                                                         "Invalid class referenced in ORDER BY clause -- see error log for more details"
3545                                                 );
3546                                         jsonIteratorFree( class_itr );
3547                                         buffer_free( order_buf );
3548                                         free(core_class);
3549                                         free(having_buf);
3550                                         buffer_free(group_buf);
3551                                         buffer_free(sql_buf);
3552                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3553                                         return NULL;
3554                                 }
3555
3556                                 osrfHash* field_list_def = oilsIDLFindPath( "/%s/fields", class_itr->key );
3557
3558                                 if ( snode->type == JSON_HASH ) {
3559
3560                                         // Hash is keyed on field names from the current class.  For each field
3561                                         // there is another layer of hash to define the sorting details, if any,
3562                                         // or a string to indicate direction of sorting.
3563                                         jsonIterator* order_itr = jsonNewIterator( snode );
3564                                         while ( (onode = jsonIteratorNext( order_itr )) ) {
3565
3566                                                 osrfHash* field_def = osrfHashGet( field_list_def, order_itr->key );
3567                                                 if( !field_def ) {
3568                                                         osrfLogError(OSRF_LOG_MARK, "%s: Invalid field \"%s\" in ORDER BY clause",
3569                                                                         MODULENAME, order_itr->key );
3570                                                         if( ctx )
3571                                                                 osrfAppSessionStatus(
3572                                                                         ctx->session,
3573                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3574                                                                         "osrfMethodException",
3575                                                                         ctx->request,
3576                                                                         "Invalid field in ORDER BY clause -- see error log for more details"
3577                                                                 );
3578                                                         jsonIteratorFree( order_itr );
3579                                                         jsonIteratorFree( class_itr );
3580                                                         buffer_free( order_buf );
3581                                                         free(core_class);
3582                                                         free(having_buf);
3583                                                         buffer_free(group_buf);
3584                                                         buffer_free(sql_buf);
3585                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3586                                                         return NULL;
3587                                                 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3588                                                         osrfLogError(OSRF_LOG_MARK, "%s: Virtual field \"%s\" in ORDER BY clause",
3589                                                                  MODULENAME, order_itr->key );
3590                                                         if( ctx )
3591                                                                 osrfAppSessionStatus(
3592                                                                         ctx->session,
3593                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3594                                                                         "osrfMethodException",
3595                                                                         ctx->request,
3596                                                                         "Virtual field in ORDER BY clause -- see error log for more details"
3597                                                         );
3598                                                         jsonIteratorFree( order_itr );
3599                                                         jsonIteratorFree( class_itr );
3600                                                         buffer_free( order_buf );
3601                                                         free(core_class);
3602                                                         free(having_buf);
3603                                                         buffer_free(group_buf);
3604                                                         buffer_free(sql_buf);
3605                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3606                                                         return NULL;
3607                                                 }
3608
3609                                                 const char* direction = NULL;
3610                                                 if ( onode->type == JSON_HASH ) {
3611                                                         if ( jsonObjectGetKeyConst( onode, "transform" ) ) {
3612                                                                 string = searchFieldTransform(
3613                                                                         class_itr->key,
3614                                                                         osrfHashGet( field_list_def, order_itr->key ),
3615                                                                         onode
3616                                                                 );
3617                                                                 if( ! string ) {
3618                                                                         if( ctx ) osrfAppSessionStatus(
3619                                                                                 ctx->session,
3620                                                                                 OSRF_STATUS_INTERNALSERVERERROR,
3621                                                                                 "osrfMethodException",
3622                                                                                 ctx->request,
3623                                                                                 "Severe query error in ORDER BY clause -- see error log for more details"
3624                                                                         );
3625                                                                         jsonIteratorFree( order_itr );
3626                                                                         jsonIteratorFree( class_itr );
3627                                                                         free(core_class);
3628                                                                         free(having_buf);
3629                                                                         buffer_free(group_buf);
3630                                                                         buffer_free(order_buf);
3631                                                                         buffer_free(sql_buf);
3632                                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3633                                                                         return NULL;
3634                                                                 }
3635                                                         } else {
3636                                                                 growing_buffer* field_buf = buffer_init(16);
3637                                                                 buffer_fadd(field_buf, "\"%s\".%s", class_itr->key, order_itr->key);
3638                                                                 string = buffer_release(field_buf);
3639                                                         }
3640
3641                                                         if ( (tmp_const = jsonObjectGetKeyConst( onode, "direction" )) ) {
3642                                                                 const char* dir = jsonObjectGetString(tmp_const);
3643                                                                 if (!strncasecmp(dir, "d", 1)) {
3644                                                                         direction = " DESC";
3645                                                                 } else {
3646                                                                         direction = " ASC";
3647                                                                 }
3648                                                         }
3649
3650                                                 } else if ( JSON_NULL == onode->type || JSON_ARRAY == onode->type ) {
3651                                                         osrfLogError( OSRF_LOG_MARK,
3652                                                                 "%s: Expected JSON_STRING in ORDER BY clause; found %s",
3653                                                                 MODULENAME, json_type( onode->type ) );
3654                                                         if( ctx )
3655                                                                 osrfAppSessionStatus(
3656                                                                         ctx->session,
3657                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3658                                                                         "osrfMethodException",
3659                                                                         ctx->request,
3660                                                                         "Malformed ORDER BY clause -- see error log for more details"
3661                                                                 );
3662                                                         jsonIteratorFree( order_itr );
3663                                                         jsonIteratorFree( class_itr );
3664                                                         free(core_class);
3665                                                         free(having_buf);
3666                                                         buffer_free(group_buf);
3667                                                         buffer_free(order_buf);
3668                                                         buffer_free(sql_buf);
3669                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3670                                                         return NULL;
3671
3672                                                 } else {
3673                                                         string = strdup(order_itr->key);
3674                                                         const char* dir = jsonObjectGetString(onode);
3675                                                         if (!strncasecmp(dir, "d", 1)) {
3676                                                                 direction = " DESC";
3677                                                         } else {
3678                                                                 direction = " ASC";
3679                                                         }
3680                                                 }
3681
3682                                                 if ( order_buf )
3683                                                         OSRF_BUFFER_ADD(order_buf, ", ");
3684                                                 else
3685                                                         order_buf = buffer_init(128);
3686
3687                                                 OSRF_BUFFER_ADD(order_buf, string);
3688                                                 free(string);
3689
3690                                                 if (direction) {
3691                                                          OSRF_BUFFER_ADD(order_buf, direction);
3692                                                 }
3693
3694                                         } // end while
3695                     jsonIteratorFree(order_itr);
3696
3697                                 } else if ( snode->type == JSON_ARRAY ) {
3698
3699                                         // Array is a list of fields from the current class
3700                                         jsonIterator* order_itr = jsonNewIterator( snode );
3701                                         while ( (onode = jsonIteratorNext( order_itr )) ) {
3702
3703                                                 const char* _f = jsonObjectGetString( onode );
3704
3705                                                 osrfHash* field_def = osrfHashGet( field_list_def, _f );
3706                                                 if( !field_def ) {
3707                                                         osrfLogError(OSRF_LOG_MARK, "%s: Invalid field \"%s\" in ORDER BY clause",
3708                                                                         MODULENAME, _f );
3709                                                         if( ctx )
3710                                                                 osrfAppSessionStatus(
3711                                                                         ctx->session,
3712                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3713                                                                         "osrfMethodException",
3714                                                                         ctx->request,
3715                                                                         "Invalid field in ORDER BY clause -- see error log for more details"
3716                                                                 );
3717                                                         jsonIteratorFree( order_itr );
3718                                                         jsonIteratorFree( class_itr );
3719                                                         buffer_free( order_buf );
3720                                                         free(core_class);
3721                                                         free(having_buf);
3722                                                         buffer_free(group_buf);
3723                                                         buffer_free(sql_buf);
3724                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3725                                                         return NULL;
3726                                                 } else if( str_is_true( osrfHashGet( field_def, "virtual" ) ) ) {
3727                                                         osrfLogError(OSRF_LOG_MARK, "%s: Virtual field \"%s\" in ORDER BY clause",
3728                                                                         MODULENAME, _f );
3729                                                         if( ctx )
3730                                                                 osrfAppSessionStatus(
3731                                                                         ctx->session,
3732                                                                         OSRF_STATUS_INTERNALSERVERERROR,
3733                                                                         "osrfMethodException",
3734                                                                         ctx->request,
3735                                                                         "Virtual field in ORDER BY clause -- see error log for more details"
3736                                                                 );
3737                                                         jsonIteratorFree( order_itr );
3738                                                         jsonIteratorFree( class_itr );
3739                                                         buffer_free( order_buf );
3740                                                         free(core_class);
3741                                                         free(having_buf);
3742                                                         buffer_free(group_buf);
3743                                                         buffer_free(sql_buf);
3744                                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3745                                                         return NULL;
3746                                                 }
3747
3748                                                 if ( order_buf )
3749                                                         OSRF_BUFFER_ADD(order_buf, ", ");
3750                                                 else
3751                                                         order_buf = buffer_init(128);
3752
3753                                                 buffer_fadd( order_buf, "\"%s\".%s", class_itr->key, _f);
3754
3755                                         } // end while
3756                                 // jsonIteratorFree(order_itr);
3757         
3758         
3759                                 // IT'S THE OOOOOOOOOOOLD STYLE!
3760                                 } else {
3761                                         osrfLogError(OSRF_LOG_MARK, 
3762                                                         "%s: Possible SQL injection attempt; direct order by is not allowed", MODULENAME);
3763                                         if (ctx) {
3764                                                 osrfAppSessionStatus(
3765                                                         ctx->session,
3766                                                         OSRF_STATUS_INTERNALSERVERERROR,
3767                                                         "osrfMethodException",
3768                                                         ctx->request,
3769                                                         "Severe query error -- see error log for more details"
3770                                                 );
3771                                         }
3772
3773                                         free(core_class);
3774                                         free(having_buf);
3775                                         buffer_free(group_buf);
3776                                         buffer_free(order_buf);
3777                                         buffer_free(sql_buf);
3778                                         if (defaultselhash) jsonObjectFree(defaultselhash);
3779                                         jsonIteratorFree(class_itr);
3780                                         return NULL;
3781                                 }
3782                         } // end while
3783                 } else {
3784                         osrfLogError(OSRF_LOG_MARK,
3785                                 "%s: Malformed ORDER BY clause; expected JSON_HASH or JSON_ARRAY, found %s",
3786                                 MODULENAME, json_type( order_hash->type ) );
3787                         if( ctx )
3788                                 osrfAppSessionStatus(
3789                                         ctx->session,
3790                                         OSRF_STATUS_INTERNALSERVERERROR,
3791                                         "osrfMethodException",
3792                                         ctx->request,
3793                                         "Malformed ORDER BY clause -- see error log for more details"
3794                                 );
3795                         buffer_free( order_buf );
3796                         free(core_class);
3797                         free(having_buf);
3798                         buffer_free(group_buf);
3799                         buffer_free(sql_buf);
3800                         if (defaultselhash) jsonObjectFree(defaultselhash);
3801                         return NULL;
3802                 }
3803                 // jsonIteratorFree(class_itr);
3804
3805                 if( order_buf )
3806                         order_by_list = buffer_release( order_buf );
3807         }
3808
3809
3810         string = buffer_release(group_buf);
3811
3812         if ( *string && ( aggregate_found || (flags & SELECT_DISTINCT) ) ) {
3813                 OSRF_BUFFER_ADD( sql_buf, " GROUP BY " );
3814                 OSRF_BUFFER_ADD( sql_buf, string );
3815         }
3816
3817         free(string);
3818
3819         if( having_buf && *having_buf ) {
3820                 OSRF_BUFFER_ADD( sql_buf, " HAVING " );
3821                 OSRF_BUFFER_ADD( sql_buf, having_buf );
3822                 free( having_buf );
3823         }
3824
3825         if( order_by_list ) {
3826
3827                 if ( *order_by_list ) {
3828                         OSRF_BUFFER_ADD( sql_buf, " ORDER BY " );
3829                         OSRF_BUFFER_ADD( sql_buf, order_by_list );
3830                 }
3831
3832                 free( order_by_list );
3833         }
3834
3835         if ( limit ){
3836                 const char* str = jsonObjectGetString(limit);
3837                 buffer_fadd( sql_buf, " LIMIT %d", atoi(str) );
3838         }
3839
3840         if (offset) {
3841                 const char* str = jsonObjectGetString(offset);
3842                 buffer_fadd( sql_buf, " OFFSET %d", atoi(str) );
3843         }
3844
3845         if (!(flags & SUBSELECT)) OSRF_BUFFER_ADD_CHAR(sql_buf, ';');
3846
3847         free(core_class);
3848         if (defaultselhash) jsonObjectFree(defaultselhash);
3849
3850         return buffer_release(sql_buf);
3851
3852 }
3853
3854 static char* buildSELECT ( jsonObject* search_hash, jsonObject* order_hash, osrfHash* meta, osrfMethodContext* ctx ) {
3855
3856         const char* locale = osrf_message_get_last_locale();
3857
3858         osrfHash* fields = osrfHashGet(meta, "fields");
3859         char* core_class = osrfHashGet(meta, "classname");
3860
3861         const jsonObject* join_hash = jsonObjectGetKeyConst( order_hash, "join" );
3862
3863         jsonObject* node = NULL;
3864         jsonObject* snode = NULL;
3865         jsonObject* onode = NULL;
3866         const jsonObject* _tmp = NULL;
3867         jsonObject* selhash = NULL;
3868         jsonObject* defaultselhash = NULL;
3869
3870         growing_buffer* sql_buf = buffer_init(128);
3871         growing_buffer* select_buf = buffer_init(128);
3872
3873         if ( !(selhash = jsonObjectGetKey( order_hash, "select" )) ) {
3874                 defaultselhash = jsonNewObjectType(JSON_HASH);
3875                 selhash = defaultselhash;
3876         }
3877         
3878         if ( !jsonObjectGetKeyConst(selhash,core_class) ) {
3879                 jsonObjectSetKey( selhash, core_class, jsonNewObjectType(JSON_ARRAY) );
3880                 jsonObject* flist = jsonObjectGetKey( selhash, core_class );
3881                 
3882                 int i = 0;
3883                 char* field;
3884
3885                 osrfStringArray* keys = osrfHashKeys( fields );
3886                 while ( (field = osrfStringArrayGetString(keys, i++)) ) {
3887                         if( ! str_is_true( osrfHashGet( osrfHashGet( fields, field ), "virtual" ) ) )
3888                                 jsonObjectPush( flist, jsonNewObject( field ) );
3889                 }
3890                 osrfStringArrayFree(keys);
3891         }
3892
3893         int first = 1;
3894         jsonIterator* class_itr = jsonNewIterator( selhash );
3895         while ( (snode = jsonIteratorNext( class_itr )) ) {
3896
3897                 char* cname = class_itr->key;
3898                 osrfHash* idlClass = osrfHashGet( oilsIDL(), cname );
3899                 if (!idlClass) continue;
3900
3901                 if (strcmp(core_class,class_itr->key)) {
3902                         if (!join_hash) continue;
3903
3904                         jsonObject* found =  jsonObjectFindPath(join_hash, "//%s", class_itr->key);
3905                         if (!found->size) {
3906                                 jsonObjectFree(found);
3907                                 continue;
3908                         }
3909
3910                         jsonObjectFree(found);
3911                 }
3912
3913                 jsonIterator* select_itr = jsonNewIterator( snode );
3914                 while ( (node = jsonIteratorNext( select_itr )) ) {
3915                         const char* item_str = jsonObjectGetString( node );
3916                         osrfHash* field = osrfHashGet( osrfHashGet( idlClass, "fields" ), item_str );
3917                         char* fname = osrfHashGet(field, "name");
3918
3919                         if (!field) continue;
3920
3921                         if (first) {
3922                                 first = 0;
3923                         } else {
3924                                 OSRF_BUFFER_ADD_CHAR(select_buf, ',');
3925                         }
3926
3927             if (locale) {
3928                         const char* i18n;
3929                                 const jsonObject* no_i18n_obj = jsonObjectGetKey( order_hash, "no_i18n" );
3930                                 if ( obj_is_true( no_i18n_obj ) )    // Suppress internationalization?
3931                                         i18n = NULL;
3932                                 else
3933                                         i18n = osrfHashGet(field, "i18n");
3934
3935                                 if( str_is_true( i18n ) ) {
3936                         char* pkey = osrfHashGet(idlClass, "primarykey");
3937                         char* tname = osrfHashGet(idlClass, "tablename");
3938
3939                     buffer_fadd(select_buf, " oils_i18n_xlate('%s', '%s', '%s', '%s', \"%s\".%s::TEXT, '%s') AS \"%s\"", tname, cname, fname, pkey, cname, pkey, locale, fname);
3940                 } else {
3941                                 buffer_fadd(select_buf, " \"%s\".%s", cname, fname);
3942                 }
3943             } else {
3944                             buffer_fadd(select_buf, " \"%s\".%s", cname, fname);
3945             }
3946                 }
3947
3948         jsonIteratorFree(select_itr);
3949         }
3950
3951     jsonIteratorFree(class_itr);
3952
3953         char* col_list = buffer_release(select_buf);
3954         char* table = getSourceDefinition(meta);
3955         if( !table )
3956                 table = strdup( "(null)" );
3957
3958         buffer_fadd(sql_buf, "SELECT %s FROM %s AS \"%s\"", col_list, table, core_class );
3959         free(col_list);
3960         free(table);
3961
3962         if ( join_hash ) {
3963                 char* join_clause = searchJOIN( join_hash, meta );
3964                 OSRF_BUFFER_ADD_CHAR(sql_buf, ' ');
3965                 OSRF_BUFFER_ADD(sql_buf, join_clause);
3966                 free(join_clause);
3967         }
3968
3969         osrfLogDebug(OSRF_LOG_MARK, "%s pre-predicate SQL =  %s",
3970                                  MODULENAME, OSRF_BUFFER_C_STR(sql_buf));
3971
3972         OSRF_BUFFER_ADD(sql_buf, " WHERE ");
3973
3974         char* pred = searchWHERE( search_hash, meta, AND_OP_JOIN, ctx );
3975         if (!pred) {
3976                 osrfAppSessionStatus(
3977                         ctx->session,
3978                         OSRF_STATUS_INTERNALSERVERERROR,
3979                                 "osrfMethodException",
3980                                 ctx->request,
3981                                 "Severe query error -- see error log for more details"
3982                         );
3983                 buffer_free(sql_buf);
3984                 if(defaultselhash) jsonObjectFree(defaultselhash);
3985                 return NULL;
3986         } else {
3987                 buffer_add(sql_buf, pred);
3988                 free(pred);
3989         }
3990
3991         if (order_hash) {
3992                 char* string = NULL;
3993                 if ( (_tmp = jsonObjectGetKeyConst( order_hash, "order_by" )) ){
3994
3995                         growing_buffer* order_buf = buffer_init(128);
3996
3997                         first = 1;
3998                         jsonIterator* class_itr = jsonNewIterator( _tmp );
3999                         while ( (snode = jsonIteratorNext( class_itr )) ) {
4000
4001                                 if (!jsonObjectGetKeyConst(selhash,class_itr->key))
4002                                         continue;
4003
4004                                 if ( snode->type == JSON_HASH ) {
4005
4006                                         jsonIterator* order_itr = jsonNewIterator( snode );
4007                                         while ( (onode = jsonIteratorNext( order_itr )) ) {
4008
4009                                                 osrfHash* field_def = oilsIDLFindPath( "/%s/fields/%s",
4010                                                                 class_itr->key, order_itr->key );
4011                                                 if ( !field_def )
4012                                                         continue;
4013
4014                                                 char* direction = NULL;
4015                                                 if ( onode->type == JSON_HASH ) {
4016                                                         if ( jsonObjectGetKeyConst( onode, "transform" ) ) {
4017                                                                 string = searchFieldTransform( class_itr->key, field_def, onode );
4018                                                                 if( ! string ) {
4019                                                                         osrfAppSessionStatus(
4020                                                                                 ctx->session,
4021                                                                                 OSRF_STATUS_INTERNALSERVERERROR,
4022                                                                                 "osrfMethodException",
4023                                                                                 ctx->request,
4024                                                                                 "Severe query error in ORDER BY clause -- see error log for more details"
4025                                                                         );
4026                                                                         jsonIteratorFree( order_itr );
4027                                                                         jsonIteratorFree( class_itr );
4028                                                                         buffer_free( order_buf );
4029                                                                         buffer_free( sql_buf );
4030                                                                         if( defaultselhash ) jsonObjectFree( defaultselhash );
4031                                                                         return NULL;
4032                                                                 }
4033                                                         } else {
4034                                                                 growing_buffer* field_buf = buffer_init(16);
4035                                                                 buffer_fadd(field_buf, "\"%s\".%s", class_itr->key, order_itr->key);
4036                                                                 string = buffer_release(field_buf);
4037                                                         }
4038
4039                                                         if ( (_tmp = jsonObjectGetKeyConst( onode, "direction" )) ) {
4040                                                                 const char* dir = jsonObjectGetString(_tmp);
4041                                                                 if (!strncasecmp(dir, "d", 1)) {
4042                                                                         direction = " DESC";
4043                                                                 } else {
4044                                                                         free(direction);
4045                                                                 }
4046                                                         }
4047
4048                                                 } else {
4049                                                         string = strdup(order_itr->key);
4050                                                         const char* dir = jsonObjectGetString(onode);
4051                                                         if (!strncasecmp(dir, "d", 1)) {
4052                                                                 direction = " DESC";
4053                                                         } else {
4054                                                                 direction = " ASC";
4055                                                         }
4056                                                 }
4057
4058                                                 if (first) {
4059                                                         first = 0;
4060                                                 } else {
4061                                                         buffer_add(order_buf, ", ");
4062                                                 }
4063
4064                                                 buffer_add(order_buf, string);
4065                                                 free(string);
4066
4067                                                 if (direction) {
4068                                                         buffer_add(order_buf, direction);
4069                                                 }
4070
4071                                         }
4072
4073                     jsonIteratorFree(order_itr);
4074
4075                                 } else {
4076                                         const char* str = jsonObjectGetString(snode);
4077                                         buffer_add(order_buf, str);
4078                                         break;
4079                                 }
4080
4081                         }
4082
4083                         jsonIteratorFree(class_itr);
4084
4085                         string = buffer_release(order_buf);
4086
4087                         if ( *string ) {
4088                                 OSRF_BUFFER_ADD( sql_buf, " ORDER BY " );
4089                                 OSRF_BUFFER_ADD( sql_buf, string );
4090                         }
4091
4092                         free(string);
4093                 }
4094
4095                 if ( (_tmp = jsonObjectGetKeyConst( order_hash, "limit" )) ){
4096                         const char* str = jsonObjectGetString(_tmp);
4097                         buffer_fadd(
4098                                 sql_buf,
4099                                 " LIMIT %d",
4100                                 atoi(str)
4101                         );
4102                 }
4103
4104                 _tmp = jsonObjectGetKeyConst( order_hash, "offset" );
4105                 if (_tmp) {
4106                         const char* str = jsonObjectGetString(_tmp);
4107                         buffer_fadd(
4108                                 sql_buf,
4109                                 " OFFSET %d",
4110                                 atoi(str)
4111                         );
4112                 }
4113         }
4114
4115         if (defaultselhash) jsonObjectFree(defaultselhash);
4116
4117         OSRF_BUFFER_ADD_CHAR(sql_buf, ';');
4118         return buffer_release(sql_buf);
4119 }
4120
4121 int doJSONSearch ( osrfMethodContext* ctx ) {
4122         if(osrfMethodVerifyContext( ctx )) {
4123                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
4124                 return -1;
4125         }
4126
4127         osrfLogDebug(OSRF_LOG_MARK, "Recieved query request");
4128
4129         int err = 0;
4130
4131         // XXX for now...
4132         dbhandle = writehandle;
4133
4134         jsonObject* hash = jsonObjectGetIndex(ctx->params, 0);
4135
4136         int flags = 0;
4137
4138         if ( obj_is_true( jsonObjectGetKey( hash, "distinct" ) ) )
4139                 flags |= SELECT_DISTINCT;
4140
4141         if ( obj_is_true( jsonObjectGetKey( hash, "no_i18n" ) ) )
4142                 flags |= DISABLE_I18N;
4143
4144         osrfLogDebug(OSRF_LOG_MARK, "Building SQL ...");
4145         char* sql = SELECT(
4146                         ctx,
4147                         jsonObjectGetKey( hash, "select" ),
4148                         jsonObjectGetKey( hash, "from" ),
4149                         jsonObjectGetKey( hash, "where" ),
4150                         jsonObjectGetKey( hash, "having" ),
4151                         jsonObjectGetKey( hash, "order_by" ),
4152                         jsonObjectGetKey( hash, "limit" ),
4153                         jsonObjectGetKey( hash, "offset" ),
4154                         flags
4155         );
4156
4157         if (!sql) {
4158                 err = -1;
4159                 return err;
4160         }
4161         
4162         osrfLogDebug(OSRF_LOG_MARK, "%s SQL =  %s", MODULENAME, sql);
4163         dbi_result result = dbi_conn_query(dbhandle, sql);
4164
4165         if(result) {
4166                 osrfLogDebug(OSRF_LOG_MARK, "Query returned with no errors");
4167
4168                 if (dbi_result_first_row(result)) {
4169                         /* JSONify the result */
4170                         osrfLogDebug(OSRF_LOG_MARK, "Query returned at least one row");
4171
4172                         do {
4173                                 jsonObject* return_val = oilsMakeJSONFromResult( result );
4174                                 osrfAppRespond( ctx, return_val );
4175                 jsonObjectFree( return_val );
4176                         } while (dbi_result_next_row(result));
4177
4178                 } else {
4179                         osrfLogDebug(OSRF_LOG_MARK, "%s returned no results for query %s", MODULENAME, sql);
4180                 }
4181
4182                 osrfAppRespondComplete( ctx, NULL );
4183
4184                 /* clean up the query */
4185                 dbi_result_free(result); 
4186
4187         } else {
4188                 err = -1;
4189                 osrfLogError(OSRF_LOG_MARK, "%s: Error with query [%s]", MODULENAME, sql);
4190                 osrfAppSessionStatus(
4191                         ctx->session,
4192                         OSRF_STATUS_INTERNALSERVERERROR,
4193                         "osrfMethodException",
4194                         ctx->request,
4195                         "Severe query error -- see error log for more details"
4196                 );
4197         }
4198
4199         free(sql);
4200         return err;
4201 }
4202
4203 static jsonObject* doFieldmapperSearch ( osrfMethodContext* ctx, osrfHash* meta,
4204                 const jsonObject* params, int* err ) {
4205
4206         // XXX for now...
4207         dbhandle = writehandle;
4208
4209         osrfHash* links = osrfHashGet(meta, "links");
4210         osrfHash* fields = osrfHashGet(meta, "fields");
4211         char* core_class = osrfHashGet(meta, "classname");
4212         char* pkey = osrfHashGet(meta, "primarykey");
4213
4214         const jsonObject* _tmp;
4215         jsonObject* obj;
4216         jsonObject* search_hash = jsonObjectGetIndex(params, 0);
4217         jsonObject* order_hash = jsonObjectGetIndex(params, 1);
4218
4219         char* sql = buildSELECT( search_hash, order_hash, meta, ctx );
4220         if (!sql) {
4221                 osrfLogDebug(OSRF_LOG_MARK, "Problem building query, returning NULL");
4222                 *err = -1;
4223                 return NULL;
4224         }
4225         
4226         osrfLogDebug(OSRF_LOG_MARK, "%s SQL =  %s", MODULENAME, sql);
4227
4228         dbi_result result = dbi_conn_query(dbhandle, sql);
4229         if( NULL == result ) {
4230                 osrfLogError(OSRF_LOG_MARK, "%s: Error retrieving %s with query [%s]",
4231                         MODULENAME, osrfHashGet(meta, "fieldmapper"), sql);
4232                 osrfAppSessionStatus(
4233                         ctx->session,
4234                         OSRF_STATUS_INTERNALSERVERERROR,
4235                         "osrfMethodException",
4236                         ctx->request,
4237                         "Severe query error -- see error log for more details"
4238                 );
4239                 *err = -1;
4240                 free(sql);
4241                 return jsonNULL;
4242
4243         } else {
4244                 osrfLogDebug(OSRF_LOG_MARK, "Query returned with no errors");
4245         }
4246
4247         jsonObject* res_list = jsonNewObjectType(JSON_ARRAY);
4248         osrfHash* dedup = osrfNewHash();
4249
4250         if (dbi_result_first_row(result)) {
4251                 /* JSONify the result */
4252                 osrfLogDebug(OSRF_LOG_MARK, "Query returned at least one row");
4253                 do {
4254                         obj = oilsMakeFieldmapperFromResult( result, meta );
4255                         char* pkey_val = oilsFMGetString( obj, pkey );
4256                         if ( osrfHashGet( dedup, pkey_val ) ) {
4257                                 jsonObjectFree(obj);
4258                                 free(pkey_val);
4259                         } else {
4260                                 osrfHashSet( dedup, pkey_val, pkey_val );
4261                                 jsonObjectPush(res_list, obj);
4262                         }
4263                 } while (dbi_result_next_row(result));
4264         } else {
4265                 osrfLogDebug(OSRF_LOG_MARK, "%s returned no results for query %s",
4266                         MODULENAME, sql );
4267         }
4268
4269         osrfHashFree(dedup);
4270         /* clean up the query */
4271         dbi_result_free(result);
4272         free(sql);
4273
4274         if (res_list->size && order_hash) {
4275                 _tmp = jsonObjectGetKeyConst( order_hash, "flesh" );
4276                 if (_tmp) {
4277                         int x = (int)jsonObjectGetNumber(_tmp);
4278                         if (x == -1 || x > max_flesh_depth) x = max_flesh_depth;
4279
4280                         const jsonObject* temp_blob;
4281                         if ((temp_blob = jsonObjectGetKeyConst( order_hash, "flesh_fields" )) && x > 0) {
4282
4283                                 jsonObject* flesh_blob = jsonObjectClone( temp_blob );
4284                                 const jsonObject* flesh_fields = jsonObjectGetKeyConst( flesh_blob, core_class );
4285
4286                                 osrfStringArray* link_fields = NULL;
4287
4288                                 if (flesh_fields) {
4289                                         if (flesh_fields->size == 1) {
4290                                                 const char* _t = jsonObjectGetString( jsonObjectGetIndex( flesh_fields, 0 ) );
4291                                                 if (!strcmp(_t,"*")) link_fields = osrfHashKeys( links );
4292                                         }
4293
4294                                         if (!link_fields) {
4295                                                 jsonObject* _f;
4296                                                 link_fields = osrfNewStringArray(1);
4297                                                 jsonIterator* _i = jsonNewIterator( flesh_fields );
4298                                                 while ((_f = jsonIteratorNext( _i ))) {
4299                                                         osrfStringArrayAdd( link_fields, jsonObjectGetString( _f ) );
4300                                                 }
4301                         jsonIteratorFree(_i);
4302                                         }
4303                                 }
4304
4305                                 jsonObject* cur;
4306                                 jsonIterator* itr = jsonNewIterator( res_list );
4307                                 while ((cur = jsonIteratorNext( itr ))) {
4308
4309                                         int i = 0;
4310                                         char* link_field;
4311                                         
4312                                         while ( (link_field = osrfStringArrayGetString(link_fields, i++)) ) {
4313
4314                                                 osrfLogDebug(OSRF_LOG_MARK, "Starting to flesh %s", link_field);
4315
4316                                                 osrfHash* kid_link = osrfHashGet(links, link_field);
4317                                                 if (!kid_link) continue;
4318
4319                                                 osrfHash* field = osrfHashGet(fields, link_field);
4320                                                 if (!field) continue;
4321
4322                                                 osrfHash* value_field = field;
4323
4324                                                 osrfHash* kid_idl = osrfHashGet(oilsIDL(), osrfHashGet(kid_link, "class"));
4325                                                 if (!kid_idl) continue;
4326
4327                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_many" ))) { // has_many
4328                                                         value_field = osrfHashGet( fields, osrfHashGet(meta, "primarykey") );
4329                                                 }
4330                                                         
4331                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "might_have" ))) { // might_have
4332                                                         value_field = osrfHashGet( fields, osrfHashGet(meta, "primarykey") );
4333                                                 }
4334
4335                                                 osrfStringArray* link_map = osrfHashGet( kid_link, "map" );
4336
4337                                                 if (link_map->size > 0) {
4338                                                         jsonObject* _kid_key = jsonNewObjectType(JSON_ARRAY);
4339                                                         jsonObjectPush(
4340                                                                 _kid_key,
4341                                                                 jsonNewObject( osrfStringArrayGetString( link_map, 0 ) )
4342                                                         );
4343
4344                                                         jsonObjectSetKey(
4345                                                                 flesh_blob,
4346                                                                 osrfHashGet(kid_link, "class"),
4347                                                                 _kid_key
4348                                                         );
4349                                                 };
4350
4351                                                 osrfLogDebug(
4352                                                         OSRF_LOG_MARK,
4353                                                         "Link field: %s, remote class: %s, fkey: %s, reltype: %s",
4354                                                         osrfHashGet(kid_link, "field"),
4355                                                         osrfHashGet(kid_link, "class"),
4356                                                         osrfHashGet(kid_link, "key"),
4357                                                         osrfHashGet(kid_link, "reltype")
4358                                                 );
4359
4360                                                 jsonObject* fake_params = jsonNewObjectType(JSON_ARRAY);
4361                                                 jsonObjectPush(fake_params, jsonNewObjectType(JSON_HASH)); // search hash
4362                                                 jsonObjectPush(fake_params, jsonNewObjectType(JSON_HASH)); // order/flesh hash
4363
4364                                                 osrfLogDebug(OSRF_LOG_MARK, "Creating dummy params object...");
4365
4366                                                 const char* search_key = jsonObjectGetString(
4367                                                         jsonObjectGetIndex(
4368                                                                 cur,
4369                                                                 atoi( osrfHashGet(value_field, "array_position") )
4370                                                         )
4371                                                 );
4372
4373                                                 if (!search_key) {
4374                                                         osrfLogDebug(OSRF_LOG_MARK, "Nothing to search for!");
4375                                                         continue;
4376                                                 }
4377
4378                                                 jsonObjectSetKey(
4379                                                         jsonObjectGetIndex(fake_params, 0),
4380                                                         osrfHashGet(kid_link, "key"),
4381                                                         jsonNewObject( search_key )
4382                                                 );
4383
4384                                                 jsonObjectSetKey(
4385                                                         jsonObjectGetIndex(fake_params, 1),
4386                                                         "flesh",
4387                                                         jsonNewNumberObject( (double)(x - 1 + link_map->size) )
4388                                                 );
4389
4390                                                 if (flesh_blob)
4391                                                         jsonObjectSetKey( jsonObjectGetIndex(fake_params, 1), "flesh_fields", jsonObjectClone(flesh_blob) );
4392
4393                                                 if (jsonObjectGetKeyConst(order_hash, "order_by")) {
4394                                                         jsonObjectSetKey(
4395                                                                 jsonObjectGetIndex(fake_params, 1),
4396                                                                 "order_by",
4397                                                                 jsonObjectClone(jsonObjectGetKeyConst(order_hash, "order_by"))
4398                                                         );
4399                                                 }
4400
4401                                                 if (jsonObjectGetKeyConst(order_hash, "select")) {
4402                                                         jsonObjectSetKey(
4403                                                                 jsonObjectGetIndex(fake_params, 1),
4404                                                                 "select",
4405                                                                 jsonObjectClone(jsonObjectGetKeyConst(order_hash, "select"))
4406                                                         );
4407                                                 }
4408
4409                                                 jsonObject* kids = doFieldmapperSearch(ctx, kid_idl, fake_params, err);
4410
4411                                                 if(*err) {
4412                                                         jsonObjectFree( fake_params );
4413                                                         osrfStringArrayFree(link_fields);
4414                                                         jsonIteratorFree(itr);
4415                                                         jsonObjectFree(res_list);
4416                                                         jsonObjectFree(flesh_blob);
4417                                                         return jsonNULL;
4418                                                 }
4419
4420                                                 osrfLogDebug(OSRF_LOG_MARK, "Search for %s return %d linked objects", osrfHashGet(kid_link, "class"), kids->size);
4421
4422                                                 jsonObject* X = NULL;
4423                                                 if ( link_map->size > 0 && kids->size > 0 ) {
4424                                                         X = kids;
4425                                                         kids = jsonNewObjectType(JSON_ARRAY);
4426
4427                                                         jsonObject* _k_node;
4428                                                         jsonIterator* _k = jsonNewIterator( X );
4429                                                         while ((_k_node = jsonIteratorNext( _k ))) {
4430                                                                 jsonObjectPush(
4431                                                                         kids,
4432                                                                         jsonObjectClone(
4433                                                                                 jsonObjectGetIndex(
4434                                                                                         _k_node,
4435                                                                                         (unsigned long)atoi(
4436                                                                                                 osrfHashGet(
4437                                                                                                         osrfHashGet(
4438                                                                                                                 osrfHashGet(
4439                                                                                                                         osrfHashGet(
4440                                                                                                                                 oilsIDL(),
4441                                                                                                                                 osrfHashGet(kid_link, "class")
4442                                                                                                                         ),
4443                                                                                                                         "fields"
4444                                                                                                                 ),
4445                                                                                                                 osrfStringArrayGetString( link_map, 0 )
4446                                                                                                         ),
4447                                                                                                         "array_position"
4448                                                                                                 )
4449                                                                                         )
4450                                                                                 )
4451                                                                         )
4452                                                                 );
4453                                                         }
4454                                                         jsonIteratorFree(_k);
4455                                                 }
4456
4457                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_a" )) || !(strcmp( osrfHashGet(kid_link, "reltype"), "might_have" ))) {
4458                                                         osrfLogDebug(OSRF_LOG_MARK, "Storing fleshed objects in %s", osrfHashGet(kid_link, "field"));
4459                                                         jsonObjectSetIndex(
4460                                                                 cur,
4461                                                                 (unsigned long)atoi( osrfHashGet( field, "array_position" ) ),
4462                                                                 jsonObjectClone( jsonObjectGetIndex(kids, 0) )
4463                                                         );
4464                                                 }
4465
4466                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_many" ))) { // has_many
4467                                                         osrfLogDebug(OSRF_LOG_MARK, "Storing fleshed objects in %s", osrfHashGet(kid_link, "field"));
4468                                                         jsonObjectSetIndex(
4469                                                                 cur,
4470                                                                 (unsigned long)atoi( osrfHashGet( field, "array_position" ) ),
4471                                                                 jsonObjectClone( kids )
4472                                                         );
4473                                                 }
4474
4475                                                 if (X) {
4476                                                         jsonObjectFree(kids);
4477                                                         kids = X;
4478                                                 }
4479
4480                                                 jsonObjectFree( kids );
4481                                                 jsonObjectFree( fake_params );
4482
4483                                                 osrfLogDebug(OSRF_LOG_MARK, "Fleshing of %s complete", osrfHashGet(kid_link, "field"));
4484                                                 osrfLogDebug(OSRF_LOG_MARK, "%s", jsonObjectToJSON(cur));
4485
4486                                         }
4487                                 }
4488                                 jsonObjectFree( flesh_blob );
4489                                 osrfStringArrayFree(link_fields);
4490                                 jsonIteratorFree(itr);
4491                         }
4492                 }
4493         }
4494
4495         return res_list;
4496 }
4497
4498
4499 static jsonObject* doUpdate(osrfMethodContext* ctx, int* err ) {
4500
4501         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
4502 #ifdef PCRUD
4503         jsonObject* target = jsonObjectGetIndex( ctx->params, 1 );
4504 #else
4505         jsonObject* target = jsonObjectGetIndex( ctx->params, 0 );
4506 #endif
4507
4508         if (!verifyObjectClass(ctx, target)) {
4509                 *err = -1;
4510                 return jsonNULL;
4511         }
4512
4513         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
4514                 osrfAppSessionStatus(
4515                         ctx->session,
4516                         OSRF_STATUS_BADREQUEST,
4517                         "osrfMethodException",
4518                         ctx->request,
4519                         "No active transaction -- required for UPDATE"
4520                 );
4521                 *err = -1;
4522                 return jsonNULL;
4523         }
4524
4525         // The following test is harmless but redundant.  If a class is
4526         // readonly, we don't register an update method for it.
4527         if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
4528                 osrfAppSessionStatus(
4529                         ctx->session,
4530                         OSRF_STATUS_BADREQUEST,
4531                         "osrfMethodException",
4532                         ctx->request,
4533                         "Cannot UPDATE readonly class"
4534                 );
4535                 *err = -1;
4536                 return jsonNULL;
4537         }
4538
4539         dbhandle = writehandle;
4540
4541         char* trans_id = osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" );
4542
4543         // Set the last_xact_id
4544         int index = oilsIDL_ntop( target->classname, "last_xact_id" );
4545         if (index > -1) {
4546                 osrfLogDebug(OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d", trans_id, target->classname, index);
4547                 jsonObjectSetIndex(target, index, jsonNewObject(trans_id));
4548         }       
4549
4550         char* pkey = osrfHashGet(meta, "primarykey");
4551         osrfHash* fields = osrfHashGet(meta, "fields");
4552
4553         char* id = oilsFMGetString( target, pkey );
4554
4555         osrfLogDebug(
4556                 OSRF_LOG_MARK,
4557                 "%s updating %s object with %s = %s",
4558                 MODULENAME,
4559                 osrfHashGet(meta, "fieldmapper"),
4560                 pkey,
4561                 id
4562         );
4563
4564         growing_buffer* sql = buffer_init(128);
4565         buffer_fadd(sql,"UPDATE %s SET", osrfHashGet(meta, "tablename"));
4566
4567         int i = 0;
4568         int first = 1;
4569         char* field_name;
4570         osrfStringArray* field_list = osrfHashKeys( fields );
4571         while ( (field_name = osrfStringArrayGetString(field_list, i++)) ) {
4572
4573                 osrfHash* field = osrfHashGet( fields, field_name );
4574
4575                 if(!( strcmp( field_name, pkey ) )) continue;
4576                 if( str_is_true( osrfHashGet(osrfHashGet(fields,field_name), "virtual") ) )
4577                         continue;
4578
4579                 const jsonObject* field_object = oilsFMGetObject( target, field_name );
4580
4581                 int value_is_numeric = 0;    // boolean
4582                 char* value;
4583                 if (field_object && field_object->classname) {
4584                         value = oilsFMGetString(
4585                                 field_object,
4586                                 (char*)oilsIDLFindPath("/%s/primarykey", field_object->classname)
4587             );
4588                 } else {
4589                         value = jsonObjectToSimpleString( field_object );
4590                         if( field_object && JSON_NUMBER == field_object->type )
4591                                 value_is_numeric = 1;
4592                 }
4593
4594                 osrfLogDebug( OSRF_LOG_MARK, "Updating %s object with %s = %s", osrfHashGet(meta, "fieldmapper"), field_name, value);
4595
4596                 if (!field_object || field_object->type == JSON_NULL) {
4597                         if ( !(!( strcmp( osrfHashGet(meta, "classname"), "au" ) ) && !( strcmp( field_name, "passwd" ) )) ) { // arg at the special case!
4598                                 if (first) first = 0;
4599                                 else OSRF_BUFFER_ADD_CHAR(sql, ',');
4600                                 buffer_fadd( sql, " %s = NULL", field_name );
4601                         }
4602                         
4603                 } else if ( value_is_numeric || !strcmp( get_primitive( field ), "number") ) {
4604                         if (first) first = 0;
4605                         else OSRF_BUFFER_ADD_CHAR(sql, ',');
4606
4607                         const char* numtype = get_datatype( field );
4608                         if ( !strncmp( numtype, "INT", 3 ) ) {
4609                                 buffer_fadd( sql, " %s = %ld", field_name, atol(value) );
4610                         } else if ( !strcmp( numtype, "NUMERIC" ) ) {
4611                                 buffer_fadd( sql, " %s = %f", field_name, atof(value) );
4612                         } else {
4613                                 // Must really be intended as a string, so quote it
4614                                 if ( dbi_conn_quote_string(dbhandle, &value) ) {
4615                                         buffer_fadd( sql, " %s = %s", field_name, value );
4616                                 } else {
4617                                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting string [%s]", MODULENAME, value);
4618                                         osrfAppSessionStatus(
4619                                                 ctx->session,
4620                                                 OSRF_STATUS_INTERNALSERVERERROR,
4621                                                 "osrfMethodException",
4622                                                 ctx->request,
4623                                                 "Error quoting string -- please see the error log for more details"
4624                                         );
4625                                         free(value);
4626                                         free(id);
4627                                         buffer_free(sql);
4628                                         *err = -1;
4629                                         return jsonNULL;
4630                                 }
4631                         }
4632
4633                         osrfLogDebug( OSRF_LOG_MARK, "%s is of type %s", field_name, numtype );
4634
4635                 } else {
4636                         if ( dbi_conn_quote_string(dbhandle, &value) ) {
4637                                 if (first) first = 0;
4638                                 else OSRF_BUFFER_ADD_CHAR(sql, ',');
4639                                 buffer_fadd( sql, " %s = %s", field_name, value );
4640
4641                         } else {
4642                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting string [%s]", MODULENAME, value);
4643                                 osrfAppSessionStatus(
4644                                         ctx->session,
4645                                         OSRF_STATUS_INTERNALSERVERERROR,
4646                                         "osrfMethodException",
4647                                         ctx->request,
4648                                         "Error quoting string -- please see the error log for more details"
4649                                 );
4650                                 free(value);
4651                                 free(id);
4652                                 buffer_free(sql);
4653                                 *err = -1;
4654                                 return jsonNULL;
4655                         }
4656                 }
4657
4658                 free(value);
4659                 
4660         }
4661
4662         jsonObject* obj = jsonNewObject(id);
4663
4664         if ( strcmp( get_primitive( osrfHashGet( osrfHashGet(meta, "fields"), pkey ) ), "number" ) )
4665                 dbi_conn_quote_string(dbhandle, &id);
4666
4667         buffer_fadd( sql, " WHERE %s = %s;", pkey, id );
4668
4669         char* query = buffer_release(sql);
4670         osrfLogDebug(OSRF_LOG_MARK, "%s: Update SQL [%s]", MODULENAME, query);
4671
4672         dbi_result result = dbi_conn_query(dbhandle, query);
4673         free(query);
4674
4675         if (!result) {
4676                 jsonObjectFree(obj);
4677                 obj = jsonNewObject(NULL);
4678                 osrfLogError(
4679                         OSRF_LOG_MARK,
4680                         "%s ERROR updating %s object with %s = %s",
4681                         MODULENAME,
4682                         osrfHashGet(meta, "fieldmapper"),
4683                         pkey,
4684                         id
4685                 );
4686         }
4687
4688         free(id);
4689
4690         return obj;
4691 }
4692
4693 static jsonObject* doDelete(osrfMethodContext* ctx, int* err ) {
4694
4695         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
4696
4697         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
4698                 osrfAppSessionStatus(
4699                         ctx->session,
4700                         OSRF_STATUS_BADREQUEST,
4701                         "osrfMethodException",
4702                         ctx->request,
4703                         "No active transaction -- required for DELETE"
4704                 );
4705                 *err = -1;
4706                 return jsonNULL;
4707         }
4708
4709         // The following test is harmless but redundant.  If a class is
4710         // readonly, we don't register a delete method for it.
4711         if( str_is_true( osrfHashGet( meta, "readonly" ) ) ) {
4712                 osrfAppSessionStatus(
4713                         ctx->session,
4714                         OSRF_STATUS_BADREQUEST,
4715                         "osrfMethodException",
4716                         ctx->request,
4717                         "Cannot DELETE readonly class"
4718                 );
4719                 *err = -1;
4720                 return jsonNULL;
4721         }
4722
4723         dbhandle = writehandle;
4724
4725         jsonObject* obj;
4726
4727         char* pkey = osrfHashGet(meta, "primarykey");
4728
4729         int _obj_pos = 0;
4730 #ifdef PCRUD
4731                 _obj_pos = 1;
4732 #endif
4733
4734         char* id;
4735         if (jsonObjectGetIndex(ctx->params, _obj_pos)->classname) {
4736                 if (!verifyObjectClass(ctx, jsonObjectGetIndex( ctx->params, _obj_pos ))) {
4737                         *err = -1;
4738                         return jsonNULL;
4739                 }
4740
4741                 id = oilsFMGetString( jsonObjectGetIndex(ctx->params, _obj_pos), pkey );
4742         } else {
4743 #ifdef PCRUD
4744         if (!verifyObjectPCRUD( ctx, NULL )) {
4745                         *err = -1;
4746                         return jsonNULL;
4747         }
4748 #endif
4749                 id = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, _obj_pos));
4750         }
4751
4752         osrfLogDebug(
4753                 OSRF_LOG_MARK,
4754                 "%s deleting %s object with %s = %s",
4755                 MODULENAME,
4756                 osrfHashGet(meta, "fieldmapper"),
4757                 pkey,
4758                 id
4759         );
4760
4761         obj = jsonNewObject(id);
4762
4763         if ( strcmp( get_primitive( osrfHashGet( osrfHashGet(meta, "fields"), pkey ) ), "number" ) )
4764                 dbi_conn_quote_string(writehandle, &id);
4765
4766         dbi_result result = dbi_conn_queryf(writehandle, "DELETE FROM %s WHERE %s = %s;", osrfHashGet(meta, "tablename"), pkey, id);
4767
4768         if (!result) {
4769                 jsonObjectFree(obj);
4770                 obj = jsonNewObject(NULL);
4771                 osrfLogError(
4772                         OSRF_LOG_MARK,
4773                         "%s ERROR deleting %s object with %s = %s",
4774                         MODULENAME,
4775                         osrfHashGet(meta, "fieldmapper"),
4776                         pkey,
4777                         id
4778                 );
4779         }
4780
4781         free(id);
4782
4783         return obj;
4784
4785 }
4786
4787
4788 static jsonObject* oilsMakeFieldmapperFromResult( dbi_result result, osrfHash* meta) {
4789         if(!(result && meta)) return jsonNULL;
4790
4791         jsonObject* object = jsonNewObject(NULL);
4792         jsonObjectSetClass(object, osrfHashGet(meta, "classname"));
4793
4794         osrfHash* fields = osrfHashGet(meta, "fields");
4795
4796         osrfLogInternal(OSRF_LOG_MARK, "Setting object class to %s ", object->classname);
4797
4798         osrfHash* _f;
4799         time_t _tmp_dt;
4800         char dt_string[256];
4801         struct tm gmdt;
4802
4803         int fmIndex;
4804         int columnIndex = 1;
4805         int attr;
4806         unsigned short type;
4807         const char* columnName;
4808
4809         /* cycle through the column list */
4810         while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
4811
4812                 osrfLogInternal(OSRF_LOG_MARK, "Looking for column named [%s]...", (char*)columnName);
4813
4814                 fmIndex = -1; // reset the position
4815                 
4816                 /* determine the field type and storage attributes */
4817                 type = dbi_result_get_field_type(result, columnName);
4818                 attr = dbi_result_get_field_attribs(result, columnName);
4819
4820                 /* fetch the fieldmapper index */
4821                 if( (_f = osrfHashGet(fields, (char*)columnName)) ) {
4822                         
4823                         if ( str_is_true( osrfHashGet(_f, "virtual") ) )
4824                                 continue;
4825                         
4826                         const char* pos = (char*)osrfHashGet(_f, "array_position");
4827                         if ( !pos ) continue;
4828
4829                         fmIndex = atoi( pos );
4830                         osrfLogInternal(OSRF_LOG_MARK, "... Found column at position [%s]...", pos);
4831                 } else {
4832                         continue;
4833                 }
4834
4835                 if (dbi_result_field_is_null(result, columnName)) {
4836                         jsonObjectSetIndex( object, fmIndex, jsonNewObject(NULL) );
4837                 } else {
4838
4839                         switch( type ) {
4840
4841                                 case DBI_TYPE_INTEGER :
4842
4843                                         if( attr & DBI_INTEGER_SIZE8 ) 
4844                                                 jsonObjectSetIndex( object, fmIndex, 
4845                                                         jsonNewNumberObject(dbi_result_get_longlong(result, columnName)));
4846                                         else 
4847                                                 jsonObjectSetIndex( object, fmIndex, 
4848                                                         jsonNewNumberObject(dbi_result_get_int(result, columnName)));
4849
4850                                         break;
4851
4852                                 case DBI_TYPE_DECIMAL :
4853                                         jsonObjectSetIndex( object, fmIndex, 
4854                                                         jsonNewNumberObject(dbi_result_get_double(result, columnName)));
4855                                         break;
4856
4857                                 case DBI_TYPE_STRING :
4858
4859
4860                                         jsonObjectSetIndex(
4861                                                 object,
4862                                                 fmIndex,
4863                                                 jsonNewObject( dbi_result_get_string(result, columnName) )
4864                                         );
4865
4866                                         break;
4867
4868                                 case DBI_TYPE_DATETIME :
4869
4870                                         memset(dt_string, '\0', sizeof(dt_string));
4871                                         memset(&gmdt, '\0', sizeof(gmdt));
4872
4873                                         _tmp_dt = dbi_result_get_datetime(result, columnName);
4874
4875
4876                                         if (!(attr & DBI_DATETIME_DATE)) {
4877                                                 gmtime_r( &_tmp_dt, &gmdt );
4878                                                 strftime(dt_string, sizeof(dt_string), "%T", &gmdt);
4879                                         } else if (!(attr & DBI_DATETIME_TIME)) {
4880                                                 localtime_r( &_tmp_dt, &gmdt );
4881                                                 strftime(dt_string, sizeof(dt_string), "%F", &gmdt);
4882                                         } else {
4883                                                 localtime_r( &_tmp_dt, &gmdt );
4884                                                 strftime(dt_string, sizeof(dt_string), "%FT%T%z", &gmdt);
4885                                         }
4886
4887                                         jsonObjectSetIndex( object, fmIndex, jsonNewObject(dt_string) );
4888
4889                                         break;
4890
4891                                 case DBI_TYPE_BINARY :
4892                                         osrfLogError( OSRF_LOG_MARK, 
4893                                                 "Can't do binary at column %s : index %d", columnName, columnIndex - 1);
4894                         }
4895                 }
4896         }
4897
4898         return object;
4899 }
4900
4901 static jsonObject* oilsMakeJSONFromResult( dbi_result result ) {
4902         if(!result) return jsonNULL;
4903
4904         jsonObject* object = jsonNewObject(NULL);
4905
4906         time_t _tmp_dt;
4907         char dt_string[256];
4908         struct tm gmdt;
4909
4910         int fmIndex;
4911         int columnIndex = 1;
4912         int attr;
4913         unsigned short type;
4914         const char* columnName;
4915
4916         /* cycle through the column list */
4917         while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
4918
4919                 osrfLogInternal(OSRF_LOG_MARK, "Looking for column named [%s]...", (char*)columnName);
4920
4921                 fmIndex = -1; // reset the position
4922                 
4923                 /* determine the field type and storage attributes */
4924                 type = dbi_result_get_field_type(result, columnName);
4925                 attr = dbi_result_get_field_attribs(result, columnName);
4926
4927                 if (dbi_result_field_is_null(result, columnName)) {
4928                         jsonObjectSetKey( object, columnName, jsonNewObject(NULL) );
4929                 } else {
4930
4931                         switch( type ) {
4932
4933                                 case DBI_TYPE_INTEGER :
4934
4935                                         if( attr & DBI_INTEGER_SIZE8 ) 
4936                                                 jsonObjectSetKey( object, columnName, jsonNewNumberObject(dbi_result_get_longlong(result, columnName)) );
4937                                         else 
4938                                                 jsonObjectSetKey( object, columnName, jsonNewNumberObject(dbi_result_get_int(result, columnName)) );
4939                                         break;
4940
4941                                 case DBI_TYPE_DECIMAL :
4942                                         jsonObjectSetKey( object, columnName, jsonNewNumberObject(dbi_result_get_double(result, columnName)) );
4943                                         break;
4944
4945                                 case DBI_TYPE_STRING :
4946                                         jsonObjectSetKey( object, columnName, jsonNewObject(dbi_result_get_string(result, columnName)) );
4947                                         break;
4948
4949                                 case DBI_TYPE_DATETIME :
4950
4951                                         memset(dt_string, '\0', sizeof(dt_string));
4952                                         memset(&gmdt, '\0', sizeof(gmdt));
4953
4954                                         _tmp_dt = dbi_result_get_datetime(result, columnName);
4955
4956
4957                                         if (!(attr & DBI_DATETIME_DATE)) {
4958                                                 gmtime_r( &_tmp_dt, &gmdt );
4959                                                 strftime(dt_string, sizeof(dt_string), "%T", &gmdt);
4960                                         } else if (!(attr & DBI_DATETIME_TIME)) {
4961                                                 localtime_r( &_tmp_dt, &gmdt );
4962                                                 strftime(dt_string, sizeof(dt_string), "%F", &gmdt);
4963                                         } else {
4964                                                 localtime_r( &_tmp_dt, &gmdt );
4965                                                 strftime(dt_string, sizeof(dt_string), "%FT%T%z", &gmdt);
4966                                         }
4967
4968                                         jsonObjectSetKey( object, columnName, jsonNewObject(dt_string) );
4969                                         break;
4970
4971                                 case DBI_TYPE_BINARY :
4972                                         osrfLogError( OSRF_LOG_MARK, 
4973                                                 "Can't do binary at column %s : index %d", columnName, columnIndex - 1);
4974                         }
4975                 }
4976         }
4977
4978         return object;
4979 }
4980
4981 // Interpret a string as true or false
4982 static int str_is_true( const char* str ) {
4983         if( NULL == str || strcasecmp( str, "true" ) )
4984                 return 0;
4985         else
4986                 return 1;
4987 }
4988
4989 // Interpret a jsonObject as true or false
4990 static int obj_is_true( const jsonObject* obj ) {
4991         if( !obj )
4992                 return 0;
4993         else switch( obj->type )
4994         {
4995                 case JSON_BOOL :
4996                         if( obj->value.b )
4997                                 return 1;
4998                         else
4999                                 return 0;
5000                 case JSON_STRING :
5001                         if( strcasecmp( obj->value.s, "true" ) )
5002                                 return 0;
5003                         else
5004                                 return 1;
5005                 case JSON_NUMBER :          // Support 1/0 for perl's sake
5006                         if( jsonObjectGetNumber( obj ) == 1.0 )
5007                                 return 1;
5008                         else
5009                                 return 0;
5010                 default :
5011                         return 0;
5012         }
5013 }
5014
5015 // Translate a numeric code into a text string identifying a type of
5016 // jsonObject.  To be used for building error messages.
5017 static const char* json_type( int code ) {
5018         switch ( code )
5019         {
5020                 case 0 :
5021                         return "JSON_HASH";
5022                 case 1 :
5023                         return "JSON_ARRAY";
5024                 case 2 :
5025                         return "JSON_STRING";
5026                 case 3 :
5027                         return "JSON_NUMBER";
5028                 case 4 :
5029                         return "JSON_NULL";
5030                 case 5 :
5031                         return "JSON_BOOL";
5032                 default :
5033                         return "(unrecognized)";
5034         }
5035 }
5036
5037 // Extract the "primitive" attribute from an IDL field definition.
5038 // If we haven't initialized the app, then we must be running in
5039 // some kind of testbed.  In that case, default to "string".
5040 static const char* get_primitive( osrfHash* field ) {
5041         const char* s = osrfHashGet( field, "primitive" );
5042         if( !s ) {
5043                 if( child_initialized )
5044                         osrfLogError(
5045                                 OSRF_LOG_MARK,
5046                                 "%s ERROR No \"datatype\" attribute for field \"%s\"",
5047                                 MODULENAME,
5048                                 osrfHashGet( field, "name" )
5049                         );
5050                 else
5051                         s = "string";
5052         }
5053         return s;
5054 }
5055
5056 // Extract the "datatype" attribute from an IDL field definition.
5057 // If we haven't initialized the app, then we must be running in
5058 // some kind of testbed.  In that case, default to to NUMERIC,
5059 // since we look at the datatype only for numbers.
5060 static const char* get_datatype( osrfHash* field ) {
5061         const char* s = osrfHashGet( field, "datatype" );
5062         if( !s ) {
5063                 if( child_initialized )
5064                         osrfLogError(
5065                                 OSRF_LOG_MARK,
5066                                 "%s ERROR No \"datatype\" attribute for field \"%s\"",
5067                                 MODULENAME,
5068                                 osrfHashGet( field, "name" )
5069                         );
5070                 else
5071                         s = "NUMERIC";
5072         }
5073         return s;
5074 }
5075
5076 /*
5077 If the input string is potentially a valid SQL identifier, return 1.
5078 Otherwise return 0.
5079
5080 Purpose: to prevent certain kinds of SQL injection.  To that end we
5081 don't necessarily need to follow all the rules exactly, such as requiring
5082 that the first character not be a digit.
5083
5084 We allow leading and trailing white space.  In between, we do not allow
5085 punctuation (except for underscores and dollar signs), control 
5086 characters, or embedded white space.
5087
5088 More pedantically we should allow quoted identifiers containing arbitrary
5089 characters, but for the foreseeable future such quoted identifiers are not
5090 likely to be an issue.
5091 */
5092 static int is_identifier( const char* s) {
5093         if( !s )
5094                 return 0;
5095
5096         // Skip leading white space
5097         while( isspace( (unsigned char) *s ) )
5098                 ++s;
5099
5100         if( !s )
5101                 return 0;   // Nothing but white space?  Not okay.
5102
5103         // Check each character until we reach white space or
5104         // end-of-string.  Letters, digits, underscores, and 
5105         // dollar signs are okay. With the exception of periods
5106         // (as in schema.identifier), control characters and other
5107         // punctuation characters are not okay.  Anything else
5108         // is okay -- it could for example be part of a multibyte
5109         // UTF8 character such as a letter with diacritical marks,
5110         // and those are allowed.
5111         do {
5112                 if( isalnum( (unsigned char) *s )
5113                         || '.' == *s
5114                         || '_' == *s
5115                         || '$' == *s )
5116                         ;  // Fine; keep going
5117                 else if(   ispunct( (unsigned char) *s )
5118                                 || iscntrl( (unsigned char) *s ) )
5119                         return 0;
5120                         ++s;
5121         } while( *s && ! isspace( (unsigned char) *s ) );
5122
5123         // If we found any white space in the above loop,
5124         // the rest had better be all white space.
5125         
5126         while( isspace( (unsigned char) *s ) )
5127                 ++s;
5128
5129         if( *s )
5130                 return 0;   // White space was embedded within non-white space
5131
5132         return 1;
5133 }
5134
5135 /*
5136 Determine whether to accept a character string as a comparison operator.
5137 Return 1 if it's good, or 0 if it's bad.
5138
5139 We don't validate it for real.  We just make sure that it doesn't contain
5140 any semicolons or white space (with a special exception for the
5141 "SIMILAR TO" operator).  The idea is to block certain kinds of SQL
5142 injection.  If it has no semicolons or white space but it's still not a
5143 valid operator, then the database will complain.
5144
5145 Another approach would be to compare the string against a short list of
5146 approved operators.  We don't do that because we want to allow custom
5147 operators like ">100*", which would be difficult or impossible to
5148 express otherwise in a JSON query.
5149 */
5150 static int is_good_operator( const char* op ) {
5151         if( !op ) return 0;   // Sanity check
5152
5153         const char* s = op;
5154         while( *s ) {
5155                 if( isspace( (unsigned char) *s ) ) {
5156                         // Special exception for SIMILAR TO.  Someday we might make
5157                         // exceptions for IS DISTINCT FROM and IS NOT DISTINCT FROM.
5158                         if( !strcasecmp( op, "similar to" ) )
5159                                 return 1;
5160                         else
5161                                 return 0;
5162                 }
5163                 else if( ';' == *s )
5164                         return 0;
5165                 ++s;
5166         }
5167         return 1;
5168 }