IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7070


Ignore:
Timestamp:
May 4, 2006, 4:42:01 PM (20 years ago)
Author:
Paul Price
Message:

Applying patch from Josh Hoblitt: Patch to add transaction control to psdb

Location:
trunk/psLib/src/db
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/db/psDB.c

    r7050 r7070  
    1212 *  @author Joshua Hoblitt
    1313 *
    14  *  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-05-03 02:35:37 $
     14 *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-05-05 02:42:01 $
    1616 *
    1717 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    123123    dbh->mysql = mysql;
    124124
     125    // explicit transactions default to false
     126    if (!psDBExplicitTrans(dbh, false)) {
     127        psError(PS_ERR_UNKNOWN, true,
     128                "failed to set transaction type", mysql_error(mysql));
     129
     130        mysql_close(mysql);
     131        psFree(dbh);
     132
     133        return NULL;
     134    }
     135
    125136    return dbh;
    126137}
     
    688699    } // end loop over rows
    689700
    690     // point of no return
    691     mysql_commit(dbh->mysql);
    692 
    693701    psFree(bind);
    694702    mysql_stmt_close(stmt);
     
    838846    rowsAffected = mysql_stmt_affected_rows(stmt);
    839847
    840     // point of no return
    841     mysql_commit(dbh->mysql);
    842 
    843848    mysql_stmt_close(stmt);
    844849
     
    879884    rows = (psS64)mysql_affected_rows(dbh->mysql);
    880885
    881     // point of no return
    882     mysql_commit(dbh->mysql);
    883 
    884886    return rows;
    885887}
     
    890892    return (long)mysql_insert_id(dbh->mysql);
    891893}
     894
     895bool psDBExplicitTrans(psDB *dbh, bool mode)
     896{
     897    // Verify database is not NULL
     898    if(dbh == NULL) {
     899        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
     900        return false;
     901    }
     902
     903    // mode needs to be inverted as autocommits are the opposide of explicit
     904    // transactions.
     905    // the return value also needs to be inverted for the same reason.
     906    // is it safe to assume my_bool always safely casts to bool?
     907    return !(bool)mysql_autocommit(dbh->mysql, !mode);
     908}
     909
     910bool psDBTransaction(psDB *dbh)
     911{
     912    // Verify database is not NULL
     913    if(dbh == NULL) {
     914        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
     915        return false;
     916    }
     917
     918    bool status = p_psDBRunQuery(dbh, "START TRANSACTION");
     919    if (!status) {
     920        psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
     921    }
     922
     923    return status;
     924}
     925
     926bool psDBCommit(psDB *dbh)
     927{
     928    // Verify database is not NULL
     929    if(dbh == NULL) {
     930        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
     931        return false;
     932    }
     933
     934    // is it safe to assume my_bool always safely casts to bool?
     935    return (bool)mysql_commit(dbh->mysql);
     936}
     937
     938bool psDBRollback(psDB *dbh)
     939{
     940    // Verify database is not NULL
     941    if(dbh == NULL) {
     942        psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB);
     943        return false;
     944    }
     945
     946    // is it safe to assume my_bool always safely casts to bool?
     947    return (bool)mysql_rollback(dbh->mysql);
     948}
     949
    892950
    893951// database utility functions
  • trunk/psLib/src/db/psDB.h

    r7050 r7070  
    1010 *  @author Joshua Hoblitt
    1111 *
    12  *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2006-05-03 02:35:37 $
     12 *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2006-05-05 02:42:01 $
    1414 *
    1515 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    285285);
    286286
     287/** Enable/Disable explicit database transactions
     288 *
     289 * This function is used to enable explicit transaction support.  It is off by
     290 * default.
     291 *
     292 * @return bool:    true if transactions are enabled
     293 */
     294bool psDBExplicitTrans(
     295    psDB *dbh,                          ///< Database handle
     296    bool mode                           ///< transactions enable/disable
     297);
     298
     299/** Start a new transaction set.
     300 *
     301 * This is only a meaningful action if explict transactions are disabled.
     302 *
     303 * @return bool:    true on success
     304 */
     305bool psDBTransaction(
     306    psDB *dbh                           ///< Database handle
     307);
     308
     309/** Commits the current transaction
     310 *
     311 * This function will commit the current transaction set (a rollback is not
     312 * possible after this function is successfully executed).  A commit also
     313 * effectively starts a new transaction if explict transactions are enabled.
     314 *
     315 * @return bool:    true on success
     316 */
     317bool psDBCommit(
     318    psDB *dbh                           ///< Database handle
     319);
     320
     321/** Rollback the current transaction
     322 *
     323 * This function will rollback the current transaction set.
     324 *
     325 * @return bool:    true on success
     326 */
     327bool psDBRollback(
     328    psDB *dbh                           ///< Database handle
     329);
     330
    287331/// @}
    288332
Note: See TracChangeset for help on using the changeset viewer.