]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_cstore.c
85e9ef35af88d47ec13c19d55c05bed9a9c21647
[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/utils.h"
4 #include "objson/object.h"
5 #include "opensrf/log.h"
6 #include "oils_utils.h"
7 #include "oils_constants.h"
8 #include "oils_event.h"
9 #include "oils_idl.h"
10 #include <dbi/dbi.h>
11
12 #include <time.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #define OILS_AUTH_CACHE_PRFX "oils_cstore_"
17 #define MODULENAME "open-ils.cstore"
18 #define PERSIST_NS "http://open-ils.org/spec/opensrf/IDL/persistance/v1"
19 #define OBJECT_NS "http://open-ils.org/spec/opensrf/IDL/objects/v1"
20 #define BASE_NS "http://opensrf.org/spec/IDL/base/v1"
21
22 int osrfAppChildInit();
23 int osrfAppInitialize();
24
25 int verifyObjectClass ( osrfMethodContext*, jsonObject* );
26
27 int beginTransaction ( osrfMethodContext* );
28 int commitTransaction ( osrfMethodContext* );
29 int rollbackTransaction ( osrfMethodContext* );
30
31 int setSavepoint ( osrfMethodContext* );
32 int releaseSavepoint ( osrfMethodContext* );
33 int rollbackSavepoint ( osrfMethodContext* );
34
35 int dispatchCRUDMethod ( osrfMethodContext* );
36 jsonObject* doCreate ( osrfMethodContext*, int* );
37 jsonObject* doRetrieve ( osrfMethodContext*, int* );
38 jsonObject* doUpdate ( osrfMethodContext*, int* );
39 jsonObject* doDelete ( osrfMethodContext*, int* );
40 jsonObject* doSearch ( osrfMethodContext*, osrfHash*, jsonObject*, int* );
41 jsonObject* oilsMakeJSONFromResult( dbi_result, osrfHash* );
42
43 char* searchWriteSimplePredicate ( osrfHash*, const char*, const char*, const char* );
44 char* searchSimplePredicate ( const char*, osrfHash*, jsonObject* );
45 char* searchFunctionPredicate ( osrfHash*, jsonObjectNode* );
46 char* searchFieldTransformPredicate ( osrfHash*, jsonObjectNode* );
47 char* searchBETWEENPredicate ( osrfHash*, jsonObject* );
48 char* searchINPredicate ( osrfHash*, jsonObject* );
49 char* searchPredicate ( osrfHash*, jsonObject* );
50
51 void userDataFree( void* );
52 void sessionDataFree( char*, void* );
53
54 dbi_conn writehandle; /* our MASTER db connection */
55 dbi_conn dbhandle; /* our CURRENT db connection */
56 osrfHash readHandles;
57 jsonObject* jsonNULL = NULL; // 
58
59
60 /* parse and store the IDL here */
61 osrfHash* idl;
62
63 int osrfAppInitialize() {
64
65         // first we register all the transaction and savepoint methods
66         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.transaction.begin", "beginTransaction", "", 0, 0 );
67         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.transaction.commit", "commitTransaction", "", 0, 0 );
68         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.transaction.rollback", "rollbackTransaction", "", 0, 0 );
69
70         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.savepoint.set", "setSavepoint", "", 1, 0 );
71         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.savepoint.release", "releaseSavepoint", "", 1, 0 );
72         osrfAppRegisterMethod( MODULENAME, "open-ils.cstore.savepoint.rollback", "rollbackSavepoint", "", 1, 0 );
73
74
75         osrfLogInfo(OSRF_LOG_MARK, "Initializing the CStore Server...");
76         osrfLogInfo(OSRF_LOG_MARK, "Finding XML file...");
77
78         char * idl_filename = osrf_settings_host_value("/apps/%s/app_settings/IDL", MODULENAME);
79         osrfLogInfo(OSRF_LOG_MARK, "Found file:");
80         osrfLogInfo(OSRF_LOG_MARK, idl_filename);
81
82         idl = oilsIDLInit( idl_filename );
83
84         if (!idl) {
85                 osrfLogError(OSRF_LOG_MARK, "Problem loading the IDL.  Seacrest out!");
86                 exit(1);
87         }
88
89         osrfStringArray* global_methods = osrfNewStringArray(6);
90
91         osrfStringArrayAdd( global_methods, "create" );
92         osrfStringArrayAdd( global_methods, "retrieve" );
93         osrfStringArrayAdd( global_methods, "update" );
94         osrfStringArrayAdd( global_methods, "delete" );
95         osrfStringArrayAdd( global_methods, "search" );
96         osrfStringArrayAdd( global_methods, "id_list" );
97
98         int c_index = 0; 
99         char* classname;
100         osrfStringArray* classes = osrfHashKeys( idl );
101         osrfLogDebug(OSRF_LOG_MARK, "%d classes loaded", classes->size );
102         osrfLogDebug(OSRF_LOG_MARK, "At least %d methods will be generated", classes->size * global_methods->size);
103         
104         while ( (classname = osrfStringArrayGetString(classes, c_index++)) ) {
105                 osrfLogInfo(OSRF_LOG_MARK, "Generating class methods for %s", classname);
106                 
107                 osrfHash* idlClass = osrfHashGet(idl, classname);
108
109                 char* virt = osrfHashGet(idlClass, "virtual");
110                 if (virt && !strcmp( virt, "true")) {
111                         osrfLogDebug(OSRF_LOG_MARK, "Class %s is virtual, skipping", classname );
112                         continue;
113                 }
114
115                 osrfLogDebug(OSRF_LOG_MARK, "HERE");
116                 
117                 int i = 0; 
118                 char* method_type;
119                 char* st_tmp;
120                 char* _fm;
121                 char* part;
122                 osrfHash* method_meta;
123                 while ( (method_type = osrfStringArrayGetString(global_methods, i++)) ) {
124                         osrfLogDebug(OSRF_LOG_MARK, "Using files to build %s class methods for %s", method_type, classname);
125
126                         if (!osrfHashGet(idlClass, "fieldmapper")) continue;
127
128                         method_meta = osrfNewHash();
129                         osrfHashSet(method_meta, idlClass, "class");
130
131                         _fm = strdup( (char*)osrfHashGet(idlClass, "fieldmapper") );
132                         part = strtok_r(_fm, ":", &st_tmp);
133
134                         growing_buffer* method_name =  buffer_init(64);
135                         buffer_fadd(method_name, "%s.direct.%s", MODULENAME, part);
136
137                         while ((part = strtok_r(NULL, ":", &st_tmp))) {
138                                 buffer_fadd(method_name, ".%s", part);
139                         }
140                         buffer_fadd(method_name, ".%s", method_type);
141
142
143                         char* method = buffer_data(method_name);
144                         buffer_free(method_name);
145                         free(_fm);
146
147                         osrfHashSet( method_meta, method, "methodname" );
148                         osrfHashSet( method_meta, method_type, "methodtype" );
149
150                         int flags = 0;
151                         if (!(strcmp( method_type, "search" )) || !(strcmp( method_type, "id_list" ))) {
152                                 flags = flags | OSRF_METHOD_STREAMING;
153                         }
154
155                         osrfAppRegisterExtendedMethod(
156                                 MODULENAME,
157                                 method,
158                                 "dispatchCRUDMethod",
159                                 "",
160                                 1,
161                                 flags,
162                                 (void*)method_meta
163                         );
164                 }
165         }
166
167         return 0;
168 }
169
170 /**
171  * Connects to the database 
172  */
173 int osrfAppChildInit() {
174
175         osrfLogDebug(OSRF_LOG_MARK, "Attempting to initialize libdbi...");
176         dbi_initialize(NULL);
177         osrfLogDebug(OSRF_LOG_MARK, "... libdbi initialized.");
178
179         char* driver    = osrf_settings_host_value("/apps/%s/app_settings/driver", MODULENAME);
180         char* user      = osrf_settings_host_value("/apps/%s/app_settings/database/user", MODULENAME);
181         char* host      = osrf_settings_host_value("/apps/%s/app_settings/database/host", MODULENAME);
182         char* port      = osrf_settings_host_value("/apps/%s/app_settings/database/port", MODULENAME);
183         char* db        = osrf_settings_host_value("/apps/%s/app_settings/database/db", MODULENAME);
184         char* pw        = osrf_settings_host_value("/apps/%s/app_settings/database/pw", MODULENAME);
185
186         osrfLogDebug(OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver);
187         writehandle = dbi_conn_new(driver);
188
189         if(!writehandle) {
190                 osrfLogError(OSRF_LOG_MARK, "Error loading database driver [%s]", driver);
191                 return -1;
192         }
193         osrfLogDebug(OSRF_LOG_MARK, "Database driver [%s] seems OK", driver);
194
195         osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database.  host=%s, "
196                 "port=%s, user=%s, pw=%s, db=%s", MODULENAME, host, port, user, pw, db );
197
198         if(host) dbi_conn_set_option(writehandle, "host", host );
199         if(port) dbi_conn_set_option_numeric( writehandle, "port", atoi(port) );
200         if(user) dbi_conn_set_option(writehandle, "username", user);
201         if(pw) dbi_conn_set_option(writehandle, "password", pw );
202         if(db) dbi_conn_set_option(writehandle, "dbname", db );
203
204         free(user);
205         free(host);
206         free(port);
207         free(db);
208         free(pw);
209
210         const char* err;
211         if (dbi_conn_connect(writehandle) < 0) {
212                 dbi_conn_error(writehandle, &err);
213                 osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", err);
214                 return -1;
215         }
216
217         osrfLogInfo(OSRF_LOG_MARK, "%s successfully connected to the database", MODULENAME);
218
219         int attr;
220         unsigned short type;
221         int i = 0; 
222         char* classname;
223         osrfStringArray* classes = osrfHashKeys( idl );
224         
225         while ( (classname = osrfStringArrayGetString(classes, i++)) ) {
226                 osrfHash* class = osrfHashGet( idl, classname );
227                 osrfHash* fields = osrfHashGet( class, "fields" );
228
229                 char* virt = osrfHashGet(class, "virtual");
230                 if (virt && !strcmp( virt, "true")) {
231                         osrfLogDebug(OSRF_LOG_MARK, "Class %s is virtual, skipping", classname );
232                         continue;
233                 }
234
235         
236                 growing_buffer* sql_buf = buffer_init(32);
237                 buffer_fadd( sql_buf, "SELECT * FROM %s WHERE 1=0;", osrfHashGet(class, "tablename") );
238
239                 char* sql = buffer_data(sql_buf);
240                 buffer_free(sql_buf);
241                 osrfLogDebug(OSRF_LOG_MARK, "%s Investigatory SQL = %s", MODULENAME, sql);
242
243                 dbi_result result = dbi_conn_query(writehandle, sql);
244                 free(sql);
245
246                 if (result) {
247
248                         int columnIndex = 1;
249                         const char* columnName;
250                         osrfHash* _f;
251                         while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
252
253                                 osrfLogInternal(OSRF_LOG_MARK, "Looking for column named [%s]...", (char*)columnName);
254
255                                 /* fetch the fieldmapper index */
256                                 if( (_f = osrfHashGet(fields, (char*)columnName)) ) {
257
258                                         osrfLogDebug(OSRF_LOG_MARK, "Found [%s] in IDL hash...", (char*)columnName);
259
260                                         /* determine the field type and storage attributes */
261                                         type = dbi_result_get_field_type(result, columnName);
262                                         attr = dbi_result_get_field_attribs(result, columnName);
263
264                                         switch( type ) {
265
266                                                 case DBI_TYPE_INTEGER :
267
268                                                         if ( !osrfHashGet(_f, "primitive") )
269                                                                 osrfHashSet(_f,"number", "primitive");
270
271                                                         if( attr & DBI_INTEGER_SIZE8 ) 
272                                                                 osrfHashSet(_f,"INT8", "datatype");
273                                                         else 
274                                                                 osrfHashSet(_f,"INT", "datatype");
275                                                         break;
276
277                                                 case DBI_TYPE_DECIMAL :
278                                                         if ( !osrfHashGet(_f, "primitive") )
279                                                                 osrfHashSet(_f,"number", "primitive");
280
281                                                         osrfHashSet(_f,"NUMERIC", "datatype");
282                                                         break;
283
284                                                 case DBI_TYPE_STRING :
285                                                         if ( !osrfHashGet(_f, "primitive") )
286                                                                 osrfHashSet(_f,"string", "primitive");
287                                                         osrfHashSet(_f,"TEXT", "datatype");
288                                                         break;
289
290                                                 case DBI_TYPE_DATETIME :
291                                                         if ( !osrfHashGet(_f, "primitive") )
292                                                                 osrfHashSet(_f,"string", "primitive");
293
294                                                         osrfHashSet(_f,"TIMESTAMP", "datatype");
295                                                         break;
296
297                                                 case DBI_TYPE_BINARY :
298                                                         if ( !osrfHashGet(_f, "primitive") )
299                                                                 osrfHashSet(_f,"string", "primitive");
300
301                                                         osrfHashSet(_f,"BYTEA", "datatype");
302                                         }
303
304                                         osrfLogDebug(
305                                                 OSRF_LOG_MARK,
306                                                 "Setting [%s] to primitive [%s] and datatype [%s]...",
307                                                 (char*)columnName,
308                                                 osrfHashGet(_f, "primitive"),
309                                                 osrfHashGet(_f, "datatype")
310                                         );
311                                 }
312                         }
313                         dbi_result_free(result);
314                 } else {
315                         osrfLogDebug(OSRF_LOG_MARK, "No data found for class [%s]...", (char*)classname);
316                 }
317         }
318
319         osrfStringArrayFree(classes);
320
321         return 0;
322 }
323
324 void userDataFree( void* blob ) {
325         osrfHashFree( (osrfHash*)blob );
326         return;
327 }
328
329 void sessionDataFree( char* key, void* item ) {
330         if (!(strcmp(key,"xact_id"))) {
331                 if (writehandle)
332                         dbi_conn_query(writehandle, "ROLLBACK;");
333                 free(item);
334         }
335
336         return;
337 }
338
339 int beginTransaction ( osrfMethodContext* ctx ) {
340         OSRF_METHOD_VERIFY_CONTEXT(ctx);
341
342         dbi_result result = dbi_conn_query(writehandle, "START TRANSACTION;");
343         if (!result) {
344                 osrfLogError(OSRF_LOG_MARK, "%s: Error starting transaction", MODULENAME );
345                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error starting transaction" );
346                 return -1;
347         } else {
348                 jsonObject* ret = jsonNewObject(ctx->session->session_id);
349                 osrfAppRespondComplete( ctx, ret );
350                 jsonObjectFree(ret);
351                 
352                 if (!ctx->session->userData) {
353                         ctx->session->userData = osrfNewHash();
354                         ((osrfHash*)ctx->session->userData)->freeItem = &sessionDataFree;
355                 }
356
357                 osrfHashSet( (osrfHash*)ctx->session->userData, strdup( ctx->session->session_id ), "xact_id" );
358                 ctx->session->userDataFree = &userDataFree;
359                 
360         }
361         return 0;
362 }
363
364 int setSavepoint ( osrfMethodContext* ctx ) {
365         OSRF_METHOD_VERIFY_CONTEXT(ctx);
366
367         char* spName = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
368
369         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
370                 osrfAppSessionStatus(
371                         ctx->session,
372                         OSRF_STATUS_INTERNALSERVERERROR,
373                         "osrfMethodException",
374                         ctx->request,
375                         "No active transaction -- required for savepoints"
376                 );
377                 return -1;
378         }
379
380         dbi_result result = dbi_conn_queryf(writehandle, "SAVEPOINT \"%s\";", spName);
381         if (!result) {
382                 osrfLogError(
383                         OSRF_LOG_MARK,
384                         "%s: Error creating savepoint %s in transaction %s",
385                         MODULENAME,
386                         spName,
387                         osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
388                 );
389                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error creating savepoint" );
390                 return -1;
391         } else {
392                 jsonObject* ret = jsonNewObject(spName);
393                 osrfAppRespondComplete( ctx, ret );
394                 jsonObjectFree(ret);
395         }
396         return 0;
397 }
398
399 int releaseSavepoint ( osrfMethodContext* ctx ) {
400         OSRF_METHOD_VERIFY_CONTEXT(ctx);
401
402         char* spName = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
403
404         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
405                 osrfAppSessionStatus(
406                         ctx->session,
407                         OSRF_STATUS_INTERNALSERVERERROR,
408                         "osrfMethodException",
409                         ctx->request,
410                         "No active transaction -- required for savepoints"
411                 );
412                 return -1;
413         }
414
415         dbi_result result = dbi_conn_queryf(writehandle, "RELEASE SAVEPOINT \"%s\";", spName);
416         if (!result) {
417                 osrfLogError(
418                         OSRF_LOG_MARK,
419                         "%s: Error releasing savepoint %s in transaction %s",
420                         MODULENAME,
421                         spName,
422                         osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
423                 );
424                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error releasing savepoint" );
425                 return -1;
426         } else {
427                 jsonObject* ret = jsonNewObject(spName);
428                 osrfAppRespondComplete( ctx, ret );
429                 jsonObjectFree(ret);
430         }
431         return 0;
432 }
433
434 int rollbackSavepoint ( osrfMethodContext* ctx ) {
435         OSRF_METHOD_VERIFY_CONTEXT(ctx);
436
437         char* spName = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
438
439         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
440                 osrfAppSessionStatus(
441                         ctx->session,
442                         OSRF_STATUS_INTERNALSERVERERROR,
443                         "osrfMethodException",
444                         ctx->request,
445                         "No active transaction -- required for savepoints"
446                 );
447                 return -1;
448         }
449
450         dbi_result result = dbi_conn_queryf(writehandle, "ROLLBACK TO SAVEPOINT \"%s\";", spName);
451         if (!result) {
452                 osrfLogError(
453                         OSRF_LOG_MARK,
454                         "%s: Error rolling back savepoint %s in transaction %s",
455                         MODULENAME,
456                         spName,
457                         osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )
458                 );
459                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error rolling back savepoint" );
460                 return -1;
461         } else {
462                 jsonObject* ret = jsonNewObject(spName);
463                 osrfAppRespondComplete( ctx, ret );
464                 jsonObjectFree(ret);
465         }
466         return 0;
467 }
468
469 int commitTransaction ( osrfMethodContext* ctx ) {
470         OSRF_METHOD_VERIFY_CONTEXT(ctx);
471
472         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
473                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "No active transaction to commit" );
474                 return -1;
475         }
476
477         dbi_result result = dbi_conn_query(writehandle, "COMMIT;");
478         if (!result) {
479                 osrfLogError(OSRF_LOG_MARK, "%s: Error committing transaction", MODULENAME );
480                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error committing transaction" );
481                 return -1;
482         } else {
483                 osrfHashRemove(ctx->session->userData, "xact_id");
484                 jsonObject* ret = jsonNewObject(ctx->session->session_id);
485                 osrfAppRespondComplete( ctx, ret );
486                 jsonObjectFree(ret);
487         }
488         return 0;
489 }
490
491 int rollbackTransaction ( osrfMethodContext* ctx ) {
492         OSRF_METHOD_VERIFY_CONTEXT(ctx);
493
494         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
495                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "No active transaction to roll back" );
496                 return -1;
497         }
498
499         dbi_result result = dbi_conn_query(writehandle, "ROLLBACK;");
500         if (!result) {
501                 osrfLogError(OSRF_LOG_MARK, "%s: Error rolling back transaction", MODULENAME );
502                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_INTERNALSERVERERROR, "osrfMethodException", ctx->request, "Error rolling back transaction" );
503                 return -1;
504         } else {
505                 osrfHashRemove(ctx->session->userData, "xact_id");
506                 jsonObject* ret = jsonNewObject(ctx->session->session_id);
507                 osrfAppRespondComplete( ctx, ret );
508                 jsonObjectFree(ret);
509         }
510         return 0;
511 }
512
513 int dispatchCRUDMethod ( osrfMethodContext* ctx ) {
514         OSRF_METHOD_VERIFY_CONTEXT(ctx);
515
516         osrfHash* meta = (osrfHash*) ctx->method->userData;
517         osrfHash* class_obj = osrfHashGet( meta, "class" );
518         
519         int err = 0;
520
521         jsonObject * obj = NULL;
522         if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "create"))
523                 obj = doCreate(ctx, &err);
524
525         if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "retrieve"))
526                 obj = doRetrieve(ctx, &err);
527
528         if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "update"))
529                 obj = doUpdate(ctx, &err);
530
531         if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "delete"))
532                 obj = doDelete(ctx, &err);
533
534         if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "search")) {
535
536                 obj = doSearch(ctx, class_obj, ctx->params, &err);
537                 if(err) return err;
538
539                 jsonObjectNode* cur;
540                 jsonObjectIterator* itr = jsonNewObjectIterator( obj );
541                 while ((cur = jsonObjectIteratorNext( itr ))) {
542                         osrfAppRespond( ctx, jsonObjectClone(cur->item) );
543                 }
544                 jsonObjectIteratorFree(itr);
545                 osrfAppRespondComplete( ctx, NULL );
546
547         } else if (!strcmp( (char*)osrfHashGet(meta, "methodtype"), "id_list")) {
548
549                 jsonObject* _p = jsonObjectClone( ctx->params );
550                 if (jsonObjectGetIndex( _p, 1 )) {
551                         jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "flesh" );
552                         jsonObjectRemoveKey( jsonObjectGetIndex( _p, 1 ), "flesh_columns" );
553                 } else {
554                         jsonObjectSetIndex( _p, 1, jsonParseString("{}") );
555                 }
556
557                 growing_buffer* sel_list = buffer_init(16);
558                 buffer_fadd(sel_list, "{ \"%s\":[\"%s\"] }", osrfHashGet( class_obj, "classname" ), osrfHashGet( class_obj, "primarykey" ));
559                 char* _s = buffer_data(sel_list);
560                 buffer_free(sel_list);
561
562                 jsonObjectSetKey( jsonObjectGetIndex( _p, 1 ), "select", jsonParseString(_s) );
563                 osrfLogDebug(OSRF_LOG_MARK, "%s: Select qualifer set to [%s]", MODULENAME, _s);
564                 free(_s);
565
566                 obj = doSearch(ctx, class_obj, _p, &err);
567                 if(err) return err;
568
569                 jsonObjectNode* cur;
570                 jsonObjectIterator* itr = jsonNewObjectIterator( obj );
571                 while ((cur = jsonObjectIteratorNext( itr ))) {
572                         osrfAppRespond(
573                                 ctx,
574                                 jsonObjectClone(
575                                         jsonObjectGetIndex(
576                                                 cur->item,
577                                                 atoi(
578                                                         osrfHashGet(
579                                                                 osrfHashGet(
580                                                                         osrfHashGet( class_obj, "fields" ),
581                                                                         osrfHashGet( class_obj, "primarykey")
582                                                                 ),
583                                                                 "array_position"
584                                                         )
585                                                 )
586                                         )
587                                 )
588                         );
589                 }
590                 jsonObjectIteratorFree(itr);
591                 osrfAppRespondComplete( ctx, NULL );
592                 
593         } else {
594                 osrfAppRespondComplete( ctx, obj );
595         }
596
597         jsonObjectFree(obj);
598
599         return err;
600 }
601
602 int verifyObjectClass ( osrfMethodContext* ctx, jsonObject* param ) {
603         
604         osrfHash* meta = (osrfHash*) ctx->method->userData;
605         osrfHash* class = osrfHashGet( meta, "class" );
606         
607         if ((strcmp( osrfHashGet(class, "classname"), param->classname ))) {
608
609                 growing_buffer* msg = buffer_init(128);
610                 buffer_fadd(
611                         msg,
612                         "%s: %s method for type %s was passed a %s",
613                         MODULENAME,
614                         osrfHashGet(meta, "methodtype"),
615                         osrfHashGet(class, "classname"),
616                         param->classname
617                 );
618
619                 char* m = buffer_data(msg);
620                 osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, m );
621
622                 buffer_free(msg);
623                 free(m);
624
625                 return 0;
626         }
627         return 1;
628 }
629
630 jsonObject* doCreate(osrfMethodContext* ctx, int* err ) {
631
632         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
633         jsonObject* target = jsonObjectGetIndex( ctx->params, 0 );
634         jsonObject* options = jsonObjectGetIndex( ctx->params, 1 );
635
636         if (!verifyObjectClass(ctx, target)) {
637                 *err = -1;
638                 return jsonNULL;
639         }
640
641         osrfLogDebug( OSRF_LOG_MARK, "Object seems to be of the correct type" );
642
643         if (!ctx->session || !ctx->session->userData || !osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
644                 osrfLogError( OSRF_LOG_MARK, "No active transaction -- required for CREATE" );
645
646                 osrfAppSessionStatus(
647                         ctx->session,
648                         OSRF_STATUS_BADREQUEST,
649                         "osrfMethodException",
650                         ctx->request,
651                         "No active transaction -- required for CREATE"
652                 );
653                 *err = -1;
654                 return jsonNULL;
655         }
656
657         char* trans_id = osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" );
658
659         // Set the last_xact_id
660         osrfHash* last_xact_id;
661         if ((last_xact_id = oilsIDLFindPath("/%s/fields/last_xact_id", target->classname))) {
662                 int index = atoi( osrfHashGet(last_xact_id, "array_position") );
663                 osrfLogDebug(OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d", trans_id, target->classname, index);
664                 jsonObjectSetIndex(target, index, jsonNewObject(trans_id));
665         }       
666
667         osrfLogDebug( OSRF_LOG_MARK, "There is a transaction running..." );
668
669         dbhandle = writehandle;
670
671         osrfHash* fields = osrfHashGet(meta, "fields");
672         char* pkey = osrfHashGet(meta, "primarykey");
673         char* seq = osrfHashGet(meta, "sequence");
674
675         growing_buffer* table_buf = buffer_init(128);
676         growing_buffer* col_buf = buffer_init(128);
677         growing_buffer* val_buf = buffer_init(128);
678
679         buffer_fadd(table_buf,"INSERT INTO %s", osrfHashGet(meta, "tablename"));
680         buffer_add(col_buf,"(");
681         buffer_add(val_buf,"VALUES (");
682
683
684         int i = 0;
685         int first = 1;
686         char* field_name;
687         osrfStringArray* field_list = osrfHashKeys( fields );
688         while ( (field_name = osrfStringArrayGetString(field_list, i++)) ) {
689
690                 osrfHash* field = osrfHashGet( fields, field_name );
691
692                 if(!( strcmp( osrfHashGet(osrfHashGet(fields,field_name), "virtual"), "true" ) )) continue;
693
694                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
695
696                 int pos = atoi(osrfHashGet(field, "array_position"));
697                 char* value = jsonObjectToSimpleString( jsonObjectGetIndex( target, pos ) );
698
699                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
700
701                 if (first) {
702                         first = 0;
703                 } else {
704                         buffer_add(col_buf, ",");
705                         buffer_add(val_buf, ",");
706                 }
707
708                 buffer_add(col_buf, field_name);
709
710                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
711
712                 if (!jsonObjectGetIndex(target, pos) || jsonObjectGetIndex(target, pos)->type == JSON_NULL) {
713                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
714
715                         buffer_add( val_buf, "DEFAULT" );
716                         
717                 } else if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
718                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
719
720                         if ( !strcmp(osrfHashGet(field, "datatype"), "INT8") ) {
721                                 buffer_fadd( val_buf, "%lld", atol(value) );
722                                 
723                         } else if ( !strcmp(osrfHashGet(field, "datatype"), "INT") ) {
724                                 buffer_fadd( val_buf, "%ld", atoll(value) );
725                                 
726                         } else if ( !strcmp(osrfHashGet(field, "datatype"), "NUMERIC") ) {
727                                 buffer_fadd( val_buf, "%f", atof(value) );
728                         }
729                 } else {
730                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
731
732                         if ( dbi_conn_quote_string(writehandle, &value) ) {
733                                 buffer_fadd( val_buf, "%s", value );
734
735                         } else {
736                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting string [%s]", MODULENAME, value);
737                                 osrfAppSessionStatus(
738                                         ctx->session,
739                                         OSRF_STATUS_INTERNALSERVERERROR,
740                                         "osrfMethodException",
741                                         ctx->request,
742                                         "Error quoting string -- please see the error log for more details"
743                                 );
744                                 free(value);
745                                 buffer_free(table_buf);
746                                 buffer_free(col_buf);
747                                 buffer_free(val_buf);
748                                 *err = -1;
749                                 return jsonNULL;
750                         }
751                 }
752
753                 osrfLogDebug( OSRF_LOG_MARK, "HERE..." );
754
755                 free(value);
756                 
757         }
758
759
760         buffer_add(col_buf,")");
761         buffer_add(val_buf,")");
762
763         growing_buffer* sql = buffer_init(128);
764         buffer_fadd(
765                 sql,
766                 "%s %s %s;",
767                 buffer_data(table_buf),
768                 buffer_data(col_buf),
769                 buffer_data(val_buf)
770         );
771         buffer_free(table_buf);
772         buffer_free(col_buf);
773         buffer_free(val_buf);
774
775         char* query = buffer_data(sql);
776         buffer_free(sql);
777
778         osrfLogDebug(OSRF_LOG_MARK, "%s: Insert SQL [%s]", MODULENAME, query);
779
780         
781         dbi_result result = dbi_conn_query(writehandle, query);
782
783         jsonObject* obj = NULL;
784
785         if (!result) {
786                 obj = jsonNewObject(NULL);
787                 osrfLogError(
788                         OSRF_LOG_MARK,
789                         "%s ERROR inserting %s object using query [%s]",
790                         MODULENAME,
791                         osrfHashGet(meta, "fieldmapper"),
792                         query
793                 );
794                 osrfAppSessionStatus(
795                         ctx->session,
796                         OSRF_STATUS_INTERNALSERVERERROR,
797                         "osrfMethodException",
798                         ctx->request,
799                         "INSERT error -- please see the error log for more details"
800                 );
801                 *err = -1;
802         } else {
803
804                 int pos = atoi(osrfHashGet( osrfHashGet(fields, pkey), "array_position" ));
805                 char* id = jsonObjectToSimpleString(jsonObjectGetIndex(target, pos));
806                 if (!id) {
807                         unsigned long long new_id = dbi_conn_sequence_last(writehandle, seq);
808                         growing_buffer* _id = buffer_init(10);
809                         buffer_fadd(_id, "%lld", new_id);
810                         id = buffer_data(_id);
811                         buffer_free(_id);
812                 }
813
814                 if (    !options
815                         || !jsonObjectGetKey( options, "quiet")
816                         || strcmp( jsonObjectToSimpleString(jsonObjectGetKey( options, "quiet")), "true" )
817                 ) {
818
819                         jsonObject* fake_params = jsonParseString("[]");
820                         jsonObjectPush(fake_params, jsonParseString("{}"));
821
822                         jsonObjectSetKey(
823                                 jsonObjectGetIndex(fake_params, 0),
824                                 osrfHashGet(meta, "primarykey"),
825                                 jsonNewObject(id)
826                         );
827
828                         jsonObject* list = doSearch( ctx,meta, fake_params, err);
829
830                         if(*err) {
831                                 jsonObjectFree( fake_params );
832                                 obj = jsonNULL;
833                         } else {
834                                 obj = jsonObjectClone( jsonObjectGetIndex(list, 0) );
835                         }
836
837                         jsonObjectFree( list );
838                         jsonObjectFree( fake_params );
839
840                 } else {
841                         obj = jsonNewObject(id);
842                 }
843
844         }
845
846         free(query);
847
848         return obj;
849
850 }
851
852
853 jsonObject* doRetrieve(osrfMethodContext* ctx, int* err ) {
854
855         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
856
857         jsonObject* obj;
858
859         char* id = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
860         jsonObject* order_hash = jsonObjectGetIndex(ctx->params, 1);
861
862         osrfLogDebug(
863                 OSRF_LOG_MARK,
864                 "%s retrieving %s object with id %s",
865                 MODULENAME,
866                 osrfHashGet(meta, "fieldmapper"),
867                 id
868         );
869
870         jsonObject* fake_params = jsonParseString("[]");
871         jsonObjectPush(fake_params, jsonParseString("{}"));
872
873         jsonObjectSetKey(
874                 jsonObjectGetIndex(fake_params, 0),
875                 osrfHashGet(meta, "primarykey"),
876                 jsonParseString(id)
877         );
878
879         if (order_hash) jsonObjectPush(fake_params, jsonObjectClone(order_hash) );
880
881         jsonObject* list = doSearch( ctx,meta, fake_params, err);
882
883         if(*err) {
884                 jsonObjectFree( fake_params );
885                 return jsonNULL;
886         }
887
888         obj = jsonObjectClone( jsonObjectGetIndex(list, 0) );
889
890         jsonObjectFree( list );
891         jsonObjectFree( fake_params );
892
893         return obj;
894 }
895
896 char* jsonNumberToDBString ( osrfHash* field, jsonObject* value ) {
897         growing_buffer* val_buf = buffer_init(32);
898
899         if ( !strncmp(osrfHashGet(field, "datatype"), "INT", 3) ) {
900                 if (value->type == JSON_NUMBER) buffer_fadd( val_buf, "%ld", (long)jsonObjectGetNumber(value) );
901                 else buffer_fadd( val_buf, "%ld", atol(jsonObjectToSimpleString(value)) );
902
903         } else if ( !strcmp(osrfHashGet(field, "datatype"), "NUMERIC") ) {
904                 if (value->type == JSON_NUMBER) buffer_fadd( val_buf, "%f",  jsonObjectGetNumber(value) );
905                 else buffer_fadd( val_buf, "%f", atof(jsonObjectToSimpleString(value)) );
906         }
907
908         char* pred = buffer_data(val_buf);
909         buffer_free(val_buf);
910
911         return pred;
912 }
913
914 char* searchINPredicate (osrfHash* field, jsonObject* node) {
915         growing_buffer* sql_buf = buffer_init(32);
916         
917         buffer_fadd(
918                 sql_buf,
919                 "%s IN (",
920                 osrfHashGet(field, "name")
921         );
922
923         int in_item_index = 0;
924         int in_item_first = 1;
925         jsonObject* in_item;
926         while ( (in_item = jsonObjectGetIndex(node, in_item_index++)) ) {
927
928                 if (in_item_first)
929                         in_item_first = 0;
930                 else
931                         buffer_add(sql_buf, ", ");
932
933                 if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
934                         char* val = jsonNumberToDBString( field, in_item );
935                         buffer_fadd( sql_buf, "%s", val );
936                         free(val);
937
938                 } else {
939                         char* key_string = jsonObjectToSimpleString(in_item);
940                         if ( dbi_conn_quote_string(dbhandle, &key_string) ) {
941                                 buffer_fadd( sql_buf, "%s", key_string );
942                                 free(key_string);
943                         } else {
944                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, key_string);
945                                 free(key_string);
946                                 buffer_free(sql_buf);
947                                 return NULL;
948                         }
949                 }
950         }
951
952         buffer_add(
953                 sql_buf,
954                 ")"
955         );
956
957         char* pred = buffer_data(sql_buf);
958         buffer_free(sql_buf);
959
960         return pred;
961 }
962
963 char* searchValueTransform( jsonObject* array ) {
964         growing_buffer* sql_buf = buffer_init(32);
965
966         char* val = NULL;
967         int func_item_index = 0;
968         int func_item_first = 2;
969         jsonObject* func_item;
970         while ( (func_item = jsonObjectGetIndex(array, func_item_index++)) ) {
971
972                 val = jsonObjectToSimpleString(func_item);
973
974                 if (func_item_first == 2) {
975                         buffer_fadd(sql_buf, "%s( ", val);
976                         free(val);
977                         func_item_first--;
978                         continue;
979                 }
980
981                 if (func_item_first)
982                         func_item_first--;
983                 else
984                         buffer_add(sql_buf, ", ");
985
986                 if ( dbi_conn_quote_string(dbhandle, &val) ) {
987                         buffer_fadd( sql_buf, "%s", val );
988                         free(val);
989                 } else {
990                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, val);
991                         free(val);
992                         buffer_free(sql_buf);
993                         return NULL;
994                 }
995         }
996
997         buffer_add(
998                 sql_buf,
999                 " )"
1000         );
1001
1002         char* pred = buffer_data(sql_buf);
1003         buffer_free(sql_buf);
1004
1005         return pred;
1006 }
1007
1008 char* searchFunctionPredicate (osrfHash* field, jsonObjectNode* node) {
1009         growing_buffer* sql_buf = buffer_init(32);
1010
1011         char* val = searchValueTransform(node->item);
1012         
1013         buffer_fadd(
1014                 sql_buf,
1015                 "%s %s %s",
1016                 osrfHashGet(field, "name"),
1017                 node->key,
1018                 val
1019         );
1020
1021         char* pred = buffer_data(sql_buf);
1022         buffer_free(sql_buf);
1023         free(val);
1024
1025         return pred;
1026 }
1027
1028 char* searchFieldTransformPredicate (osrfHash* field, jsonObjectNode* node) {
1029         growing_buffer* sql_buf = buffer_init(32);
1030         
1031         char* field_transform = jsonObjectToSimpleString( jsonObjectGetKey( node->item, "transform" ) );
1032         char* value = NULL;
1033
1034         if (jsonObjectGetKey( node->item, "value" )->type == JSON_ARRAY) {
1035                 value = searchValueTransform(jsonObjectGetKey( node->item, "value" ));
1036         } else if (jsonObjectGetKey( node->item, "value" )->type != JSON_NULL) {
1037                 if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
1038                         value = jsonNumberToDBString( field, jsonObjectGetKey( node->item, "value" ) );
1039                 } else {
1040                         value = jsonObjectToSimpleString(jsonObjectGetKey( node->item, "value" ));
1041                         if ( !dbi_conn_quote_string(dbhandle, &value) ) {
1042                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, value);
1043                                 free(value);
1044                                 return NULL;
1045                         }
1046                 }
1047         }
1048
1049         buffer_fadd(
1050                 sql_buf,
1051                 "%s(%s) %s %s",
1052                 field_transform,
1053                 osrfHashGet(field, "name"),
1054                 node->key,
1055                 value
1056         );
1057
1058         char* pred = buffer_data(sql_buf);
1059         buffer_free(sql_buf);
1060
1061         return pred;
1062 }
1063
1064 char* searchSimplePredicate (const char* orig_op, osrfHash* field, jsonObject* node) {
1065
1066         char* val = NULL;
1067
1068         if (node->type != JSON_NULL) {
1069                 if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
1070                         val = jsonNumberToDBString( field, node );
1071                 } else {
1072                         val = jsonObjectToSimpleString(node);
1073                 }
1074         }
1075
1076         char* pred = searchWriteSimplePredicate( field, osrfHashGet(field, "name"), orig_op, val );
1077
1078         if (val) free(val);
1079
1080         return pred;
1081 }
1082
1083 char* searchWriteSimplePredicate ( osrfHash* field, const char* left, const char* orig_op, const char* right ) {
1084
1085         char* val = NULL;
1086         char* op = NULL;
1087         if (right == NULL) {
1088                 val = strdup("NULL");
1089
1090                 if (strcmp( orig_op, "=" ))
1091                         op = strdup("IS NOT");
1092                 else
1093                         op = strdup("IS");
1094
1095         } else if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
1096                 val = strdup(right);
1097                 op = strdup(orig_op);
1098
1099         } else {
1100                 val = strdup(right);
1101                 if ( !dbi_conn_quote_string(dbhandle, &val) ) {
1102                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key string [%s]", MODULENAME, val);
1103                         free(val);
1104                         return NULL;
1105                 }
1106                 op = strdup(orig_op);
1107         }
1108
1109         growing_buffer* sql_buf = buffer_init(16);
1110         buffer_fadd( sql_buf, "%s %s %s", left, op, val );
1111         free(val);
1112         free(op);
1113
1114         char* pred = buffer_data(sql_buf);
1115         buffer_free(sql_buf);
1116
1117         return pred;
1118
1119 }
1120
1121 char* searchBETWEENPredicate (osrfHash* field, jsonObject* node) {
1122
1123         char* x_string;
1124         char* y_string;
1125
1126         if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
1127                 x_string = jsonNumberToDBString(field, jsonObjectGetIndex(node,0));
1128                 y_string = jsonNumberToDBString(field, jsonObjectGetIndex(node,1));
1129
1130         } else {
1131                 x_string = jsonObjectToSimpleString(jsonObjectGetIndex(node,0));
1132                 y_string = jsonObjectToSimpleString(jsonObjectGetIndex(node,1));
1133                 if ( !(dbi_conn_quote_string(dbhandle, &x_string) && dbi_conn_quote_string(dbhandle, &y_string)) ) {
1134                         osrfLogError(OSRF_LOG_MARK, "%s: Error quoting key strings [%s] and [%s]", MODULENAME, x_string, y_string);
1135                         free(x_string);
1136                         free(y_string);
1137                         return NULL;
1138                 }
1139         }
1140
1141         growing_buffer* sql_buf = buffer_init(32);
1142         buffer_fadd( sql_buf, "%s BETWEEN %s AND %s", osrfHashGet(field, "name"), x_string, y_string );
1143         free(x_string);
1144         free(y_string);
1145
1146         char* pred = buffer_data(sql_buf);
1147         buffer_free(sql_buf);
1148
1149         return pred;
1150 }
1151
1152 char* searchPredicate ( osrfHash* field, jsonObject* node ) {
1153
1154         char* pred = NULL;
1155         if (node->type == JSON_ARRAY) { // equality IN search
1156                 pred = searchINPredicate( field, node );
1157         } else if (node->type == JSON_HASH) { // non-equality search
1158                 jsonObjectNode* pred_node;
1159                 jsonObjectIterator* pred_itr = jsonNewObjectIterator( node );
1160                 while ( (pred_node = jsonObjectIteratorNext( pred_itr )) ) {
1161                         if ( !(strcasecmp( pred_node->key,"between" )) )
1162                                 pred = searchBETWEENPredicate( field, pred_node->item );
1163                         else if ( !(strcasecmp( pred_node->key,"in" )) )
1164                                 pred = searchINPredicate( field, pred_node->item );
1165                         else if ( pred_node->item->type == JSON_ARRAY )
1166                                 pred = searchFunctionPredicate( field, pred_node );
1167                         else if ( pred_node->item->type == JSON_HASH )
1168                                 pred = searchFieldTransformPredicate( field, pred_node );
1169                         else 
1170                                 pred = searchSimplePredicate( pred_node->key, field, pred_node->item );
1171
1172                         break;
1173                 }
1174         } else if (node->type == JSON_NULL) { // IS NULL search
1175                 growing_buffer* _p = buffer_init(16);
1176                 buffer_fadd(
1177                         _p,
1178                         "%s IS NULL",
1179                         osrfHashGet(field, "name")
1180                 );
1181                 pred = buffer_data(_p);
1182                 buffer_free(_p);
1183         } else { // equality search
1184                 pred = searchSimplePredicate( "=", field, node );
1185         }
1186
1187         return pred;
1188
1189 }
1190
1191 jsonObject* doSearch(osrfMethodContext* ctx, osrfHash* meta, jsonObject* params, int* err ) {
1192
1193         // XXX for now...
1194         dbhandle = writehandle;
1195
1196         osrfHash* links = osrfHashGet(meta, "links");
1197         osrfHash* fields = osrfHashGet(meta, "fields");
1198         char* core_class = osrfHashGet(meta, "classname");
1199
1200         jsonObjectNode* node = NULL;
1201         jsonObjectNode* snode = NULL;
1202         jsonObject* _tmp;
1203         jsonObject* obj;
1204         jsonObject* search_hash = jsonObjectGetIndex(params, 0);
1205         jsonObject* order_hash = jsonObjectGetIndex(params, 1);
1206
1207         growing_buffer* sql_buf = buffer_init(128);
1208         buffer_add(sql_buf, "SELECT");
1209
1210         int first = 1;
1211         if ( (_tmp = jsonObjectGetKey( order_hash, "select" )) ) {
1212
1213                 jsonObjectIterator* class_itr = jsonNewObjectIterator( _tmp );
1214                 while ( (snode = jsonObjectIteratorNext( class_itr )) ) {
1215
1216                         osrfHash* idlClass = osrfHashGet( idl, snode->key );
1217                         if (!idlClass) continue;
1218                         char* cname = osrfHashGet(idlClass, "classname");
1219
1220                         jsonObjectIterator* select_itr = jsonNewObjectIterator( snode->item );
1221                         while ( (node = jsonObjectIteratorNext( select_itr )) ) {
1222                                 osrfHash* field = osrfHashGet( osrfHashGet( idlClass, "fields" ), jsonObjectToSimpleString(node->item) );
1223                                 char* fname = osrfHashGet(field, "name");
1224
1225                                 if (!field) continue;
1226
1227                                 if (first) {
1228                                         first = 0;
1229                                 } else {
1230                                         buffer_add(sql_buf, ",");
1231                                 }
1232
1233                                 buffer_fadd(sql_buf, " \"%s\".%s", cname, fname, cname, fname);
1234                         }
1235                 }
1236         } else {
1237                 buffer_add(sql_buf, " *");
1238         }
1239
1240         buffer_fadd(sql_buf, " FROM %s AS \"%s\" WHERE ", osrfHashGet(meta, "tablename"), core_class );
1241
1242
1243         char* pred;
1244         first = 1;
1245         jsonObjectIterator* search_itr = jsonNewObjectIterator( search_hash );
1246         while ( (node = jsonObjectIteratorNext( search_itr )) ) {
1247                 osrfHash* field = osrfHashGet( fields, node->key );
1248
1249                 if (!field) continue;
1250
1251                 if (first) {
1252                         first = 0;
1253                 } else {
1254                         buffer_add(sql_buf, " AND ");
1255                 }
1256
1257                 pred = searchPredicate( field, node->item);
1258                 buffer_fadd( sql_buf, "%s", pred );
1259                 free(pred);
1260         }
1261
1262         jsonObjectIteratorFree(search_itr);
1263
1264         if (order_hash) {
1265                 char* string;
1266                 if ( (_tmp = jsonObjectGetKey( jsonObjectGetKey( order_hash, "order_by" ), core_class ) ) ){
1267                         string = jsonObjectToSimpleString(_tmp);
1268                         buffer_fadd(
1269                                 sql_buf,
1270                                 " ORDER BY %s",
1271                                 string
1272                         );
1273                         free(string);
1274                 }
1275
1276                 if ( (_tmp = jsonObjectGetKey( order_hash, "limit" )) ){
1277                         string = jsonObjectToSimpleString(_tmp);
1278                         buffer_fadd(
1279                                 sql_buf,
1280                                 " LIMIT %d",
1281                                 atoi(string)
1282                         );
1283                         free(string);
1284                 }
1285
1286                 _tmp = jsonObjectGetKey( order_hash, "offset" );
1287                 if (_tmp) {
1288                         string = jsonObjectToSimpleString(_tmp);
1289                         buffer_fadd(
1290                                 sql_buf,
1291                                 " OFFSET %d",
1292                                 atoi(string)
1293                         );
1294                         free(string);
1295                 }
1296         }
1297
1298         buffer_add(sql_buf, ";");
1299
1300         char* sql = buffer_data(sql_buf);
1301         buffer_free(sql_buf);
1302         
1303         osrfLogDebug(OSRF_LOG_MARK, "%s SQL =  %s", MODULENAME, sql);
1304         dbi_result result = dbi_conn_query(dbhandle, sql);
1305
1306         jsonObject* res_list = jsonParseString("[]");
1307         if(result) {
1308                 osrfLogDebug(OSRF_LOG_MARK, "Query returned with no errors");
1309
1310                 if (dbi_result_first_row(result)) {
1311                         /* JSONify the result */
1312                         osrfLogDebug(OSRF_LOG_MARK, "Query returned at least one row");
1313                         do {
1314                                 obj = oilsMakeJSONFromResult( result, meta );
1315                                 jsonObjectPush(res_list, obj);
1316                         } while (dbi_result_next_row(result));
1317                 } else {
1318                         osrfLogDebug(OSRF_LOG_MARK, "%s returned no results for query %s", MODULENAME, sql);
1319                 }
1320
1321                 /* clean up the query */
1322                 dbi_result_free(result); 
1323
1324         } else {
1325                 osrfLogError(OSRF_LOG_MARK, "%s: Error retrieving %s with query [%s]", MODULENAME, osrfHashGet(meta, "fieldmapper"), sql);
1326                 osrfAppSessionStatus(
1327                         ctx->session,
1328                         OSRF_STATUS_INTERNALSERVERERROR,
1329                         "osrfMethodException",
1330                         ctx->request,
1331                         "Severe query error -- see error log for more details"
1332                 );
1333                 *err = -1;
1334                 free(sql);
1335                 jsonObjectFree(res_list);
1336                 return jsonNULL;
1337
1338         }
1339
1340         free(sql);
1341
1342         if (res_list->size && order_hash) {
1343                 _tmp = jsonObjectGetKey( order_hash, "flesh" );
1344                 if (_tmp) {
1345                         int x = (int)jsonObjectGetNumber(_tmp);
1346
1347                         jsonObject* flesh_blob = NULL;
1348                         if ((flesh_blob = jsonObjectGetKey( order_hash, "flesh_fields" )) && x > 0) {
1349
1350                                 flesh_blob = jsonObjectClone( flesh_blob );
1351                                 jsonObject* flesh_fields = jsonObjectGetKey( flesh_blob, core_class );
1352
1353                                 osrfStringArray* link_fields = NULL;
1354
1355                                 if (flesh_fields) {
1356                                         if (flesh_fields->size == 1) {
1357                                                 char* _t = jsonObjectToSimpleString( jsonObjectGetIndex( flesh_fields, 0 ) );
1358                                                 if (!strcmp(_t,"*")) link_fields = osrfHashKeys( links );
1359                                                 free(_t);
1360                                         }
1361
1362                                         if (!link_fields) {
1363                                                 jsonObjectNode* _f;
1364                                                 link_fields = osrfNewStringArray(1);
1365                                                 jsonObjectIterator* _i = jsonNewObjectIterator( flesh_fields );
1366                                                 while ((_f = jsonObjectIteratorNext( _i ))) {
1367                                                         osrfStringArrayAdd( link_fields, jsonObjectToSimpleString( _f->item ) );
1368                                                 }
1369                                         }
1370                                 }
1371
1372                                 jsonObjectNode* cur;
1373                                 jsonObjectIterator* itr = jsonNewObjectIterator( res_list );
1374                                 while ((cur = jsonObjectIteratorNext( itr ))) {
1375
1376                                         int i = 0;
1377                                         char* link_field;
1378                                         
1379                                         while ( (link_field = osrfStringArrayGetString(link_fields, i++)) ) {
1380
1381                                                 osrfLogDebug(OSRF_LOG_MARK, "Starting to flesh %s", link_field);
1382
1383                                                 osrfHash* kid_link = osrfHashGet(links, link_field);
1384                                                 if (!kid_link) continue;
1385
1386                                                 osrfHash* field = osrfHashGet(fields, link_field);
1387                                                 if (!field) continue;
1388
1389                                                 osrfHash* value_field = field;
1390
1391                                                 osrfHash* kid_idl = osrfHashGet(idl, osrfHashGet(kid_link, "class"));
1392                                                 if (!kid_idl) continue;
1393
1394                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_many" ))) { // has_many
1395                                                         value_field = osrfHashGet( fields, osrfHashGet(meta, "primarykey") );
1396                                                 }
1397                                                         
1398                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "might_have" ))) { // might_have
1399                                                         value_field = osrfHashGet( fields, osrfHashGet(meta, "primarykey") );
1400                                                 }
1401
1402                                                 osrfStringArray* link_map = osrfHashGet( kid_link, "map" );
1403
1404                                                 if (link_map->size > 0) {
1405                                                         jsonObject* _kid_key = jsonParseString("[]");
1406                                                         jsonObjectPush(
1407                                                                 _kid_key,
1408                                                                 jsonNewObject( osrfStringArrayGetString( link_map, 0 ) )
1409                                                         );
1410
1411                                                         jsonObjectSetKey(
1412                                                                 flesh_blob,
1413                                                                 osrfHashGet(kid_link, "class"),
1414                                                                 _kid_key
1415                                                         );
1416                                                 };
1417
1418                                                 osrfLogDebug(
1419                                                         OSRF_LOG_MARK,
1420                                                         "Link field: %s, remote class: %s, fkey: %s, reltype: %s",
1421                                                         osrfHashGet(kid_link, "field"),
1422                                                         osrfHashGet(kid_link, "class"),
1423                                                         osrfHashGet(kid_link, "key"),
1424                                                         osrfHashGet(kid_link, "reltype")
1425                                                 );
1426
1427                                                 jsonObject* fake_params = jsonParseString("[]");
1428                                                 jsonObjectPush(fake_params, jsonParseString("{}")); // search hash
1429                                                 jsonObjectPush(fake_params, jsonParseString("{}")); // order/flesh hash
1430
1431                                                 osrfLogDebug(OSRF_LOG_MARK, "Creating dummy params object...");
1432
1433                                                 char* search_key =
1434                                                 jsonObjectToSimpleString(
1435                                                         jsonObjectGetIndex(
1436                                                                 cur->item,
1437                                                                 atoi( osrfHashGet(value_field, "array_position") )
1438                                                         )
1439                                                 );
1440
1441                                                 if (!search_key) {
1442                                                         osrfLogDebug(OSRF_LOG_MARK, "Nothing to search for!");
1443                                                         continue;
1444                                                 }
1445                                                         
1446                                                 jsonObjectSetKey(
1447                                                         jsonObjectGetIndex(fake_params, 0),
1448                                                         osrfHashGet(kid_link, "key"),
1449                                                         jsonNewObject( search_key )
1450                                                 );
1451
1452                                                 free(search_key);
1453
1454
1455                                                 jsonObjectSetKey(
1456                                                         jsonObjectGetIndex(fake_params, 1),
1457                                                         "flesh",
1458                                                         jsonNewNumberObject( (double)(x - 1 + link_map->size) )
1459                                                 );
1460
1461                                                 if (flesh_blob)
1462                                                         jsonObjectSetKey( jsonObjectGetIndex(fake_params, 1), "flesh_fields", jsonObjectClone(flesh_blob) );
1463
1464                                                 if (jsonObjectGetKey(order_hash, "order_by")) {
1465                                                         jsonObjectSetKey(
1466                                                                 jsonObjectGetIndex(fake_params, 1),
1467                                                                 "order_by",
1468                                                                 jsonObjectClone(jsonObjectGetKey(order_hash, "order_by"))
1469                                                         );
1470                                                 }
1471
1472                                                 jsonObject* kids = doSearch(ctx, kid_idl, fake_params, err);
1473
1474                                                 if(*err) {
1475                                                         jsonObjectFree( fake_params );
1476                                                         osrfStringArrayFree(link_fields);
1477                                                         jsonObjectIteratorFree(itr);
1478                                                         jsonObjectFree(res_list);
1479                                                         return jsonNULL;
1480                                                 }
1481
1482                                                 osrfLogDebug(OSRF_LOG_MARK, "Search for %s return %d linked objects", osrfHashGet(kid_link, "class"), kids->size);
1483
1484                                                 jsonObject* X = NULL;
1485                                                 if ( link_map->size > 0 && kids->size > 0 ) {
1486                                                         X = kids;
1487                                                         kids = jsonParseString("[]");
1488
1489                                                         jsonObjectNode* _k_node;
1490                                                         jsonObjectIterator* _k = jsonNewObjectIterator( X );
1491                                                         while ((_k_node = jsonObjectIteratorNext( _k ))) {
1492                                                                 jsonObjectPush(
1493                                                                         kids,
1494                                                                         jsonObjectClone(
1495                                                                                 jsonObjectGetIndex(
1496                                                                                         _k_node->item,
1497                                                                                         (unsigned long)atoi(
1498                                                                                                 osrfHashGet(
1499                                                                                                         osrfHashGet(
1500                                                                                                                 osrfHashGet(
1501                                                                                                                         osrfHashGet(
1502                                                                                                                                 idl,
1503                                                                                                                                 osrfHashGet(kid_link, "class")
1504                                                                                                                         ),
1505                                                                                                                         "fields"
1506                                                                                                                 ),
1507                                                                                                                 osrfStringArrayGetString( link_map, 0 )
1508                                                                                                         ),
1509                                                                                                         "array_position"
1510                                                                                                 )
1511                                                                                         )
1512                                                                                 )
1513                                                                         )
1514                                                                 );
1515                                                         }
1516                                                 }
1517
1518                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_a" ))) {
1519                                                         osrfLogDebug(OSRF_LOG_MARK, "Storing fleshed objects in %s", osrfHashGet(kid_link, "field"));
1520                                                         jsonObjectSetIndex(
1521                                                                 cur->item,
1522                                                                 (unsigned long)atoi( osrfHashGet( field, "array_position" ) ),
1523                                                                 jsonObjectClone( jsonObjectGetIndex(kids, 0) )
1524                                                         );
1525                                                 }
1526
1527                                                 if (!(strcmp( osrfHashGet(kid_link, "reltype"), "has_many" ))) { // has_many
1528                                                         osrfLogDebug(OSRF_LOG_MARK, "Storing fleshed objects in %s", osrfHashGet(kid_link, "field"));
1529                                                         jsonObjectSetIndex(
1530                                                                 cur->item,
1531                                                                 (unsigned long)atoi( osrfHashGet( field, "array_position" ) ),
1532                                                                 jsonObjectClone( kids )
1533                                                         );
1534                                                 }
1535
1536                                                 if (X) {
1537                                                         jsonObjectFree(kids);
1538                                                         kids = X;
1539                                                 }
1540
1541                                                 jsonObjectFree( kids );
1542                                                 jsonObjectFree( fake_params );
1543
1544                                                 osrfLogDebug(OSRF_LOG_MARK, "Fleshing of %s complete", osrfHashGet(kid_link, "field"));
1545                                                 osrfLogDebug(OSRF_LOG_MARK, "%s", jsonObjectToJSON(cur->item));
1546
1547                                         }
1548                                 }
1549                                 jsonObjectFree( flesh_blob );
1550                                 osrfStringArrayFree(link_fields);
1551                                 jsonObjectIteratorFree(itr);
1552                         }
1553                 }
1554         }
1555
1556         return res_list;
1557 }
1558
1559
1560 jsonObject* doUpdate(osrfMethodContext* ctx, int* err ) {
1561
1562         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
1563         jsonObject* target = jsonObjectGetIndex(ctx->params, 0);
1564
1565         if (!verifyObjectClass(ctx, target)) {
1566                 *err = -1;
1567                 return jsonNULL;
1568         }
1569
1570         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
1571                 osrfAppSessionStatus(
1572                         ctx->session,
1573                         OSRF_STATUS_BADREQUEST,
1574                         "osrfMethodException",
1575                         ctx->request,
1576                         "No active transaction -- required for UPDATE"
1577                 );
1578                 *err = -1;
1579                 return jsonNULL;
1580         }
1581
1582         dbhandle = writehandle;
1583
1584         char* trans_id = osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" );
1585
1586         // Set the last_xact_id
1587         osrfHash* last_xact_id;
1588         if ((last_xact_id = oilsIDLFindPath("/%s/fields/last_xact_id", target->classname))) {
1589                 int index = atoi( osrfHashGet(last_xact_id, "array_position") );
1590                 osrfLogDebug(OSRF_LOG_MARK, "Setting last_xact_id to %s on %s at position %d", trans_id, target->classname, index);
1591                 jsonObjectSetIndex(target, index, jsonNewObject(trans_id));
1592         }       
1593
1594         char* pkey = osrfHashGet(meta, "primarykey");
1595         osrfHash* fields = osrfHashGet(meta, "fields");
1596
1597         char* id =
1598                 jsonObjectToSimpleString(
1599                         jsonObjectGetIndex(
1600                                 target,
1601                                 atoi( osrfHashGet( osrfHashGet( fields, pkey ), "array_position" ) )
1602                         )
1603                 );
1604
1605         osrfLogDebug(
1606                 OSRF_LOG_MARK,
1607                 "%s updating %s object with %s = %s",
1608                 MODULENAME,
1609                 osrfHashGet(meta, "fieldmapper"),
1610                 pkey,
1611                 id
1612         );
1613
1614         growing_buffer* sql = buffer_init(128);
1615         buffer_fadd(sql,"UPDATE %s SET", osrfHashGet(meta, "tablename"));
1616
1617         int i = 0;
1618         int first = 1;
1619         char* field_name;
1620         osrfStringArray* field_list = osrfHashKeys( fields );
1621         while ( (field_name = osrfStringArrayGetString(field_list, i++)) ) {
1622
1623                 osrfHash* field = osrfHashGet( fields, field_name );
1624
1625                 if(!( strcmp( field_name, pkey ) )) continue;
1626                 if(!( strcmp( osrfHashGet(osrfHashGet(fields,field_name), "virtual"), "true" ) )) continue;
1627
1628                 int pos = atoi(osrfHashGet(field, "array_position"));
1629                 char* value = jsonObjectToSimpleString( jsonObjectGetIndex( target, pos ) );
1630
1631                 osrfLogDebug( OSRF_LOG_MARK, "Updating %s object with %s = %s", osrfHashGet(meta, "fieldmapper"), field_name, value);
1632
1633                 if (!jsonObjectGetIndex(target, pos) || jsonObjectGetIndex(target, pos)->type == JSON_NULL) {
1634                         if ( !(!( strcmp( osrfHashGet(meta, "classname"), "au" ) ) && !( strcmp( field_name, "passwd" ) )) ) { // arg at the special case!
1635                                 if (first) first = 0;
1636                                 else buffer_add(sql, ",");
1637                                 buffer_fadd( sql, " %s = NULL", field_name );
1638                         }
1639                         
1640                 } else if ( !strcmp(osrfHashGet(field, "primitive"), "number") ) {
1641                         if (first) first = 0;
1642                         else buffer_add(sql, ",");
1643
1644                         if ( !strncmp(osrfHashGet(field, "datatype"), "INT", (size_t)3) ) {
1645                                 buffer_fadd( sql, " %s = %ld", field_name, atol(value) );
1646                         } else if ( !strcmp(osrfHashGet(field, "datatype"), "NUMERIC") ) {
1647                                 buffer_fadd( sql, " %s = %f", field_name, atof(value) );
1648                         }
1649
1650                         osrfLogDebug( OSRF_LOG_MARK, "%s is of type %s", field_name, osrfHashGet(field, "datatype"));
1651
1652                 } else {
1653                         if ( dbi_conn_quote_string(dbhandle, &value) ) {
1654                                 if (first) first = 0;
1655                                 else buffer_add(sql, ",");
1656                                 buffer_fadd( sql, " %s = %s", field_name, value );
1657
1658                         } else {
1659                                 osrfLogError(OSRF_LOG_MARK, "%s: Error quoting string [%s]", MODULENAME, value);
1660                                 osrfAppSessionStatus(
1661                                         ctx->session,
1662                                         OSRF_STATUS_INTERNALSERVERERROR,
1663                                         "osrfMethodException",
1664                                         ctx->request,
1665                                         "Error quoting string -- please see the error log for more details"
1666                                 );
1667                                 free(value);
1668                                 free(id);
1669                                 buffer_free(sql);
1670                                 *err = -1;
1671                                 return jsonNULL;
1672                         }
1673                 }
1674
1675                 free(value);
1676                 
1677         }
1678
1679         jsonObject* obj = jsonParseString(id);
1680
1681         if ( strcmp( osrfHashGet( osrfHashGet( osrfHashGet(meta, "fields"), pkey ), "primitive" ), "number" ) )
1682                 dbi_conn_quote_string(dbhandle, &id);
1683
1684         buffer_fadd( sql, " WHERE %s = %s;", pkey, id );
1685
1686         char* query = buffer_data(sql);
1687         buffer_free(sql);
1688
1689         osrfLogDebug(OSRF_LOG_MARK, "%s: Update SQL [%s]", MODULENAME, query);
1690
1691         dbi_result result = dbi_conn_query(dbhandle, query);
1692         free(query);
1693
1694         if (!result) {
1695                 jsonObjectFree(obj);
1696                 obj = jsonNewObject(NULL);
1697                 osrfLogError(
1698                         OSRF_LOG_MARK,
1699                         "%s ERROR updating %s object with %s = %s",
1700                         MODULENAME,
1701                         osrfHashGet(meta, "fieldmapper"),
1702                         pkey,
1703                         id
1704                 );
1705         }
1706
1707         free(id);
1708
1709         return obj;
1710 }
1711
1712 jsonObject* doDelete(osrfMethodContext* ctx, int* err ) {
1713
1714         osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
1715
1716         if (!osrfHashGet( (osrfHash*)ctx->session->userData, "xact_id" )) {
1717                 osrfAppSessionStatus(
1718                         ctx->session,
1719                         OSRF_STATUS_BADREQUEST,
1720                         "osrfMethodException",
1721                         ctx->request,
1722                         "No active transaction -- required for DELETE"
1723                 );
1724                 *err = -1;
1725                 return jsonNULL;
1726         }
1727
1728         dbhandle = writehandle;
1729
1730         jsonObject* obj;
1731
1732         char* pkey = osrfHashGet(meta, "primarykey");
1733
1734         char* id;
1735         if (jsonObjectGetIndex(ctx->params, 0)->classname) {
1736                 if (!verifyObjectClass(ctx, jsonObjectGetIndex( ctx->params, 0 ))) {
1737                         *err = -1;
1738                         return jsonNULL;
1739                 }
1740
1741                 id = jsonObjectToSimpleString(
1742                         jsonObjectGetIndex(
1743                                 jsonObjectGetIndex(ctx->params, 0),
1744                                 atoi( osrfHashGet( osrfHashGet( osrfHashGet(meta, "fields"), pkey ), "array_position") )
1745                         )
1746                 );
1747         } else {
1748                 id = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
1749         }
1750
1751         osrfLogDebug(
1752                 OSRF_LOG_MARK,
1753                 "%s deleting %s object with %s = %s",
1754                 MODULENAME,
1755                 osrfHashGet(meta, "fieldmapper"),
1756                 pkey,
1757                 id
1758         );
1759
1760         obj = jsonParseString(id);
1761
1762         if ( strcmp( osrfHashGet( osrfHashGet( osrfHashGet(meta, "fields"), pkey ), "primitive" ), "number" ) )
1763                 dbi_conn_quote_string(writehandle, &id);
1764
1765         dbi_result result = dbi_conn_queryf(writehandle, "DELETE FROM %s WHERE %s = %s;", osrfHashGet(meta, "tablename"), pkey, id);
1766
1767         if (!result) {
1768                 jsonObjectFree(obj);
1769                 obj = jsonNewObject(NULL);
1770                 osrfLogError(
1771                         OSRF_LOG_MARK,
1772                         "%s ERROR deleting %s object with %s = %s",
1773                         MODULENAME,
1774                         osrfHashGet(meta, "fieldmapper"),
1775                         pkey,
1776                         id
1777                 );
1778         }
1779
1780         free(id);
1781
1782         return obj;
1783
1784 }
1785
1786
1787 jsonObject* oilsMakeJSONFromResult( dbi_result result, osrfHash* meta) {
1788         if(!(result && meta)) return jsonNULL;
1789
1790         jsonObject* object = jsonParseString("[]");
1791         jsonObjectSetClass(object, osrfHashGet(meta, "classname"));
1792
1793         osrfHash* fields = osrfHashGet(meta, "fields");
1794
1795         osrfLogInternal(OSRF_LOG_MARK, "Setting object class to %s ", object->classname);
1796
1797         osrfHash* _f;
1798         time_t _tmp_dt;
1799         char dt_string[256];
1800         struct tm gmdt;
1801
1802         int fmIndex;
1803         int columnIndex = 1;
1804         int attr;
1805         unsigned short type;
1806         const char* columnName;
1807
1808         /* cycle through the column list */
1809         while( (columnName = dbi_result_get_field_name(result, columnIndex++)) ) {
1810
1811                 osrfLogInternal(OSRF_LOG_MARK, "Looking for column named [%s]...", (char*)columnName);
1812
1813                 fmIndex = -1; // reset the position
1814                 
1815                 /* determine the field type and storage attributes */
1816                 type = dbi_result_get_field_type(result, columnName);
1817                 attr = dbi_result_get_field_attribs(result, columnName);
1818
1819                 /* fetch the fieldmapper index */
1820                 if( (_f = osrfHashGet(fields, (char*)columnName)) ) {
1821                         char* virt = (char*)osrfHashGet(_f, "virtual");
1822                         char* pos = (char*)osrfHashGet(_f, "array_position");
1823
1824                         if ( !virt || !pos || !(strcmp( virt, "true" )) ) continue;
1825
1826                         fmIndex = atoi( pos );
1827                         osrfLogInternal(OSRF_LOG_MARK, "... Found column at position [%s]...", pos);
1828                 } else {
1829                         continue;
1830                 }
1831
1832                 if (dbi_result_field_is_null(result, columnName)) {
1833                         jsonObjectSetIndex( object, fmIndex, jsonNewObject(NULL) );
1834                 } else {
1835
1836                         switch( type ) {
1837
1838                                 case DBI_TYPE_INTEGER :
1839
1840                                         if( attr & DBI_INTEGER_SIZE8 ) 
1841                                                 jsonObjectSetIndex( object, fmIndex, 
1842                                                         jsonNewNumberObject(dbi_result_get_longlong(result, columnName)));
1843                                         else 
1844                                                 jsonObjectSetIndex( object, fmIndex, 
1845                                                         jsonNewNumberObject(dbi_result_get_long(result, columnName)));
1846
1847                                         break;
1848
1849                                 case DBI_TYPE_DECIMAL :
1850                                         jsonObjectSetIndex( object, fmIndex, 
1851                                                         jsonNewNumberObject(dbi_result_get_double(result, columnName)));
1852                                         break;
1853
1854                                 case DBI_TYPE_STRING :
1855
1856
1857                                         jsonObjectSetIndex(
1858                                                 object,
1859                                                 fmIndex,
1860                                                 jsonNewObject( dbi_result_get_string(result, columnName) )
1861                                         );
1862
1863                                         break;
1864
1865                                 case DBI_TYPE_DATETIME :
1866
1867                                         memset(dt_string, '\0', 256);
1868                                         memset(&gmdt, '\0', sizeof(gmdt));
1869                                         memset(&_tmp_dt, '\0', sizeof(_tmp_dt));
1870
1871                                         _tmp_dt = dbi_result_get_datetime(result, columnName);
1872
1873                                         localtime_r( &_tmp_dt, &gmdt );
1874
1875                                         if (!(attr & DBI_DATETIME_DATE)) {
1876                                                 strftime(dt_string, 255, "%T", &gmdt);
1877                                         } else if (!(attr & DBI_DATETIME_TIME)) {
1878                                                 strftime(dt_string, 255, "%F", &gmdt);
1879                                         } else {
1880                                                 strftime(dt_string, 255, "%FT%T%z", &gmdt);
1881                                         }
1882
1883                                         jsonObjectSetIndex( object, fmIndex, jsonNewObject(dt_string) );
1884
1885                                         break;
1886
1887                                 case DBI_TYPE_BINARY :
1888                                         osrfLogError( OSRF_LOG_MARK, 
1889                                                 "Can't do binary at column %s : index %d", columnName, columnIndex - 1);
1890                         }
1891                 }
1892         }
1893
1894         return object;
1895 }
1896