Changeset 21394
- Timestamp:
- Feb 6, 2009, 1:38:15 PM (17 years ago)
- 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 23 23 * 4.1.2 or newer is required. 24 24 * 25 * $Id: psDB.c,v 1.170 2008-10-06 22:15:38eugene Exp $25 * $Id: psDB.c,v 1.170.18.1 2009-02-06 23:38:15 eugene Exp $ 26 26 */ 27 27 … … 254 254 PS_ASSERT_PTR_NON_NULL(dbname, false); 255 255 256 psString query = NULL;257 psStringAppend(&query, "CREATE DATABASE %s", dbname);258 259 256 // 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); 261 258 if (!status) { 262 259 psError(PS_ERR_UNKNOWN, false, "Failed to create new database."); 263 260 } 264 265 psFree(query);266 261 267 262 psTrace("psLib.db", PS_LOG_INFO, "created a database named %s", dbname); … … 295 290 PS_ASSERT_PTR_NON_NULL(dbname, false); 296 291 297 char *query = NULL;298 psStringAppend(&query, "DROP DATABASE %s", dbname);299 300 292 // 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); 302 294 if (!status) { 303 295 psError(PS_ERR_UNKNOWN, false, "Failed to drop database."); 304 296 } 305 306 psFree(query);307 297 308 298 psTrace("psLib.db", PS_LOG_INFO, "dropped database %s", dbname); … … 346 336 347 337 // 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); 353 339 if (!status) { 354 340 psError(PS_ERR_UNKNOWN, false, _("Failed to drop table.")); 355 341 } 356 357 psFree(query);358 342 359 343 psTrace("psLib.db", PS_LOG_INFO, "dropped table %s", tableName); … … 818 802 819 803 bool 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 819 bool p_psDBRunQueryF(psDB *dbh, 820 820 const char *format, 821 821 ...) … … 832 832 va_end(ap); 833 833 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); 841 835 842 836 psFree(query); 843 837 844 return true;845 } 846 847 long p_psDBRunQueryPrepared (psDB *dbh,838 return status; 839 } 840 841 long p_psDBRunQueryPreparedF(psDB *dbh, 848 842 const psArray *rowSet, 849 843 const char *format, … … 855 849 856 850 psString query = NULL; 857 858 // start lock on query cache859 if (psMemGetThreadSafety()) {860 pthread_mutex_lock(&preparedQueryMutex);861 }862 863 // initalize the prepared query cache864 if (!preparedQuery) {865 preparedQuery = psHashAlloc(10);866 psMemSetPersistent(preparedQuery, true);867 }868 851 869 852 // generate query string … … 873 856 va_end(ap); 874 857 858 long status = p_psDBRunQueryPrepared(dbh, rowSet, query); 859 860 psFree(query); 861 862 return status; 863 } 864 865 long 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 875 885 psTrace("psLib.db", PS_LOG_INFO, "Preparing SQL:\n%s", query); 876 886 … … 889 899 psError(PS_ERR_UNKNOWN, true, "Failed to prepare query. Error: %s", mysql_stmt_error(*stmt)); 890 900 mysql_stmt_close(*stmt); 891 psFree(query);892 901 893 902 // end lock on query cache … … 909 918 pthread_mutex_unlock(&preparedQueryMutex); 910 919 } 911 912 psFree(query);913 920 914 921 // how many place holders are in our query … … 2041 2048 // get field names 2042 2049 while ((item = psListGetAndIncrement(cursor))) { 2043 psStringAppend(&query, item->name);2050 psStringAppend(&query, "%s", item->name); 2044 2051 2045 2052 // + , + _ between every field name -
branches/eam_branch_20090206/psLib/src/db/psDB.h
r14761 r21394 22 22 * perform basic database operations. 23 23 * 24 * $Id: psDB.h,v 1.37 2007-09-05 23:20:04 jhoblittExp $24 * $Id: psDB.h,v 1.37.46.1 2009-02-06 23:38:15 eugene Exp $ 25 25 */ 26 26 … … 119 119 ); 120 120 121 /** Executes a SQL query121 /** Formats and Executes a SQL query 122 122 * 123 123 * This function will execute a string as a raw SQL query. No additional … … 127 127 * @return bool: true on success 128 128 */ 129 bool p_psDBRunQuery (129 bool p_psDBRunQueryF( 130 130 psDB *dbh, ///< Database handle 131 131 const char *format, ///< SQL string to execute … … 133 133 ) PS_ATTR_FORMAT(printf, 2, 3); 134 134 135 /** Executes a SQL query as a prepared statement135 /** Executes a SQL query 136 136 * 137 137 * This function will execute a string as a raw SQL query. No additional … … 139 139 * dialect is provided. Caveat emptor. 140 140 * 141 * @return bool: true on success 142 */ 143 bool 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 * 141 154 * @return long: the number of database rows affected 142 155 */ 143 long p_psDBRunQueryPrepared (156 long p_psDBRunQueryPreparedF( 144 157 psDB *dbh, ///< Database handle 145 158 const psArray *rowSet, ///< row data as psArray of psMetadata … … 147 160 ... 148 161 ) 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 */ 171 long 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 ); 149 176 150 177 /** Fetches the result of a SQL query
Note:
See TracChangeset
for help on using the changeset viewer.
