IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3188


Ignore:
Timestamp:
Feb 10, 2005, 10:56:41 AM (21 years ago)
Author:
jhoblitt
Message:

add psDBUpdateRows() (incomplete)
add psDBGenerateSetSQL()
add psDBGenerateUpdateRowSQL()

Location:
trunk/archive/psdb
Files:
3 edited

Legend:

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

    r3175 r3188  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-10 00:02:16 $
     10 *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-10 20:56:41 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    6161static char    *psDBGenerateSelectColumnSQL(const char *tableName, const char *col, const psU64 limit);
    6262static char    *psDBGenerateSelectRowsSQL(const char *tableName, psMetadata *where);
     63static char    *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values);
     64static char    *psDBGenerateSetSQL(psMetadata *set);
    6365
    6466psDB *psDBInit(const char *host, const char *user, const char *passwd, const char *dbname)
     
    628630    // loop over rows
    629631    for (j = 0; j < rowSet->n; j++) {
    630         cursor = psListIteratorAlloc(row->list, 0);
     632        // extract the psList, from the psMetadata, in the current psArray element
     633        cursor = psListIteratorAlloc(((psMetadata *)rowSet->data[j])->list, 0);
    631634
    632635        // reset bind for each row
     
    660663                } else {
    661664                    // error, not a pointer to a string
     665                    mysql_rollback(dbh->mysql);
     666
     667                    psFree(cursor);
     668                    psFree(bind);
     669                    mysql_stmt_close(stmt);
     670
     671                    return false;
    662672                }
    663673            }
     
    672682            fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
    673683
     684            mysql_rollback(dbh->mysql);
     685
    674686            psFree(bind);
    675687            mysql_stmt_close(stmt);
     
    681693            fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
    682694
     695            mysql_rollback(dbh->mysql);
     696
    683697            psFree(bind);
    684698            mysql_stmt_close(stmt);
     
    702716{
    703717    return psDBSelectRows(dbh, tableName, NULL);
     718}
     719
     720psS64 psDBUpdateRows(psDB *dbh, const char *tableName, psMetadata *where, psMetadata *values)
     721{
     722    char            *query;
     723    MYSQL_STMT      *stmt;              // prepared db statement
     724    psU32           paramCount;         // number of placeholders in query
     725    MYSQL_BIND      *bind;              // field values to insert
     726    psU64           rowsAffected;
     727
     728    query = psDBGenerateUpdateRowSQL(tableName, where, values);
     729    if (!query) {
     730        return -1;
     731    }
     732
     733    printf("complete query is %s\n", query);
     734
     735    stmt = mysql_stmt_init(dbh->mysql);
     736    if (!stmt) {
     737        // call abort?
     738        fprintf(stderr, " mysql_stmt_init(), out of memory\n");
     739
     740        return -1;
     741    }
     742
     743    if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {
     744            fprintf(stderr, "Failed to prepare query.  Error: %s\n", mysql_stmt_error(stmt));
     745
     746            mysql_stmt_close(stmt);
     747            psFree(query);
     748
     749            return -1;
     750    }
     751
     752    psFree(query);
     753
     754    // how many place holders are in our query
     755    paramCount = (psU32)mysql_stmt_param_count(stmt);
     756
     757    // structure larger enough to hold one field of data per place holder
     758    bind = psAlloc(sizeof(MYSQL_BIND) * paramCount);
     759
     760    if (mysql_stmt_bind_param(stmt, bind)) {
     761        fprintf(stderr, "Failed to bind params.  Error: %s\n", mysql_stmt_error(stmt));
     762
     763        mysql_rollback(dbh->mysql);
     764
     765        psFree(bind);
     766        mysql_stmt_close(stmt);
     767
     768        return -1;
     769    }
     770
     771    if (mysql_stmt_execute(stmt)) {
     772        fprintf(stderr, "Failed to execute prepared statement.  Error: %s\n", mysql_stmt_error(stmt));
     773
     774        mysql_rollback(dbh->mysql);
     775
     776        psFree(bind);
     777        mysql_stmt_close(stmt);
     778
     779        return -1;
     780    }
     781
     782    psFree(bind);
     783
     784    // point of no return
     785    mysql_commit(dbh->mysql);
     786
     787    rowsAffected = (psU64)mysql_stmt_affected_rows(stmt);
     788
     789    mysql_stmt_close(stmt);
     790
     791    return rowsAffected;
     792}
     793
     794static char *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values)
     795{
     796    char            *query;
     797    char            *string;
     798
     799    if ((!values) || (!where)) {
     800        return NULL;
     801    }
     802
     803    // init query with a psMemBlock
     804    query = psStringCopy("UPDATE ");
     805    psStringAppend(&query, tableName);
     806
     807    string = psDBGenerateSetSQL(values);
     808    if (!string) {
     809        return NULL;
     810    }
     811    psStringAppend(&query, " ");
     812    psStringAppend(&query, string);
     813    psFree(string);
     814
     815    string = psDBGenerateWhereSQL(where);
     816    if (!string) {
     817        return NULL;
     818    }
     819    psStringAppend(&query, " ");
     820    psStringAppend(&query, string);
     821    psFree(string);
     822
     823    return query;
    704824}
    705825
     
    785905        if (!cursor->offEnd) {
    786906            psStringAppend(&query, " AND ");
     907        }
     908    }
     909
     910    psFree(cursor);
     911
     912    return query;
     913}
     914
     915static char *psDBGenerateSetSQL(psMetadata *set)
     916{
     917    char            *query;
     918    psListIterator  *cursor;
     919    psMetadataItem  *item;
     920
     921    // init query with a psMemBlock
     922    query = psStringCopy("SET ");
     923
     924    cursor = psListIteratorAlloc(set->list, 0);
     925
     926    // find column name
     927    while ((item = psListGetNext(cursor))) {
     928        // + column name + _ + = + _ + ?
     929        psStringAppend(&query, item->name);
     930        psStringAppend(&query, " = ?");
     931
     932        // + ", " after every column declaration except the last one
     933        if (!cursor->offEnd) {
     934            psStringAppend(&query, ",  ");
    787935        }
    788936    }
  • trunk/archive/psdb/psDB.h

    r3175 r3188  
    1010 *  @author Joshua Hoblitt
    1111 *
    12  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2005-02-10 00:02:16 $
     12 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2005-02-10 20:56:41 $
    1414 *
    1515 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    5555psArray *psDBDumpRows(psDB *dbh, const char *tableName);
    5656
     57psS64 psDBUpdateRows(psDB *dbh, const char *tableName, psMetadata *where, psMetadata *values);
     58
    5759/// @}
    5860
  • trunk/archive/psdb/test.c

    r3180 r3188  
    147147    }
    148148
    149     //psDBInsertOneRow();
     149    // psDBInsertOneRow();
    150150    {
    151151        psMetadata      *md;
     
    162162    }
    163163
    164     //psDBInsertRows();
     164    // psDBInsertRows();
    165165    {
    166166        psArray         *rowSet;
     
    197197    }
    198198
    199     //psDBDumpRows();
     199    // psDBDumpRows();
    200200    {
    201201        psArray         *resultSet;
     
    206206    }
    207207
     208    // psDBUpdateRows();
     209    {
     210        psMetadata      *where;
     211        psMetadata      *value;
     212        psMetadataItem  *item;
     213        psU64           rowsAffected;
     214
     215        where = psMetadataAlloc();
     216        item = psMetadataItemAlloc("color", PS_TYPE_PTR, PS_META_STR, NULL, "pink");
     217        psMetadataAddItem(where, item, 0);
     218        psFree(item);
     219
     220        value = psMetadataAlloc();
     221        item = psMetadataItemAlloc("color", PS_TYPE_PTR, PS_META_STR, NULL, "HOT pink");
     222        psMetadataAddItem(value, item, 0);
     223        psFree(item);
     224
     225        rowsAffected = psDBUpdateRows(dbh, "horse", where, value);
     226        psFree(where);
     227        psFree(value);
     228
     229        OK(rowsAffected, "psDBUpdateRows()");
     230    }
     231   
    208232    // cleanup other tests
    209     psDBDropTable(dbh, "horse");
     233    // psDBDropTable(dbh, "horse");
    210234    psDBDropTable(dbh, "yak");
    211235
Note: See TracChangeset for help on using the changeset viewer.