apr_dbd.h revision 259065
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* Overview of what this is and does: 18 * http://www.apache.org/~niq/dbd.html 19 */ 20 21#ifndef APR_DBD_H 22#define APR_DBD_H 23 24#include "apu.h" 25#include "apr_pools.h" 26 27#ifdef __cplusplus 28extern "C" { 29#endif 30 31/** 32 * @file apr_dbd.h 33 * @brief APR-UTIL DBD library 34 */ 35/** 36 * @defgroup APR_Util_DBD DBD routines 37 * @ingroup APR_Util 38 * @{ 39 */ 40 41/** 42 * Mapping of C to SQL types, used for prepared statements. 43 * @remarks 44 * For apr_dbd_p[v]query/select functions, in and out parameters are always 45 * const char * (i.e. regular nul terminated strings). LOB types are passed 46 * with four (4) arguments: payload, length, table and column, all as const 47 * char *, where table and column are reserved for future use by Oracle. 48 * @remarks 49 * For apr_dbd_p[v]bquery/select functions, in and out parameters are 50 * described next to each enumeration constant and are generally native binary 51 * types or some APR data type. LOB types are passed with four (4) arguments: 52 * payload (char*), length (apr_size_t*), table (char*) and column (char*). 53 * Table and column are reserved for future use by Oracle. 54 */ 55typedef enum { 56 APR_DBD_TYPE_NONE, 57 APR_DBD_TYPE_TINY, /**< \%hhd : in, out: char* */ 58 APR_DBD_TYPE_UTINY, /**< \%hhu : in, out: unsigned char* */ 59 APR_DBD_TYPE_SHORT, /**< \%hd : in, out: short* */ 60 APR_DBD_TYPE_USHORT, /**< \%hu : in, out: unsigned short* */ 61 APR_DBD_TYPE_INT, /**< \%d : in, out: int* */ 62 APR_DBD_TYPE_UINT, /**< \%u : in, out: unsigned int* */ 63 APR_DBD_TYPE_LONG, /**< \%ld : in, out: long* */ 64 APR_DBD_TYPE_ULONG, /**< \%lu : in, out: unsigned long* */ 65 APR_DBD_TYPE_LONGLONG, /**< \%lld : in, out: apr_int64_t* */ 66 APR_DBD_TYPE_ULONGLONG, /**< \%llu : in, out: apr_uint64_t* */ 67 APR_DBD_TYPE_FLOAT, /**< \%f : in, out: float* */ 68 APR_DBD_TYPE_DOUBLE, /**< \%lf : in, out: double* */ 69 APR_DBD_TYPE_STRING, /**< \%s : in: char*, out: char** */ 70 APR_DBD_TYPE_TEXT, /**< \%pDt : in: char*, out: char** */ 71 APR_DBD_TYPE_TIME, /**< \%pDi : in: char*, out: char** */ 72 APR_DBD_TYPE_DATE, /**< \%pDd : in: char*, out: char** */ 73 APR_DBD_TYPE_DATETIME, /**< \%pDa : in: char*, out: char** */ 74 APR_DBD_TYPE_TIMESTAMP, /**< \%pDs : in: char*, out: char** */ 75 APR_DBD_TYPE_ZTIMESTAMP, /**< \%pDz : in: char*, out: char** */ 76 APR_DBD_TYPE_BLOB, /**< \%pDb : in: char* apr_size_t* char* char*, out: apr_bucket_brigade* */ 77 APR_DBD_TYPE_CLOB, /**< \%pDc : in: char* apr_size_t* char* char*, out: apr_bucket_brigade* */ 78 APR_DBD_TYPE_NULL /**< \%pDn : in: void*, out: void** */ 79} apr_dbd_type_e; 80 81/* These are opaque structs. Instantiation is up to each backend */ 82typedef struct apr_dbd_driver_t apr_dbd_driver_t; 83typedef struct apr_dbd_t apr_dbd_t; 84typedef struct apr_dbd_transaction_t apr_dbd_transaction_t; 85typedef struct apr_dbd_results_t apr_dbd_results_t; 86typedef struct apr_dbd_row_t apr_dbd_row_t; 87typedef struct apr_dbd_prepared_t apr_dbd_prepared_t; 88 89/** apr_dbd_init: perform once-only initialisation. Call once only. 90 * 91 * @param pool - pool to register any shutdown cleanups, etc 92 */ 93APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool); 94 95/** apr_dbd_get_driver: get the driver struct for a name 96 * 97 * @param pool - (process) pool to register cleanup 98 * @param name - driver name 99 * @param driver - pointer to driver struct. 100 * @return APR_SUCCESS for success 101 * @return APR_ENOTIMPL for no driver (when DSO not enabled) 102 * @return APR_EDSOOPEN if DSO driver file can't be opened 103 * @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver 104 */ 105APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name, 106 const apr_dbd_driver_t **driver); 107 108/** apr_dbd_open_ex: open a connection to a backend 109 * 110 * @param pool - working pool 111 * @param params - arguments to driver (implementation-dependent) 112 * @param handle - pointer to handle to return 113 * @param driver - driver struct. 114 * @param error - descriptive error. 115 * @return APR_SUCCESS for success 116 * @return APR_EGENERAL if driver exists but connection failed 117 * @remarks PostgreSQL: the params is passed directly to the PQconnectdb() 118 * function (check PostgreSQL documentation for more details on the syntax). 119 * @remarks SQLite2: the params is split on a colon, with the first part used 120 * as the filename and second part converted to an integer and used as file 121 * mode. 122 * @remarks SQLite3: the params is passed directly to the sqlite3_open() 123 * function as a filename to be opened (check SQLite3 documentation for more 124 * details). 125 * @remarks Oracle: the params can have "user", "pass", "dbname" and "server" 126 * keys, each followed by an equal sign and a value. Such key/value pairs can 127 * be delimited by space, CR, LF, tab, semicolon, vertical bar or comma. 128 * @remarks MySQL: the params can have "host", "port", "user", "pass", 129 * "dbname", "sock", "flags" "fldsz", "group" and "reconnect" keys, each 130 * followed by an equal sign and a value. Such key/value pairs can be 131 * delimited by space, CR, LF, tab, semicolon, vertical bar or comma. For 132 * now, "flags" can only recognise CLIENT_FOUND_ROWS (check MySQL manual for 133 * details). The value associated with "fldsz" determines maximum amount of 134 * memory (in bytes) for each of the fields in the result set of prepared 135 * statements. By default, this value is 1 MB. The value associated with 136 * "group" determines which group from configuration file to use (see 137 * MYSQL_READ_DEFAULT_GROUP option of mysql_options() in MySQL manual). 138 * Reconnect is set to 1 by default (i.e. true). 139 * @remarks FreeTDS: the params can have "username", "password", "appname", 140 * "dbname", "host", "charset", "lang" and "server" keys, each followed by an 141 * equal sign and a value. 142 */ 143APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver, 144 apr_pool_t *pool, const char *params, 145 apr_dbd_t **handle, 146 const char **error); 147 148/** apr_dbd_open: open a connection to a backend 149 * 150 * @param pool - working pool 151 * @param params - arguments to driver (implementation-dependent) 152 * @param handle - pointer to handle to return 153 * @param driver - driver struct. 154 * @return APR_SUCCESS for success 155 * @return APR_EGENERAL if driver exists but connection failed 156 * @see apr_dbd_open_ex 157 */ 158APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver, 159 apr_pool_t *pool, const char *params, 160 apr_dbd_t **handle); 161 162/** apr_dbd_close: close a connection to a backend 163 * 164 * @param handle - handle to close 165 * @param driver - driver struct. 166 * @return APR_SUCCESS for success or error status 167 */ 168APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver, 169 apr_dbd_t *handle); 170 171/* apr-function-shaped versions of things */ 172 173/** apr_dbd_name: get the name of the driver 174 * 175 * @param driver - the driver 176 * @return - name 177 */ 178APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver); 179 180/** apr_dbd_native_handle: get native database handle of the underlying db 181 * 182 * @param driver - the driver 183 * @param handle - apr_dbd handle 184 * @return - native handle 185 */ 186APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver, 187 apr_dbd_t *handle); 188 189/** check_conn: check status of a database connection 190 * 191 * @param driver - the driver 192 * @param pool - working pool 193 * @param handle - the connection to check 194 * @return APR_SUCCESS or error 195 */ 196APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool, 197 apr_dbd_t *handle); 198 199/** apr_dbd_set_dbname: select database name. May be a no-op if not supported. 200 * 201 * @param driver - the driver 202 * @param pool - working pool 203 * @param handle - the connection 204 * @param name - the database to select 205 * @return 0 for success or error code 206 */ 207APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool, 208 apr_dbd_t *handle, const char *name); 209 210/** apr_dbd_transaction_start: start a transaction. May be a no-op. 211 * 212 * @param driver - the driver 213 * @param pool - a pool to use for error messages (if any). 214 * @param handle - the db connection 215 * @param trans - ptr to a transaction. May be null on entry 216 * @return 0 for success or error code 217 * @remarks Note that transaction modes, set by calling 218 * apr_dbd_transaction_mode_set(), will affect all query/select calls within 219 * a transaction. By default, any error in query/select during a transaction 220 * will cause the transaction to inherit the error code and any further 221 * query/select calls will fail immediately. Put transaction in "ignore 222 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback. 223 */ 224APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver, 225 apr_pool_t *pool, 226 apr_dbd_t *handle, 227 apr_dbd_transaction_t **trans); 228 229/** apr_dbd_transaction_end: end a transaction 230 * (commit on success, rollback on error). 231 * May be a no-op. 232 * 233 * @param driver - the driver 234 * @param handle - the db connection 235 * @param trans - the transaction. 236 * @return 0 for success or error code 237 */ 238APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver, 239 apr_pool_t *pool, 240 apr_dbd_transaction_t *trans); 241 242#define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */ 243#define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */ 244#define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */ 245 246/** apr_dbd_transaction_mode_get: get the mode of transaction 247 * 248 * @param driver - the driver 249 * @param trans - the transaction 250 * @return mode of transaction 251 */ 252APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver, 253 apr_dbd_transaction_t *trans); 254 255/** apr_dbd_transaction_mode_set: set the mode of transaction 256 * 257 * @param driver - the driver 258 * @param trans - the transaction 259 * @param mode - new mode of the transaction 260 * @return the mode of transaction in force after the call 261 */ 262APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver, 263 apr_dbd_transaction_t *trans, 264 int mode); 265 266/** apr_dbd_query: execute an SQL query that doesn't return a result set 267 * 268 * @param driver - the driver 269 * @param handle - the connection 270 * @param nrows - number of rows affected. 271 * @param statement - the SQL statement to execute 272 * @return 0 for success or error code 273 */ 274APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle, 275 int *nrows, const char *statement); 276 277/** apr_dbd_select: execute an SQL query that returns a result set 278 * 279 * @param driver - the driver 280 * @param pool - pool to allocate the result set 281 * @param handle - the connection 282 * @param res - pointer to result set pointer. May point to NULL on entry 283 * @param statement - the SQL statement to execute 284 * @param random - 1 to support random access to results (seek any row); 285 * 0 to support only looping through results in order 286 * (async access - faster) 287 * @return 0 for success or error code 288 */ 289APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool, 290 apr_dbd_t *handle, apr_dbd_results_t **res, 291 const char *statement, int random); 292 293/** apr_dbd_num_cols: get the number of columns in a results set 294 * 295 * @param driver - the driver 296 * @param res - result set. 297 * @return number of columns 298 */ 299APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver, 300 apr_dbd_results_t *res); 301 302/** apr_dbd_num_tuples: get the number of rows in a results set 303 * of a synchronous select 304 * 305 * @param driver - the driver 306 * @param res - result set. 307 * @return number of rows, or -1 if the results are asynchronous 308 */ 309APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver, 310 apr_dbd_results_t *res); 311 312/** apr_dbd_get_row: get a row from a result set 313 * 314 * @param driver - the driver 315 * @param pool - pool to allocate the row 316 * @param res - result set pointer 317 * @param row - pointer to row pointer. May point to NULL on entry 318 * @param rownum - row number (counting from 1), or -1 for "next row". 319 * Ignored if random access is not supported. 320 * @return 0 for success, -1 for rownum out of range or data finished 321 */ 322APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool, 323 apr_dbd_results_t *res, apr_dbd_row_t **row, 324 int rownum); 325 326/** apr_dbd_get_entry: get an entry from a row 327 * 328 * @param driver - the driver 329 * @param row - row pointer 330 * @param col - entry number 331 * @return value from the row, or NULL if col is out of bounds. 332 */ 333APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver, 334 apr_dbd_row_t *row, int col); 335 336/** apr_dbd_get_name: get an entry name from a result set 337 * 338 * @param driver - the driver 339 * @param res - result set pointer 340 * @param col - entry number 341 * @return name of the entry, or NULL if col is out of bounds. 342 */ 343APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver, 344 apr_dbd_results_t *res, int col); 345 346 347/** apr_dbd_error: get current error message (if any) 348 * 349 * @param driver - the driver 350 * @param handle - the connection 351 * @param errnum - error code from operation that returned an error 352 * @return the database current error message, or message for errnum 353 * (implementation-dependent whether errnum is ignored) 354 */ 355APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver, 356 apr_dbd_t *handle, int errnum); 357 358/** apr_dbd_escape: escape a string so it is safe for use in query/select 359 * 360 * @param driver - the driver 361 * @param pool - pool to alloc the result from 362 * @param string - the string to escape 363 * @param handle - the connection 364 * @return the escaped, safe string 365 */ 366APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver, 367 apr_pool_t *pool, const char *string, 368 apr_dbd_t *handle); 369 370/** apr_dbd_prepare: prepare a statement 371 * 372 * @param driver - the driver 373 * @param pool - pool to alloc the result from 374 * @param handle - the connection 375 * @param query - the SQL query 376 * @param label - A label for the prepared statement. 377 * use NULL for temporary prepared statements 378 * (eg within a Request in httpd) 379 * @param statement - statement to prepare. May point to null on entry. 380 * @return 0 for success or error code 381 * @remarks To specify parameters of the prepared query, use \%s, \%d etc. 382 * (see below for full list) in place of database specific parameter syntax 383 * (e.g. for PostgreSQL, this would be $1, $2, for SQLite3 this would be ? 384 * etc.). For instance: "SELECT name FROM customers WHERE name=%s" would be 385 * a query that this function understands. 386 * @remarks Here is the full list of format specifiers that this function 387 * understands and what they map to in SQL: \%hhd (TINY INT), \%hhu (UNSIGNED 388 * TINY INT), \%hd (SHORT), \%hu (UNSIGNED SHORT), \%d (INT), \%u (UNSIGNED 389 * INT), \%ld (LONG), \%lu (UNSIGNED LONG), \%lld (LONG LONG), \%llu 390 * (UNSIGNED LONG LONG), \%f (FLOAT, REAL), \%lf (DOUBLE PRECISION), \%s 391 * (VARCHAR), \%pDt (TEXT), \%pDi (TIME), \%pDd (DATE), \%pDa (DATETIME), 392 * \%pDs (TIMESTAMP), \%pDz (TIMESTAMP WITH TIME ZONE), \%pDb (BLOB), \%pDc 393 * (CLOB) and \%pDn (NULL). Not all databases have support for all these 394 * types, so the underlying driver will attempt the "best match" where 395 * possible. A \% followed by any letter not in the above list will be 396 * interpreted as VARCHAR (i.e. \%s). 397 */ 398APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool, 399 apr_dbd_t *handle, const char *query, 400 const char *label, 401 apr_dbd_prepared_t **statement); 402 403 404/** apr_dbd_pquery: query using a prepared statement + args 405 * 406 * @param driver - the driver 407 * @param pool - working pool 408 * @param handle - the connection 409 * @param nrows - number of rows affected. 410 * @param statement - the prepared statement to execute 411 * @param nargs - ignored (for backward compatibility only) 412 * @param args - args to prepared statement 413 * @return 0 for success or error code 414 */ 415APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool, 416 apr_dbd_t *handle, int *nrows, 417 apr_dbd_prepared_t *statement, int nargs, 418 const char **args); 419 420/** apr_dbd_pselect: select using a prepared statement + args 421 * 422 * @param driver - the driver 423 * @param pool - working pool 424 * @param handle - the connection 425 * @param res - pointer to query results. May point to NULL on entry 426 * @param statement - the prepared statement to execute 427 * @param random - Whether to support random-access to results 428 * @param nargs - ignored (for backward compatibility only) 429 * @param args - args to prepared statement 430 * @return 0 for success or error code 431 */ 432APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool, 433 apr_dbd_t *handle, apr_dbd_results_t **res, 434 apr_dbd_prepared_t *statement, int random, 435 int nargs, const char **args); 436 437/** apr_dbd_pvquery: query using a prepared statement + args 438 * 439 * @param driver - the driver 440 * @param pool - working pool 441 * @param handle - the connection 442 * @param nrows - number of rows affected. 443 * @param statement - the prepared statement to execute 444 * @param ... - varargs list 445 * @return 0 for success or error code 446 */ 447APU_DECLARE_NONSTD(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, 448 apr_pool_t *pool, 449 apr_dbd_t *handle, int *nrows, 450 apr_dbd_prepared_t *statement, ...); 451 452/** apr_dbd_pvselect: select using a prepared statement + args 453 * 454 * @param driver - the driver 455 * @param pool - working pool 456 * @param handle - the connection 457 * @param res - pointer to query results. May point to NULL on entry 458 * @param statement - the prepared statement to execute 459 * @param random - Whether to support random-access to results 460 * @param ... - varargs list 461 * @return 0 for success or error code 462 */ 463APU_DECLARE_NONSTD(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, 464 apr_pool_t *pool, apr_dbd_t *handle, 465 apr_dbd_results_t **res, 466 apr_dbd_prepared_t *statement, 467 int random, ...); 468 469/** apr_dbd_pbquery: query using a prepared statement + binary args 470 * 471 * @param driver - the driver 472 * @param pool - working pool 473 * @param handle - the connection 474 * @param nrows - number of rows affected. 475 * @param statement - the prepared statement to execute 476 * @param args - binary args to prepared statement 477 * @return 0 for success or error code 478 */ 479APU_DECLARE(int) apr_dbd_pbquery(const apr_dbd_driver_t *driver, 480 apr_pool_t *pool, apr_dbd_t *handle, 481 int *nrows, apr_dbd_prepared_t *statement, 482 const void **args); 483 484/** apr_dbd_pbselect: select using a prepared statement + binary args 485 * 486 * @param driver - the driver 487 * @param pool - working pool 488 * @param handle - the connection 489 * @param res - pointer to query results. May point to NULL on entry 490 * @param statement - the prepared statement to execute 491 * @param random - Whether to support random-access to results 492 * @param args - binary args to prepared statement 493 * @return 0 for success or error code 494 */ 495APU_DECLARE(int) apr_dbd_pbselect(const apr_dbd_driver_t *driver, 496 apr_pool_t *pool, 497 apr_dbd_t *handle, apr_dbd_results_t **res, 498 apr_dbd_prepared_t *statement, int random, 499 const void **args); 500 501/** apr_dbd_pvbquery: query using a prepared statement + binary args 502 * 503 * @param driver - the driver 504 * @param pool - working pool 505 * @param handle - the connection 506 * @param nrows - number of rows affected. 507 * @param statement - the prepared statement to execute 508 * @param ... - varargs list of binary args 509 * @return 0 for success or error code 510 */ 511APU_DECLARE_NONSTD(int) apr_dbd_pvbquery(const apr_dbd_driver_t *driver, 512 apr_pool_t *pool, 513 apr_dbd_t *handle, int *nrows, 514 apr_dbd_prepared_t *statement, ...); 515 516/** apr_dbd_pvbselect: select using a prepared statement + binary args 517 * 518 * @param driver - the driver 519 * @param pool - working pool 520 * @param handle - the connection 521 * @param res - pointer to query results. May point to NULL on entry 522 * @param statement - the prepared statement to execute 523 * @param random - Whether to support random-access to results 524 * @param ... - varargs list of binary args 525 * @return 0 for success or error code 526 */ 527APU_DECLARE_NONSTD(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver, 528 apr_pool_t *pool, apr_dbd_t *handle, 529 apr_dbd_results_t **res, 530 apr_dbd_prepared_t *statement, 531 int random, ...); 532 533/** apr_dbd_datum_get: get a binary entry from a row 534 * 535 * @param driver - the driver 536 * @param row - row pointer 537 * @param col - entry number 538 * @param type - type of data to get 539 * @param data - pointer to data, allocated by the caller 540 * @return APR_SUCCESS on success, APR_ENOENT if data is NULL or APR_EGENERAL 541 */ 542APU_DECLARE(apr_status_t) apr_dbd_datum_get(const apr_dbd_driver_t *driver, 543 apr_dbd_row_t *row, int col, 544 apr_dbd_type_e type, void *data); 545 546/** @} */ 547 548#ifdef __cplusplus 549} 550#endif 551 552#endif 553