1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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
30
* @brief APR-UTIL DBD library
33
* @defgroup APR_Util_DBD DBD routines
38
/* These are opaque structs. Instantiation is up to each backend */
39
typedef struct apr_dbd_driver_t apr_dbd_driver_t;
40
typedef struct apr_dbd_t apr_dbd_t;
41
typedef struct apr_dbd_transaction_t apr_dbd_transaction_t;
42
typedef struct apr_dbd_results_t apr_dbd_results_t;
43
typedef struct apr_dbd_row_t apr_dbd_row_t;
44
typedef struct apr_dbd_prepared_t apr_dbd_prepared_t;
46
/** apr_dbd_init: perform once-only initialisation. Call once only.
48
* @param pool - pool to register any shutdown cleanups, etc
50
APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool);
52
/** apr_dbd_get_driver: get the driver struct for a name
54
* @param pool - (process) pool to register cleanup
55
* @param name - driver name
56
* @param driver - pointer to driver struct.
57
* @return APR_SUCCESS for success
58
* @return APR_ENOTIMPL for no driver (when DSO not enabled)
59
* @return APR_EDSOOPEN if DSO driver file can't be opened
60
* @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver
62
APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name,
63
const apr_dbd_driver_t **driver);
65
/** apr_dbd_open: open a connection to a backend
67
* @param ptmp - working pool
68
* @param params - arguments to driver (implementation-dependent)
69
* @param handle - pointer to handle to return
70
* @param driver - driver struct.
71
* @return APR_SUCCESS for success
72
* @return APR_EGENERAL if driver exists but connection failed
74
APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
75
apr_pool_t *ptmp, const char *params,
78
/** apr_dbd_close: close a connection to a backend.
79
* Only required for explicit close or
81
* @param handle - handle to close
82
* @param driver - driver struct.
83
* @return APR_SUCCESS for success or error status
85
APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
88
/* apr-function-shaped versions of things */
90
/** apr_dbd_name: get the name of the driver
92
* @param driver - the driver
95
APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver);
97
/** apr_dbd_native_handle: get native database handle of the underlying db
99
* @param driver - the driver
100
* @param handle - apr_dbd handle
101
* @return - native handle
103
APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
106
/** check_conn: check status of a database connection
108
* @param driver - the driver
109
* @param pool - working pool
110
* @param handle - the connection to check
111
* @return APR_SUCCESS or error
113
APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver, apr_pool_t *pool,
116
/** apr_dbd_set_dbname: select database name. May be a no-op if not supported.
118
* @param driver - the driver
119
* @param pool - working pool
120
* @param handle - the connection
121
* @param name - the database to select
122
* @return 0 for success or error code
124
APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver, apr_pool_t *pool,
125
apr_dbd_t *handle, const char *name);
127
/** apr_dbd_transaction_start: start a transaction. May be a no-op.
129
* @param driver - the driver
130
* @param pool - a pool to use for error messages (if any).
131
* @param handle - the db connection
132
* @param transaction - ptr to a transaction. May be null on entry
133
* @return 0 for success or error code
135
APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
138
apr_dbd_transaction_t **trans);
140
/** apr_dbd_transaction_end: end a transaction
141
* (commit on success, rollback on error).
144
* @param driver - the driver
145
* @param handle - the db connection
146
* @param transaction - the transaction.
147
* @return 0 for success or error code
149
APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
151
apr_dbd_transaction_t *trans);
153
/** apr_dbd_query: execute an SQL query that doesn't return a result set
155
* @param driver - the driver
156
* @param handle - the connection
157
* @param nrows - number of rows affected.
158
* @param statement - the SQL statement to execute
159
* @return 0 for success or error code
161
APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver, apr_dbd_t *handle,
162
int *nrows, const char *statement);
164
/** apr_dbd_select: execute an SQL query that returns a result set
166
* @param driver - the driver
167
* @param pool - pool to allocate the result set
168
* @param handle - the connection
169
* @param res - pointer to result set pointer. May point to NULL on entry
170
* @param statement - the SQL statement to execute
171
* @param random - 1 to support random access to results (seek any row);
172
* 0 to support only looping through results in order
173
* (async access - faster)
174
* @return 0 for success or error code
176
APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver, apr_pool_t *pool,
177
apr_dbd_t *handle, apr_dbd_results_t **res,
178
const char *statement, int random);
180
/** apr_dbd_num_cols: get the number of columns in a results set
182
* @param driver - the driver
183
* @param res - result set.
184
* @return number of columns
186
APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
187
apr_dbd_results_t *res);
189
/** apr_dbd_num_tuples: get the number of rows in a results set
190
* of a synchronous select
192
* @param driver - the driver
193
* @param res - result set.
194
* @return number of rows, or -1 if the results are asynchronous
196
APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
197
apr_dbd_results_t *res);
199
/** apr_dbd_get_row: get a row from a result set
201
* @param driver - the driver
202
* @param pool - pool to allocate the row
203
* @param res - result set pointer
204
* @param row - pointer to row pointer. May point to NULL on entry
205
* @param rownum - row number, or -1 for "next row". Ignored if random
206
* access is not supported.
207
* @return 0 for success, -1 for rownum out of range or data finished
209
APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
210
apr_dbd_results_t *res, apr_dbd_row_t **row,
213
/** apr_dbd_get_entry: get an entry from a row
215
* @param driver - the driver
216
* @param row - row pointer
217
* @param col - entry number
218
* @return value from the row, or NULL if col is out of bounds.
220
APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
221
apr_dbd_row_t *row, int col);
223
/** apr_dbd_error: get current error message (if any)
225
* @param driver - the driver
226
* @param handle - the connection
227
* @param errnum - error code from operation that returned an error
228
* @return the database current error message, or message for errnum
229
* (implementation-dependent whether errnum is ignored)
231
APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
232
apr_dbd_t *handle, int errnum);
234
/** apr_dbd_escape: escape a string so it is safe for use in query/select
236
* @param driver - the driver
237
* @param pool - pool to alloc the result from
238
* @param string - the string to escape
239
* @param handle - the connection
240
* @return the escaped, safe string
242
APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
243
apr_pool_t *pool, const char *string,
246
/** apr_dbd_prepare: prepare a statement
248
* @param driver - the driver
249
* @param pool - pool to alloc the result from
250
* @param handle - the connection
251
* @param query - the SQL query
252
* @param label - A label for the prepared statement.
253
* use NULL for temporary prepared statements
254
* (eg within a Request in httpd)
255
* @param statement - statement to prepare. May point to null on entry.
256
* @return 0 for success or error code
258
APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver, apr_pool_t *pool,
259
apr_dbd_t *handle, const char *query,
261
apr_dbd_prepared_t **statement);
264
/** apr_dbd_pquery: query using a prepared statement + args
266
* @param driver - the driver
267
* @param pool - working pool
268
* @param handle - the connection
269
* @param nrows - number of rows affected.
270
* @param statement - the prepared statement to execute
271
* @param nargs - number of args to prepared statement
272
* @param args - args to prepared statement
273
* @return 0 for success or error code
275
APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
276
apr_dbd_t *handle, int *nrows,
277
apr_dbd_prepared_t *statement, int nargs,
280
/** apr_dbd_pselect: select using a prepared statement + args
282
* @param driver - the driver
283
* @param pool - working pool
284
* @param handle - the connection
285
* @param res - pointer to query results. May point to NULL on entry
286
* @param statement - the prepared statement to execute
287
* @param random - Whether to support random-access to results
288
* @param nargs - number of args to prepared statement
289
* @param args - args to prepared statement
290
* @return 0 for success or error code
292
APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
293
apr_dbd_t *handle, apr_dbd_results_t **res,
294
apr_dbd_prepared_t *statement, int random,
295
int nargs, const char **args);
297
/** apr_dbd_pvquery: query using a prepared statement + args
299
* @param driver - the driver
300
* @param pool - working pool
301
* @param handle - the connection
302
* @param nrows - number of rows affected.
303
* @param statement - the prepared statement to execute
304
* @param ... - varargs list
305
* @return 0 for success or error code
307
APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver, apr_pool_t *pool,
308
apr_dbd_t *handle, int *nrows,
309
apr_dbd_prepared_t *statement, ...);
311
/** apr_dbd_pvselect: select using a prepared statement + args
313
* @param driver - the driver
314
* @param pool - working pool
315
* @param handle - the connection
316
* @param res - pointer to query results. May point to NULL on entry
317
* @param statement - the prepared statement to execute
318
* @param random - Whether to support random-access to results
319
* @param ... - varargs list
320
* @return 0 for success or error code
322
APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver, apr_pool_t *pool,
323
apr_dbd_t *handle, apr_dbd_results_t **res,
324
apr_dbd_prepared_t *statement, int random,