IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3157


Ignore:
Timestamp:
Feb 7, 2005, 7:03:09 PM (21 years ago)
Author:
jhoblitt
Message:

improve code comments
additional error checking
macro cleanups
code tidying & ordering changes

File:
1 edited

Legend:

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

    r3154 r3157  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-08 02:55:32 $
     10 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-08 05:03:09 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    8888    psFree(dbh);
    8989
    90     // these lookup tables should probably be moved into psDB
     90    // these lookup tables should probably be moved into psDB to prevent them
     91    // from being flushed in the case that a database handle is closed while
     92    // one or more are still open
    9193    psDBMySQLToSQLTableCleanup();
    9294    psDBPSToSQLTableCleanup();
     
    102104    // init query with a psMemBlock
    103105    query = psStringCopy("CREATE DATABASE ");
     106
    104107    psStringAppend(&query, dbname);
    105108
     
    114117bool psDBChange(psDB *dbh, const char *dbname)
    115118{
    116     if(mysql_select_db(dbh->mysql, dbname) != 0) {
     119    if (mysql_select_db(dbh->mysql, dbname) != 0) {
    117120        fprintf(stderr, "Failed to change database.  Error: %s\n", mysql_error(dbh->mysql));
    118121           
     
    130133    // init query with a psMemBlock
    131134    query = psStringCopy("DROP DATABASE ");
     135
    132136    psStringAppend(&query, dbname);
    133137   
     
    146150
    147151    query = psDBGenerateCreateTableSQL(tableName, md);
    148 
    149     fprintf(stderr, "complete query is: %s\n", query);
     152    if (!query) {
     153        return NULL;
     154    }
    150155
    151156    status = psDBRunQuery(dbh, query);
     157
    152158    psFree(query);
    153159
     
    162168    // init query with a psMemBlock
    163169    query = psStringCopy("DROP TABLE ");
     170
    164171    psStringAppend(&query, tableName);
    165172
     
    173180psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, const psU64 limit)
    174181{
    175     MYSQL_RES       *result;
     182    MYSQL_RES       *result;            // complete db result set
    176183    MYSQL_ROW       row;
    177184    char            *query;
    178     psU64           rowCount;
    179     psU64           dataSize;
    180     unsigned int    fieldCount;
    181     psArray         *column = NULL;
    182     psPtr           data;
     185    psU64           rowCount;           // number of rows in db result set
     186    psU64           dataSize;           // size of field
     187    unsigned int    fieldCount;         // number of fields in db result set
     188    psArray         *column = NULL;     // return array
     189    psPtr           data;               // copy of result field
    183190
    184191    query = psDBGenerateSelectColumnSQL(tableName, col, limit);
    185 
    186     if (psDBRunQuery(dbh, query)) {
    187         result = mysql_store_result(dbh->mysql);
    188     } else {
    189         // error
     192    if (!query) {
     193        return NULL;
     194    }
     195
     196    if (!psDBRunQuery(dbh, query)) {
    190197        psFree(query);
    191198
     
    193200    }
    194201
    195     if (result) {
    196         rowCount = (psU64)mysql_num_rows(result);
    197 
    198         // pre-allocate enough elements to hold the complete result set
    199         // then reset n to 0 so elements are added from the beginning of
    200         // the array
    201         column = psArrayAlloc(rowCount);
    202         column->n = 0;
    203         //column = psArrayAlloc(0);
    204 
    205         while ((row = mysql_fetch_row(result))) {
    206             dataSize = (psU64)(mysql_fetch_lengths(result))[0];
    207 
    208             fprintf(stderr, "data: %s, size %d\n", row[0], (int)dataSize);
    209 
    210             //data = psAlloc(dataSize + 1);
    211             //memcpy(data, row[0], dataSize);
    212 
    213             if (row[0] == NULL) {
    214                 data = psStringCopy("");
    215                 fprintf(stderr, "data is null\n");
    216                 fprintf(stderr, "new data: %s %d\n", (char *)data, (int)strlen(data));
    217             } else {
    218                 data = psStringNCopy(row[0], dataSize);
    219             }
    220 
    221             psArrayAdd(column, 0, data);
    222         }
    223     } else {
     202    result = mysql_store_result(dbh->mysql);
     203    if (!result) {
    224204        // no result set
    225205        fieldCount = mysql_field_count(dbh->mysql);
    226206
    227         if (fieldCount == 0) {
    228             // query result was empty
    229 
    230         } else {
     207        // if field count is zero the query should have returned no data.  If
     208        // it's non-zero then something bad has happened.
     209        if (fieldCount != 0) {
    231210            // error, should have gotten data
    232211            fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
    233         }
     212
     213            psFree(query);
     214
     215            return NULL;
     216        }
     217    }
     218
     219    rowCount = (psU64)mysql_num_rows(result);
     220
     221    // pre-allocate enough elements to hold the complete result set
     222    // then reset n to 0 so elements are added from the beginning of
     223    // the array
     224    column = psArrayAlloc(rowCount);
     225    column->n = 0;
     226
     227    while ((row = mysql_fetch_row(result))) {
     228        // get the first element of lengths array that is part of the
     229        // result set
     230        dataSize = (psU64)*mysql_fetch_lengths(result);
     231
     232        // represent NULL as an empty string
     233        if (row[0] == NULL) {
     234            data = psStringCopy("");
     235        } else {
     236            data = psStringNCopy(row[0], dataSize);
     237        }
     238
     239        // add field to return array
     240        psArrayAdd(column, 0, data);
    234241    }
    235242
     
    240247}
    241248
    242 // a = assign too, b = source psArray, c = conversion function, d = type to cast to, e = psElemType
    243 #define STRINGARRAYTOPTYPE(a, b, c, d, e)   for (i = 0; i < b->n; i++) { \
    244                                                 if (strlen(b->data[i])) { \
    245                                                     a = (d)atoi(b->data[i]); \
    246                                                 } else { \
    247                                                     myNaN = psDBGetPTypeNaN(e); \
    248                                                     a = *(d *)myNaN; \
    249                                                     psFree(myNaN); \
    250                                                 } \
    251                                             }
     249// dest = assign too, source = source string psArray, conv = conversion function,
     250// type = type to cast to, pType = psElemType
     251#define STRINGARRAYTOPTYPE(dest, source, conv, type, pType) \
     252{ \
     253    psPtr           myNaN; \
     254    int             i; \
     255\
     256    for (i = 0; i < source->n; i++) { \
     257        if (strlen(source->data[i])) { \
     258            dest[i] = (type)conv(source->data[i]); \
     259        } else { \
     260            myNaN = psDBGetPTypeNaN(pType); \
     261            dest[i] = *(type *)myNaN; \
     262            psFree(myNaN); \
     263        } \
     264    } \
     265}
    252266
    253267psVector *psDBSelectColumnNum(psDB *dbh, const char *tableName, const char *col, psElemType pType, const psU64 limit)
    254268{
    255     psArray         *stringColumn;
    256     psVector        *column;
    257     int             i;
    258     psPtr           myNaN;
     269    psArray         *stringColumn;      // source psArray
     270    psVector        *column;            // dest psVector
    259271
    260272    stringColumn = psDBSelectColumn(dbh, tableName, col, limit);
    261 
    262     if (stringColumn) {
    263         column = psVectorAlloc(stringColumn->n, pType);
    264     } else {
    265         // psDBSelectColumn() returned NULL
     273    if (!stringColumn) {
    266274        return NULL;
    267275    }
    268276
    269     // long long is 64b and long is 32b on x86
     277    column = psVectorAlloc(stringColumn->n, pType);
     278
     279    // conversion functions are a portability issue
    270280    switch (pType) {
    271281        case PS_TYPE_S8:
    272             STRINGARRAYTOPTYPE(column->data.S8[i], stringColumn, atoi, psS8, PS_TYPE_S8);
     282            STRINGARRAYTOPTYPE(column->data.S8, stringColumn, atoi, psS8, PS_TYPE_S8);
    273283            break;
    274284        case PS_TYPE_S16:
    275             STRINGARRAYTOPTYPE(column->data.S16[i], stringColumn, atoi, psS16, PS_TYPE_S16);
     285            STRINGARRAYTOPTYPE(column->data.S16, stringColumn, atoi, psS16, PS_TYPE_S16);
    276286            break;
    277287        case PS_TYPE_S32:
    278             STRINGARRAYTOPTYPE(column->data.S32[i], stringColumn, atoi, psS32, PS_TYPE_S32);
     288            STRINGARRAYTOPTYPE(column->data.S32, stringColumn, atoi, psS32, PS_TYPE_S32);
    279289            break;
    280290        case PS_TYPE_S64:
    281             STRINGARRAYTOPTYPE(column->data.S64[i], stringColumn, atoll, psS64, PS_TYPE_S64);
     291            STRINGARRAYTOPTYPE(column->data.S64, stringColumn, atoll, psS64, PS_TYPE_S64);
    282292            break;
    283293        case PS_TYPE_U8:
    284294            // missing from psVector in SDRS
    285             STRINGARRAYTOPTYPE(column->data.U8[i], stringColumn, atoi, psU8, PS_TYPE_U8);
     295            STRINGARRAYTOPTYPE(column->data.U8, stringColumn, atoi, psU8, PS_TYPE_U8);
    286296            break;
    287297        case PS_TYPE_U16:
    288             STRINGARRAYTOPTYPE(column->data.U16[i], stringColumn, atoi, psU16, PS_TYPE_U16);
     298            STRINGARRAYTOPTYPE(column->data.U16, stringColumn, atoi, psU16, PS_TYPE_U16);
    289299            break;
    290300        case PS_TYPE_U32:
    291             STRINGARRAYTOPTYPE(column->data.U32[i], stringColumn, atoi, psU32, PS_TYPE_U32);
     301            STRINGARRAYTOPTYPE(column->data.U32, stringColumn, atoi, psU32, PS_TYPE_U32);
    292302            break;
    293303        case PS_TYPE_U64:
    294             STRINGARRAYTOPTYPE(column->data.U64[i], stringColumn, atoll, psU64, PS_TYPE_U64);
     304            STRINGARRAYTOPTYPE(column->data.U64, stringColumn, atoll, psU64, PS_TYPE_U64);
    295305            break;
    296306        case PS_TYPE_F32:
    297             STRINGARRAYTOPTYPE(column->data.F32[i], stringColumn, atof, psF32, PS_TYPE_F32);
     307            STRINGARRAYTOPTYPE(column->data.F32, stringColumn, atof, psF32, PS_TYPE_F32);
    298308            break;
    299309        case PS_TYPE_F64:
    300             STRINGARRAYTOPTYPE(column->data.F64[i], stringColumn, atof, psF64, PS_TYPE_F64);
     310            STRINGARRAYTOPTYPE(column->data.F64, stringColumn, atof, psF64, PS_TYPE_F64);
    301311            break;
    302312        case PS_TYPE_C32:
    303313            // this is a bogus SQL type
    304             STRINGARRAYTOPTYPE(column->data.C32[i], stringColumn, atof, psC32, PS_TYPE_C32);
     314            STRINGARRAYTOPTYPE(column->data.C32, stringColumn, atof, psC32, PS_TYPE_C32);
    305315            break;
    306316        case PS_TYPE_C64:
    307317            // this is a bogus SQL type
    308             STRINGARRAYTOPTYPE(column->data.C64[i], stringColumn, atof, psC64, PS_TYPE_C64);
     318            STRINGARRAYTOPTYPE(column->data.C64, stringColumn, atof, psC64, PS_TYPE_C64);
    309319            break;
    310320        case PS_TYPE_BOOL:
     
    323333psArray *psDBSelectRows(psDB *dbh, const char *tableName, psMetadata *where)
    324334{
    325     MYSQL_RES       *result;
     335    MYSQL_RES       *result;            // complete db result set
    326336    MYSQL_ROW       row;
    327     MYSQL_FIELD     *field;
     337    MYSQL_FIELD     *field;             // field type info
    328338    char            *query;
    329     psU64           rowCount;
    330     psU64           fieldCount;
    331     psU64           *fieldLength;
    332     psArray         *resultSet;
    333     int             i; // field index
    334     psMetadataItem  *item;
    335     psMetadata      *md;
     339    psU64           rowCount;           // number of rows in db result set
     340    psU64           fieldCount;         // number of fields in db result set
     341    psU64           *fieldLength;       // field sizes
     342    psArray         *resultSet;         // return array
     343    int             i;                  // field index
     344    psMetadataItem  *item;              // field of a row
     345    psMetadata      *md;                // a row
    336346    psHash          *mysqlToSQLTable;
    337347    psHash          *sqlToPSTable;
    338348    char            *key;
    339349    char            *value;
    340     psU32           pType;
    341     psU32           type;
     350    psU32           pType;              // psElemType of a field
     351    psU32           type;               // psMetadataType of a a field
    342352    char            *sqlType;
    343     psPtr           data;
     353    psPtr           data;               // copy of result field
    344354
    345355    query = psDBGenerateSelectRowsSQL(tableName, where);
    346 
    347     fprintf(stderr, "complete query is: %s\n", query);
    348 
    349     if (psDBRunQuery(dbh, query)) {
    350         result = mysql_store_result(dbh->mysql);
    351     } else {
     356    if (!query) {
     357        return NULL;
     358    }
     359
     360    if (!psDBRunQuery(dbh, query)) {
    352361        // error
    353362        psFree(query);
     
    356365    }
    357366
    358     if (result) {
    359         rowCount = (psU64)mysql_num_rows(result);
    360 
    361         // pre-allocate enough elements to hold the complete result set
    362         // then reset n to 0 so elements are added from the beginning of
    363         // the array
    364         resultSet = psArrayAlloc(rowCount);
    365         resultSet->n = 0;
    366 
    367         field = mysql_fetch_fields(result);
    368         fieldCount = mysql_num_fields(result);
    369 
    370         mysqlToSQLTable = psDBGetMySQLToSQLTable();
    371         sqlToPSTable = psDBGetSQLToPSTable();
    372 
    373         while ((row = mysql_fetch_row(result))) {
    374             md = psMetadataAlloc();
    375             fieldLength = (psU64 *)(mysql_fetch_lengths(result));
    376 
    377             for (i = 0; i < fieldCount; i++) {
    378                 // lookup MySQL column type
    379                 // MySQL column types can not be directly translated to PS types
    380                 // as the the mysql types do not tell you if the value is
    381                 // signed or unsigned
    382                 key = psDBIntToString((psU64)field[i].type);
    383                 value = psHashLookup(mysqlToSQLTable, key);
    384                 sqlType = psStringCopy(value);
    385                 psFree(key);
    386 
    387                 if (field->flags & UNSIGNED_FLAG) {
    388                     psStringPrepend(&sqlType, "UNSIGNED ");
    389                 }
    390 
    391                 // convert MySQL type to PS type
    392                 value = psHashLookup(sqlToPSTable, sqlType);
    393                 pType = (psU32)atol(value);
    394                 psFree(sqlType);
    395 
    396                 fprintf(stderr, "sqlType is: %s\n", sqlType);
    397 
    398                 // convert NULLs appropriately
    399                 if (pType == PS_TYPE_PTR) {
    400                     type = PS_META_STR;
    401                     data = fieldLength[i] ? psStringNCopy(row[i], fieldLength[i])
    402                                           : psDBGetPTypeNaN(pType);
    403                 } else {
    404                     type = PS_META_PRIMITIVE;
    405                     if (fieldLength[i]) {
    406                         data = psAlloc(fieldLength[i]);
    407                         memcpy(data, row[i], fieldLength[i]);
    408                     } else {
    409                         data = psDBGetPTypeNaN(pType);
    410                     }
    411                 }
    412 
    413                 // add field to row
    414                 item = psMetadataItemAlloc(field[i].name, pType, type, NULL, data);
    415                 psMetadataAddItem(md, item, i);
    416 
    417                 psFree(data);
    418                 psFree(item);
    419             }
    420 
    421             // add row to result set
    422             psArrayAdd(resultSet, 0, md);
    423         }
    424     } else {
     367    result = mysql_store_result(dbh->mysql);
     368    if (!result) {
    425369        // no result set
    426         resultSet = NULL;
    427 
    428370        fieldCount = mysql_field_count(dbh->mysql);
    429371
    430         if (fieldCount == 0) {
    431             // query result was empty
    432 
    433         } else {
     372        // if field count is zero the query should have returned no data.  If
     373        // it's non-zero then something bad has happened.
     374        if (fieldCount != 0) {
    434375            // error, should have gotten data
    435376            fprintf(stderr, "Query returned no data.  Error: %s\n", mysql_error(dbh->mysql));
    436         }
    437     }
    438 
    439     mysql_free_result(result);
     377
     378            psFree(query);
     379
     380            return NULL;
     381        }
     382    }
     383
     384    rowCount = (psU64)mysql_num_rows(result);
     385
     386    // pre-allocate enough elements to hold the complete result set
     387    // then reset n to 0 so elements are added from the beginning of
     388    // the array
     389    resultSet = psArrayAlloc(rowCount);
     390    resultSet->n = 0;
     391
     392    field = mysql_fetch_fields(result);
     393    fieldCount = mysql_num_fields(result);
     394
     395    mysqlToSQLTable = psDBGetMySQLToSQLTable();
     396    sqlToPSTable = psDBGetSQLToPSTable();
     397
     398    while ((row = mysql_fetch_row(result))) {
     399        // allocate new psMetadata to represent a row
     400        md = psMetadataAlloc();
     401
     402        fieldLength = (psU64 *)(mysql_fetch_lengths(result));
     403
     404        for (i = 0; i < fieldCount; i++) {
     405            // lookup MySQL column type
     406            key = psDBIntToString((psU64)field[i].type);
     407            value = psHashLookup(mysqlToSQLTable, key);
     408            sqlType = psStringCopy(value);
     409            psFree(key);
     410
     411            // MySQL column types can not be directly translated to PS
     412            // types as the the mysql types do not tell you if the value is
     413            // signed or unsigned.  The result is this ugly conversion from
     414            // mysql -> ascii -> ptype
     415            if (field->flags & UNSIGNED_FLAG) {
     416                psStringPrepend(&sqlType, "UNSIGNED ");
     417            }
     418
     419            // convert MySQL type to PS type
     420            value = psHashLookup(sqlToPSTable, sqlType);
     421            pType = (psU32)atol(value);
     422            psFree(sqlType);
     423
     424            // copy field data and convert NULLs to the appropriate NaN value
     425            if (pType == PS_TYPE_PTR) {
     426                type = PS_META_STR;
     427                data = fieldLength[i] ? psStringNCopy(row[i], fieldLength[i])
     428                                      : psDBGetPTypeNaN(pType);
     429            } else {
     430                type = PS_META_PRIMITIVE;
     431                if (fieldLength[i]) {
     432                    data = psAlloc(fieldLength[i]);
     433                    memcpy(data, row[i], fieldLength[i]);
     434                } else {
     435                    data = psDBGetPTypeNaN(pType);
     436                }
     437            }
     438
     439            // add new field to row
     440            item = psMetadataItemAlloc(field[i].name, pType, type, NULL, data);
     441            psMetadataAddItem(md, item, i);
     442            psFree(item);
     443
     444            psFree(data);
     445        }
     446
     447        // add row to result set
     448        psArrayAdd(resultSet, 0, md);
     449    }
     450
    440451    psFree(mysqlToSQLTable);
    441452    psFree(sqlToPSTable);
     453
     454    mysql_free_result(result);
    442455    psFree(query);
    443456
     
    448461{
    449462    char            *query;
    450     MYSQL_STMT      *stmt;
    451     MYSQL_BIND      *bind;
     463    MYSQL_STMT      *stmt;              // prepared db statement
     464    MYSQL_BIND      *bind;              // field values to insert
    452465    psU32           paramCount;
    453466    psHash          *mysqlFieldType;
     
    460473
    461474    query = psDBGenerateInsertRowSQL(tableName, row);
    462 
    463     fprintf(stderr, "complete query is: %s\n", query);
     475    if (!query) {
     476        return NULL;
     477    }
    464478
    465479    stmt = mysql_stmt_init(dbh->mysql);
     
    585599    }
    586600
     601    // end of field names
    587602    psStringAppend(&query, ") VALUES (");
    588603
     
    593608        psStringAppend(&query, "?");
    594609
     610        // + ", " between every place holder
    595611        if (!cursor->offEnd) {
    596             // + , + _
    597612            psStringAppend(&query, ", ");
    598613        }
     
    615630    query = psStringCopy("WHERE ");
    616631
    617     // get Metadata iterator
    618632    cursor = psListIteratorAlloc(where->list, 0);
    619633
     
    641655        // + " and " after every column declaration except the last one
    642656        if (!cursor->offEnd) {
    643             // + and
    644657            psStringAppend(&query, " AND ");
    645658        }
     
    668681    psStringAppend(&query, " (");
    669682
    670     // get pType lookup table
    671683    colType = psDBGetPSToSQLTable();
    672684
    673     // get Metadata iterator
    674685    cursor = psListIteratorAlloc(table->list, 0);
    675686
     
    688699            // + column type
    689700            psStringAppend(&query, value);
    690         } else if (item->type == PS_META_STR) {
     701        } else if ((item->type == PS_META_STR) && (item->pType == PS_TYPE_PTR)) {
    691702            // + varchar( + length + )
    692703            psStringAppend(&query, "varchar(");
     
    694705            psStringAppend(&query, ")");
    695706        } else {
    696             // error
     707            // error, invalid type
     708            return NULL;
    697709        }
    698710
    699711        // add a , after every column declaration except the last one
    700712        if (!cursor->offEnd) {
    701             // + ,
    702713            psStringAppend(&query, ", ");
    703714        }
     
    723734    psFree(cursor);
    724735
    725     // + ) + _ + ENGINE=innodb;
     736    // end column types + table type
    726737    psStringAppend(&query, ") ENGINE=innodb");
    727738
     
    773784}
    774785
    775 #define MYNAN(a, b) myNaN = psAlloc(sizeof(a));\
    776                     *(a *)myNaN = b;
     786#define MYNAN(dest, type, nan) \
     787    dest = psAlloc(sizeof(type)); \
     788    *(type *)dest = nan;
    777789
    778790static psPtr psDBGetPTypeNaN(psElemType pType)
     
    782794    switch (pType) {
    783795        case PS_TYPE_S8:
    784             myNaN = psAlloc(sizeof(psS8));
    785             *(psS8 *)myNaN = PS_MAX_S8;
     796            MYNAN(myNaN, psS8, PS_MAX_S8);
    786797            break;
    787798        case PS_TYPE_S16:
    788             MYNAN(psS16, PS_MAX_S16)
     799            MYNAN(myNaN, psS16, PS_MAX_S16);
    789800            break;
    790801        case PS_TYPE_S32:
    791             MYNAN(psS32, PS_MAX_S32)
     802            MYNAN(myNaN, psS32, PS_MAX_S32);
    792803            break;
    793804        case PS_TYPE_S64:
    794             MYNAN(psS64, PS_MAX_S64)
     805            MYNAN(myNaN, psS64, PS_MAX_S64);
    795806            break;
    796807        case PS_TYPE_U8:
    797             MYNAN(psU8, PS_MAX_U8)
     808            MYNAN(myNaN, psU8, PS_MAX_U8);
    798809            break;
    799810        case PS_TYPE_U16:
    800             MYNAN(psU16, PS_MAX_U16)
     811            MYNAN(myNaN, psU16, PS_MAX_U16);
    801812            break;
    802813        case PS_TYPE_U32:
    803             MYNAN(psU32, PS_MAX_U32)
     814            MYNAN(myNaN, psU32, PS_MAX_U32);
    804815            break;
    805816        case PS_TYPE_U64:
    806             MYNAN(psU64, PS_MAX_U64)
     817            MYNAN(myNaN, psU64, PS_MAX_U64);
    807818            break;
    808819        case PS_TYPE_F32:
    809             MYNAN(psF32, NAN)
     820            MYNAN(myNaN, psF32, NAN);
    810821            break;
    811822        case PS_TYPE_F64:
    812             MYNAN(psF64, NAN)
     823            MYNAN(myNaN, psF64, NAN);
    813824            break;
    814825        case PS_TYPE_C32:
    815826            // this is a bogus SQL type
    816             MYNAN(psC32, NAN)
     827            MYNAN(myNaN, psC32, NAN);
    817828            break;
    818829        case PS_TYPE_C64:
    819830            // this is a bogus SQL type
    820             MYNAN(psC64, NAN)
     831            MYNAN(myNaN, psC64, NAN);
    821832            break;
    822833        case PS_TYPE_BOOL:
     
    824835            break;
    825836        case PS_TYPE_PTR:
    826             MYNAN(char, '\0')
     837            MYNAN(myNaN, char, '\0');
    827838            break;
    828839    }
     
    831842}
    832843
    833 // a = type , b = data, c = NaN value
    834 #define ISPTYPENAN(a, b, c) *(a *)b == c;
     844#define ISTYPENAN(type, data, nan) *(type *)data == nan
    835845
    836846static bool psDBIsPTypeNaN(psElemType pType, psPtr data)
     
    840850    switch (pType) {
    841851        case PS_TYPE_S8:
    842             isNaN = ISPTYPENAN(psS8, data, PS_MAX_S8)
     852            isNaN = ISTYPENAN(psS8, data, PS_MAX_S8);
    843853            break;
    844854        case PS_TYPE_S16:
    845             isNaN = ISPTYPENAN(psS16, data, PS_MAX_S16)
     855            isNaN = ISTYPENAN(psS16, data, PS_MAX_S16);
    846856            break;
    847857        case PS_TYPE_S32:
    848             isNaN = ISPTYPENAN(psS32, data, PS_MAX_S32)
     858            isNaN = ISTYPENAN(psS32, data, PS_MAX_S32);
    849859            break;
    850860        case PS_TYPE_S64:
    851             isNaN = ISPTYPENAN(psS64, data, PS_MAX_S64)
     861            isNaN = ISTYPENAN(psS64, data, PS_MAX_S64);
    852862            break;
    853863        case PS_TYPE_U8:
    854             isNaN = ISPTYPENAN(psU8, data, PS_MAX_U8)
     864            isNaN = ISTYPENAN(psU8, data, PS_MAX_U8);
    855865            break;
    856866        case PS_TYPE_U16:
    857             isNaN = ISPTYPENAN(psU16, data, PS_MAX_U16)
     867            isNaN = ISTYPENAN(psU16, data, PS_MAX_U16);
    858868            break;
    859869        case PS_TYPE_U32:
    860             isNaN = ISPTYPENAN(psU32, data, PS_MAX_U32)
     870            isNaN = ISTYPENAN(psU32, data, PS_MAX_U32);
    861871            break;
    862872        case PS_TYPE_U64:
    863             isNaN = ISPTYPENAN(psU64, data, PS_MAX_U64)
     873            isNaN = ISTYPENAN(psU64, data, PS_MAX_U64);
    864874            break;
    865875        case PS_TYPE_F32:
    866             isNaN = ISPTYPENAN(psF32, data, NAN)
     876            isNaN = ISTYPENAN(psF32, data, NAN);
    867877            break;
    868878        case PS_TYPE_F64:
    869             isNaN = ISPTYPENAN(psF64, data, NAN)
     879            isNaN = ISTYPENAN(psF64, data, NAN);
    870880            break;
    871881        case PS_TYPE_C32:
    872882            // this is a bogus SQL type
    873             isNaN = ISPTYPENAN(psC32, data, NAN)
     883            isNaN = ISTYPENAN(psC32, data, NAN);
    874884            break;
    875885        case PS_TYPE_C64:
    876886            // this is a bogus SQL type
    877             isNaN = ISPTYPENAN(psC64, data, NAN)
     887            isNaN = ISTYPENAN(psC64, data, NAN);
    878888            break;
    879889        case PS_TYPE_BOOL:
     
    881891            break;
    882892        case PS_TYPE_PTR:
    883             isNaN = ISPTYPENAN(char, data, '\0')
     893            isNaN = ISTYPENAN(char, data, '\0');
    884894            break;
    885895    }
     
    888898}
    889899
    890 #define PTYPETOSTRING(a, b) length = (size_t)log10((double)*(a *)data) + 1;\
    891                             string = psAlloc(length);\
    892                             snprintf(string, length, b, *(a *)data);
     900#define TYPETOSTRING(type, format, data) \
     901    (size_t)log10((double)*(type *)data) + 1; \
     902    string = psAlloc(length); \
     903    snprintf(string, length, format, *(type *)data);
    893904
    894905static char *psDBPTypeAsString(psPtr data, psElemType pType)
     
    897908    char            *string;
    898909
     910    // formats are a portability issue
    899911    switch (pType) {
    900912        case PS_TYPE_S8:
    901             PTYPETOSTRING(psS8, "%i");
     913            length = TYPETOSTRING(psS8, "%i", data);
    902914            break;
    903915        case PS_TYPE_S16:
    904             PTYPETOSTRING(psS16, "%i");
     916            length = TYPETOSTRING(psS16, "%i", data);
    905917            break;
    906918        case PS_TYPE_S32:
    907             PTYPETOSTRING(psS32, "%i");
     919            length = TYPETOSTRING(psS32, "%i", data);
    908920            break;
    909921        case PS_TYPE_S64:
    910             PTYPETOSTRING(psS64, "%li");
     922            length = TYPETOSTRING(psS64, "%li", data);
    911923            break;
    912924        case PS_TYPE_U8:
    913             PTYPETOSTRING(psU8, "%u");
     925            length = TYPETOSTRING(psU8, "%u", data);
    914926            break;
    915927        case PS_TYPE_U16:
    916             PTYPETOSTRING(psU16, "%u");
     928            length = TYPETOSTRING(psU16, "%u", data);
    917929            break;
    918930        case PS_TYPE_U32:
    919             PTYPETOSTRING(psU32, "%u");
     931            length = TYPETOSTRING(psU32, "%u", data);
    920932            break;
    921933        case PS_TYPE_U64:
    922             PTYPETOSTRING(psU64, "%lu");
     934            length = TYPETOSTRING(psU64, "%lu", data);
    923935            break;
    924936        case PS_TYPE_F32:
    925             PTYPETOSTRING(psF32, "%f");
     937            length = TYPETOSTRING(psF32, "%f", data);
    926938            break;
    927939        case PS_TYPE_F64:
    928             PTYPETOSTRING(psF32, "%f");
     940            length = TYPETOSTRING(psF32, "%f", data);
    929941            break;
    930942        case PS_TYPE_C32:
     
    935947            break;
    936948        case PS_TYPE_BOOL:
    937             PTYPETOSTRING(bool, "%u");
     949            length = TYPETOSTRING(bool, "%u", data);
    938950            break;
    939951        case PS_TYPE_PTR:
     
    10001012        lookupTable = psHashAlloc(14);
    10011013
     1014        // no support for CHAR, TEXT or GLOB
    10021015        psDBAddToLookupTable(lookupTable, PS_TYPE_S8,  "TINYINT");
    10031016        psDBAddToLookupTable(lookupTable, PS_TYPE_S16, "SMALLINT");
     
    10131026        psDBAddToLookupTable(lookupTable, PS_TYPE_C64, "PS_TYPE_C64 is not supported");
    10141027        psDBAddToLookupTable(lookupTable, PS_TYPE_BOOL,"BOOLEAN");
    1015         // no support for CHAR, TEXT or GLOB
    10161028        psDBAddToLookupTable(lookupTable, PS_TYPE_PTR, "VARCHAR");
    10171029    }
Note: See TracChangeset for help on using the changeset viewer.