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

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Mar 25 16:00:55 EDT 2010


Author: scottmk
Date: 2010-03-25 16:00:50 -0400 (Thu, 25 Mar 2010)
New Revision: 15993

Modified:
   trunk/Open-ILS/src/c-apps/oils_cstore.c
Log:
Reduce the use of conditional compilation, by replacing #ifdefs
with ordinary "if" tests whenever possible.

This commit is part of a project to refactor oil_cstore.c so that
some of its functionality can be used for other services.

M    Open-ILS/src/c-apps/oils_cstore.c


Modified: trunk/Open-ILS/src/c-apps/oils_cstore.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_cstore.c	2010-03-25 19:42:53 UTC (rev 15992)
+++ trunk/Open-ILS/src/c-apps/oils_cstore.c	2010-03-25 20:00:50 UTC (rev 15993)
@@ -21,11 +21,14 @@
 
 #ifdef RSTORE
 #  define MODULENAME "open-ils.reporter-store"
+#  define ENFORCE_PCRUD 0
 #else
 #  ifdef PCRUD
 #    define MODULENAME "open-ils.pcrud"
+#    define ENFORCE_PCRUD 1
 #  else
 #    define MODULENAME "open-ils.cstore"
+#    define ENFORCE_PCRUD 0
 #  endif
 #endif
 
@@ -80,10 +83,8 @@
 	int in_use;            // boolean
 };
 
-#ifdef PCRUD
 static int timeout_needs_resetting;
 static time_t time_next_reset;
-#endif
 
 int osrfAppChildInit();
 int osrfAppInitialize();
@@ -149,13 +150,14 @@
 static ClassInfo* add_joined_class( const char* alias, const char* classname );
 static void clear_query_stack( void );
 
-#ifdef PCRUD
 static const jsonObject* verifyUserPCRUD( osrfMethodContext* );
 static int verifyObjectPCRUD( osrfMethodContext*, const jsonObject* );
 static char* org_tree_root( osrfMethodContext* ctx );
 static jsonObject* single_hash( const char* key, const char* value );
-#endif
 
+static const int enforce_pcrud = ENFORCE_PCRUD;     // Boolean
+static const char modulename[] = MODULENAME;
+
 static int child_initialized = 0;   /* boolean */
 
 static dbi_conn writehandle; /* our MASTER db connection */
@@ -246,13 +248,14 @@
 		return 1; /* return non-zero to indicate error */
 
 	growing_buffer* method_name = buffer_init(64);
-#ifndef PCRUD
-	// Generic search thingy
-	buffer_add(method_name, MODULENAME);
-	buffer_add(method_name, ".json_query");
-	osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
+
+	if( ! enforce_pcrud ) {
+		// Generic search thingy (not for PCRUD)
+		buffer_add(method_name, MODULENAME);
+		buffer_add(method_name, ".json_query");
+		osrfAppRegisterMethod( MODULENAME, OSRF_BUFFER_C_STR(method_name),
 			"doJSONSearch", "", 1, OSRF_METHOD_STREAMING );
-#endif
+	}
 
 	// first we register all the transaction and savepoint methods
 	buffer_reset(method_name);
@@ -336,14 +339,17 @@
 			continue;
 		}
 
-#ifdef PCRUD
-		// For PCRUD, ignore classes with no permacrud attribute
-		osrfHash* idlClass_permacrud = osrfHashGet(idlClass, "permacrud");
-		if (!idlClass_permacrud) {
-			osrfLogDebug( OSRF_LOG_MARK, "Skipping class \"%s\"; no permacrud in IDL", classname );
-			continue;
+		osrfHash* idlClass_permacrud = NULL;
+		if( enforce_pcrud ) {
+			// For PCRUD, ignore classes with no permacrud section
+			idlClass_permacrud = osrfHashGet(idlClass, "permacrud");
+			if (!idlClass_permacrud) {
+				osrfLogDebug( OSRF_LOG_MARK, 
+					"Skipping class \"%s\"; no permacrud in IDL", classname );
+				continue;
+			}
 		}
-#endif
+
 		const char* readonly = osrfHashGet(idlClass, "readonly");
 
 		int i;
@@ -352,16 +358,16 @@
 			osrfLogDebug(OSRF_LOG_MARK,
 				"Using files to build %s class methods for %s", method_type, classname);
 
-#ifdef PCRUD
-			// Treat "id_list" or "search" as forms of "retrieve"
-			const char* tmp_method = method_type;
-			if ( *tmp_method == 'i' || *tmp_method == 's') {  // "id_list" or "search"
-				tmp_method = "retrieve";
+			if( enforce_pcrud ) {
+				// Treat "id_list" or "search" as forms of "retrieve"
+				const char* tmp_method = method_type;
+				if ( *tmp_method == 'i' || *tmp_method == 's') {  // "id_list" or "search"
+					tmp_method = "retrieve";
+				}
+				// Skip this method if there is no permacrud entry for it
+				if (!osrfHashGet( idlClass_permacrud, tmp_method ))
+					continue;
 			}
-			// Skip this method if there is no permacrud entry for it
-			if (!osrfHashGet( idlClass_permacrud, tmp_method ))
-				continue;
-#endif
 
 			// No create, update, or delete methods for a readonly class
 			if ( str_is_true( readonly )
@@ -371,28 +377,28 @@
 			buffer_reset( method_name );
 
 			// Build the method name
-#ifdef PCRUD
-			// For PCRUD: MODULENAME.method_type.classname
-			buffer_fadd(method_name, "%s.%s.%s", MODULENAME, method_type, classname);
-#else
-			// For non-PCRUD: MODULENAME.MODULENAME.direct.XXX.method_type
-			// where XXX is the fieldmapper name from the IDL, with every run of
-			// one or more consecutive colons replaced by a period.
-			char* st_tmp = NULL;
-			char* part = NULL;
-			char* _fm = strdup( idlClass_fieldmapper );
-			part = strtok_r(_fm, ":", &st_tmp);
+			if( enforce_pcrud ) {
+				// For PCRUD: MODULENAME.method_type.classname
+				buffer_fadd(method_name, "%s.%s.%s", modulename, method_type, classname);
+			} else {
+				// For non-PCRUD: MODULENAME.MODULENAME.direct.XXX.method_type
+				// where XXX is the fieldmapper name from the IDL, with every run of
+				// one or more consecutive colons replaced by a period.
+				char* st_tmp = NULL;
+				char* part = NULL;
+				char* _fm = strdup( idlClass_fieldmapper );
+				part = strtok_r(_fm, ":", &st_tmp);
 
-			buffer_fadd(method_name, "%s.direct.%s", MODULENAME, part);
+				buffer_fadd(method_name, "%s.direct.%s", modulename, part);
 
-			while ((part = strtok_r(NULL, ":", &st_tmp))) {
+				while ((part = strtok_r(NULL, ":", &st_tmp))) {
+					OSRF_BUFFER_ADD_CHAR(method_name, '.');
+					OSRF_BUFFER_ADD(method_name, part);
+				}
 				OSRF_BUFFER_ADD_CHAR(method_name, '.');
-				OSRF_BUFFER_ADD(method_name, part);
+				OSRF_BUFFER_ADD(method_name, method_type);
+				free(_fm);
 			}
-			OSRF_BUFFER_ADD_CHAR(method_name, '.');
-			OSRF_BUFFER_ADD(method_name, method_type);
-			free(_fm);
-#endif
 
 			// For an id_list or search method we specify the OSRF_METHOD_STREAMING option.
 			// The consequence is that we implicitly create an atomic method in addition to
@@ -763,7 +769,6 @@
 }
 /*@}*/
 
-#ifdef PCRUD
 /**
 	@brief Save the user's login in the userData for the current application session.
 	@param ctx Pointer to the method context.
@@ -921,7 +926,6 @@
 	else
 		return NULL;
 }
-#endif
 
 /**
 	@brief Implement the transaction.begin method.
@@ -941,12 +945,12 @@
 		return -1;
 	}
 
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	dbi_result result = dbi_conn_query(writehandle, "START TRANSACTION;");
 	if (!result) {
@@ -983,13 +987,13 @@
 	}
 
 	int spNamePos = 0;
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	spNamePos = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		spNamePos = 1;
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	// Verify that a transaction is pending
 	const char* trans_id = getXactId( ctx );
@@ -1047,13 +1051,13 @@
 	}
 
 	int spNamePos = 0;
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	spNamePos = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		spNamePos = 1;
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	// Verify that a transaction is pending
 	const char* trans_id = getXactId( ctx );
@@ -1111,13 +1115,13 @@
 	}
 
 	int spNamePos = 0;
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	spNamePos = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		spNamePos = 1;
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	// Verify that a transaction is pending
 	const char* trans_id = getXactId( ctx );
@@ -1173,12 +1177,12 @@
 		return -1;
 	}
 
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	// Verify that a transaction is pending
 	const char* trans_id = getXactId( ctx );
@@ -1221,12 +1225,12 @@
 		return -1;
 	}
 
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-	const jsonObject* user = verifyUserPCRUD( ctx );
-	if (!user)
-		return -1;
-#endif
+	if( enforce_pcrud ) {
+		timeout_needs_resetting = 1;
+		const jsonObject* user = verifyUserPCRUD( ctx );
+		if (!user)
+			return -1;
+	}
 
 	// Verify that a transaction is pending
 	const char* trans_id = getXactId( ctx );
@@ -1271,9 +1275,8 @@
 	int err = 0;                // to be returned to caller
 	jsonObject * obj = NULL;    // to be returned to client
 
-#ifdef PCRUD
-	timeout_needs_resetting = 1;
-#endif
+	if( enforce_pcrud )
+		timeout_needs_resetting = 1;
 
 	// Get the method type so that we can branch on it
 	osrfHash* method_meta = (osrfHash*) ctx->method->userData;
@@ -1308,13 +1311,13 @@
 		jsonObject* where_clause;
 		jsonObject* rest_of_query;
 
-#ifdef PCRUD
-		where_clause  = jsonObjectGetIndex( ctx->params, 1 );
-		rest_of_query = jsonObjectGetIndex( ctx->params, 2 );
-#else
-		where_clause  = jsonObjectGetIndex( ctx->params, 0 );
-		rest_of_query = jsonObjectGetIndex( ctx->params, 1 );
-#endif
+		if( enforce_pcrud ) {
+			where_clause  = jsonObjectGetIndex( ctx->params, 1 );
+			rest_of_query = jsonObjectGetIndex( ctx->params, 2 );
+		} else {
+			where_clause  = jsonObjectGetIndex( ctx->params, 0 );
+			rest_of_query = jsonObjectGetIndex( ctx->params, 1 );
+		}
 
 		// Do the query
 		osrfHash* class_obj = osrfHashGet( method_meta, "class" );
@@ -1327,10 +1330,8 @@
 		jsonObject* cur = 0;
 		unsigned long res_idx = 0;
 		while((cur = jsonObjectGetIndex( obj, res_idx++ ) )) {
-#ifdef PCRUD
-			if(!verifyObjectPCRUD(ctx, cur))
+			if( enforce_pcrud && !verifyObjectPCRUD(ctx, cur))
 				continue;
-#endif
 			osrfAppRespond( ctx, cur );
 		}
 		osrfAppRespondComplete( ctx, NULL );
@@ -1352,13 +1353,13 @@
 		// We use the where clause without change.  But we need to massage the rest of the
 		// query, so we work with a copy of it instead of modifying the original.
 
-#ifdef PCRUD
-		where_clause  = jsonObjectGetIndex( ctx->params, 1 );
-		rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 2 ) );
-#else
-		where_clause  = jsonObjectGetIndex( ctx->params, 0 );
-		rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 1 ) );
-#endif
+		if( enforce_pcrud ) {
+			where_clause  = jsonObjectGetIndex( ctx->params, 1 );
+			rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 2 ) );
+		} else {
+			where_clause  = jsonObjectGetIndex( ctx->params, 0 );
+			rest_of_query = jsonObjectClone( jsonObjectGetIndex( ctx->params, 1 ) );
+		}
 
 		// Eliminate certain SQL clauses, if present
 		if ( rest_of_query ) {
@@ -1396,13 +1397,10 @@
 		jsonObject* cur;
 		unsigned long res_idx = 0;
 		while((cur = jsonObjectGetIndex( obj, res_idx++ ) )) {
-#ifdef PCRUD
-			if(!verifyObjectPCRUD(ctx, cur))
+			if( enforce_pcrud && !verifyObjectPCRUD(ctx, cur))
 				continue;
-#endif
 			osrfAppRespond( ctx,
-				oilsFMGetObject( cur, osrfHashGet( class_obj, "primarykey" ) )
-			);
+				oilsFMGetObject( cur, osrfHashGet( class_obj, "primarykey" ) ) );
 		}
 		osrfAppRespondComplete( ctx, NULL );
 
@@ -1451,16 +1449,12 @@
 		return 0;
 	}
 
-	int ret = 1;
-#ifdef PCRUD
-	ret = verifyObjectPCRUD( ctx, param );
-#endif
-
-	return ret;
+	if( enforce_pcrud )
+		return verifyObjectPCRUD( ctx, param );
+	else
+		return 1;
 }
 
-#ifdef PCRUD
-
 /**
 	@brief (PCRUD only) Verify that the user is properly logged in.
 	@param ctx Pointer to the method context.
@@ -1971,20 +1965,22 @@
 	jsonObjectSetKey( hash, key, jsonNewObject( value ) );
 	return hash;
 }
-#endif
 
 
 static jsonObject* doCreate(osrfMethodContext* ctx, int* err ) {
 
 	osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
-#ifdef PCRUD
-	jsonObject* target = jsonObjectGetIndex( ctx->params, 1 );
-	jsonObject* options = jsonObjectGetIndex( ctx->params, 2 );
-#else
-	jsonObject* target = jsonObjectGetIndex( ctx->params, 0 );
-	jsonObject* options = jsonObjectGetIndex( ctx->params, 1 );
-#endif
+	jsonObject* target = NULL;
+	jsonObject* options = NULL;
 
+	if( enforce_pcrud ) {
+		target = jsonObjectGetIndex( ctx->params, 1 );
+		options = jsonObjectGetIndex( ctx->params, 2 );
+	} else {
+		target = jsonObjectGetIndex( ctx->params, 0 );
+		options = jsonObjectGetIndex( ctx->params, 1 );
+	}
+
 	if (!verifyObjectClass(ctx, target)) {
 		*err = -1;
 		return jsonNULL;
@@ -2234,10 +2230,10 @@
 	int id_pos = 0;
 	int order_pos = 1;
 
-#ifdef PCRUD
-	id_pos = 1;
-	order_pos = 2;
-#endif
+	if( enforce_pcrud ) {
+		id_pos = 1;
+		order_pos = 2;
+	}
 
 	// Get the class metadata
 	osrfHash* class_def = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
@@ -2273,24 +2269,24 @@
 	jsonObject* obj = jsonObjectExtractIndex( list, 0 );
 	jsonObjectFree( list );
 
-#ifdef PCRUD
-	if(!verifyObjectPCRUD(ctx, obj)) {
-		jsonObjectFree(obj);
-		*err = -1;
+	if( enforce_pcrud ) {
+		if(!verifyObjectPCRUD(ctx, obj)) {
+			jsonObjectFree(obj);
+			*err = -1;
 
-		growing_buffer* msg = buffer_init(128);
-		OSRF_BUFFER_ADD( msg, MODULENAME );
-		OSRF_BUFFER_ADD( msg, ": Insufficient permissions to retrieve object" );
+			growing_buffer* msg = buffer_init(128);
+			OSRF_BUFFER_ADD( msg, MODULENAME );
+			OSRF_BUFFER_ADD( msg, ": Insufficient permissions to retrieve object" );
 
-		char* m = buffer_release(msg);
-		osrfAppSessionStatus( ctx->session, OSRF_STATUS_NOTALLOWED, "osrfMethodException",
-				ctx->request, m );
+			char* m = buffer_release(msg);
+			osrfAppSessionStatus( ctx->session, OSRF_STATUS_NOTALLOWED, "osrfMethodException",
+					ctx->request, m );
 
-		free(m);
+			free(m);
 
-		return jsonNULL;
+			return jsonNULL;
+		}
 	}
-#endif
 
 	return obj;
 }
@@ -5592,12 +5588,13 @@
 static jsonObject* doUpdate(osrfMethodContext* ctx, int* err ) {
 
 	osrfHash* meta = osrfHashGet( (osrfHash*) ctx->method->userData, "class" );
-#ifdef PCRUD
-	jsonObject* target = jsonObjectGetIndex( ctx->params, 1 );
-#else
-	jsonObject* target = jsonObjectGetIndex( ctx->params, 0 );
-#endif
 
+	jsonObject* target = NULL;
+	if( enforce_pcrud )
+		target = jsonObjectGetIndex( ctx->params, 1 );
+	else
+		target = jsonObjectGetIndex( ctx->params, 0 );
+
 	if (!verifyObjectClass(ctx, target)) {
 		*err = -1;
 		return jsonNULL;
@@ -5833,9 +5830,8 @@
 	char* pkey = osrfHashGet(meta, "primarykey");
 
 	int _obj_pos = 0;
-#ifdef PCRUD
+	if( enforce_pcrud )
 		_obj_pos = 1;
-#endif
 
 	char* id;
 	if (jsonObjectGetIndex(ctx->params, _obj_pos)->classname) {
@@ -5846,12 +5842,10 @@
 
 		id = oilsFMGetString( jsonObjectGetIndex(ctx->params, _obj_pos), pkey );
 	} else {
-#ifdef PCRUD
-		if (!verifyObjectPCRUD( ctx, NULL )) {
+		if( enforce_pcrud && !verifyObjectPCRUD( ctx, NULL )) {
 			*err = -1;
 			return jsonNULL;
 		}
-#endif
 		id = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, _obj_pos));
 	}
 



More information about the open-ils-commits mailing list