IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 11, 2005, 1:19:25 PM (21 years ago)
Author:
jhoblitt
Message:

complete implementation of psDBUpdateRows()
add psDBPackRow()

File:
1 edited

Legend:

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

    r3195 r3196  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-11 21:52:33 $
     10 *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-11 23:19:25 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    5454// database utility functions
    5555static bool     psDBRunQuery(psDB *dbh, const char *query);
     56static inline bool psDBPackRow(MYSQL_BIND *bind, psMetadata *values, psU32 paramCount);
    5657
    5758// SQL generation functions
     
    407408        md = psMetadataAlloc();
    408409
    409         fieldLength = (psU64 *)(mysql_fetch_lengths(result));
     410        fieldLength = (psU64 *)mysql_fetch_lengths(result);
    410411
    411412        for (i = 0; i < fieldCount; i++) {
     
    636637        i = 0;
    637638        while ((item = psListGetNext(cursor))) {
     639            // lookup pType -> mysql type
    638640            key = psDBIntToString((psU64)item->pType);
    639641            mType = psHashLookup(mysqlFieldType, key);
     
    752754    paramCount = (psU32)mysql_stmt_param_count(stmt);
    753755
    754     // structure larger enough to hold one field of data per place holder
     756    // structure large enough to hold one field of data per place holder
    755757    bind = psAlloc(sizeof(MYSQL_BIND) * paramCount);
     758
     759    // init bind
     760    memset(bind, 0, sizeof(MYSQL_BIND) * paramCount);
     761
     762    if (!psDBPackRow(bind, values, paramCount)) {
     763        psFree(bind);
     764        mysql_stmt_close(stmt);
     765
     766        return -1;
     767    }
    756768
    757769    if (mysql_stmt_bind_param(stmt, bind)) {
     
    787799
    788800    return rowsAffected;
     801}
     802
     803static inline bool psDBPackRow(MYSQL_BIND *bind, psMetadata *values, psU32 paramCount)
     804{
     805    psListIterator  *cursor;            // row iterator
     806    psMetadataItem  *item;              // field in row
     807    psHash          *mysqlFieldType;    // type lookup table
     808    mysqlType       *mType;             // type tmp variable
     809    bool            isNull = true;      // used in a MYSQL_BIND to indicate NULL
     810    char            *key;               // hash tmp variable
     811    int             i;
     812
     813    // check size of values == paramCount ?
     814
     815    cursor = psListIteratorAlloc(values->list, 0);
     816
     817    mysqlFieldType = psDBGetPSToMySQLTable();
     818
     819    // loop over fields
     820    i = 0;
     821    while ((item = psListGetNext(cursor))) {
     822        // lookup pType -> mysql type
     823        key = psDBIntToString((psU64)item->pType);
     824        mType = psHashLookup(mysqlFieldType, key);
     825        psFree(key);
     826
     827        // input data length is determined by the MYSQL_TYPE_* unless it's a string
     828        bind[i].buffer  = (char *)item->data.V;
     829        bind[i].buffer_type = mType->type;
     830        bind[i].is_unsigned = mType->isUnsigned;
     831
     832        // convert NaNs to NULL
     833        bind[i].is_null = psDBIsPTypeNaN(item->pType, item->data.V)
     834                        ? (my_bool *)&isNull
     835                        : NULL;
     836
     837        if (item->pType == PS_TYPE_PTR) {
     838            if (item->type == PS_META_STR) {
     839                if (*(char *)item->data.V == '\0') {
     840                    bind[i].is_null = (my_bool *)&isNull;
     841                } else {
     842                    bind[i].buffer_length = (unsigned long)strlen((char *)item->data.V);
     843                    bind[i].is_null = NULL;
     844                }
     845            } else {
     846                // error, not a pointer to a string
     847                psFree(cursor);
     848
     849                return false;
     850            }
     851        }
     852
     853        // increment field index
     854        i++;
     855    }
     856
     857    psFree(cursor);
     858    psFree(mysqlFieldType);
     859
     860    return true;
    789861}
    790862
Note: See TracChangeset for help on using the changeset viewer.