Introduce apr_dbd_open_ex()
[apr-util.git] / include / apr_dbd.h
blob5ec7f63f873a2c5db5626ab92a0e6cdf7a5a6b9b
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* Overview of what this is and does:
18 * http://www.apache.org/~niq/dbd.html
21 #ifndef APR_DBD_H
22 #define APR_DBD_H
24 #include "apu.h"
25 #include "apr_pools.h"
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
31 /**
32 * @file apr_dbd.h
33 * @brief APR-UTIL DBD library
35 /**
36 * @defgroup APR_Util_DBD DBD routines
37 * @ingroup APR_Util
38 * @{
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.
55 typedef 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;
81 /* These are opaque structs. Instantiation is up to each backend */
82 typedef struct apr_dbd_driver_t apr_dbd_driver_t;
83 typedef struct apr_dbd_t apr_dbd_t;
84 typedef struct apr_dbd_transaction_t apr_dbd_transaction_t;
85 typedef struct apr_dbd_results_t apr_dbd_results_t;
86 typedef struct apr_dbd_row_t apr_dbd_row_t;
87 typedef struct apr_dbd_prepared_t apr_dbd_prepared_t;
89 /** apr_dbd_init: perform once-only initialisation. Call once only.
91 * @param pool - pool to register any shutdown cleanups, etc
93 APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool);
95 /** apr_dbd_get_driver: get the driver struct for a name
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
105 APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name,
106 const apr_dbd_driver_t **driver);
108 /** apr_dbd_open_ex: open a connection to a backend
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" and "group" keys, each followed by an
130 * equal sign and a value. Such key/value pairs can be delimited by space,
131 * CR, LF, tab, semicolon, vertical bar or comma. For now, "flags" can only
132 * recognise CLIENT_FOUND_ROWS (check MySQL manual for details). The value
133 * associated with "fldsz" determines maximum amount of memory (in bytes) for
134 * each of the fields in the result set of prepared statements. By default,
135 * this value is 1 MB. The value associated with "group" determines which
136 * group from configuration file to use (see MYSQL_READ_DEFAULT_GROUP option
137 * of mysql_options() in MySQL manual).
139 APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver,
140 apr_pool_t *pool, const char *params,
141 apr_dbd_t **handle,
142 const char **error);
144 /** apr_dbd_open: open a connection to a backend
146 * @param pool - working pool
147 * @param params - arguments to driver (implementation-dependent)
148 * @param handle - pointer to handle to return
149 * @param driver - driver struct.
150 * @return APR_SUCCESS for success
151 * @return APR_EGENERAL if driver exists but connection failed
152 * @see apr_dbd_open_ex
154 APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
155 apr_pool_t *pool, const char *params,
156 apr_dbd_t **handle);
158 /** apr_dbd_close: close a connection to a backend
160 * @param handle - handle to close
161 * @param driver - driver struct.
162 * @return APR_SUCCESS for success or error status
164 APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
165 apr_dbd_t *handle);
167 /* apr-function-shaped versions of things */
169 /** apr_dbd_name: get the name of the driver
171 * @param driver - the driver
172 * @return - name
174 APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver);
176 /** apr_dbd_native_handle: get native database handle of the underlying db
178 * @param driver - the driver
179 * @param handle - apr_dbd handle
180 * @return - native handle
182 APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
183 apr_dbd_t *handle);
185 /** check_conn: check status of a database connection
187 * @param driver - the driver
188 * @param pool - working pool
189 * @param handle - the connection to check
190 * @return APR_SUCCESS or error
192 APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool,
193 apr_dbd_t *handle);
195 /** apr_dbd_set_dbname: select database name. May be a no-op if not supported.
197 * @param driver - the driver
198 * @param pool - working pool
199 * @param handle - the connection
200 * @param name - the database to select
201 * @return 0 for success or error code
203 APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool,
204 apr_dbd_t *handle, const char *name);
206 /** apr_dbd_transaction_start: start a transaction. May be a no-op.
208 * @param driver - the driver
209 * @param pool - a pool to use for error messages (if any).
210 * @param handle - the db connection
211 * @param trans - ptr to a transaction. May be null on entry
212 * @return 0 for success or error code
213 * @remarks Note that transaction modes, set by calling
214 * apr_dbd_transaction_mode_set(), will affect all query/select calls within
215 * a transaction. By default, any error in query/select during a transaction
216 * will cause the transaction to inherit the error code and any further
217 * query/select calls will fail immediately. Put transaction in "ignore
218 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback.
220 APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
221 apr_pool_t *pool,
222 apr_dbd_t *handle,
223 apr_dbd_transaction_t **trans);
225 /** apr_dbd_transaction_end: end a transaction
226 * (commit on success, rollback on error).
227 * May be a no-op.
229 * @param driver - the driver
230 * @param handle - the db connection
231 * @param trans - the transaction.
232 * @return 0 for success or error code
234 APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
235 apr_pool_t *pool,
236 apr_dbd_transaction_t *trans);
238 #define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */
239 #define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */
240 #define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */
242 /** apr_dbd_transaction_mode_get: get the mode of transaction
244 * @param driver - the driver
245 * @param trans - the transaction
246 * @return mode of transaction
248 APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver,
249 apr_dbd_transaction_t *trans);
251 /** apr_dbd_transaction_mode_set: set the mode of transaction
253 * @param driver - the driver
254 * @param trans - the transaction
255 * @param mode - new mode of the transaction
256 * @return the mode of transaction in force after the call
258 APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver,
259 apr_dbd_transaction_t *trans,
260 int mode);
262 /** apr_dbd_query: execute an SQL query that doesn't return a result set
264 * @param driver - the driver
265 * @param handle - the connection
266 * @param nrows - number of rows affected.
267 * @param statement - the SQL statement to execute
268 * @return 0 for success or error code
270 APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle,
271 int *nrows, const char *statement);
273 /** apr_dbd_select: execute an SQL query that returns a result set
275 * @param driver - the driver
276 * @param pool - pool to allocate the result set
277 * @param handle - the connection
278 * @param res - pointer to result set pointer. May point to NULL on entry
279 * @param statement - the SQL statement to execute
280 * @param random - 1 to support random access to results (seek any row);
281 * 0 to support only looping through results in order
282 * (async access - faster)
283 * @return 0 for success or error code
285 APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool,
286 apr_dbd_t *handle, apr_dbd_results_t **res,
287 const char *statement, int random);
289 /** apr_dbd_num_cols: get the number of columns in a results set
291 * @param driver - the driver
292 * @param res - result set.
293 * @return number of columns
295 APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
296 apr_dbd_results_t *res);
298 /** apr_dbd_num_tuples: get the number of rows in a results set
299 * of a synchronous select
301 * @param driver - the driver
302 * @param res - result set.
303 * @return number of rows, or -1 if the results are asynchronous
305 APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
306 apr_dbd_results_t *res);
308 /** apr_dbd_get_row: get a row from a result set
310 * @param driver - the driver
311 * @param pool - pool to allocate the row
312 * @param res - result set pointer
313 * @param row - pointer to row pointer. May point to NULL on entry
314 * @param rownum - row number, or -1 for "next row". Ignored if random
315 * access is not supported.
316 * @return 0 for success, -1 for rownum out of range or data finished
318 APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
319 apr_dbd_results_t *res, apr_dbd_row_t **row,
320 int rownum);
322 /** apr_dbd_get_entry: get an entry from a row
324 * @param driver - the driver
325 * @param row - row pointer
326 * @param col - entry number
327 * @return value from the row, or NULL if col is out of bounds.
329 APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
330 apr_dbd_row_t *row, int col);
332 /** apr_dbd_get_name: get an entry name from a result set
334 * @param driver - the driver
335 * @param res - result set pointer
336 * @param col - entry number
337 * @return name of the entry, or NULL if col is out of bounds.
339 APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver,
340 apr_dbd_results_t *res, int col);
343 /** apr_dbd_error: get current error message (if any)
345 * @param driver - the driver
346 * @param handle - the connection
347 * @param errnum - error code from operation that returned an error
348 * @return the database current error message, or message for errnum
349 * (implementation-dependent whether errnum is ignored)
351 APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
352 apr_dbd_t *handle, int errnum);
354 /** apr_dbd_escape: escape a string so it is safe for use in query/select
356 * @param driver - the driver
357 * @param pool - pool to alloc the result from
358 * @param string - the string to escape
359 * @param handle - the connection
360 * @return the escaped, safe string
362 APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
363 apr_pool_t *pool, const char *string,
364 apr_dbd_t *handle);
366 /** apr_dbd_prepare: prepare a statement
368 * @param driver - the driver
369 * @param pool - pool to alloc the result from
370 * @param handle - the connection
371 * @param query - the SQL query
372 * @param label - A label for the prepared statement.
373 * use NULL for temporary prepared statements
374 * (eg within a Request in httpd)
375 * @param statement - statement to prepare. May point to null on entry.
376 * @return 0 for success or error code
377 * @remarks To specify parameters of the prepared query, use \%s, \%d etc.
378 * (see below for full list) in place of database specific parameter syntax
379 * (e.g. for PostgreSQL, this would be $1, $2, for SQLite3 this would be ?
380 * etc.). For instance: "SELECT name FROM customers WHERE name=%s" would be
381 * a query that this function understands.
382 * @remarks Here is the full list of format specifiers that this function
383 * understands and what they map to in SQL: \%hhd (TINY INT), \%hhu (UNSIGNED
384 * TINY INT), \%hd (SHORT), \%hu (UNSIGNED SHORT), \%d (INT), \%u (UNSIGNED
385 * INT), \%ld (LONG), \%lu (UNSIGNED LONG), \%lld (LONG LONG), \%llu
386 * (UNSIGNED LONG LONG), \%f (FLOAT, REAL), \%lf (DOUBLE PRECISION), \%s
387 * (VARCHAR), \%pDt (TEXT), \%pDi (TIME), \%pDd (DATE), \%pDa (DATETIME),
388 * \%pDs (TIMESTAMP), \%pDz (TIMESTAMP WITH TIME ZONE), \%pDb (BLOB), \%pDc
389 * (CLOB) and \%pDn (NULL). Not all databases have support for all these
390 * types, so the underlying driver will attempt the "best match" where
391 * possible. A \% followed by any letter not in the above list will be
392 * interpreted as VARCHAR (i.e. \%s).
394 APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool,
395 apr_dbd_t *handle, const char *query,
396 const char *label,
397 apr_dbd_prepared_t **statement);
400 /** apr_dbd_pquery: query using a prepared statement + args
402 * @param driver - the driver
403 * @param pool - working pool
404 * @param handle - the connection
405 * @param nrows - number of rows affected.
406 * @param statement - the prepared statement to execute
407 * @param nargs - ignored (for backward compatibility only)
408 * @param args - args to prepared statement
409 * @return 0 for success or error code
411 APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
412 apr_dbd_t *handle, int *nrows,
413 apr_dbd_prepared_t *statement, int nargs,
414 const char **args);
416 /** apr_dbd_pselect: select using a prepared statement + args
418 * @param driver - the driver
419 * @param pool - working pool
420 * @param handle - the connection
421 * @param res - pointer to query results. May point to NULL on entry
422 * @param statement - the prepared statement to execute
423 * @param random - Whether to support random-access to results
424 * @param nargs - ignored (for backward compatibility only)
425 * @param args - args to prepared statement
426 * @return 0 for success or error code
428 APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
429 apr_dbd_t *handle, apr_dbd_results_t **res,
430 apr_dbd_prepared_t *statement, int random,
431 int nargs, const char **args);
433 /** apr_dbd_pvquery: query using a prepared statement + args
435 * @param driver - the driver
436 * @param pool - working pool
437 * @param handle - the connection
438 * @param nrows - number of rows affected.
439 * @param statement - the prepared statement to execute
440 * @param ... - varargs list
441 * @return 0 for success or error code
443 APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
444 apr_dbd_t *handle, int *nrows,
445 apr_dbd_prepared_t *statement, ...);
447 /** apr_dbd_pvselect: select using a prepared statement + args
449 * @param driver - the driver
450 * @param pool - working pool
451 * @param handle - the connection
452 * @param res - pointer to query results. May point to NULL on entry
453 * @param statement - the prepared statement to execute
454 * @param random - Whether to support random-access to results
455 * @param ... - varargs list
456 * @return 0 for success or error code
458 APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
459 apr_dbd_t *handle, apr_dbd_results_t **res,
460 apr_dbd_prepared_t *statement, int random,
461 ...);
463 /** apr_dbd_pbquery: query using a prepared statement + binary args
465 * @param driver - the driver
466 * @param pool - working pool
467 * @param handle - the connection
468 * @param nrows - number of rows affected.
469 * @param statement - the prepared statement to execute
470 * @param args - binary args to prepared statement
471 * @return 0 for success or error code
473 APU_DECLARE(int) apr_dbd_pbquery(const apr_dbd_driver_t *driver,
474 apr_pool_t *pool, apr_dbd_t *handle,
475 int *nrows, apr_dbd_prepared_t *statement,
476 const void **args);
478 /** apr_dbd_pbselect: select using a prepared statement + binary args
480 * @param driver - the driver
481 * @param pool - working pool
482 * @param handle - the connection
483 * @param res - pointer to query results. May point to NULL on entry
484 * @param statement - the prepared statement to execute
485 * @param random - Whether to support random-access to results
486 * @param args - binary args to prepared statement
487 * @return 0 for success or error code
489 APU_DECLARE(int) apr_dbd_pbselect(const apr_dbd_driver_t *driver,
490 apr_pool_t *pool,
491 apr_dbd_t *handle, apr_dbd_results_t **res,
492 apr_dbd_prepared_t *statement, int random,
493 const void **args);
495 /** apr_dbd_pvbquery: query using a prepared statement + binary args
497 * @param driver - the driver
498 * @param pool - working pool
499 * @param handle - the connection
500 * @param nrows - number of rows affected.
501 * @param statement - the prepared statement to execute
502 * @param ... - varargs list of binary args
503 * @return 0 for success or error code
505 APU_DECLARE(int) apr_dbd_pvbquery(const apr_dbd_driver_t *driver,
506 apr_pool_t *pool,
507 apr_dbd_t *handle, int *nrows,
508 apr_dbd_prepared_t *statement, ...);
510 /** apr_dbd_pvbselect: select using a prepared statement + binary args
512 * @param driver - the driver
513 * @param pool - working pool
514 * @param handle - the connection
515 * @param res - pointer to query results. May point to NULL on entry
516 * @param statement - the prepared statement to execute
517 * @param random - Whether to support random-access to results
518 * @param ... - varargs list of binary args
519 * @return 0 for success or error code
521 APU_DECLARE(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver,
522 apr_pool_t *pool,
523 apr_dbd_t *handle, apr_dbd_results_t **res,
524 apr_dbd_prepared_t *statement, int random,
525 ...);
527 /** apr_dbd_datum_get: get a binary entry from a row
529 * @param driver - the driver
530 * @param row - row pointer
531 * @param col - entry number
532 * @param type - type of data to get
533 * @param data - pointer to data, allocated by the caller
534 * @return APR_SUCCESS on success, APR_ENOENT if data is NULL or APR_EGENERAL
536 APU_DECLARE(apr_status_t) apr_dbd_datum_get(const apr_dbd_driver_t *driver,
537 apr_dbd_row_t *row, int col,
538 apr_dbd_type_e type, void *data);
540 /** @} */
542 #ifdef __cplusplus
544 #endif
546 #endif