Index: trunk/psLib/src/db/psDB.c
===================================================================
--- trunk/psLib/src/db/psDB.c	(revision 7651)
+++ trunk/psLib/src/db/psDB.c	(revision 7652)
@@ -12,6 +12,6 @@
  *  @author Joshua Hoblitt
  *
- *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-23 02:57:04 $
+ *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-23 03:49:16 $
  *
  *  Copyright 2005 Joshua Hoblitt, University of Hawaii
@@ -179,10 +179,5 @@
 void psDBCleanup(psDB *dbh)
 {
-    // Check if argument dbh is NULL
-    if(!dbh) {
-        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, );
 
     // Attempt to close specified database connection
@@ -216,11 +211,12 @@
                 const char *dbname)
 {
-    char            *query = NULL;
-    bool            status;
-
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(dbname, false);
+
+    char *query = NULL;
     psStringAppend(&query, "CREATE DATABASE %s", dbname);
 
     // the MySQL C API notes that mysql_create_db() is deprecated
-    status = p_psDBRunQuery(dbh, query);
+    bool status = p_psDBRunQuery(dbh, query);
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
@@ -235,10 +231,6 @@
                 const char *dbname)
 {
-    // Verify database object is valid
-    if(dbh == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(dbname, false);
 
     // Attempt to select new database
@@ -256,11 +248,12 @@
               const char *dbname)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(dbname, false);
+
     char            *query = NULL;
-    bool            status;
-
     psStringAppend(&query, "DROP DATABASE %s", dbname);
 
     // the MySQL C API notes that mysql_drop_db() is deprecated
-    status = p_psDBRunQuery(dbh, query);
+    bool status = p_psDBRunQuery(dbh, query);
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "Failed to drop database.");
@@ -276,15 +269,10 @@
                      const psMetadata *md)
 {
-    char            *query;
-    bool            status;
-
-    // Verify the database object is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(tableName, false);
+    PS_ASSERT_PTR_NON_NULL(md, false);
 
     // Generate SQL query string
-    query = psDBGenerateCreateTableSQL(tableName, md);
+    char *query = psDBGenerateCreateTableSQL(tableName, md);
     if (!query) {
         psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL);
@@ -293,10 +281,9 @@
 
     // Run SQL query to create table
-    status = p_psDBRunQuery(dbh, query);
+    bool status = p_psDBRunQuery(dbh, query);
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_CREATE_FAIL);
     }
 
-    // Free query string
     psFree(query);
 
@@ -307,12 +294,13 @@
                    const char *tableName)
 {
-    char            *query = NULL;
-    bool            status;
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(tableName, false);
 
     // Create SQL command string to drop table
+    char *query = NULL;
     psStringAppend(&query, "DROP TABLE %s", tableName);
 
     // Execute query
-    status = p_psDBRunQuery(dbh, query);
+    bool status = p_psDBRunQuery(dbh, query);
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_DROP_FAIL);
@@ -329,7 +317,10 @@
                           unsigned long long limit)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
+    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
+    PS_ASSERT_PTR_NON_NULL(col, NULL);
+
     MYSQL_RES       *result;            // complete db result set
     MYSQL_ROW       row;                // single row of db result set
-    char            *query;             // SQL query
     my_ulonglong    rowCount;           // number of rows in db result set
     unsigned long   dataSize;           // size of field
@@ -339,5 +330,5 @@
 
     // Generate SQL query string
-    query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
+    char *query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
     if (!query) {
         psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL);
@@ -434,8 +425,9 @@
                               unsigned long long limit)
 {
-    psArray         *stringColumn;      // source psArray
-    psVector        *column;            // dest psVector
-
-    stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
+    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
+    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
+    PS_ASSERT_PTR_NON_NULL(col, NULL);
+
+    psArray *stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
     if (!stringColumn) {
         // could be an error or the result set was just empty
@@ -443,5 +435,5 @@
     }
 
-    column = psVectorAlloc(stringColumn->n, type);
+    psVector *column = psVectorAlloc(stringColumn->n, type);
     //    column->n = column->nalloc;
 
@@ -521,4 +513,7 @@
                         unsigned long long limit)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
+    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
+
     // Create select row query
     char *query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit);
@@ -544,14 +539,10 @@
                       const psMetadata *row)
 {
-    psArray         *rowSet;           // psArray of row to insert
-
-    // Check for null row
-    if(row == NULL) {
-        psError(PS_ERR_UNKNOWN,true,PS_ERRORTEXT_psDB_INSERT_ROW_FAIL);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(tableName, false);
+    PS_ASSERT_PTR_NON_NULL(row, false);
 
     // Create array to store single row
-    rowSet = psArrayAlloc(1);
+    psArray *rowSet = psArrayAlloc(1);
     rowSet->n = 0;
     psArrayAdd(rowSet, 0, (psPtr)row);
@@ -602,4 +593,7 @@
                       const char *tableName)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(tableName, false);
+
     return psDBSelectRows(dbh, tableName, NULL, 0);
 }
@@ -608,23 +602,13 @@
                          const char *tableName)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, false);
+    PS_ASSERT_PTR_NON_NULL(tableName, false);
+
     MYSQL_RES       *result;
     MYSQL_FIELD     *field;
     unsigned int    fieldCount;
-    psMetadata      *table;
     psU32           pType;
     unsigned int    i;
     psPtr           column;
-
-    // Verify database object is not null
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return NULL;
-    }
-
-    // Verify table name is not null
-    if(tableName == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_NULL_TABLE);
-        return NULL;
-    }
 
     // find column types
@@ -636,6 +620,9 @@
     field = mysql_fetch_fields(result);
     fieldCount = mysql_num_fields(result);
-
-    table = psMetadataAlloc();
+    // Clean up mysql memory
+    mysql_free_result(result);
+
+    // this metadata is returned
+    psMetadata *table = psMetadataAlloc();
 
     // fetch each column and load into psMetadata
@@ -682,5 +669,4 @@
     psArray *rowData = psArrayAlloc(1);
     psArrayAdd(rowData, 0, values);
-
     long rowsAffected = p_psDBRunQueryPrepared(dbh, rowData, query);
     psFree(rowData);
@@ -699,15 +685,9 @@
                     unsigned long long limit)
 {
-    char            *query;
-    psS64           rows = 0;
-
-    // Verify database is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return -1;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
+    PS_ASSERT_PTR_NON_NULL(tableName, -1);
 
     // Create SQL statement string
-    query = psDBGenerateDeleteRowSQL(tableName, where,limit);
+    char *query = psDBGenerateDeleteRowSQL(tableName, where,limit);
     if (!query) {
         psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
@@ -725,5 +705,5 @@
 
     // Get the number of affected row for the delete command
-    rows = (psS64)mysql_affected_rows(dbh->mysql);
+    psS64 rows = (psS64)mysql_affected_rows(dbh->mysql);
 
     return rows;
@@ -732,4 +712,6 @@
 long psDBLastInsertID(psDB *dbh)
 {
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
+
     // XXX return type is actually my_ulonglong - should the return be psU64?
     return (long)mysql_insert_id(dbh->mysql);
@@ -738,9 +720,5 @@
 bool psDBExplicitTrans(psDB *dbh, bool mode)
 {
-    // Verify database is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
 
     // mode needs to be inverted as autocommits are the opposide of explicit
@@ -753,9 +731,5 @@
 bool psDBTransaction(psDB *dbh)
 {
-    // Verify database is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
 
     bool status = p_psDBRunQuery(dbh, "START TRANSACTION");
@@ -769,9 +743,5 @@
 bool psDBCommit(psDB *dbh)
 {
-    // Verify database is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
 
     // is it safe to assume my_bool always safely casts to bool?
@@ -782,9 +752,5 @@
 bool psDBRollback(psDB *dbh)
 {
-    // Verify database is not NULL
-    if(dbh == NULL) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(dbh, -1);
 
     // is it safe to assume my_bool always safely casts to bool?
