Changeset 3230 for trunk/archive/psdb/psDB.h
- Timestamp:
- Feb 15, 2005, 12:11:21 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/psdb/psDB.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/psdb/psDB.h
r3223 r3230 10 10 * @author Joshua Hoblitt 11 11 * 12 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $13 * @date $Date: 2005-02-15 07:28:50$12 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2005-02-15 22:11:21 $ 14 14 * 15 15 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 25 25 /// @{ 26 26 27 /** Database handle 28 * 29 * An opaque object representing a database connection. 30 * 31 */ 27 32 typedef struct { 28 33 MYSQL *mysql; ///< MySQL database handle 29 34 } psDB; 30 35 31 psDB *psDBInit(const char *host, const char *user, const char *passwd, const char *dbname); 32 33 void psDBCleanup(psDB *dbh); 34 35 bool psDBCreate(psDB *dbh, const char *dbname); 36 37 bool psDBChange(psDB *dbh, const char *dbname); 38 39 bool psDBDrop(psDB *dbh, const char *dbname); 40 41 bool psDBCreateTable(psDB *dbh, const char *tableName, psMetadata *md); 42 43 bool psDBDropTable(psDB *dbh, const char *tableName); 44 45 psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, const psU64 limit); 46 47 psVector *psDBSelectColumnNum(psDB *dbh, const char *tableName, const char *col, psElemType pType, const psU64 limit); 48 49 psArray *psDBSelectRows(psDB *dbh, const char *tableName, psMetadata *where); 50 51 bool psDBInsertOneRow(psDB *dbh, const char *tableName, psMetadata *row); 52 53 bool psDBInsertRows(psDB *dbh, const char *tableName, psArray *rowSet); 54 55 psArray *psDBDumpRows(psDB *dbh, const char *tableName); 56 57 psMetadata *psDBDumpCols(psDB *dbh, const char *tableName); 58 59 psS64 psDBUpdateRows(psDB *dbh, const char *tableName, psMetadata *where, psMetadata *values); 60 61 psS64 psDBDeleteRows(psDB *dbh, const char *tableName, psMetadata *where); 36 /** Opens a new database connection 37 * 38 * @return A new psDB object if the database connection is successful or NULL on 39 * failure. 40 */ 41 psDB *psDBInit( 42 const char *host, ///< Database server hostname 43 const char *user, ///< Database username 44 const char *passwd, ///< Database password 45 const char *dbname ///< Database namespace 46 ); 47 48 /** Closes a database connection 49 */ 50 void psDBCleanup( 51 psDB *dbh ///< Database handle 52 ); 53 54 /** Creates a new database namespace 55 * 56 * @return true on success 57 */ 58 bool psDBCreate( 59 psDB *dbh, ///< Database handle 60 const char *dbname ///< New database namespace 61 ); 62 63 /** Changes the current database namespace 64 * 65 * @return true on success 66 */ 67 bool psDBChange( 68 psDB *dbh, ///< Database handle 69 const char *dbname ///< Database namespace 70 ); 71 72 /** Drops a database namespace 73 * 74 * @return true on success 75 */ 76 bool psDBDrop( 77 psDB *dbh, ///< Database handle 78 const char *dbname ///< Database namespace 79 ); 80 81 /** Creates a new database table 82 * 83 * This function generates and executes the SQL needed to create a table named 84 * "tableName", with the column names and data types as described in "md". Each 85 * data item in the psMetadata collection represents a single table field. The 86 * name of the field is given by the name of the psMetadataItem and the data 87 * type is give by the psMetadataItem.type and psMetadataItem.ptype entries. A 88 * lookup table should be used to convert from PSLib types into MySQL 89 * compatible SQL data types. For example, a PS_META_STR would map to an SQL99 90 * varchar. If the value of type is PS_META_STR then the psMetadataItem.data 91 * element is set to a string with the length for the field written as a text 92 * string. The value of the psMetadataItem.data element is unused for the 93 * PS_META_PRIMITIVE types. Other psMetadata types beyond PS_META_STR and 94 * PS_META_PRIMITIVE are not allowed in a table definition. 95 * 96 * Database indexes can be specified setting the "comment" field to "Primary 97 * Key" or "Key". Comments are otherwise ignored. 98 * 99 * @return true on success 100 */ 101 bool psDBCreateTable( 102 psDB *dbh, ///< Database handle 103 const char *tableName, ///< Table name 104 psMetadata *md ///< Column names, types, and indexes 105 ); 106 107 /** Deletes a database table 108 * 109 * @return true on success 110 */ 111 bool psDBDropTable( 112 psDB *dbh, ///< Database handle 113 const char *tableName ///< Table name 114 ); 115 116 /** Selects a column from a table 117 * 118 * This function generates and executes the SQL needed to select an entire 119 * column from a table or up to "limit" rows from it. If "limit" is 0, the 120 * entire range is returned. 121 * 122 * @return A psArray of strings or NULL on failure 123 */ 124 psArray *psDBSelectColumn( 125 psDB *dbh, ///< Database handle 126 const char *tableName, ///< Table name 127 const char *col, ///< Column name 128 const psU64 limit ///< Maximum number of elements to return 129 ); 130 131 /** Selects a column from a table and casts it to a given type 132 * 133 * This function generates and executes the SQL needed to select an entire 134 * column from a table or up to "limit" rows from it. If "limit" is 0, the 135 * entire range is returned. The data in the column is cast to to "pType". 136 * 137 * @return A psVector or NULL on failure 138 */ 139 psVector *psDBSelectColumnNum( 140 psDB *dbh, ///< Database handle 141 const char *tableName, ///< Table name 142 const char *col, ///< Column name 143 psElemType pType, ///< Resulting psVector type 144 const psU64 limit ///< Maximum number of elements to return 145 ); 146 147 /** Selects a set of rows from a table 148 * 149 * This function returns rows from the specified table which match the 150 * restrictions given by "where". The restrictions are specified as field / 151 * value pairs. The psMetadata collection "where" must consist of valid 152 * database fields. The selected rows are returned as a psArray of psMetadata 153 * values, one per row. 154 * 155 * Currently, the "where" specification only supports the PS_META_STR type. 156 * The string value can be a SQL match pattern, e.g. "%foo%", or an empty 157 * string, e.g. "", to match NULL field values. 158 * 159 * @return A psArray of psMetadata or NULL on failure 160 */ 161 psArray *psDBSelectRows( 162 psDB *dbh, ///< Database handle 163 const char *tableName, ///< Table name 164 psMetadata *where ///< Row match criteria 165 ); 166 167 /** Insert a single row into a table 168 * 169 * This function inserts the data from "row" into "tableName". 170 * 171 * The "row" specification uses the psMetadataItem name as the column name. 172 * The field values maybe specified in any order. psMetadata types beyond 173 * PS_META_STR and PS_META_PRIMITIVE are not supported. If fields are 174 * specified in "row" that do not exist in "tableName", the insert will fail. 175 * 176 * @return true on success 177 */ 178 bool psDBInsertOneRow( 179 psDB *dbh, ///< Database handle 180 const char *tableName, ///< Table name 181 psMetadata *row ///< Row description 182 ); 183 184 /** Insert a set of rows into a table 185 * 186 * This function inserts the data from "rowSet" into "tableName". 187 * 188 * "rowSet" is a psArray of psMetadata containing row specifications identical to 189 * those used in psDBInsertOneRow(). 190 * 191 * @return true on success 192 */ 193 bool psDBInsertRows( 194 psDB *dbh, ///< Database handle 195 const char *tableName, ///< Table name 196 psArray *rowSet ///< Set of rows to insert 197 ); 198 199 /** Retrieves all rows from a table 200 * 201 * This function fetches all rows as an psArray of psMetadata. The rows are in 202 * the same psMetadata format as used in psDBInsertOneRow() & psDBInsertRows(). 203 * 204 * @return A psArray of psMetadata or NULL on failure 205 */ 206 psArray *psDBDumpRows( 207 psDB *dbh, ///< Database handle 208 const char *tableName ///< Table name 209 ); 210 211 /** Retrieves all columns from a table 212 * 213 * This function fetches all columns, as either a psVector or a psArray 214 * depending on whether or not the column is numeric, and return them in a 215 * psMetadata structure where psMetadataItem.name contains the column's name. 216 * 217 * @return A psMetadata containing either a psArrays or psVector per column 218 */ 219 psMetadata *psDBDumpCols( 220 psDB *dbh, ///< Database handle 221 const char *tableName ///< Table name 222 ); 223 224 /** Updates the field values, as specified, in a table 225 * 226 * This function updates the fields contained in "values" in the row(s) that 227 * have a field with the value indicated by "where". Where "where" is in the 228 * same format as used in psDBSelectRows(). 229 * 230 * The "values" specification uses the same format as the row specification 231 * used in psDBInsertOneRow(), etc. 232 * 233 * @return The number of rows modified or a negative value on error 234 */ 235 psS64 psDBUpdateRows( 236 psDB *dbh, ///< Database handle 237 const char *tableName, ///< Table name 238 psMetadata *where, ///< Row match criteria 239 psMetadata *values ///< new field values 240 ); 241 242 /** Deletes rows, as specified, in a table 243 * 244 * Delete the rows that are matched by "where" using the same semantics for 245 * "where" as in psDBUpdateRow(). 246 * 247 * If "where" is NULL, all rows in the table will be removed and regardless of 248 * the number of rows that were dropped, only 1 will be returned on success. 249 * 250 * @return The number of rows removed or a negative value on error 251 */ 252 psS64 psDBDeleteRows( 253 psDB *dbh, ///< Database handle 254 const char *tableName, ///< Table name 255 psMetadata *where ///< Row match criteria 256 ); 62 257 63 258 /// @}
Note:
See TracChangeset
for help on using the changeset viewer.
