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