IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 11, 2005, 4:40:21 PM (21 years ago)
Author:
jhoblitt
Message:

fix dereference error in psDBSelectRows()
add psDBDumpCols()
add psDBMySQLToPType()

File:
1 edited

Legend:

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

    r3198 r3202  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-11 23:46:57 $
     10 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-12 02:40:21 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    4141static void     psDBPSToMySQLTableCleanup(void);
    4242static psPtr    psDBMySQLTypeAlloc(enum enum_field_types type, bool isUnsigned);
     43static psU32 psDBMySQLToPType(psU32 type, psU32 flags);
    4344
    4445// pType utility functions
     
    421422            // signed or unsigned.  The result is this ugly conversion from
    422423            // mysql -> ascii -> ptype
    423             if (field->flags & UNSIGNED_FLAG) {
     424            if (field[i].flags & UNSIGNED_FLAG) {
    424425                psStringPrepend(&sqlType, "UNSIGNED ");
    425426            }
     
    582583{
    583584    return psDBSelectRows(dbh, tableName, NULL);
     585}
     586
     587psMetadata *psDBDumpCols(psDB *dbh, const char *tableName)
     588{
     589    MYSQL_RES       *result;
     590    MYSQL_FIELD     *field;
     591    psU32           fieldCount;
     592    psMetadata      *table;
     593    psMetadataItem  *item;
     594    psU32           pType;
     595    psU32           i;
     596    psPtr           column;
     597
     598    // find column types
     599    result = mysql_list_fields(dbh->mysql, tableName, NULL);
     600    field = mysql_fetch_fields(result);
     601    fieldCount = mysql_num_fields(result);
     602
     603    table = psMetadataAlloc();
     604
     605    // fetch each column and load into psMetadata
     606    for (i =0; i < fieldCount; i++) {
     607        // find ptype of column
     608        pType = psDBMySQLToPType(field[i].type, field[i].flags);
     609       
     610        // if the ptype is PS_TYPE_PTR assume that it's a string and fetch the
     611        // column as an psArray of strings; otherwise fetch the column as a
     612        // psVector. 
     613        if (pType == PS_TYPE_PTR) {
     614            // PS_META_UNKNOWN -> PS_META_ARRAY ?
     615            column = psDBSelectColumn(dbh, tableName, field[i].name, 0);
     616            item = psMetadataItemAlloc(field[i].name, PS_TYPE_PTR, PS_META_UNKNOWN, "psArray", column);
     617            psMetadataAddItem(table, item, 0);
     618            psFree(column);
     619            psFree(item);
     620        } else {
     621            column = psDBSelectColumnNum(dbh, tableName, field[i].name, pType, 0);
     622            item = psMetadataItemAlloc(field[i].name, PS_TYPE_PTR, PS_META_VEC, "psVector", column);
     623            psMetadataAddItem(table, item, 0);
     624            //psFree(column);
     625            psFree(item);
     626        }
     627    }
     628   
     629    return table;
    584630}
    585631
     
    12251271
    12261272    return newSize;
     1273}
     1274
     1275static psU32 psDBMySQLToPType(psU32 type, psU32 flags)
     1276{
     1277    psHash          *mysqlToSQLTable;   // type lookup table
     1278    psHash          *sqlToPSTable;      // type lookup table
     1279    char            *key;               // hash tmp value
     1280    char            *value;             // hash tmp value
     1281    char            *sqlType;           // copy of lookup table result
     1282    psU32           pType;              // psElemType of a field
     1283
     1284    mysqlToSQLTable = psDBGetMySQLToSQLTable();
     1285    sqlToPSTable    = psDBGetSQLToPSTable();
     1286
     1287    // lookup MySQL column type
     1288    key     = psDBIntToString((psU64)type);
     1289    value   = psHashLookup(mysqlToSQLTable, key);
     1290    sqlType = psStringCopy(value);
     1291    psFree(key);
     1292
     1293    // MySQL column types can not be directly translated to PS
     1294    // types as the the mysql types do not tell you if the value is
     1295    // signed or unsigned.  The result is this ugly conversion from
     1296    // mysql -> ascii -> ptype
     1297    if (flags & UNSIGNED_FLAG) {
     1298        psStringPrepend(&sqlType, "UNSIGNED ");
     1299    }
     1300
     1301    // convert MySQL type to PS type
     1302    value = psHashLookup(sqlToPSTable, sqlType);
     1303    pType = (psU32)atol(value);
     1304    psFree(sqlType);
     1305
     1306    return pType;
    12271307}
    12281308
Note: See TracChangeset for help on using the changeset viewer.