Index: /trunk/archive/psdb/psDB.c
===================================================================
--- /trunk/archive/psdb/psDB.c	(revision 3243)
+++ /trunk/archive/psdb/psDB.c	(revision 3244)
@@ -8,6 +8,6 @@
  *  @author Joshua Hoblitt
  *
- *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-02-16 02:56:45 $
+ *  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-02-16 21:43:35 $
  *
  *  Copyright 2005 Joshua Hoblitt, University of Hawaii
@@ -36,5 +36,5 @@
 // SQL generation functions
 static char    *psDBGenerateCreateTableSQL(const char *tableName, psMetadata *where);
-static char    *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, const psU64 limit);
+static char    *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, psU64 limit);
 static char    *psDBGenerateInsertRowSQL(const char *tableName, psMetadata *row);
 static char    *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values);
@@ -61,6 +61,6 @@
 
 static psPtr    psDBMySQLTypeAlloc(enum enum_field_types type, bool isUnsigned);
-static void     psDBAddToLookupTable(psHash *lookupTable, const psU32 type, const char *string);
-static void     psDBAddVoidToLookupTable(psHash *lookupTable, const psU32 type, psPtr value);
+static void     psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string);
+static void     psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value);
 
 // pType utility functions
@@ -85,12 +85,9 @@
     mysql = mysql_init(NULL);
     if (!mysql) {
-        // call abort?
-        fprintf(stderr, " mysql_init(), out of memory\n");
-
-        return NULL;
+         psAbort(__func__, "mysql_init(), out of memory.");
     }
 
     if (!mysql_real_connect(mysql, host, user, passwd, dbname, 0, NULL, 0)) {
-        fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));
+        psError(PS_ERR_UNKNOWN, true, "Failed to connect to database.  Error: %s", mysql_error(mysql));
 
         mysql_close(mysql);
@@ -129,4 +126,7 @@
     // the MySQL C API notes that mysql_create_db() is deprecated
     status = psDBRunQuery(dbh, query);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
+    }
 
     psFree(query);
@@ -138,5 +138,5 @@
 {
     if (mysql_select_db(dbh->mysql, dbname) != 0) {
-        fprintf(stderr, "Failed to change database.  Error: %s\n", mysql_error(dbh->mysql));
+        psError(PS_ERR_UNKNOWN, true, "Failed to change database.  Error: %s", mysql_error(dbh->mysql));
            
         return false;
@@ -155,4 +155,7 @@
     // the MySQL C API notes that mysql_drop_db() is deprecated
     status = psDBRunQuery(dbh, query);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to drop database.");
+    }
 
     psFree(query);
@@ -168,8 +171,13 @@
     query = psDBGenerateCreateTableSQL(tableName, md);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return NULL;
     }
 
     status = psDBRunQuery(dbh, query);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to create table.");
+    }
 
     psFree(query);
@@ -186,4 +194,7 @@
 
     status = psDBRunQuery(dbh, query);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to drop table.");
+    }
 
     psFree(query);
@@ -192,5 +203,5 @@
 }
 
-psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, const psU64 limit)
+psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, psU64 limit)
 {
     MYSQL_RES       *result;            // complete db result set
@@ -205,8 +216,12 @@
     query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return NULL;
     }
 
     if (!psDBRunQuery(dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "Query execution failed.");
+
         psFree(query);
 
@@ -224,6 +239,5 @@
         // then something bad has happened.
         if (fieldCount != 0) {
-            // error, should have gotten data
-            fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data.  Error: %s", mysql_error(dbh->mysql));
 
             return NULL;
@@ -285,4 +299,5 @@
     stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
     if (!stringColumn) {
+        // could be an error or the result set was just empty
         return NULL;
     }
@@ -361,9 +376,12 @@
     query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return NULL;
     }
 
     if (!psDBRunQuery(dbh, query)) {
-        // error
+        psError(PS_ERR_UNKNOWN, false, "Query execution failed.");
+
         psFree(query);
 
@@ -381,6 +399,5 @@
         // it's non-zero then something bad has happened.
         if (fieldCount != 0) {
-            // error, should have gotten data
-            fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data.  Error: %s", mysql_error(dbh->mysql));
 
             return NULL;
@@ -452,4 +469,6 @@
 
     if (!psDBInsertRows(dbh, tableName, rowSet)) {
+        psError(PS_ERR_UNKNOWN, false, "Insert failed.");
+
         psFree(rowSet);
 
@@ -477,4 +496,6 @@
     query = psDBGenerateInsertRowSQL(tableName, row);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return NULL;
     }
@@ -482,17 +503,14 @@
     stmt = mysql_stmt_init(dbh->mysql);
     if (!stmt) {
-        // call abort?
-        fprintf(stderr, " mysql_stmt_init(), out of memory\n");
+        psAbort(__func__, "mysql_stmt_init(), out of memory.");
+    }
+
+    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
+        psError(PS_ERR_UNKNOWN, true, "Failed to prepare query.  Error: %s", mysql_stmt_error(stmt));
+
+        mysql_stmt_close(stmt);
+        psFree(query);
 
         return false;
-    }
-
-    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
-	fprintf(stderr, "Failed to prepare query.  Error: %s\n", mysql_stmt_error(stmt));
-
-	mysql_stmt_close(stmt);
-	psFree(query);
-
-	return false;
     }
 
@@ -513,5 +531,5 @@
 
         if (!psDBPackRow(bind, row, paramCount)) {
-            fprintf(stderr, "Failed to pack params into bind structure.\n");
+            psError(PS_ERR_UNKNOWN, false, "Failed to pack params into bind structure.");
 
             mysql_rollback(dbh->mysql);
@@ -522,5 +540,5 @@
 
         if (mysql_stmt_bind_param(stmt, bind)) {
-            fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
+            psError(PS_ERR_UNKNOWN, true, "Failed to bind params.  Error: %s", mysql_stmt_error(stmt));
 
             mysql_rollback(dbh->mysql);
@@ -533,5 +551,6 @@
 
         if (mysql_stmt_execute(stmt)) {
-            fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
+            psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement.  Error: %s",
+                mysql_stmt_error(stmt));
 
             mysql_rollback(dbh->mysql);
@@ -570,4 +589,8 @@
     // find column types
     result = mysql_list_fields(dbh->mysql, tableName, NULL);
+    if (!result) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Failed to retrieve column types."); 
+    }
+
     field = mysql_fetch_fields(result);
     fieldCount = mysql_num_fields(result);
@@ -610,4 +633,6 @@
     query = psDBGenerateUpdateRowSQL(tableName, where, values);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return -1;
     }
@@ -615,17 +640,14 @@
     stmt = mysql_stmt_init(dbh->mysql);
     if (!stmt) {
-        // call abort?
-        fprintf(stderr, " mysql_stmt_init(), out of memory\n");
+        psAbort(__func__, "mysql_stmt_init(), out of memory.");
+    }
+
+    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
+        psError(PS_ERR_UNKNOWN, true, "Failed to prepare query.  Error: %s", mysql_stmt_error(stmt));
+
+        mysql_stmt_close(stmt);
+        psFree(query);
 
         return -1;
-    }
-
-    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
-            fprintf(stderr, "Failed to prepare query.  Error: %s\n", mysql_stmt_error(stmt));
-
-            mysql_stmt_close(stmt);
-            psFree(query);
-
-            return -1;
     }
 
@@ -649,5 +671,5 @@
 
     if (mysql_stmt_bind_param(stmt, bind)) {
-        fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
+        psError(PS_ERR_UNKNOWN, true, "Failed to bind params.  Error: %s", mysql_stmt_error(stmt));
 
         mysql_rollback(dbh->mysql);
@@ -660,5 +682,6 @@
 
     if (mysql_stmt_execute(stmt)) {
-        fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
+        psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement.  Error: %s",
+            mysql_stmt_error(stmt));
 
         mysql_rollback(dbh->mysql);
@@ -688,8 +711,12 @@
     query = psDBGenerateDeleteRowSQL(tableName, where);
     if (!query) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
+
         return -1;
     }
 
     if (!psDBRunQuery(dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "Delete failed.");
+
         mysql_rollback(dbh->mysql);
 
@@ -703,5 +730,4 @@
     if (!where) {
         // we truncated the table so mysql_affected_rows() won't work
-
         return 1;
     }
@@ -719,5 +745,5 @@
 {
     if (mysql_real_query(dbh->mysql, query, (unsigned long)strlen(query)) !=0) {
-        fprintf(stderr, "Failed to execute query.  Error: %s\n", mysql_error(dbh->mysql));
+        psError(PS_ERR_UNKNOWN, true, "Failed to execute query.  Error: %s\n", mysql_error(dbh->mysql));
            
         return false;
@@ -764,4 +790,7 @@
             } else {
                 // error, not a pointer to a string
+                psError(PS_ERR_BAD_PARAMETER_TYPE , true,
+                    "Only types of PS_META_PRIMITIVE and PS_META_STR are supported.");
+
                 psFree(cursor);
 
@@ -796,4 +825,6 @@
 
     if (!table) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, "table param may not be null.");
+
         return NULL;
     }
@@ -815,5 +846,7 @@
             psStringAppend(&query, "%s VARCHAR(%s)", item->name, item->data.V);
         } else {
-            // error, invalid type
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                "Only types of PS_META_PRIMITIVE and PS_META_STR are supported.");
+
             psFree(query);
             psFree(cursor);
@@ -864,4 +897,6 @@
         whereSQL = psDBGenerateWhereSQL(where);
         if (!whereSQL) {
+            psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
+
             psFree(query);
 
@@ -932,4 +967,6 @@
 
     if ((!values) || (!where)) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, "values and where params may not be NULL.");
+
         return NULL;
     }
@@ -937,4 +974,6 @@
     setSQL = psDBGenerateSetSQL(values);
     if (!setSQL) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
+
         return NULL;
     }
@@ -942,4 +981,6 @@
     whereSQL = psDBGenerateWhereSQL(where);
     if (!whereSQL) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
+
         return NULL;
     }
@@ -966,4 +1007,6 @@
     whereSQL = psDBGenerateWhereSQL(where);
     if (!whereSQL) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
+
         return NULL;
     }
@@ -981,4 +1024,6 @@
 
     if (!where) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, "where param may not be NULL.");
+
         return NULL;
     }
@@ -999,5 +1044,6 @@
             }
         } else {
-            // error, invalid where
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Only a type of PS_META_STR is supported.");
+
             psFree(cursor);
             psFree(query);
@@ -1024,4 +1070,6 @@
 
     if (!set) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, "set param may not be NULL.");
+
         return NULL;
     }
@@ -1065,8 +1113,15 @@
     // lookup MySQL column type
     key     = psDBIntToString((psU64)type); 
-    value   = psHashLookup(mysqlToSQLTable, key);
-    sqlType = psStringCopy(value);
+    sqlType = psHashLookup(mysqlToSQLTable, key);
     psFree(key);
     psFree(mysqlToSQLTable);
+
+    if (!sqlType) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
+
+        psFree(sqlToPSTable);
+
+        return -1;
+    }
 
     // MySQL column types can not be directly translated to PS
@@ -1080,7 +1135,13 @@
     // convert MySQL type to PS type
     value = psHashLookup(sqlToPSTable, sqlType);
+    psFree(sqlToPSTable);
+
+    if (!value) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
+
+        return -1;
+    }
+
     pType = (psU32)atol(value);
-    psFree(sqlType);
-    psFree(sqlToPSTable);
 
     return pType;
@@ -1098,6 +1159,11 @@
     sqlType = psHashLookup(pTypeToSQLTable, key);
     psFree(key);
-
     psFree(pTypeToSQLTable);
+
+    if (!sqlType) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
+
+        return NULL;
+    }
 
     psMemIncrRefCounter(sqlType);
@@ -1117,6 +1183,11 @@
     mType = psHashLookup(pTypeToMySQLTable, key);
     psFree(key);
-
     psFree(pTypeToMySQLTable);
+
+    if (!mType) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
+
+        return NULL;
+    }
 
     psMemIncrRefCounter(mType);
@@ -1306,5 +1377,5 @@
 }
 
-static void psDBAddToLookupTable(psHash *lookupTable, const psU32 type, const char *string)
+static void psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string)
 {
     char            *key;
@@ -1320,5 +1391,5 @@
 }
 
-static void psDBAddVoidToLookupTable(psHash *lookupTable, const psU32 type, psPtr value)
+static void psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value)
 {
     char            *key;
