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

Generated on Mon Apr 4 18:24:44 2005 for Pan-STARRS Foundation Library by  doxygen 1.3.9.1