IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21394


Ignore:
Timestamp:
Feb 6, 2009, 1:38:15 PM (17 years ago)
Author:
eugene
Message:

define formatted and unformatted versions of p_psDBRunQuery and p_psDBRunQueryPrepared

Location:
branches/eam_branch_20090206/psLib/src/db
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20090206/psLib/src/db/psDB.c

    r19927 r21394  
    2323 * 4.1.2 or newer is required.
    2424 *
    25  * $Id: psDB.c,v 1.170 2008-10-06 22:15:38 eugene Exp $
     25 * $Id: psDB.c,v 1.170.18.1 2009-02-06 23:38:15 eugene Exp $
    2626 */
    2727
     
    254254    PS_ASSERT_PTR_NON_NULL(dbname, false);
    255255
    256     psString query = NULL;
    257     psStringAppend(&query, "CREATE DATABASE %s", dbname);
    258 
    259256    // the MySQL C API notes that mysql_create_db() is deprecated
    260     bool status = p_psDBRunQuery(dbh, query);
     257    bool status = p_psDBRunQueryF(dbh, "CREATE DATABASE %s", dbname);
    261258    if (!status) {
    262259        psError(PS_ERR_UNKNOWN, false, "Failed to create new database.");
    263260    }
    264 
    265     psFree(query);
    266261
    267262    psTrace("psLib.db", PS_LOG_INFO, "created a database named %s", dbname);
     
    295290    PS_ASSERT_PTR_NON_NULL(dbname, false);
    296291
    297     char            *query = NULL;
    298     psStringAppend(&query, "DROP DATABASE %s", dbname);
    299 
    300292    // the MySQL C API notes that mysql_drop_db() is deprecated
    301     bool status = p_psDBRunQuery(dbh, query);
     293    bool status = p_psDBRunQueryF(dbh, "DROP DATABASE %s", dbname);
    302294    if (!status) {
    303295        psError(PS_ERR_UNKNOWN, false, "Failed to drop database.");
    304296    }
    305 
    306     psFree(query);
    307297
    308298    psTrace("psLib.db", PS_LOG_INFO, "dropped database %s", dbname);
     
    346336
    347337    // Create SQL command string to drop table
    348     psString query = NULL;
    349     psStringAppend(&query, "DROP TABLE %s", tableName);
    350 
    351     // Execute query
    352     bool status = p_psDBRunQuery(dbh, query);
     338    bool status = p_psDBRunQueryF(dbh, "DROP TABLE %s", tableName);
    353339    if (!status) {
    354340        psError(PS_ERR_UNKNOWN, false, _("Failed to drop table."));
    355341    }
    356 
    357     psFree(query);
    358342
    359343    psTrace("psLib.db", PS_LOG_INFO, "dropped table %s", tableName);
     
    818802
    819803bool p_psDBRunQuery(psDB *dbh,
     804                    const char *query)
     805{
     806    PS_ASSERT_PTR_NON_NULL(dbh, false);
     807    PS_ASSERT_PTR_NON_NULL(query, false);
     808
     809    psTrace("psLib.db", PS_LOG_INFO, "Executing SQL:\n%s", query);
     810
     811    if (mysql_real_query(dbh->mysql, query, (unsigned long)strlen(query)) !=0) {
     812        psError(mysqlTopsErr(dbh->mysql), true, _("Failed to execute SQL query.  Error: %s"), mysql_error(dbh->mysql));
     813        return false;
     814    }
     815
     816    return true;
     817}
     818
     819bool p_psDBRunQueryF(psDB *dbh,
    820820                    const char *format,
    821821                    ...)
     
    832832    va_end(ap);
    833833
    834     psTrace("psLib.db", PS_LOG_INFO, "Executing SQL:\n%s", query);
    835 
    836     if (mysql_real_query(dbh->mysql, query, (unsigned long)strlen(query)) !=0) {
    837         psError(mysqlTopsErr(dbh->mysql), true, _("Failed to execute SQL query.  Error: %s"), mysql_error(dbh->mysql));
    838         psFree(query);
    839         return false;
    840     }
     834    bool status = p_psDBRunQuery (dbh, query);
    841835
    842836    psFree(query);
    843837
    844     return true;
    845 }
    846 
    847 long p_psDBRunQueryPrepared(psDB *dbh,
     838    return status;
     839}
     840
     841long p_psDBRunQueryPreparedF(psDB *dbh,
    848842                            const psArray *rowSet,
    849843                            const char *format,
     
    855849
    856850    psString query = NULL;
    857 
    858     // start lock on query cache
    859     if (psMemGetThreadSafety()) {
    860         pthread_mutex_lock(&preparedQueryMutex);
    861     }
    862 
    863     // initalize the prepared query cache
    864     if (!preparedQuery) {
    865         preparedQuery = psHashAlloc(10);
    866         psMemSetPersistent(preparedQuery, true);
    867     }
    868851
    869852    // generate query string
     
    873856    va_end(ap);
    874857
     858    long status = p_psDBRunQueryPrepared(dbh, rowSet, query);
     859
     860    psFree(query);
     861
     862    return status;
     863}
     864
     865long p_psDBRunQueryPrepared(psDB *dbh,
     866                            const psArray *rowSet,
     867                            const char *query
     868    )                           
     869{
     870    PS_ASSERT_PTR_NON_NULL(dbh, -1);
     871    PS_ASSERT_PTR_NON_NULL(rowSet, -1);
     872    PS_ASSERT_PTR_NON_NULL(query, -1);
     873
     874    // start lock on query cache
     875    if (psMemGetThreadSafety()) {
     876        pthread_mutex_lock(&preparedQueryMutex);
     877    }
     878
     879    // initalize the prepared query cache
     880    if (!preparedQuery) {
     881        preparedQuery = psHashAlloc(10);
     882        psMemSetPersistent(preparedQuery, true);
     883    }
     884
    875885    psTrace("psLib.db", PS_LOG_INFO, "Preparing SQL:\n%s", query);
    876886
     
    889899            psError(PS_ERR_UNKNOWN, true, "Failed to prepare query.  Error: %s", mysql_stmt_error(*stmt));
    890900            mysql_stmt_close(*stmt);
    891             psFree(query);
    892901
    893902            // end lock on query cache
     
    909918        pthread_mutex_unlock(&preparedQueryMutex);
    910919    }
    911 
    912     psFree(query);
    913920
    914921    // how many place holders are in our query
     
    20412048    // get field names
    20422049    while ((item = psListGetAndIncrement(cursor))) {
    2043         psStringAppend(&query, item->name);
     2050        psStringAppend(&query, "%s", item->name);
    20442051
    20452052        // + , + _ between every field name
  • branches/eam_branch_20090206/psLib/src/db/psDB.h

    r14761 r21394  
    2222 * perform basic database operations.
    2323 *
    24  * $Id: psDB.h,v 1.37 2007-09-05 23:20:04 jhoblitt Exp $
     24 * $Id: psDB.h,v 1.37.46.1 2009-02-06 23:38:15 eugene Exp $
    2525 */
    2626
     
    119119);
    120120
    121 /** Executes a SQL query
     121/** Formats and Executes a SQL query
    122122 *
    123123 * This function will execute a string as a raw SQL query.  No additional
     
    127127 * @return bool:    true on success
    128128 */
    129 bool p_psDBRunQuery(
     129bool p_psDBRunQueryF(
    130130    psDB *dbh,                         ///< Database handle
    131131    const char *format,                ///< SQL string to execute
     
    133133) PS_ATTR_FORMAT(printf, 2, 3);
    134134
    135 /** Executes a SQL query as a prepared statement
     135/** Executes a SQL query
    136136 *
    137137 * This function will execute a string as a raw SQL query.  No additional
     
    139139 * dialect is provided.  Caveat emptor.
    140140 *
     141 * @return bool:    true on success
     142 */
     143bool p_psDBRunQuery(
     144    psDB *dbh,                         ///< Database handle
     145    const char *query                ///< SQL string to execute
     146    );
     147
     148/** Formats and Executes a SQL query as a prepared statement
     149 *
     150 * This function will execute a string as a raw SQL query.  No additional
     151 * processing of the string or abstraction of the underlying database's SQL
     152 * dialect is provided.  Caveat emptor.
     153 *
    141154 * @return long:    the number of database rows affected
    142155 */
    143 long p_psDBRunQueryPrepared(
     156long p_psDBRunQueryPreparedF(
    144157    psDB *dbh,                          ///< Database handle
    145158    const psArray *rowSet,              ///< row data as psArray of psMetadata
     
    147160    ...
    148161) PS_ATTR_FORMAT(printf, 3, 4);
     162
     163/** Executes a SQL query as a prepared statement
     164 *
     165 * This function will execute a string as a raw SQL query.  No additional
     166 * processing of the string or abstraction of the underlying database's SQL
     167 * dialect is provided.  Caveat emptor.
     168 *
     169 * @return long:    the number of database rows affected
     170 */
     171long p_psDBRunQueryPrepared(
     172    psDB *dbh,                          ///< Database handle
     173    const psArray *rowSet,              ///< row data as psArray of psMetadata
     174    const char *query                   ///< SQL string to execute
     175);
    149176
    150177/** Fetches the result of a SQL query
Note: See TracChangeset for help on using the changeset viewer.