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 driver - driver struct. 111 * @param pool - working pool 112 * @param params - arguments to driver (implementation-dependent) 113 * @param handle - pointer to handle to return 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 */ 140APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver, 141 apr_pool_t *pool, const char *params, 142 apr_dbd_t **handle, 143 const char **error); 144 145/** apr_dbd_open: open a connection to a backend 146 * 147 * @param driver - driver struct. 148 * @param pool - working pool 149 * @param params - arguments to driver (implementation-dependent) 150 * @param handle - pointer to handle to return 151 * @return APR_SUCCESS for success 152 * @return APR_EGENERAL if driver exists but connection failed 153 * @see apr_dbd_open_ex 154 */ 155APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver, 156 apr_pool_t *pool, const char *params, 157 apr_dbd_t **handle); 158 159/** apr_dbd_close: close a connection to a backend 160 * 161 * @param driver - driver struct. 162 * @param handle - handle to close 163 * @return APR_SUCCESS for success or error status 164 */ 165APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver, 166 apr_dbd_t *handle); 167 168/* apr-function-shaped versions of things */ 169 170/** apr_dbd_name: get the name of the driver 171 * 172 * @param driver - the driver 173 * @return - name 174 */ 175APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver); 176 177/** apr_dbd_native_handle: get native database handle of the underlying db 178 * 179 * @param driver - the driver 180 * @param handle - apr_dbd handle 181 * @return - native handle 182 */ 183APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver, 184 apr_dbd_t *handle); 185 186/** check_conn: check status of a database connection 187 * 188 * @param driver - the driver 189 * @param pool - working pool 190 * @param handle - the connection to check 191 * @return APR_SUCCESS or error 192 */ 193APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool, 194 apr_dbd_t *handle); 195 196/** apr_dbd_set_dbname: select database name. May be a no-op if not supported. 197 * 198 * @param driver - the driver 199 * @param pool - working pool 200 * @param handle - the connection 201 * @param name - the database to select 202 * @return 0 for success or error code 203 */ 204APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool, 205 apr_dbd_t *handle, const char *name); 206 207/** apr_dbd_transaction_start: start a transaction. May be a no-op. 208 * 209 * @param driver - the driver 210 * @param pool - a pool to use for error messages (if any). 211 * @param handle - the db connection 212 * @param trans - ptr to a transaction. May be null on entry 213 * @return 0 for success or error code 214 * @remarks Note that transaction modes, set by calling 215 * apr_dbd_transaction_mode_set(), will affect all query/select calls within 216 * a transaction. By default, any error in query/select during a transaction 217 * will cause the transaction to inherit the error code and any further 218 * query/select calls will fail immediately. Put transaction in "ignore 219 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback. 220 */ 221APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver, 222 apr_pool_t *pool, 223 apr_dbd_t *handle, 224 apr_dbd_transaction_t **trans); 225 226/** apr_dbd_transaction_end: end a transaction 227 * (commit on success, rollback on error). 228 * May be a no-op. 229 * 230 * @param driver - the driver 231 * @param handle - the db connection 232 * @param trans - the transaction. 233 * @return 0 for success or error code 234 */ 235APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver, 236 apr_pool_t *pool, 237 apr_dbd_transaction_t *trans); 238 239#define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */ 240#define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */ 241#define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */ 242 243/** apr_dbd_transaction_mode_get: get the mode of transaction 244 * 245 * @param driver - the driver 246 * @param trans - the transaction 247 * @return mode of transaction 248 */ 249APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver, 250 apr_dbd_transaction_t *trans); 251 252/** apr_dbd_transaction_mode_set: set the mode of transaction 253 * 254 * @param driver - the driver 255 * @param trans - the transaction 256 * @param mode - new mode of the transaction 257 * @return the mode of transaction in force after the call 258 */ 259APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver, 260 apr_dbd_transaction_t *trans, 261 int mode); 262 263/** apr_dbd_query: execute an SQL query that doesn't return a result set 264 * 265 * @param driver - the driver 266 * @param handle - the connection 267 * @param nrows - number of rows affected. 268 * @param statement - the SQL statement to execute 269 * @return 0 for success or error code 270 */ 271APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle, 272 int *nrows, const char *statement); 273 274/** apr_dbd_select: execute an SQL query that returns a result set 275 * 276 * @param driver - the driver 277 * @param pool - pool to allocate the result set 278 * @param handle - the connection 279 * @param res - pointer to result set pointer. May point to NULL on entry 280 * @param statement - the SQL statement to execute 281 * @param random - 1 to support random access to results (seek any row); 282 * 0 to support only looping through results in order 283 * (async access - faster) 284 * @return 0 for success or error code 285 */ 286APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool, 287 apr_dbd_t *handle, apr_dbd_results_t **res, 288 const char *statement, int random); 289 290/** apr_dbd_num_cols: get the number of columns in a results set 291 * 292 * @param driver - the driver 293 * @param res - result set. 294 * @return number of columns 295 */ 296APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver, 297 apr_dbd_results_t *res); 298 299/** apr_dbd_num_tuples: get the number of rows in a results set 300 * of a synchronous select 301 * 302 * @param driver - the driver 303 * @param res - result set. 304 * @return number of rows, or -1 if the results are asynchronous 305 */ 306APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver, 307 apr_dbd_results_t *res); 308 309/** apr_dbd_get_row: get a row from a result set 310 * 311 * @param driver - the driver 312 * @param pool - pool to allocate the row 313 * @param res - result set pointer 314 * @param row - pointer to row pointer. May point to NULL on entry 315 * @param rownum - row number (counting from 1), or -1 for "next row". 316 * Ignored if random access is not supported. 317 * @return 0 for success, -1 for rownum out of range or data finished 318 */ 319APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool, 320 apr_dbd_results_t *res, apr_dbd_row_t **row, 321 int rownum); 322 323/** apr_dbd_get_entry: get an entry from a row 324 * 325 * @param driver - the driver 326 * @param row - row pointer 327 * @param col - entry number 328 * @return value from the row, or NULL if col is out of bounds. 329 */ 330APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver, 331 apr_dbd_row_t *row, int col); 332 333/** apr_dbd_get_name: get an entry name from a result set 334 * 335 * @param driver - the driver 336 * @param res - result set pointer 337 * @param col - entry number 338 * @return name of the entry, or NULL if col is out of bounds. 339 */ 340APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver, 341 apr_dbd_results_t *res, int col); 342 343 344/** apr_dbd_error: get current error message (if any) 345 * 346 * @param driver - the driver 347 * @param handle - the connection 348 * @param errnum - error code from operation that returned an error 349 * @return the database current error message, or message for errnum 350 * (implementation-dependent whether errnum is ignored) 351 */ 352APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver, 353 apr_dbd_t *handle, int errnum); 354 355/** apr_dbd_escape: escape a string so it is safe for use in query/select 356 * 357 * @param driver - the driver 358 * @param pool - pool to alloc the result from 359 * @param string - the string to escape 360 * @param handle - the connection 361 * @return the escaped, safe string 362 */ 363APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver, 364 apr_pool_t *pool, const char *string, 365 apr_dbd_t *handle); 366 367/** apr_dbd_prepare: prepare a statement 368 * 369 * @param driver - the driver 370 * @param pool - pool to alloc the result from 371 * @param handle - the connection 372 * @param query - the SQL query 373 * @param label - A label for the prepared statement. 374 * use NULL for temporary prepared statements 375 * (eg within a Request in httpd) 376 * @param statement - statement to prepare. May point to null on entry. 377 * @return 0 for success or error code 378 * @remarks To specify parameters of the prepared query, use \%s, \%d etc. 379 * (see below for full list) in place of database specific parameter syntax 380 * (e.g. for PostgreSQL, this would be $1, $2, for SQLite3 this would be ? 381 * etc.). For instance: "SELECT name FROM customers WHERE name=%s" would be 382 * a query that this function understands. 383 * @remarks Here is the full list of format specifiers that this function 384 * understands and what they map to in SQL: \%hhd (TINY INT), \%hhu (UNSIGNED 385 * TINY INT), \%hd (SHORT), \%hu (UNSIGNED SHORT), \%d (INT), \%u (UNSIGNED 386 * INT), \%ld (LONG), \%lu (UNSIGNED LONG), \%lld (LONG LONG), \%llu 387 * (UNSIGNED LONG LONG), \%f (FLOAT, REAL), \%lf (DOUBLE PRECISION), \%s 388 * (VARCHAR), \%pDt (TEXT), \%pDi (TIME), \%pDd (DATE), \%pDa (DATETIME), 389 * \%pDs (TIMESTAMP), \%pDz (TIMESTAMP WITH TIME ZONE), \%pDb (BLOB), \%pDc 390 * (CLOB) and \%pDn (NULL). Not all databases have support for all these 391 * types, so the underlying driver will attempt the "best match" where 392 * possible. A \% followed by any letter not in the above list will be 393 * interpreted as VARCHAR (i.e. \%s). 394 */ 395APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool, 396 apr_dbd_t *handle, const char *query, 397 const char *label, 398 apr_dbd_prepared_t **statement); 399 400 401/** apr_dbd_pquery: query using a prepared statement + args 402 * 403 * @param driver - the driver 404 * @param pool - working pool 405 * @param handle - the connection 406 * @param nrows - number of rows affected. 407 * @param statement - the prepared statement to execute 408 * @param nargs - ignored (for backward compatibility only) 409 * @param args - args to prepared statement 410 * @return 0 for success or error code 411 */ 412APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool, 413 apr_dbd_t *handle, int *nrows, 414 apr_dbd_prepared_t *statement, int nargs, 415 const char **args); 416 417/** apr_dbd_pselect: select using a prepared statement + args 418 * 419 * @param driver - the driver 420 * @param pool - working pool 421 * @param handle - the connection 422 * @param res - pointer to query results. May point to NULL on entry 423 * @param statement - the prepared statement to execute 424 * @param random - Whether to support random-access to results 425 * @param nargs - ignored (for backward compatibility only) 426 * @param args - args to prepared statement 427 * @return 0 for success or error code 428 */ 429APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool, 430 apr_dbd_t *handle, apr_dbd_results_t **res, 431 apr_dbd_prepared_t *statement, int random, 432 int nargs, const char **args); 433 434/** apr_dbd_pvquery: query using a prepared statement + args 435 * 436 * @param driver - the driver 437 * @param pool - working pool 438 * @param handle - the connection 439 * @param nrows - number of rows affected. 440 * @param statement - the prepared statement to execute 441 * @param ... - varargs list 442 * @return 0 for success or error code 443 */ 444APU_DECLARE_NONSTD(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, 445 apr_pool_t *pool, 446 apr_dbd_t *handle, int *nrows, 447 apr_dbd_prepared_t *statement, ...); 448 449/** apr_dbd_pvselect: select using a prepared statement + args 450 * 451 * @param driver - the driver 452 * @param pool - working pool 453 * @param handle - the connection 454 * @param res - pointer to query results. May point to NULL on entry 455 * @param statement - the prepared statement to execute 456 * @param random - Whether to support random-access to results 457 * @param ... - varargs list 458 * @return 0 for success or error code 459 */ 460APU_DECLARE_NONSTD(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, 461 apr_pool_t *pool, apr_dbd_t *handle, 462 apr_dbd_results_t **res, 463 apr_dbd_prepared_t *statement, 464 int random, ...); 465 466/** apr_dbd_pbquery: query using a prepared statement + binary args 467 * 468 * @param driver - the driver 469 * @param pool - working pool 470 * @param handle - the connection 471 * @param nrows - number of rows affected. 472 * @param statement - the prepared statement to execute 473 * @param args - binary args to prepared statement 474 * @return 0 for success or error code 475 */ 476APU_DECLARE(int) apr_dbd_pbquery(const apr_dbd_driver_t *driver, 477 apr_pool_t *pool, apr_dbd_t *handle, 478 int *nrows, apr_dbd_prepared_t *statement, 479 const void **args); 480 481/** apr_dbd_pbselect: select using a prepared statement + binary args 482 * 483 * @param driver - the driver 484 * @param pool - working pool 485 * @param handle - the connection 486 * @param res - pointer to query results. May point to NULL on entry 487 * @param statement - the prepared statement to execute 488 * @param random - Whether to support random-access to results 489 * @param args - binary args to prepared statement 490 * @return 0 for success or error code 491 */ 492APU_DECLARE(int) apr_dbd_pbselect(const apr_dbd_driver_t *driver, 493 apr_pool_t *pool, 494 apr_dbd_t *handle, apr_dbd_results_t **res, 495 apr_dbd_prepared_t *statement, int random, 496 const void **args); 497 498/** apr_dbd_pvbquery: query using a prepared statement + binary args 499 * 500 * @param driver - the driver 501 * @param pool - working pool 502 * @param handle - the connection 503 * @param nrows - number of rows affected. 504 * @param statement - the prepared statement to execute 505 * @param ... - varargs list of binary args 506 * @return 0 for success or error code 507 */ 508APU_DECLARE_NONSTD(int) apr_dbd_pvbquery(const apr_dbd_driver_t *driver, 509 apr_pool_t *pool, 510 apr_dbd_t *handle, int *nrows, 511 apr_dbd_prepared_t *statement, ...); 512 513/** apr_dbd_pvbselect: select using a prepared statement + binary args 514 * 515 * @param driver - the driver 516 * @param pool - working pool 517 * @param handle - the connection 518 * @param res - pointer to query results. May point to NULL on entry 519 * @param statement - the prepared statement to execute 520 * @param random - Whether to support random-access to results 521 * @param ... - varargs list of binary args 522 * @return 0 for success or error code 523 */ 524APU_DECLARE_NONSTD(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver, 525 apr_pool_t *pool, apr_dbd_t *handle, 526 apr_dbd_results_t **res, 527 apr_dbd_prepared_t *statement, 528 int random, ...); 529 530/** apr_dbd_datum_get: get a binary entry from a row 531 * 532 * @param driver - the driver 533 * @param row - row pointer 534 * @param col - entry number 535 * @param type - type of data to get 536 * @param data - pointer to data, allocated by the caller 537 * @return APR_SUCCESS on success, APR_ENOENT if data is NULL or APR_EGENERAL 538 */ 539APU_DECLARE(apr_status_t) apr_dbd_datum_get(const apr_dbd_driver_t *driver, 540 apr_dbd_row_t *row, int col, 541 apr_dbd_type_e type, void *data); 542 543/** @} */ 544 545#ifdef __cplusplus 546} 547#endif 548 549#endif 550