00001 /** @file psDB.h 00002 * 00003 * @brief database types and functions 00004 * 00005 * This file defines the abstract database type and functions that 00006 * perform basic database operations. 00007 * 00008 * @ingroup DataBase 00009 * 00010 * @author Joshua Hoblitt 00011 * 00012 * @version $Revision: 1.20 $ $Name: rel12 $ 00013 * @date $Date: 2006/06/23 02:57:04 $ 00014 * 00015 * Copyright 2005 Joshua Hoblitt, University of Hawaii 00016 */ 00017 00018 #ifndef PS_DB_H 00019 #define PS_DB_H 1 00020 00021 #ifndef OMIT_PSDB 00022 00023 #include "psType.h" 00024 #include "psMetadata.h" 00025 00026 /// @addtogroup DataBase 00027 /// @{ 00028 00029 /** Database handle 00030 * 00031 * An opaque object representing a database connection. 00032 * 00033 */ 00034 typedef struct 00035 { 00036 void* mysql; ///< MySQL database handle 00037 } 00038 psDB; 00039 00040 /** Opens a new database connection 00041 * 00042 * @return psDB*: A new psDB object if the database connection is successful or NULL on 00043 * failure. 00044 */ 00045 psDB *psDBInit( 00046 const char *host, ///< Database server hostname 00047 const char *user, ///< Database username 00048 const char *passwd, ///< Database password 00049 const char *dbname ///< Database namespace 00050 ); 00051 00052 /** Closes a database connection */ 00053 void psDBCleanup( 00054 psDB *dbh ///< Database handle 00055 ); 00056 00057 /** Creates a new database namespace 00058 * 00059 * @return bool: true on success 00060 */ 00061 bool psDBCreate( 00062 psDB *dbh, ///< Database handle 00063 const char *dbname ///< New database namespace 00064 ); 00065 00066 /** Changes the current database namespace 00067 * 00068 * @return bool: true on success 00069 */ 00070 bool psDBChange( 00071 psDB *dbh, ///< Database handle 00072 const char *dbname ///< Database namespace 00073 ); 00074 00075 /** Drops a database namespace 00076 * 00077 * @return bool: true on success 00078 */ 00079 bool psDBDrop( 00080 psDB *dbh, ///< Database handle 00081 const char *dbname ///< Database namespace 00082 ); 00083 00084 /** Executes a SQL query 00085 * 00086 * This function will execute a string as a raw SQL query. No additional 00087 * processing of the string or abstraction of the underlying database's SQL 00088 * dialect is provided. Caveat emptor. 00089 * 00090 * @return bool: true on success 00091 */ 00092 bool p_psDBRunQuery( 00093 psDB *dbh, ///< Database handle 00094 const char *format, ///< SQL string to execute 00095 ... ///< Arguments for name formatting and metadata item data. 00096 ); 00097 00098 /** Executes a SQL query as a prepared statement 00099 * 00100 * This function will execute a string as a raw SQL query. No additional 00101 * processing of the string or abstraction of the underlying database's SQL 00102 * dialect is provided. Caveat emptor. 00103 * 00104 * @return long: the number of database rows affected 00105 */ 00106 long p_psDBRunQueryPrepared( 00107 psDB *dbh, ///< Database handle 00108 const psArray *rowSet, ///< row data as psArray of psMetadata 00109 const char *format, ///< SQL string to execute 00110 ... 00111 ); 00112 00113 /** Fetches the result of a SQL query 00114 * 00115 * This function returns the result of the most recent SQL query as a psArray 00116 * of psMetadata. Caveat emptor. 00117 * 00118 * @return psArray*: A psArray of psMetadata or NULL on failure 00119 */ 00120 psArray *p_psDBFetchResult( 00121 psDB *dbh ///< Database handle 00122 ); 00123 00124 /** Creates a new database table 00125 * 00126 * This function generates and executes the SQL needed to create a table named 00127 * "tableName", with the column names and data types as described in "md". Each 00128 * data item in the psMetadata collection represents a single table field. The 00129 * name of the field is given by the name of the psMetadataItem and the data 00130 * type is give by the psMetadataItem.type and psMetadataItem.ptype entries. A 00131 * lookup table should be used to convert from PSLib types into MySQL 00132 * compatible SQL data types. For example, a PS_DATA_STRING would map to an SQL99 00133 * varchar. If the value of type is PS_DATA_STRING then the psMetadataItem.data 00134 * element is set to a string with the length for the field written as a text 00135 * string. The value of the psMetadataItem.data element is unused for the 00136 * PS_META_PRIMITIVE types. Other psMetadata types beyond PS_DATA_STRING and 00137 * PS_META_PRIMITIVE are not allowed in a table definition. 00138 * 00139 * Database indexes can be specified setting the "comment" field to "Primary 00140 * Key" or "Key". Comments are otherwise ignored. 00141 * 00142 * @return bool: true on success 00143 */ 00144 bool psDBCreateTable( 00145 psDB *dbh, ///< Database handle 00146 const char *tableName, ///< Table name 00147 const psMetadata *md ///< Column names, types, and indexes 00148 ); 00149 00150 /** Deletes a database table 00151 * 00152 * @return bool: true on success 00153 */ 00154 bool psDBDropTable( 00155 psDB *dbh, ///< Database handle 00156 const char *tableName ///< Table name 00157 ); 00158 00159 /** Selects a column from a table 00160 * 00161 * This function generates and executes the SQL needed to select an entire 00162 * column from a table or up to "limit" rows from it. If "limit" is 0, the 00163 * entire range is returned. 00164 * 00165 * @return psArray*: A psArray of strings or NULL on failure 00166 */ 00167 psArray *psDBSelectColumn( 00168 psDB *dbh, ///< Database handle 00169 const char *tableName, ///< Table name 00170 const char *col, ///< Column name 00171 unsigned long long limit ///< Maximum number of elements to return 00172 ); 00173 00174 /** Selects a column from a table and casts it to a given type 00175 * 00176 * This function generates and executes the SQL needed to select an entire 00177 * column from a table or up to "limit" rows from it. If "limit" is 0, the 00178 * entire range is returned. The data in the column is cast to to "pType". 00179 * 00180 * @return psVector*: A psVector or NULL on failure 00181 */ 00182 psVector *psDBSelectColumnNum( 00183 psDB *dbh, ///< Database handle 00184 const char *tableName, ///< Table name 00185 const char *col, ///< Column name 00186 psElemType type, ///< Resulting psVector type 00187 unsigned long long limit ///< Maximum number of elements to return 00188 ); 00189 00190 /** Selects a set of rows from a table 00191 * 00192 * This function returns rows from the specified table which match the 00193 * restrictions given by "where". The restrictions are specified as field / 00194 * value pairs. The psMetadata collection "where" must consist of valid 00195 * database fields. The selected rows are returned as a psArray of psMetadata 00196 * values, one per row. 00197 * 00198 * Currently, the "where" specification only supports the PS_DATA_STRING type. 00199 * The string value can be a SQL match pattern, e.g. "%foo%", or an empty 00200 * string, e.g. "", to match NULL field values. 00201 * 00202 * @return psArray*: A psArray of psMetadata or NULL on failure 00203 */ 00204 psArray *psDBSelectRows( 00205 psDB *dbh, ///< Database handle 00206 const char *tableName, ///< Table name 00207 const psMetadata *where, ///< Row match criteria 00208 unsigned long long limit ///< Maximum number of elements to return 00209 ); 00210 00211 /** Insert a single row into a table 00212 * 00213 * This function inserts the data from "row" into "tableName". 00214 * 00215 * The "row" specification uses the psMetadataItem name as the column name. 00216 * The field values may be specified in any order. psMetadata types beyond 00217 * PS_DATA_STRING and PS_META_PRIMITIVE are not supported. If fields are 00218 * specified in "row" that do not exist in "tableName", the insert will fail. 00219 * 00220 * @return bool: true on success 00221 */ 00222 bool psDBInsertOneRow( 00223 psDB *dbh, ///< Database handle 00224 const char *tableName, ///< Table name 00225 const psMetadata *row ///< Row description 00226 ); 00227 00228 /** Insert a set of rows into a table 00229 * 00230 * This function inserts the data from "rowSet" into "tableName". 00231 * 00232 * "rowSet" is a psArray of psMetadata containing row specifications identical to 00233 * those used in psDBInsertOneRow(). 00234 * 00235 * @return bool: true on success 00236 */ 00237 bool psDBInsertRows( 00238 psDB *dbh, ///< Database handle 00239 const char *tableName, ///< Table name 00240 const psArray *rowSet ///< Set of rows to insert 00241 ); 00242 00243 /** Retrieves all rows from a table 00244 * 00245 * This function fetches all rows as an psArray of psMetadata. The rows are in 00246 * the same psMetadata format as used in psDBInsertOneRow() & psDBInsertRows(). 00247 * 00248 * @return psArray*: A psArray of psMetadata or NULL on failure 00249 */ 00250 psArray *psDBDumpRows( 00251 psDB *dbh, ///< Database handle 00252 const char *tableName ///< Table name 00253 ); 00254 00255 /** Retrieves all columns from a table 00256 * 00257 * This function fetches all columns, as either a psVector or a psArray 00258 * depending on whether or not the column is numeric, and return them in a 00259 * psMetadata structure where psMetadataItem.name contains the column's name. 00260 * 00261 * @return psMetadata*: A psMetadata containing either a psArrays or psVector per column 00262 */ 00263 psMetadata *psDBDumpCols( 00264 psDB *dbh, ///< Database handle 00265 const char *tableName ///< Table name 00266 ); 00267 00268 /** Updates the field values, as specified, in a table 00269 * 00270 * This function updates the fields contained in "values" in the row(s) that 00271 * have a field with the value indicated by "where". Where "where" is in the 00272 * same format as used in psDBSelectRows(). 00273 * 00274 * The "values" specification uses the same format as the row specification 00275 * used in psDBInsertOneRow(), etc. 00276 * 00277 * @return long: The number of rows modified or a negative value on error 00278 */ 00279 long psDBUpdateRows( 00280 psDB *dbh, ///< Database handle 00281 const char *tableName, ///< Table name 00282 const psMetadata *where, ///< Row match criteria 00283 psMetadata *values ///< new field values 00284 ); 00285 00286 /** Deletes rows, as specified, in a table 00287 * 00288 * Delete the rows that are matched by "where" using the same semantics for 00289 * "where" as in psDBUpdateRow(). 00290 * 00291 * If "where" is NULL, all rows in the table will be removed and regardless of 00292 * the number of rows that were dropped, only 1 will be returned on success. 00293 * 00294 * @return long: The number of rows removed or a negative value on error 00295 */ 00296 long psDBDeleteRows( 00297 psDB *dbh, ///< Database handle 00298 const char *tableName, ///< Table name 00299 const psMetadata *where, ///< Row match criteria 00300 unsigned long long limit ///< Maximum number of rows to delete 00301 ); 00302 00303 /** Get the last insert ID 00304 * 00305 * Returns the value of MySQLs 'LAST_INSERT_ID()' function 00306 * 00307 * @return long: The last insert ID 00308 */ 00309 long psDBLastInsertID( 00310 psDB *dbh ///< Database handle 00311 ); 00312 00313 /** Enable/Disable explicit database transactions 00314 * 00315 * This function is used to enable explicit transaction support. It is off by 00316 * default. 00317 * 00318 * @return bool: true if transactions are enabled 00319 */ 00320 bool psDBExplicitTrans( 00321 psDB *dbh, ///< Database handle 00322 bool mode ///< transactions enable/disable 00323 ); 00324 00325 /** Start a new transaction set. 00326 * 00327 * This is only a meaningful action if explict transactions are disabled. 00328 * 00329 * @return bool: true on success 00330 */ 00331 bool psDBTransaction( 00332 psDB *dbh ///< Database handle 00333 ); 00334 00335 /** Commits the current transaction 00336 * 00337 * This function will commit the current transaction set (a rollback is not 00338 * possible after this function is successfully executed). A commit also 00339 * effectively starts a new transaction if explict transactions are enabled. 00340 * 00341 * @return bool: true on success 00342 */ 00343 bool psDBCommit( 00344 psDB *dbh ///< Database handle 00345 ); 00346 00347 /** Rollback the current transaction 00348 * 00349 * This function will rollback the current transaction set. 00350 * 00351 * @return bool: true on success 00352 */ 00353 bool psDBRollback( 00354 psDB *dbh ///< Database handle 00355 ); 00356 00357 /// @} 00358 00359 #else 00360 typedef void psDB; 00361 #endif // OMIT_PSDB 00362 00363 #endif // PS_DB_H
1.4.4