psDB.h

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

Generated on Fri Feb 2 22:24:35 2007 for pslib by  doxygen 1.5.1