#include #include #include #include #include "libdb.h" #define MAXARGS 5 char **SwRI_dbHierarchy (int numargs, char **args); char **SQL_dbHierarchy (int numargs, char **args); char **dbHierarchy (int numargs, ...) { va_list argp; char *args [MAXARGS]; int i; assert (numargs < 5); memset (args, 0, sizeof (args)); va_start (argp, numargs); for (i = 0; i < numargs; i++) args [i] = va_arg (argp, char *); va_end (argp); #ifdef ALL_DB if (dbGetDatabaseType () == SwRI) { return (SwRI_dbHierarchy (numargs, args)); } else #endif return (SQL_dbHierarchy (numargs, args)); } char **SQL_dbHierarchy (int numargs, char **args) { char query_str [1024]; char **ret_val = NULL; int num; DB_RESULT *result; DB_ROW row; /* build up my query */ strcpy (query_str, "SELECT DISTINCT ("); switch (numargs) { case 0 : strcat (query_str, "project"); break; case 1 : strcat (query_str, "mission"); break; case 2 : strcat (query_str, "experiment"); break; case 3 : strcat (query_str, "instrument"); break; case 4 : strcat (query_str, "virtual"); break; } strcat (query_str, ") FROM hierarchy"); if (numargs != 0) strcat (query_str, " WHERE "); switch (numargs) { case 4 : strcat (query_str, "instrument = '"); strcat (query_str, args [3]); strcat (query_str, "' AND "); case 3 : strcat (query_str, "experiment = '"); strcat (query_str, args [2]); strcat (query_str, "' AND "); case 2 : strcat (query_str, "mission = '"); strcat (query_str, args [1]); strcat (query_str, "' AND "); case 1 : strcat (query_str, "project = '"); strcat (query_str, args [0]); strcat (query_str, "'"); case 0 : strcat (query_str, " ORDER BY data_key;"); } /* query the database */ if ((result = (DB_RESULT *) dbQueryStore (query_str)) != NULL) { ret_val = (char **) malloc ((dbNumberRows (result) + 1) * sizeof (char *)); num = 0; while ((row = (DB_ROW) dbFetchRow (result))) { ret_val [num] = strdup (row [0]); num++; } dbFreeResult (result); ret_val [num] = NULL; } return ret_val; } void dbFreeHierarchyList (char **ret_val) { int num = 0; while (ret_val [num] != NULL) { free (ret_val [num]); num++; } free (ret_val); }