[open-ils-commits] r13513 - trunk/Open-ILS/src/c-apps (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Tue Jul 7 10:32:14 EDT 2009


Author: scottmk
Date: 2009-07-07 10:32:12 -0400 (Tue, 07 Jul 2009)
New Revision: 13513

Modified:
   trunk/Open-ILS/src/c-apps/oils_cstore.c
Log:
More tinkering with oils_cstore.c:

1. In a couple of spots: replaced jsonObjectClone() with
jsonObjectExtractIndex(), so that we can use an existing
object instead of copying it.

2. In verifyObjectClass(): protected against formatting a NULL
into an error message.

3. In verifyObjectPCRUD(): eliminated a pointless strdup().

4. Rearranged doRetrieve() a bit for clarity, and renamed some
variables for the same reason.  Also added a couple of comments.


Modified: trunk/Open-ILS/src/c-apps/oils_cstore.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_cstore.c	2009-07-07 01:21:39 UTC (rev 13512)
+++ trunk/Open-ILS/src/c-apps/oils_cstore.c	2009-07-07 14:32:12 UTC (rev 13513)
@@ -869,6 +869,10 @@
 
     if (!param->classname || (strcmp( osrfHashGet(class, "classname"), param->classname ))) {
 
+		const char* temp_classname = param->classname;
+		if( ! temp_classname )
+			temp_classname = "(null)";
+
         growing_buffer* msg = buffer_init(128);
         buffer_fadd(
                 msg,
@@ -876,7 +880,7 @@
                 MODULENAME,
                 osrfHashGet(meta, "methodtype"),
                 osrfHashGet(class, "classname"),
-                param->classname
+                temp_classname
                 );
 
         char* m = buffer_release(msg);
@@ -926,22 +930,20 @@
 
 static int verifyObjectPCRUD (  osrfMethodContext* ctx, const jsonObject* obj ) {
 
-    dbhandle = writehandle;
+	dbhandle = writehandle;
 
-    osrfHash* meta = (osrfHash*) ctx->method->userData;
-    osrfHash* class = osrfHashGet( meta, "class" );
-    char* method_type = strdup( osrfHashGet(meta, "methodtype") );
-    int fetch = 0;
+	osrfHash* meta = (osrfHash*) ctx->method->userData;
+	osrfHash* class = osrfHashGet( meta, "class" );
+	const char* method_type = osrfHashGet( meta, "methodtype" );
+	int fetch = 0;
 
-    if ( ( *method_type == 's' || *method_type == 'i' ) ) {
-        free(method_type);
-        method_type = strdup("retrieve"); // search and id_list are equivelant to retrieve for this
-    } else if ( *method_type == 'u' || *method_type == 'd' ) {
-        fetch = 1; // MUST go to the db for the object for update and delete
-    }
+	if ( ( *method_type == 's' || *method_type == 'i' ) ) {
+		method_type = "retrieve"; // search and id_list are equivalant to retrieve for this
+	} else if ( *method_type == 'u' || *method_type == 'd' ) {
+		fetch = 1; // MUST go to the db for the object for update and delete
+	}
 
-    osrfHash* pcrud = osrfHashGet( osrfHashGet(class, "permacrud"), method_type );
-    free(method_type);
+	osrfHash* pcrud = osrfHashGet( osrfHashGet(class, "permacrud"), method_type );
 
     if (!pcrud) {
         // No permacrud for this method type on this class
@@ -1026,10 +1028,9 @@
 
         if (fetch) {
 			jsonObject* _tmp_params = single_hash( pkey, pkey_value );
-			jsonObject* _list = doFieldmapperSearch(
-					ctx, class, _tmp_params, NULL, &err );
+			jsonObject* _list = doFieldmapperSearch( ctx, class, _tmp_params, NULL, &err );
 
-			param = jsonObjectClone(jsonObjectGetIndex(_list, 0));
+			param = jsonObjectExtractIndex(_list, 0);
 
             jsonObjectFree(_tmp_params);
             jsonObjectFree(_list);
@@ -1553,7 +1554,10 @@
 
 }
 
-
+/*
+ * Fetch one row from a specified table, using a specified value
+ * for the primary key
+*/
 static jsonObject* doRetrieve(osrfMethodContext* ctx, int* err ) {
 
     int id_pos = 0;
@@ -1564,37 +1568,36 @@
     order_pos = 2;
 #endif
 
-	osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
+	osrfHash* class_def = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
 
-	const char* id = jsonObjectGetString(jsonObjectGetIndex(ctx->params, id_pos));
-	jsonObject* order_hash = jsonObjectGetIndex(ctx->params, order_pos);
+	const jsonObject* id_obj = jsonObjectGetIndex(ctx->params, id_pos);  // key value
 
 	osrfLogDebug(
 		OSRF_LOG_MARK,
 		"%s retrieving %s object with primary key value of %s",
 		MODULENAME,
-		osrfHashGet(meta, "fieldmapper"),
-		id
+		osrfHashGet( class_def, "fieldmapper" ),
+		jsonObjectGetString( id_obj )
 	);
 
-	jsonObject* key_param = jsonNewObjectType( JSON_HASH );
+	// Build a WHERE clause based on the key value
+	jsonObject* where_clause = jsonNewObjectType( JSON_HASH );
 	jsonObjectSetKey( 
-		key_param,
-		osrfHashGet( meta, "primarykey" ),
-		jsonObjectClone( jsonObjectGetIndex(ctx->params, id_pos) )
+		where_clause,
+		osrfHashGet( class_def, "primarykey" ),
+		jsonObjectClone( id_obj )
 	);
 
-	jsonObject* list = doFieldmapperSearch( ctx, meta, key_param, order_hash, err );
+	jsonObject* rest_of_query = jsonObjectGetIndex(ctx->params, order_pos);
 
-	if(*err) {
-		jsonObjectFree( key_param );
+	jsonObject* list = doFieldmapperSearch( ctx, class_def, where_clause, rest_of_query, err );
+
+	jsonObjectFree( where_clause );
+	if(*err)
 		return jsonNULL;
-	}
 
-	jsonObject* obj = jsonObjectClone( jsonObjectGetIndex(list, 0) );
-
+	jsonObject* obj = jsonObjectExtractIndex( list, 0 );
 	jsonObjectFree( list );
-	jsonObjectFree( key_param );
 
 #ifdef PCRUD
 	if(!verifyObjectPCRUD(ctx, obj)) {



More information about the open-ils-commits mailing list