IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7652


Ignore:
Timestamp:
Jun 22, 2006, 5:49:16 PM (20 years ago)
Author:
jhoblitt
Message:

misc cleanups: assertions, memory leaks, etc. (unfinished)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/db/psDB.c

    r7651 r7652  
    1212 *  @author Joshua Hoblitt
    1313 *
    14  *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-06-23 02:57:04 $
     14 *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-06-23 03:49:16 $
    1616 *
    1717 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    179179void psDBCleanup(psDB *dbh)
    180180{
    181     // Check if argument dbh is NULL
    182     if(!dbh) {
    183         psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    184                 PS_ERRORTEXT_psDB_INVALID_PSDB);
    185         return;
    186     }
     181    PS_ASSERT_PTR_NON_NULL(dbh, );
    187182
    188183    // Attempt to close specified database connection
     
    216211                const char *dbname)
    217212{
    218     char            *query = NULL;
    219     bool            status;
    220 
     213    PS_ASSERT_PTR_NON_NULL(dbh, false);
     214    PS_ASSERT_PTR_NON_NULL(dbname, false);
     215
     216    char *query = NULL;
    221217    psStringAppend(&query, "CREATE DATABASE %s", dbname);
    222218
    223219    // the MySQL C API notes that mysql_create_db() is deprecated
    224     status = p_psDBRunQuery(dbh, query);
     220    bool status = p_psDBRunQuery(dbh, query);
    225221    if (!status) {
    226222        psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
     
    235231                const char *dbname)
    236232{
    237     // Verify database object is valid
    238     if(dbh == NULL) {
    239         psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    240                 PS_ERRORTEXT_psDB_INVALID_PSDB);
    241         return false;
    242     }
     233    PS_ASSERT_PTR_NON_NULL(dbh, false);
     234    PS_ASSERT_PTR_NON_NULL(dbname, false);
    243235
    244236    // Attempt to select new database
     
    256248              const char *dbname)
    257249{
     250    PS_ASSERT_PTR_NON_NULL(dbh, false);
     251    PS_ASSERT_PTR_NON_NULL(dbname, false);
     252
    258253    char            *query = NULL;
    259     bool            status;
    260 
    261254    psStringAppend(&query, "DROP DATABASE %s", dbname);
    262255
    263256    // the MySQL C API notes that mysql_drop_db() is deprecated
    264     status = p_psDBRunQuery(dbh, query);
     257    bool status = p_psDBRunQuery(dbh, query);
    265258    if (!status) {
    266259        psError(PS_ERR_UNKNOWN, false, "Failed to drop database.");
     
    276269                     const psMetadata *md)
    277270{
    278     char            *query;
    279     bool            status;
    280 
    281     // Verify the database object is not NULL
    282     if(dbh == NULL) {
    283         psError(PS_ERR_BAD_PARAMETER_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB);
    284         return false;
    285     }
     271    PS_ASSERT_PTR_NON_NULL(dbh, false);
     272    PS_ASSERT_PTR_NON_NULL(tableName, false);
     273    PS_ASSERT_PTR_NON_NULL(md, false);
    286274
    287275    // Generate SQL query string
    288     query = psDBGenerateCreateTableSQL(tableName, md);
     276    char *query = psDBGenerateCreateTableSQL(tableName, md);
    289277    if (!query) {
    290278        psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL);
     
    293281
    294282    // Run SQL query to create table
    295     status = p_psDBRunQuery(dbh, query);
     283    bool status = p_psDBRunQuery(dbh, query);
    296284    if (!status) {
    297285        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_CREATE_FAIL);
    298286    }
    299287
    300     // Free query string
    301288    psFree(query);
    302289
     
    307294                   const char *tableName)
    308295{
    309     char            *query = NULL;
    310     bool            status;
     296    PS_ASSERT_PTR_NON_NULL(dbh, false);
     297    PS_ASSERT_PTR_NON_NULL(tableName, false);
    311298
    312299    // Create SQL command string to drop table
     300    char *query = NULL;
    313301    psStringAppend(&query, "DROP TABLE %s", tableName);
    314302
    315303    // Execute query
    316     status = p_psDBRunQuery(dbh, query);
     304    bool status = p_psDBRunQuery(dbh, query);
    317305    if (!status) {
    318306        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_DROP_FAIL);
     
    329317                          unsigned long long limit)
    330318{
     319    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
     320    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
     321    PS_ASSERT_PTR_NON_NULL(col, NULL);
     322
    331323    MYSQL_RES       *result;            // complete db result set
    332324    MYSQL_ROW       row;                // single row of db result set
    333     char            *query;             // SQL query
    334325    my_ulonglong    rowCount;           // number of rows in db result set
    335326    unsigned long   dataSize;           // size of field
     
    339330
    340331    // Generate SQL query string
    341     query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
     332    char *query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
    342333    if (!query) {
    343334        psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL);
     
    434425                              unsigned long long limit)
    435426{
    436     psArray         *stringColumn;      // source psArray
    437     psVector        *column;            // dest psVector
    438 
    439     stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
     427    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
     428    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
     429    PS_ASSERT_PTR_NON_NULL(col, NULL);
     430
     431    psArray *stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
    440432    if (!stringColumn) {
    441433        // could be an error or the result set was just empty
     
    443435    }
    444436
    445     column = psVectorAlloc(stringColumn->n, type);
     437    psVector *column = psVectorAlloc(stringColumn->n, type);
    446438    //    column->n = column->nalloc;
    447439
     
    521513                        unsigned long long limit)
    522514{
     515    PS_ASSERT_PTR_NON_NULL(dbh, NULL);
     516    PS_ASSERT_PTR_NON_NULL(tableName, NULL);
     517
    523518    // Create select row query
    524519    char *query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit);
     
    544539                      const psMetadata *row)
    545540{
    546     psArray         *rowSet;           // psArray of row to insert
    547 
    548     // Check for null row
    549     if(row == NULL) {
    550         psError(PS_ERR_UNKNOWN,true,PS_ERRORTEXT_psDB_INSERT_ROW_FAIL);
    551         return false;
    552     }
     541    PS_ASSERT_PTR_NON_NULL(dbh, false);
     542    PS_ASSERT_PTR_NON_NULL(tableName, false);
     543    PS_ASSERT_PTR_NON_NULL(row, false);
    553544
    554545    // Create array to store single row
    555     rowSet = psArrayAlloc(1);
     546    psArray *rowSet = psArrayAlloc(1);
    556547    rowSet->n = 0;
    557548    psArrayAdd(rowSet, 0, (psPtr)row);
     
    602593                      const char *tableName)
    603594{
     595    PS_ASSERT_PTR_NON_NULL(dbh, false);
     596    PS_ASSERT_PTR_NON_NULL(tableName, false);
     597
    604598    return psDBSelectRows(dbh, tableName, NULL, 0);
    605599}
     
    608602                         const char *tableName)
    609603{
     604    PS_ASSERT_PTR_NON_NULL(dbh, false);
     605    PS_ASSERT_PTR_NON_NULL(tableName, false);
     606
    610607    MYSQL_RES       *result;
    611608    MYSQL_FIELD     *field;
    612609    unsigned int    fieldCount;
    613     psMetadata      *table;
    614610    psU32           pType;
    615611    unsigned int    i;
    616612    psPtr           column;
    617 
    618     // Verify database object is not null
    619     if(dbh == NULL) {
    620         psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB);
    621         return NULL;
    622     }
    623 
    624     // Verify table name is not null
    625     if(tableName == NULL) {
    626         psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_NULL_TABLE);
    627         return NULL;
    628     }
    629613
    630614    // find column types
     
    636620    field = mysql_fetch_fields(result);
    637621    fieldCount = mysql_num_fields(result);
    638 
    639     table = psMetadataAlloc();
     622    // Clean up mysql memory
     623    mysql_free_result(result);
     624
     625    // this metadata is returned
     626    psMetadata *table = psMetadataAlloc();
    640627
    641628    // fetch each column and load into psMetadata
     
    682669    psArray *rowData = psArrayAlloc(1);
    683670    psArrayAdd(rowData, 0, values);
    684 
    685671    long rowsAffected = p_psDBRunQueryPrepared(dbh, rowData, query);
    686672    psFree(rowData);
     
    699685                    unsigned long long limit)
    700686{
    701     char            *query;
    702     psS64           rows = 0;
    703 
    704     // Verify database is not NULL
    705     if(dbh == NULL) {
    706         psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
    707         return -1;
    708     }
     687    PS_ASSERT_PTR_NON_NULL(dbh, -1);
     688    PS_ASSERT_PTR_NON_NULL(tableName, -1);
    709689
    710690    // Create SQL statement string
    711     query = psDBGenerateDeleteRowSQL(tableName, where,limit);
     691    char *query = psDBGenerateDeleteRowSQL(tableName, where,limit);
    712692    if (!query) {
    713693        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     
    725705
    726706    // Get the number of affected row for the delete command
    727     rows = (psS64)mysql_affected_rows(dbh->mysql);
     707    psS64 rows = (psS64)mysql_affected_rows(dbh->mysql);
    728708
    729709    return rows;
     
    732712long psDBLastInsertID(psDB *dbh)
    733713{
     714    PS_ASSERT_PTR_NON_NULL(dbh, -1);
     715
    734716    // XXX return type is actually my_ulonglong - should the return be psU64?
    735717    return (long)mysql_insert_id(dbh->mysql);
     
    738720bool psDBExplicitTrans(psDB *dbh, bool mode)
    739721{
    740     // Verify database is not NULL
    741     if(dbh == NULL) {
    742         psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
    743         return false;
    744     }
     722    PS_ASSERT_PTR_NON_NULL(dbh, -1);
    745723
    746724    // mode needs to be inverted as autocommits are the opposide of explicit
     
    753731bool psDBTransaction(psDB *dbh)
    754732{
    755     // Verify database is not NULL
    756     if(dbh == NULL) {
    757         psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
    758         return false;
    759     }
     733    PS_ASSERT_PTR_NON_NULL(dbh, -1);
    760734
    761735    bool status = p_psDBRunQuery(dbh, "START TRANSACTION");
     
    769743bool psDBCommit(psDB *dbh)
    770744{
    771     // Verify database is not NULL
    772     if(dbh == NULL) {
    773         psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
    774         return false;
    775     }
     745    PS_ASSERT_PTR_NON_NULL(dbh, -1);
    776746
    777747    // is it safe to assume my_bool always safely casts to bool?
     
    782752bool psDBRollback(psDB *dbh)
    783753{
    784     // Verify database is not NULL
    785     if(dbh == NULL) {
    786         psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
    787         return false;
    788     }
     754    PS_ASSERT_PTR_NON_NULL(dbh, -1);
    789755
    790756    // is it safe to assume my_bool always safely casts to bool?
Note: See TracChangeset for help on using the changeset viewer.