Unify parsing of prepared statements in DBD
[apr-util.git] / include / apr_dbd.h
blob96295151f83d7a4b8532e9c77d1e8409c7336790
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: 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 * @return APR_SUCCESS for success
115 * @return APR_EGENERAL if driver exists but connection failed
116 * @remarks PostgreSQL: the params is passed directly to the PQconnectdb()
117 * function (check PostgreSQL documentation for more details on the syntax).
118 * @remarks SQLite2: the params is split on a colon, with the first part used
119 * as the filename and second part converted to an integer and used as file
120 * mode.
121 * @remarks SQLite3: the params is passed directly to the sqlite3_open()
122 * function as a filename to be opened (check SQLite3 documentation for more
123 * details).
124 * @remarks Oracle: the params can have "user", "pass", "dbname" and "server"
125 * keys, each followed by an equal sign and a value. Such key/value pairs can
126 * be delimited by space, CR, LF, tab, semicolon, vertical bar or comma.
127 * @remarks MySQL: the params can have "host", "port", "user", "pass",
128 * "dbname", "sock", "flags" and "fldsz" keys, each followed by an equal sign
129 * and a value. Such key/value pairs can be delimited by space, CR, LF, tab,
130 * semicolon, vertical bar or comma. For now, "flags" can only recognise
131 * CLIENT_FOUND_ROWS (check MySQL manual for details). The value associated
132 * with "fldsz" determines maximum amount of memory (in bytes) for each of
133 * the fields in the result set of prepared statements. By default, this
134 * value is 1 MB.
136 APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
137 apr_pool_t *pool, const char *params,
138 apr_dbd_t **handle);
140 /** apr_dbd_close: close a connection to a backend
142 * @param handle - handle to close
143 * @param driver - driver struct.
144 * @return APR_SUCCESS for success or error status
146 APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
147 apr_dbd_t *handle);
149 /* apr-function-shaped versions of things */
151 /** apr_dbd_name: get the name of the driver
153 * @param driver - the driver
154 * @return - name
156 APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver);
158 /** apr_dbd_native_handle: get native database handle of the underlying db
160 * @param driver - the driver
161 * @param handle - apr_dbd handle
162 * @return - native handle
164 APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
165 apr_dbd_t *handle);
167 /** check_conn: check status of a database connection
169 * @param driver - the driver
170 * @param pool - working pool
171 * @param handle - the connection to check
172 * @return APR_SUCCESS or error
174 APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool,
175 apr_dbd_t *handle);
177 /** apr_dbd_set_dbname: select database name. May be a no-op if not supported.
179 * @param driver - the driver
180 * @param pool - working pool
181 * @param handle - the connection
182 * @param name - the database to select
183 * @return 0 for success or error code
185 APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool,
186 apr_dbd_t *handle, const char *name);
188 /** apr_dbd_transaction_start: start a transaction. May be a no-op.
190 * @param driver - the driver
191 * @param pool - a pool to use for error messages (if any).
192 * @param handle - the db connection
193 * @param trans - ptr to a transaction. May be null on entry
194 * @return 0 for success or error code
195 * @remarks Note that transaction modes, set by calling
196 * apr_dbd_transaction_mode_set(), will affect all query/select calls within
197 * a transaction. By default, any error in query/select during a transaction
198 * will cause the transaction to inherit the error code and any further
199 * query/select calls will fail immediately. Put transaction in "ignore
200 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback.
202 APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
203 apr_pool_t *pool,
204 apr_dbd_t *handle,
205 apr_dbd_transaction_t **trans);
207 /** apr_dbd_transaction_end: end a transaction
208 * (commit on success, rollback on error).
209 * May be a no-op.
211 * @param driver - the driver
212 * @param handle - the db connection
213 * @param trans - the transaction.
214 * @return 0 for success or error code
216 APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
217 apr_pool_t *pool,
218 apr_dbd_transaction_t *trans);
220 #define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */
221 #define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */
222 #define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */
224 /** apr_dbd_transaction_mode_get: get the mode of transaction
226 * @param driver - the driver
227 * @param trans - the transaction
228 * @return mode of transaction
230 APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver,
231 apr_dbd_transaction_t *trans);
233 /** apr_dbd_transaction_mode_set: set the mode of transaction
235 * @param driver - the driver
236 * @param trans - the transaction
237 * @param mode - new mode of the transaction
238 * @return the mode of transaction in force after the call
240 APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver,
241 apr_dbd_transaction_t *trans,
242 int mode);
244 /** apr_dbd_query: execute an SQL query that doesn't return a result set
246 * @param driver - the driver
247 * @param handle - the connection
248 * @param nrows - number of rows affected.
249 * @param statement - the SQL statement to execute
250 * @return 0 for success or error code
252 APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle,
253 int *nrows, const char *statement);
255 /** apr_dbd_select: execute an SQL query that returns a result set
257 * @param driver - the driver
258 * @param pool - pool to allocate the result set
259 * @param handle - the connection
260 * @param res - pointer to result set pointer. May point to NULL on entry
261 * @param statement - the SQL statement to execute
262 * @param random - 1 to support random access to results (seek any row);
263 * 0 to support only looping through results in order
264 * (async access - faster)
265 * @return 0 for success or error code
267 APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool,
268 apr_dbd_t *handle, apr_dbd_results_t **res,
269 const char *statement, int random);
271 /** apr_dbd_num_cols: get the number of columns in a results set
273 * @param driver - the driver
274 * @param res - result set.
275 * @return number of columns
277 APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
278 apr_dbd_results_t *res);
280 /** apr_dbd_num_tuples: get the number of rows in a results set
281 * of a synchronous select
283 * @param driver - the driver
284 * @param res - result set.
285 * @return number of rows, or -1 if the results are asynchronous
287 APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
288 apr_dbd_results_t *res);
290 /** apr_dbd_get_row: get a row from a result set
292 * @param driver - the driver
293 * @param pool - pool to allocate the row
294 * @param res - result set pointer
295 * @param row - pointer to row pointer. May point to NULL on entry
296 * @param rownum - row number, or -1 for "next row". Ignored if random
297 * access is not supported.
298 * @return 0 for success, -1 for rownum out of range or data finished
300 APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
301 apr_dbd_results_t *res, apr_dbd_row_t **row,
302 int rownum);
304 /** apr_dbd_get_entry: get an entry from a row
306 * @param driver - the driver
307 * @param row - row pointer
308 * @param col - entry number
309 * @return value from the row, or NULL if col is out of bounds.
311 APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
312 apr_dbd_row_t *row, int col);
314 /** apr_dbd_get_name: get an entry name from a result set
316 * @param driver - the driver
317 * @param res - result set pointer
318 * @param col - entry number
319 * @return name of the entry, or NULL if col is out of bounds.
321 APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver,
322 apr_dbd_results_t *res, int col);
325 /** apr_dbd_error: get current error message (if any)
327 * @param driver - the driver
328 * @param handle - the connection
329 * @param errnum - error code from operation that returned an error
330 * @return the database current error message, or message for errnum
331 * (implementation-dependent whether errnum is ignored)
333 APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
334 apr_dbd_t *handle, int errnum);
336 /** apr_dbd_escape: escape a string so it is safe for use in query/select
338 * @param driver - the driver
339 * @param pool - pool to alloc the result from
340 * @param string - the string to escape
341 * @param handle - the connection
342 * @return the escaped, safe string
344 APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
345 apr_pool_t *pool, const char *string,
346 apr_dbd_t *handle);
348 /** apr_dbd_prepare: prepare a statement
350 * @param driver - the driver
351 * @param pool - pool to alloc the result from
352 * @param handle - the connection
353 * @param query - the SQL query
354 * @param label - A label for the prepared statement.
355 * use NULL for temporary prepared statements
356 * (eg within a Request in httpd)
357 * @param statement - statement to prepare. May point to null on entry.
358 * @return 0 for success or error code
359 * @remarks To specify parameters of the prepared query, use \%s, \%d etc.
360 * (see below for full list) in place of database specific parameter syntax
361 * (e.g. for PostgreSQL, this would be $1, $2, for SQLite3 this would be ?
362 * etc.). For instance: "SELECT name FROM customers WHERE name=%s" would be
363 * a query that this function understands.
364 * @remarks Here is the full list of format specifiers that this function
365 * understands and what they map to in SQL: \%hhd (TINY INT), \%hhu (UNSIGNED
366 * TINY INT), \%hd (SHORT), \%hu (UNSIGNED SHORT), \%d (INT), \%u (UNSIGNED
367 * INT), \%ld (LONG), \%lu (UNSIGNED LONG), \%lld (LONG LONG), \%llu
368 * (UNSIGNED LONG LONG), \%f (FLOAT, REAL), \%lf (DOUBLE PRECISION), \%s
369 * (VARCHAR), \%pDt (TEXT), \%pDi (TIME), \%pDd (DATE), \%pDa (SQL:
370 * DATETIME), \%pDs (TIMESTAMP), \%pDz (TIMESTAMP WITH TIME ZONE), \%pDb
371 * (BLOB), \%pDc (CLOB) and \%pDn (NULL). Not all databases have support for
372 * all these types, so the underlying driver will attempt the "best match"
373 * where possible. A \% followed by any letter not in the above list will be
374 * interpreted as VARCHAR (i.e. \%s).
376 APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool,
377 apr_dbd_t *handle, const char *query,
378 const char *label,
379 apr_dbd_prepared_t **statement);
382 /** apr_dbd_pquery: query using a prepared statement + args
384 * @param driver - the driver
385 * @param pool - working pool
386 * @param handle - the connection
387 * @param nrows - number of rows affected.
388 * @param statement - the prepared statement to execute
389 * @param nargs - ignored (for backward compatibility only)
390 * @param args - args to prepared statement
391 * @return 0 for success or error code
393 APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
394 apr_dbd_t *handle, int *nrows,
395 apr_dbd_prepared_t *statement, int nargs,
396 const char **args);
398 /** apr_dbd_pselect: select using a prepared statement + args
400 * @param driver - the driver
401 * @param pool - working pool
402 * @param handle - the connection
403 * @param res - pointer to query results. May point to NULL on entry
404 * @param statement - the prepared statement to execute
405 * @param random - Whether to support random-access to results
406 * @param nargs - ignored (for backward compatibility only)
407 * @param args - args to prepared statement
408 * @return 0 for success or error code
410 APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
411 apr_dbd_t *handle, apr_dbd_results_t **res,
412 apr_dbd_prepared_t *statement, int random,
413 int nargs, const char **args);
415 /** apr_dbd_pvquery: query using a prepared statement + args
417 * @param driver - the driver
418 * @param pool - working pool
419 * @param handle - the connection
420 * @param nrows - number of rows affected.
421 * @param statement - the prepared statement to execute
422 * @param ... - varargs list
423 * @return 0 for success or error code
425 APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
426 apr_dbd_t *handle, int *nrows,
427 apr_dbd_prepared_t *statement, ...);
429 /** apr_dbd_pvselect: select using a prepared statement + args
431 * @param driver - the driver
432 * @param pool - working pool
433 * @param handle - the connection
434 * @param res - pointer to query results. May point to NULL on entry
435 * @param statement - the prepared statement to execute
436 * @param random - Whether to support random-access to results
437 * @param ... - varargs list
438 * @return 0 for success or error code
440 APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
441 apr_dbd_t *handle, apr_dbd_results_t **res,
442 apr_dbd_prepared_t *statement, int random,
443 ...);
445 /** apr_dbd_pbquery: query using a prepared statement + binary args
447 * @param driver - the driver
448 * @param pool - working pool
449 * @param handle - the connection
450 * @param nrows - number of rows affected.
451 * @param statement - the prepared statement to execute
452 * @param args - binary args to prepared statement
453 * @return 0 for success or error code
455 APU_DECLARE(int) apr_dbd_pbquery(const apr_dbd_driver_t *driver,
456 apr_pool_t *pool, apr_dbd_t *handle,
457 int *nrows, apr_dbd_prepared_t *statement,
458 const void **args);
460 /** apr_dbd_pbselect: select using a prepared statement + binary args
462 * @param driver - the driver
463 * @param pool - working pool
464 * @param handle - the connection
465 * @param res - pointer to query results. May point to NULL on entry
466 * @param statement - the prepared statement to execute
467 * @param random - Whether to support random-access to results
468 * @param args - binary args to prepared statement
469 * @return 0 for success or error code
471 APU_DECLARE(int) apr_dbd_pbselect(const apr_dbd_driver_t *driver,
472 apr_pool_t *pool,
473 apr_dbd_t *handle, apr_dbd_results_t **res,
474 apr_dbd_prepared_t *statement, int random,
475 const void **args);
477 /** apr_dbd_pvbquery: query using a prepared statement + binary args
479 * @param driver - the driver
480 * @param pool - working pool
481 * @param handle - the connection
482 * @param nrows - number of rows affected.
483 * @param statement - the prepared statement to execute
484 * @param ... - varargs list of binary args
485 * @return 0 for success or error code
487 APU_DECLARE(int) apr_dbd_pvbquery(const apr_dbd_driver_t *driver,
488 apr_pool_t *pool,
489 apr_dbd_t *handle, int *nrows,
490 apr_dbd_prepared_t *statement, ...);
492 /** apr_dbd_pvbselect: select using a prepared statement + binary args
494 * @param driver - the driver
495 * @param pool - working pool
496 * @param handle - the connection
497 * @param res - pointer to query results. May point to NULL on entry
498 * @param statement - the prepared statement to execute
499 * @param random - Whether to support random-access to results
500 * @param ... - varargs list of binary args
501 * @return 0 for success or error code
503 APU_DECLARE(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver,
504 apr_pool_t *pool,
505 apr_dbd_t *handle, apr_dbd_results_t **res,
506 apr_dbd_prepared_t *statement, int random,
507 ...);
509 /** apr_dbd_datum_get: get a binary entry from a row
511 * @param driver - the driver
512 * @param row - row pointer
513 * @param col - entry number
514 * @param type - type of data to get
515 * @param data - pointer to data, allocated by the caller
516 * @return APR_SUCCESS on success, APR_ENOENT if data is NULL or APR_EGENERAL
518 APU_DECLARE(apr_status_t) apr_dbd_datum_get(const apr_dbd_driver_t *driver,
519 apr_dbd_row_t *row, int col,
520 apr_dbd_type_e type, void *data);
522 /** @} */
524 #ifdef __cplusplus
526 #endif
528 #endif