Document that we count rows form 1 in DBD.
[apr-util.git] / include / apr_dbd.h
blobd64fc713c820e98864cb27cb260a0826352c2772
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).
138 * @remarks FreeTDS: the params can have "username", "password", "appname",
139 * "dbname", "host", "charset", "lang" and "server" keys, each followed by an
140 * equal sign and a value.
142 APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver,
143 apr_pool_t *pool, const char *params,
144 apr_dbd_t **handle,
145 const char **error);
147 /** apr_dbd_open: open a connection to a backend
149 * @param pool - working pool
150 * @param params - arguments to driver (implementation-dependent)
151 * @param handle - pointer to handle to return
152 * @param driver - driver struct.
153 * @return APR_SUCCESS for success
154 * @return APR_EGENERAL if driver exists but connection failed
155 * @see apr_dbd_open_ex
157 APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
158 apr_pool_t *pool, const char *params,
159 apr_dbd_t **handle);
161 /** apr_dbd_close: close a connection to a backend
163 * @param handle - handle to close
164 * @param driver - driver struct.
165 * @return APR_SUCCESS for success or error status
167 APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
168 apr_dbd_t *handle);
170 /* apr-function-shaped versions of things */
172 /** apr_dbd_name: get the name of the driver
174 * @param driver - the driver
175 * @return - name
177 APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver);
179 /** apr_dbd_native_handle: get native database handle of the underlying db
181 * @param driver - the driver
182 * @param handle - apr_dbd handle
183 * @return - native handle
185 APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
186 apr_dbd_t *handle);
188 /** check_conn: check status of a database connection
190 * @param driver - the driver
191 * @param pool - working pool
192 * @param handle - the connection to check
193 * @return APR_SUCCESS or error
195 APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool,
196 apr_dbd_t *handle);
198 /** apr_dbd_set_dbname: select database name. May be a no-op if not supported.
200 * @param driver - the driver
201 * @param pool - working pool
202 * @param handle - the connection
203 * @param name - the database to select
204 * @return 0 for success or error code
206 APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool,
207 apr_dbd_t *handle, const char *name);
209 /** apr_dbd_transaction_start: start a transaction. May be a no-op.
211 * @param driver - the driver
212 * @param pool - a pool to use for error messages (if any).
213 * @param handle - the db connection
214 * @param trans - ptr to a transaction. May be null on entry
215 * @return 0 for success or error code
216 * @remarks Note that transaction modes, set by calling
217 * apr_dbd_transaction_mode_set(), will affect all query/select calls within
218 * a transaction. By default, any error in query/select during a transaction
219 * will cause the transaction to inherit the error code and any further
220 * query/select calls will fail immediately. Put transaction in "ignore
221 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback.
223 APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
224 apr_pool_t *pool,
225 apr_dbd_t *handle,
226 apr_dbd_transaction_t **trans);
228 /** apr_dbd_transaction_end: end a transaction
229 * (commit on success, rollback on error).
230 * May be a no-op.
232 * @param driver - the driver
233 * @param handle - the db connection
234 * @param trans - the transaction.
235 * @return 0 for success or error code
237 APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
238 apr_pool_t *pool,
239 apr_dbd_transaction_t *trans);
241 #define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */
242 #define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */
243 #define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */
245 /** apr_dbd_transaction_mode_get: get the mode of transaction
247 * @param driver - the driver
248 * @param trans - the transaction
249 * @return mode of transaction
251 APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver,
252 apr_dbd_transaction_t *trans);
254 /** apr_dbd_transaction_mode_set: set the mode of transaction
256 * @param driver - the driver
257 * @param trans - the transaction
258 * @param mode - new mode of the transaction
259 * @return the mode of transaction in force after the call
261 APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver,
262 apr_dbd_transaction_t *trans,
263 int mode);
265 /** apr_dbd_query: execute an SQL query that doesn't return a result set
267 * @param driver - the driver
268 * @param handle - the connection
269 * @param nrows - number of rows affected.
270 * @param statement - the SQL statement to execute
271 * @return 0 for success or error code
273 APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle,
274 int *nrows, const char *statement);
276 /** apr_dbd_select: execute an SQL query that returns a result set
278 * @param driver - the driver
279 * @param pool - pool to allocate the result set
280 * @param handle - the connection
281 * @param res - pointer to result set pointer. May point to NULL on entry
282 * @param statement - the SQL statement to execute
283 * @param random - 1 to support random access to results (seek any row);
284 * 0 to support only looping through results in order
285 * (async access - faster)
286 * @return 0 for success or error code
288 APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool,
289 apr_dbd_t *handle, apr_dbd_results_t **res,
290 const char *statement, int random);
292 /** apr_dbd_num_cols: get the number of columns in a results set
294 * @param driver - the driver
295 * @param res - result set.
296 * @return number of columns
298 APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
299 apr_dbd_results_t *res);
301 /** apr_dbd_num_tuples: get the number of rows in a results set
302 * of a synchronous select
304 * @param driver - the driver
305 * @param res - result set.
306 * @return number of rows, or -1 if the results are asynchronous
308 APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
309 apr_dbd_results_t *res);
311 /** apr_dbd_get_row: get a row from a result set
313 * @param driver - the driver
314 * @param pool - pool to allocate the row
315 * @param res - result set pointer
316 * @param row - pointer to row pointer. May point to NULL on entry
317 * @param rownum - row number (counting from 1), or -1 for "next row".
318 * Ignored if random access is not supported.
319 * @return 0 for success, -1 for rownum out of range or data finished
321 APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
322 apr_dbd_results_t *res, apr_dbd_row_t **row,
323 int rownum);
325 /** apr_dbd_get_entry: get an entry from a row
327 * @param driver - the driver
328 * @param row - row pointer
329 * @param col - entry number
330 * @return value from the row, or NULL if col is out of bounds.
332 APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
333 apr_dbd_row_t *row, int col);
335 /** apr_dbd_get_name: get an entry name from a result set
337 * @param driver - the driver
338 * @param res - result set pointer
339 * @param col - entry number
340 * @return name of the entry, or NULL if col is out of bounds.
342 APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver,
343 apr_dbd_results_t *res, int col);
346 /** apr_dbd_error: get current error message (if any)
348 * @param driver - the driver
349 * @param handle - the connection
350 * @param errnum - error code from operation that returned an error
351 * @return the database current error message, or message for errnum
352 * (implementation-dependent whether errnum is ignored)
354 APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
355 apr_dbd_t *handle, int errnum);
357 /** apr_dbd_escape: escape a string so it is safe for use in query/select
359 * @param driver - the driver
360 * @param pool - pool to alloc the result from
361 * @param string - the string to escape
362 * @param handle - the connection
363 * @return the escaped, safe string
365 APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
366 apr_pool_t *pool, const char *string,
367 apr_dbd_t *handle);
369 /** apr_dbd_prepare: prepare a statement
371 * @param driver - the driver
372 * @param pool - pool to alloc the result from
373 * @param handle - the connection
374 * @param query - the SQL query
375 * @param label - A label for the prepared statement.
376 * use NULL for temporary prepared statements
377 * (eg within a Request in httpd)
378 * @param statement - statement to prepare. May point to null on entry.
379 * @return 0 for success or error code
380 * @remarks To specify parameters of the prepared query, use \%s, \%d etc.
381 * (see below for full list) in place of database specific parameter syntax
382 * (e.g. for PostgreSQL, this would be $1, $2, for SQLite3 this would be ?
383 * etc.). For instance: "SELECT name FROM customers WHERE name=%s" would be
384 * a query that this function understands.
385 * @remarks Here is the full list of format specifiers that this function
386 * understands and what they map to in SQL: \%hhd (TINY INT), \%hhu (UNSIGNED
387 * TINY INT), \%hd (SHORT), \%hu (UNSIGNED SHORT), \%d (INT), \%u (UNSIGNED
388 * INT), \%ld (LONG), \%lu (UNSIGNED LONG), \%lld (LONG LONG), \%llu
389 * (UNSIGNED LONG LONG), \%f (FLOAT, REAL), \%lf (DOUBLE PRECISION), \%s
390 * (VARCHAR), \%pDt (TEXT), \%pDi (TIME), \%pDd (DATE), \%pDa (DATETIME),
391 * \%pDs (TIMESTAMP), \%pDz (TIMESTAMP WITH TIME ZONE), \%pDb (BLOB), \%pDc
392 * (CLOB) and \%pDn (NULL). Not all databases have support for all these
393 * types, so the underlying driver will attempt the "best match" where
394 * possible. A \% followed by any letter not in the above list will be
395 * interpreted as VARCHAR (i.e. \%s).
397 APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool,
398 apr_dbd_t *handle, const char *query,
399 const char *label,
400 apr_dbd_prepared_t **statement);
403 /** apr_dbd_pquery: query using a prepared statement + args
405 * @param driver - the driver
406 * @param pool - working pool
407 * @param handle - the connection
408 * @param nrows - number of rows affected.
409 * @param statement - the prepared statement to execute
410 * @param nargs - ignored (for backward compatibility only)
411 * @param args - args to prepared statement
412 * @return 0 for success or error code
414 APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
415 apr_dbd_t *handle, int *nrows,
416 apr_dbd_prepared_t *statement, int nargs,
417 const char **args);
419 /** apr_dbd_pselect: select using a prepared statement + args
421 * @param driver - the driver
422 * @param pool - working pool
423 * @param handle - the connection
424 * @param res - pointer to query results. May point to NULL on entry
425 * @param statement - the prepared statement to execute
426 * @param random - Whether to support random-access to results
427 * @param nargs - ignored (for backward compatibility only)
428 * @param args - args to prepared statement
429 * @return 0 for success or error code
431 APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
432 apr_dbd_t *handle, apr_dbd_results_t **res,
433 apr_dbd_prepared_t *statement, int random,
434 int nargs, const char **args);
436 /** apr_dbd_pvquery: query using a prepared statement + args
438 * @param driver - the driver
439 * @param pool - working pool
440 * @param handle - the connection
441 * @param nrows - number of rows affected.
442 * @param statement - the prepared statement to execute
443 * @param ... - varargs list
444 * @return 0 for success or error code
446 APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
447 apr_dbd_t *handle, int *nrows,
448 apr_dbd_prepared_t *statement, ...);
450 /** apr_dbd_pvselect: select using a prepared statement + args
452 * @param driver - the driver
453 * @param pool - working pool
454 * @param handle - the connection
455 * @param res - pointer to query results. May point to NULL on entry
456 * @param statement - the prepared statement to execute
457 * @param random - Whether to support random-access to results
458 * @param ... - varargs list
459 * @return 0 for success or error code
461 APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
462 apr_dbd_t *handle, apr_dbd_results_t **res,
463 apr_dbd_prepared_t *statement, int random,
464 ...);
466 /** apr_dbd_pbquery: query using a prepared statement + binary args
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
476 APU_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);
481 /** apr_dbd_pbselect: select using a prepared statement + binary args
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
492 APU_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);
498 /** apr_dbd_pvbquery: query using a prepared statement + binary args
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
508 APU_DECLARE(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, ...);
513 /** apr_dbd_pvbselect: select using a prepared statement + binary args
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
524 APU_DECLARE(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver,
525 apr_pool_t *pool,
526 apr_dbd_t *handle, apr_dbd_results_t **res,
527 apr_dbd_prepared_t *statement, int random,
528 ...);
530 /** apr_dbd_datum_get: get a binary entry from a row
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
539 APU_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);
543 /** @} */
545 #ifdef __cplusplus
547 #endif
549 #endif