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