Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

psDB.h

Go to the documentation of this file.
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.1.1.1 $ $Name:  $
00013  *  @date $Date: 2005/06/15 21:08:12 $
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 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  */
00054 void psDBCleanup(
00055     psDB *dbh                           ///< Database handle
00056 );
00057 
00058 /** Creates a new database namespace
00059  *
00060  * @return true on success
00061  */
00062 bool psDBCreate(
00063     psDB *dbh,                          ///< Database handle
00064     const char *dbname                  ///< New database namespace
00065 );
00066 
00067 /** Changes the current database namespace
00068  *
00069  * @return true on success
00070  */
00071 bool psDBChange(
00072     psDB *dbh,                          ///< Database handle
00073     const char *dbname                  ///< Database namespace
00074 );
00075 
00076 /** Drops a database namespace
00077  *
00078  * @return true on success
00079  */
00080 bool psDBDrop(
00081     psDB *dbh,                          ///< Database handle
00082     const char *dbname                  ///< Database namespace
00083 );
00084 
00085 /** Executes a SQL query
00086  *
00087  * This function will execute a string as a raw SQL query.  No additional
00088  * processing of the string or abstraction of the underlying database's SQL
00089  * dialect is provided.  Caveat emptor.
00090  *
00091  * @return true on success
00092  */
00093 bool p_psDBRunQuery(
00094     psDB *dbh,                          ///< Database handle
00095     const char *query                   ///< SQL string to execute
00096 );
00097 
00098 /** Creates a new database table
00099  *
00100  * This function generates and executes the SQL needed to create a table named
00101  * "tableName", with the column names and data types as described in "md".  Each
00102  * data item in the psMetadata collection represents a single table field.  The
00103  * name of the field is given by the name of the psMetadataItem and the data
00104  * type is give by the psMetadataItem.type and psMetadataItem.ptype entries.  A
00105  * lookup table should be used to convert from PSLib types into MySQL
00106  * compatible SQL data types.  For example, a PS_META_STR would map to an SQL99
00107  * varchar.  If the value of type is PS_META_STR then the psMetadataItem.data
00108  * element is set to a string with the length for the field written as a text
00109  * string.  The value of the psMetadataItem.data element is unused for the
00110  * PS_META_PRIMITIVE types.  Other psMetadata types beyond PS_META_STR and
00111  * PS_META_PRIMITIVE are not allowed in a table definition.
00112  *
00113  * Database indexes can be specified setting the "comment" field to "Primary
00114  * Key" or "Key".  Comments are otherwise ignored.
00115  *
00116  * @return true on success
00117  */
00118 bool psDBCreateTable(
00119     psDB *dbh,                          ///< Database handle
00120     const char *tableName,              ///< Table name
00121     psMetadata *md                      ///< Column names, types, and indexes
00122 );
00123 
00124 /** Deletes a database table
00125  *
00126  * @return true on success
00127 */
00128 bool psDBDropTable(
00129     psDB *dbh,                          ///< Database handle
00130     const char *tableName               ///< Table name
00131 );
00132 
00133 /** Selects a column from a table
00134  *
00135  * This function generates and executes the SQL needed to select an entire
00136  * column from a table or up to "limit" rows from it.  If "limit" is 0, the
00137  * entire range is returned.
00138  *
00139  * @return A psArray of strings or NULL on failure
00140  */
00141 psArray *psDBSelectColumn(
00142     psDB *dbh,                          ///< Database handle
00143     const char *tableName,              ///< Table name
00144     const char *col,                    ///< Column name
00145     const psU64 limit                   ///< Maximum number of elements to return
00146 );
00147 
00148 /** Selects a column from a table and casts it to a given type
00149  *
00150  * This function generates and executes the SQL needed to select an entire
00151  * column from a table or up to "limit" rows from it.  If "limit" is 0, the
00152  * entire range is returned.  The data in the column is cast to to "pType".
00153  *
00154  * @return A psVector or NULL on failure
00155  */
00156 psVector *psDBSelectColumnNum(
00157     psDB *dbh,                          ///< Database handle
00158     const char *tableName,              ///< Table name
00159     const char *col,                    ///< Column name
00160     psElemType pType,                   ///< Resulting psVector type
00161     const psU64 limit                   ///< Maximum number of elements to return
00162 );
00163 
00164 /** Selects a set of rows from a table
00165  *
00166  * This function returns rows from the specified table which match the
00167  * restrictions given by "where".  The restrictions are specified as field /
00168  * value pairs.  The psMetadata collection "where" must consist of valid
00169  * database fields.  The selected rows are returned as a psArray of psMetadata
00170  * values, one per row.
00171  *
00172  * Currently, the "where" specification only supports the PS_META_STR type.
00173  * The string value can be a SQL match pattern, e.g. "%foo%", or an empty
00174  * string, e.g. "", to match NULL field values.
00175  *
00176  * @return A psArray of psMetadata or NULL on failure
00177  */
00178 psArray *psDBSelectRows(
00179     psDB *dbh,                          ///< Database handle
00180     const char *tableName,              ///< Table name
00181     psMetadata *where,                  ///< Row match criteria
00182     const psU64 limit                   ///< Maximum number of elements to return
00183 );
00184 
00185 /** Insert a single row into a table
00186  *
00187  * This function inserts the data from "row" into "tableName".
00188  *
00189  * The "row" specification uses the psMetadataItem name as the column name.
00190  * The field values may be specified in any order.  psMetadata types beyond
00191  * PS_META_STR and PS_META_PRIMITIVE are not supported.  If fields are
00192  * specified in "row" that do not exist in "tableName", the insert will fail.
00193  *
00194  * @return true on success
00195  */
00196 bool psDBInsertOneRow(
00197     psDB *dbh,                          ///< Database handle
00198     const char *tableName,              ///< Table name
00199     psMetadata *row                     ///< Row description
00200 );
00201 
00202 /** Insert a set of rows into a table
00203  *
00204  * This function inserts the data from "rowSet" into "tableName".
00205  *
00206  * "rowSet" is a psArray of psMetadata containing row specifications identical to
00207  * those used in psDBInsertOneRow().
00208  *
00209  * @return true on success
00210  */
00211 bool psDBInsertRows(
00212     psDB *dbh,                          ///< Database handle
00213     const char *tableName,              ///< Table name
00214     psArray *rowSet                     ///< Set of rows to insert
00215 );
00216 
00217 /** Retrieves all rows from a table
00218  *
00219  * This function fetches all rows as an psArray of psMetadata.  The rows are in
00220  * the same psMetadata format as used in psDBInsertOneRow() & psDBInsertRows().
00221  *
00222  * @return A psArray of psMetadata or NULL on failure
00223  */
00224 psArray *psDBDumpRows(
00225     psDB *dbh,                          ///< Database handle
00226     const char *tableName               ///< Table name
00227 );
00228 
00229 /** Retrieves all columns from a table
00230  *
00231  * This function fetches all columns, as either a psVector or a psArray
00232  * depending on whether or not the column is numeric, and return them in a
00233  * psMetadata structure where psMetadataItem.name contains the column's name.
00234  *
00235  * @return A psMetadata containing either a psArrays or psVector per column
00236  */
00237 psMetadata *psDBDumpCols(
00238     psDB *dbh,                          ///< Database handle
00239     const char *tableName               ///< Table name
00240 );
00241 
00242 /** Updates the field values, as specified, in a table
00243  *
00244  * This function updates the fields contained in "values" in the row(s) that
00245  * have a field with the value indicated by "where".  Where "where" is in the
00246  * same format as used in psDBSelectRows().
00247  *
00248  * The "values" specification uses the same format as the row specification
00249  * used in psDBInsertOneRow(), etc.
00250  *
00251  * @return The number of rows modified or a negative value on error
00252  */
00253 psS64 psDBUpdateRows(
00254     psDB *dbh,                          ///< Database handle
00255     const char *tableName,              ///< Table name
00256     psMetadata *where,                  ///< Row match criteria
00257     psMetadata *values                  ///< new field values
00258 );
00259 
00260 /** Deletes rows, as specified, in a table
00261  *
00262  * Delete the rows that are matched by "where" using the same semantics for
00263  * "where" as in psDBUpdateRow().
00264  *
00265  * If "where" is NULL, all rows in the table will be removed and regardless of
00266  * the number of rows that were dropped, only 1 will be returned on success.
00267  *
00268  * @return The number of rows removed or a negative value on error
00269  */
00270 psS64 psDBDeleteRows(
00271     psDB *dbh,                          ///< Database handle
00272     const char *tableName,             ///< Table name
00273     psMetadata *where                  ///< Row match criteria
00274 );
00275 
00276 /// @}
00277 
00278 #endif // OMIT_PSDB
00279 
00280 #endif // PS_DB_H

Generated on Wed Jun 15 11:00:56 2005 for Pan-STARRS Foundation Library by  doxygen 1.4.1