IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3244


Ignore:
Timestamp:
Feb 16, 2005, 11:43:35 AM (21 years ago)
Author:
jhoblitt
Message:

change to psAbort() & psError() for error handling
remove const from some prototypes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/psdb/psDB.c

    r3243 r3244  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-16 02:56:45 $
     10 *  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-16 21:43:35 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    3636// SQL generation functions
    3737static char    *psDBGenerateCreateTableSQL(const char *tableName, psMetadata *where);
    38 static char    *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, const psU64 limit);
     38static char    *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, psU64 limit);
    3939static char    *psDBGenerateInsertRowSQL(const char *tableName, psMetadata *row);
    4040static char    *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values);
     
    6161
    6262static psPtr    psDBMySQLTypeAlloc(enum enum_field_types type, bool isUnsigned);
    63 static void     psDBAddToLookupTable(psHash *lookupTable, const psU32 type, const char *string);
    64 static void     psDBAddVoidToLookupTable(psHash *lookupTable, const psU32 type, psPtr value);
     63static void     psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string);
     64static void     psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value);
    6565
    6666// pType utility functions
     
    8585    mysql = mysql_init(NULL);
    8686    if (!mysql) {
    87         // call abort?
    88         fprintf(stderr, " mysql_init(), out of memory\n");
    89 
    90         return NULL;
     87         psAbort(__func__, "mysql_init(), out of memory.");
    9188    }
    9289
    9390    if (!mysql_real_connect(mysql, host, user, passwd, dbname, 0, NULL, 0)) {
    94         fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));
     91        psError(PS_ERR_UNKNOWN, true, "Failed to connect to database.  Error: %s", mysql_error(mysql));
    9592
    9693        mysql_close(mysql);
     
    129126    // the MySQL C API notes that mysql_create_db() is deprecated
    130127    status = psDBRunQuery(dbh, query);
     128    if (!status) {
     129        psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
     130    }
    131131
    132132    psFree(query);
     
    138138{
    139139    if (mysql_select_db(dbh->mysql, dbname) != 0) {
    140         fprintf(stderr, "Failed to change database.  Error: %s\n", mysql_error(dbh->mysql));
     140        psError(PS_ERR_UNKNOWN, true, "Failed to change database.  Error: %s", mysql_error(dbh->mysql));
    141141           
    142142        return false;
     
    155155    // the MySQL C API notes that mysql_drop_db() is deprecated
    156156    status = psDBRunQuery(dbh, query);
     157    if (!status) {
     158        psError(PS_ERR_UNKNOWN, false, "Failed to drop database.");
     159    }
    157160
    158161    psFree(query);
     
    168171    query = psDBGenerateCreateTableSQL(tableName, md);
    169172    if (!query) {
     173        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     174
    170175        return NULL;
    171176    }
    172177
    173178    status = psDBRunQuery(dbh, query);
     179    if (!status) {
     180        psError(PS_ERR_UNKNOWN, false, "Failed to create table.");
     181    }
    174182
    175183    psFree(query);
     
    186194
    187195    status = psDBRunQuery(dbh, query);
     196    if (!status) {
     197        psError(PS_ERR_UNKNOWN, false, "Failed to drop table.");
     198    }
    188199
    189200    psFree(query);
     
    192203}
    193204
    194 psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, const psU64 limit)
     205psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, psU64 limit)
    195206{
    196207    MYSQL_RES       *result;            // complete db result set
     
    205216    query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);
    206217    if (!query) {
     218        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     219
    207220        return NULL;
    208221    }
    209222
    210223    if (!psDBRunQuery(dbh, query)) {
     224        psError(PS_ERR_UNKNOWN, false, "Query execution failed.");
     225
    211226        psFree(query);
    212227
     
    224239        // then something bad has happened.
    225240        if (fieldCount != 0) {
    226             // error, should have gotten data
    227             fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
     241            psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data.  Error: %s", mysql_error(dbh->mysql));
    228242
    229243            return NULL;
     
    285299    stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
    286300    if (!stringColumn) {
     301        // could be an error or the result set was just empty
    287302        return NULL;
    288303    }
     
    361376    query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit);
    362377    if (!query) {
     378        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     379
    363380        return NULL;
    364381    }
    365382
    366383    if (!psDBRunQuery(dbh, query)) {
    367         // error
     384        psError(PS_ERR_UNKNOWN, false, "Query execution failed.");
     385
    368386        psFree(query);
    369387
     
    381399        // it's non-zero then something bad has happened.
    382400        if (fieldCount != 0) {
    383             // error, should have gotten data
    384             fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
     401            psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data.  Error: %s", mysql_error(dbh->mysql));
    385402
    386403            return NULL;
     
    452469
    453470    if (!psDBInsertRows(dbh, tableName, rowSet)) {
     471        psError(PS_ERR_UNKNOWN, false, "Insert failed.");
     472
    454473        psFree(rowSet);
    455474
     
    477496    query = psDBGenerateInsertRowSQL(tableName, row);
    478497    if (!query) {
     498        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     499
    479500        return NULL;
    480501    }
     
    482503    stmt = mysql_stmt_init(dbh->mysql);
    483504    if (!stmt) {
    484         // call abort?
    485         fprintf(stderr, " mysql_stmt_init(), out of memory\n");
     505        psAbort(__func__, "mysql_stmt_init(), out of memory.");
     506    }
     507
     508    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
     509        psError(PS_ERR_UNKNOWN, true, "Failed to prepare query.  Error: %s", mysql_stmt_error(stmt));
     510
     511        mysql_stmt_close(stmt);
     512        psFree(query);
    486513
    487514        return false;
    488     }
    489 
    490     if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
    491         fprintf(stderr, "Failed to prepare query.  Error: %s\n", mysql_stmt_error(stmt));
    492 
    493         mysql_stmt_close(stmt);
    494         psFree(query);
    495 
    496         return false;
    497515    }
    498516
     
    513531
    514532        if (!psDBPackRow(bind, row, paramCount)) {
    515             fprintf(stderr, "Failed to pack params into bind structure.\n");
     533            psError(PS_ERR_UNKNOWN, false, "Failed to pack params into bind structure.");
    516534
    517535            mysql_rollback(dbh->mysql);
     
    522540
    523541        if (mysql_stmt_bind_param(stmt, bind)) {
    524             fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
     542            psError(PS_ERR_UNKNOWN, true, "Failed to bind params.  Error: %s", mysql_stmt_error(stmt));
    525543
    526544            mysql_rollback(dbh->mysql);
     
    533551
    534552        if (mysql_stmt_execute(stmt)) {
    535             fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
     553            psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement.  Error: %s",
     554                mysql_stmt_error(stmt));
    536555
    537556            mysql_rollback(dbh->mysql);
     
    570589    // find column types
    571590    result = mysql_list_fields(dbh->mysql, tableName, NULL);
     591    if (!result) {
     592        psError(PS_ERR_UNEXPECTED_NULL, true, "Failed to retrieve column types.");
     593    }
     594
    572595    field = mysql_fetch_fields(result);
    573596    fieldCount = mysql_num_fields(result);
     
    610633    query = psDBGenerateUpdateRowSQL(tableName, where, values);
    611634    if (!query) {
     635        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     636
    612637        return -1;
    613638    }
     
    615640    stmt = mysql_stmt_init(dbh->mysql);
    616641    if (!stmt) {
    617         // call abort?
    618         fprintf(stderr, " mysql_stmt_init(), out of memory\n");
     642        psAbort(__func__, "mysql_stmt_init(), out of memory.");
     643    }
     644
     645    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
     646        psError(PS_ERR_UNKNOWN, true, "Failed to prepare query.  Error: %s", mysql_stmt_error(stmt));
     647
     648        mysql_stmt_close(stmt);
     649        psFree(query);
    619650
    620651        return -1;
    621     }
    622 
    623     if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
    624             fprintf(stderr, "Failed to prepare query.  Error: %s\n", mysql_stmt_error(stmt));
    625 
    626             mysql_stmt_close(stmt);
    627             psFree(query);
    628 
    629             return -1;
    630652    }
    631653
     
    649671
    650672    if (mysql_stmt_bind_param(stmt, bind)) {
    651         fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
     673        psError(PS_ERR_UNKNOWN, true, "Failed to bind params.  Error: %s", mysql_stmt_error(stmt));
    652674
    653675        mysql_rollback(dbh->mysql);
     
    660682
    661683    if (mysql_stmt_execute(stmt)) {
    662         fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
     684        psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement.  Error: %s",
     685            mysql_stmt_error(stmt));
    663686
    664687        mysql_rollback(dbh->mysql);
     
    688711    query = psDBGenerateDeleteRowSQL(tableName, where);
    689712    if (!query) {
     713        psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed.");
     714
    690715        return -1;
    691716    }
    692717
    693718    if (!psDBRunQuery(dbh, query)) {
     719        psError(PS_ERR_UNKNOWN, false, "Delete failed.");
     720
    694721        mysql_rollback(dbh->mysql);
    695722
     
    703730    if (!where) {
    704731        // we truncated the table so mysql_affected_rows() won't work
    705 
    706732        return 1;
    707733    }
     
    719745{
    720746    if (mysql_real_query(dbh->mysql, query, (unsigned long)strlen(query)) !=0) {
    721         fprintf(stderr, "Failed to execute query.  Error: %s\n", mysql_error(dbh->mysql));
     747        psError(PS_ERR_UNKNOWN, true, "Failed to execute query.  Error: %s\n", mysql_error(dbh->mysql));
    722748           
    723749        return false;
     
    764790            } else {
    765791                // error, not a pointer to a string
     792                psError(PS_ERR_BAD_PARAMETER_TYPE , true,
     793                    "Only types of PS_META_PRIMITIVE and PS_META_STR are supported.");
     794
    766795                psFree(cursor);
    767796
     
    796825
    797826    if (!table) {
     827        psError(PS_ERR_BAD_PARAMETER_NULL, true, "table param may not be null.");
     828
    798829        return NULL;
    799830    }
     
    815846            psStringAppend(&query, "%s VARCHAR(%s)", item->name, item->data.V);
    816847        } else {
    817             // error, invalid type
     848            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
     849                "Only types of PS_META_PRIMITIVE and PS_META_STR are supported.");
     850
    818851            psFree(query);
    819852            psFree(cursor);
     
    864897        whereSQL = psDBGenerateWhereSQL(where);
    865898        if (!whereSQL) {
     899            psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
     900
    866901            psFree(query);
    867902
     
    932967
    933968    if ((!values) || (!where)) {
     969        psError(PS_ERR_BAD_PARAMETER_NULL, true, "values and where params may not be NULL.");
     970
    934971        return NULL;
    935972    }
     
    937974    setSQL = psDBGenerateSetSQL(values);
    938975    if (!setSQL) {
     976        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
     977
    939978        return NULL;
    940979    }
     
    942981    whereSQL = psDBGenerateWhereSQL(where);
    943982    if (!whereSQL) {
     983        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
     984
    944985        return NULL;
    945986    }
     
    9661007    whereSQL = psDBGenerateWhereSQL(where);
    9671008    if (!whereSQL) {
     1009        psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed.");
     1010
    9681011        return NULL;
    9691012    }
     
    9811024
    9821025    if (!where) {
     1026        psError(PS_ERR_BAD_PARAMETER_NULL, true, "where param may not be NULL.");
     1027
    9831028        return NULL;
    9841029    }
     
    9991044            }
    10001045        } else {
    1001             // error, invalid where
     1046            psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Only a type of PS_META_STR is supported.");
     1047
    10021048            psFree(cursor);
    10031049            psFree(query);
     
    10241070
    10251071    if (!set) {
     1072        psError(PS_ERR_BAD_PARAMETER_NULL, true, "set param may not be NULL.");
     1073
    10261074        return NULL;
    10271075    }
     
    10651113    // lookup MySQL column type
    10661114    key     = psDBIntToString((psU64)type);
    1067     value   = psHashLookup(mysqlToSQLTable, key);
    1068     sqlType = psStringCopy(value);
     1115    sqlType = psHashLookup(mysqlToSQLTable, key);
    10691116    psFree(key);
    10701117    psFree(mysqlToSQLTable);
     1118
     1119    if (!sqlType) {
     1120        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
     1121
     1122        psFree(sqlToPSTable);
     1123
     1124        return -1;
     1125    }
    10711126
    10721127    // MySQL column types can not be directly translated to PS
     
    10801135    // convert MySQL type to PS type
    10811136    value = psHashLookup(sqlToPSTable, sqlType);
     1137    psFree(sqlToPSTable);
     1138
     1139    if (!value) {
     1140        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
     1141
     1142        return -1;
     1143    }
     1144
    10821145    pType = (psU32)atol(value);
    1083     psFree(sqlType);
    1084     psFree(sqlToPSTable);
    10851146
    10861147    return pType;
     
    10981159    sqlType = psHashLookup(pTypeToSQLTable, key);
    10991160    psFree(key);
    1100 
    11011161    psFree(pTypeToSQLTable);
     1162
     1163    if (!sqlType) {
     1164        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
     1165
     1166        return NULL;
     1167    }
    11021168
    11031169    psMemIncrRefCounter(sqlType);
     
    11171183    mType = psHashLookup(pTypeToMySQLTable, key);
    11181184    psFree(key);
    1119 
    11201185    psFree(pTypeToMySQLTable);
     1186
     1187    if (!mType) {
     1188        psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed.");
     1189
     1190        return NULL;
     1191    }
    11211192
    11221193    psMemIncrRefCounter(mType);
     
    13061377}
    13071378
    1308 static void psDBAddToLookupTable(psHash *lookupTable, const psU32 type, const char *string)
     1379static void psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string)
    13091380{
    13101381    char            *key;
     
    13201391}
    13211392
    1322 static void psDBAddVoidToLookupTable(psHash *lookupTable, const psU32 type, psPtr value)
     1393static void psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value)
    13231394{
    13241395    char            *key;
Note: See TracChangeset for help on using the changeset viewer.