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
25 #include "apr_pools.h"
33 * @brief APR-UTIL DBD library
36 * @defgroup APR_Util_DBD DBD routines
41 /* These are opaque structs. Instantiation is up to each backend */
42 typedef struct apr_dbd_driver_t apr_dbd_driver_t
;
43 typedef struct apr_dbd_t apr_dbd_t
;
44 typedef struct apr_dbd_transaction_t apr_dbd_transaction_t
;
45 typedef struct apr_dbd_results_t apr_dbd_results_t
;
46 typedef struct apr_dbd_row_t apr_dbd_row_t
;
47 typedef struct apr_dbd_prepared_t apr_dbd_prepared_t
;
49 /** apr_dbd_init: perform once-only initialisation. Call once only.
51 * @param pool - pool to register any shutdown cleanups, etc
53 APU_DECLARE(apr_status_t
) apr_dbd_init(apr_pool_t
*pool
);
55 /** apr_dbd_get_driver: get the driver struct for a name
57 * @param pool - (process) pool to register cleanup
58 * @param name - driver name
59 * @param driver - pointer to driver struct.
60 * @return APR_SUCCESS for success
61 * @return APR_ENOTIMPL for no driver (when DSO not enabled)
62 * @return APR_EDSOOPEN if DSO driver file can't be opened
63 * @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver
65 APU_DECLARE(apr_status_t
) apr_dbd_get_driver(apr_pool_t
*pool
, const char *name
,
66 const apr_dbd_driver_t
**driver
);
68 /** apr_dbd_open: open a connection to a backend
70 * @param pool - working pool
71 * @param params - arguments to driver (implementation-dependent)
72 * @param handle - pointer to handle to return
73 * @param driver - driver struct.
74 * @return APR_SUCCESS for success
75 * @return APR_EGENERAL if driver exists but connection failed
76 * @remarks PostgreSQL: the params is passed directly to the PQconnectdb()
77 * function (check PostgreSQL documentation for more details on the syntax).
78 * @remarks SQLite2: the params is split on a colon, with the first part used
79 * as the filename and second part converted to an integer and used as file
81 * @remarks SQLite3: the params is passed directly to the sqlite3_open()
82 * function as a filename to be opened (check SQLite3 documentation for more
84 * @remarks Oracle: the params can have "user", "pass", "dbname" and "server"
85 * keys, each followed by an equal sign and a value. Such key/value pairs can
86 * be delimited by space, CR, LF, tab, semicolon, vertical bar or comma.
87 * @remarks MySQL: the params can have "host", "port", "user", "pass",
88 * "dbname", "sock", "flags" and "fldsz" keys, each followed by an equal sign
89 * and a value. Such key/value pairs can be delimited by space, CR, LF, tab,
90 * semicolon, vertical bar or comma. For now, "flags" can only recognise
91 * CLIENT_FOUND_ROWS (check MySQL manual for details). The value associated
92 * with "fldsz" determines maximum amount of memory (in bytes) for each of
93 * the fields in the result set of prepared statements. By default, this
96 APU_DECLARE(apr_status_t
) apr_dbd_open(const apr_dbd_driver_t
*driver
,
97 apr_pool_t
*pool
, const char *params
,
100 /** apr_dbd_close: close a connection to a backend
102 * @param handle - handle to close
103 * @param driver - driver struct.
104 * @return APR_SUCCESS for success or error status
106 APU_DECLARE(apr_status_t
) apr_dbd_close(const apr_dbd_driver_t
*driver
,
109 /* apr-function-shaped versions of things */
111 /** apr_dbd_name: get the name of the driver
113 * @param driver - the driver
116 APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t
*driver
);
118 /** apr_dbd_native_handle: get native database handle of the underlying db
120 * @param driver - the driver
121 * @param handle - apr_dbd handle
122 * @return - native handle
124 APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t
*driver
,
127 /** check_conn: check status of a database connection
129 * @param driver - the driver
130 * @param pool - working pool
131 * @param handle - the connection to check
132 * @return APR_SUCCESS or error
134 APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
137 /** apr_dbd_set_dbname: select database name. May be a no-op if not supported.
139 * @param driver - the driver
140 * @param pool - working pool
141 * @param handle - the connection
142 * @param name - the database to select
143 * @return 0 for success or error code
145 APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
146 apr_dbd_t
*handle
, const char *name
);
148 /** apr_dbd_transaction_start: start a transaction. May be a no-op.
150 * @param driver - the driver
151 * @param pool - a pool to use for error messages (if any).
152 * @param handle - the db connection
153 * @param trans - ptr to a transaction. May be null on entry
154 * @return 0 for success or error code
155 * @remarks Note that transaction modes, set by calling
156 * apr_dbd_transaction_mode_set(), will affect all query/select calls within
157 * a transaction. By default, any error in query/select during a transaction
158 * will cause the transaction to inherit the error code and any further
159 * query/select calls will fail immediately. Put transaction in "ignore
160 * errors" mode to avoid that. Use "rollback" mode to do explicit rollback.
162 APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t
*driver
,
165 apr_dbd_transaction_t
**trans
);
167 /** apr_dbd_transaction_end: end a transaction
168 * (commit on success, rollback on error).
171 * @param driver - the driver
172 * @param handle - the db connection
173 * @param trans - the transaction.
174 * @return 0 for success or error code
176 APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t
*driver
,
178 apr_dbd_transaction_t
*trans
);
180 #define APR_DBD_TRANSACTION_COMMIT 0x00 /**< commit the transaction */
181 #define APR_DBD_TRANSACTION_ROLLBACK 0x01 /**< rollback the transaction */
182 #define APR_DBD_TRANSACTION_IGNORE_ERRORS 0x02 /**< ignore transaction errors */
184 /** apr_dbd_transaction_mode_get: get the mode of transaction
186 * @param driver - the driver
187 * @param trans - the transaction
188 * @return mode of transaction
190 APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t
*driver
,
191 apr_dbd_transaction_t
*trans
);
193 /** apr_dbd_transaction_mode_set: set the mode of transaction
195 * @param driver - the driver
196 * @param trans - the transaction
197 * @param mode - new mode of the transaction
198 * @return the mode of transaction in force after the call
200 APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t
*driver
,
201 apr_dbd_transaction_t
*trans
,
204 /** apr_dbd_query: execute an SQL query that doesn't return a result set
206 * @param driver - the driver
207 * @param handle - the connection
208 * @param nrows - number of rows affected.
209 * @param statement - the SQL statement to execute
210 * @return 0 for success or error code
212 APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t
*driver
, apr_dbd_t
*handle
,
213 int *nrows
, const char *statement
);
215 /** apr_dbd_select: execute an SQL query that returns a result set
217 * @param driver - the driver
218 * @param pool - pool to allocate the result set
219 * @param handle - the connection
220 * @param res - pointer to result set pointer. May point to NULL on entry
221 * @param statement - the SQL statement to execute
222 * @param random - 1 to support random access to results (seek any row);
223 * 0 to support only looping through results in order
224 * (async access - faster)
225 * @return 0 for success or error code
227 APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
228 apr_dbd_t
*handle
, apr_dbd_results_t
**res
,
229 const char *statement
, int random
);
231 /** apr_dbd_num_cols: get the number of columns in a results set
233 * @param driver - the driver
234 * @param res - result set.
235 * @return number of columns
237 APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t
*driver
,
238 apr_dbd_results_t
*res
);
240 /** apr_dbd_num_tuples: get the number of rows in a results set
241 * of a synchronous select
243 * @param driver - the driver
244 * @param res - result set.
245 * @return number of rows, or -1 if the results are asynchronous
247 APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t
*driver
,
248 apr_dbd_results_t
*res
);
250 /** apr_dbd_get_row: get a row from a result set
252 * @param driver - the driver
253 * @param pool - pool to allocate the row
254 * @param res - result set pointer
255 * @param row - pointer to row pointer. May point to NULL on entry
256 * @param rownum - row number, or -1 for "next row". Ignored if random
257 * access is not supported.
258 * @return 0 for success, -1 for rownum out of range or data finished
260 APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
261 apr_dbd_results_t
*res
, apr_dbd_row_t
**row
,
264 /** apr_dbd_get_entry: get an entry from a row
266 * @param driver - the driver
267 * @param row - row pointer
268 * @param col - entry number
269 * @return value from the row, or NULL if col is out of bounds.
271 APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t
*driver
,
272 apr_dbd_row_t
*row
, int col
);
274 /** apr_dbd_get_name: get an entry name from a result set
276 * @param driver - the driver
277 * @param res - result set pointer
278 * @param col - entry number
279 * @return name of the entry, or NULL if col is out of bounds.
281 APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t
*driver
,
282 apr_dbd_results_t
*res
, int col
);
285 /** apr_dbd_error: get current error message (if any)
287 * @param driver - the driver
288 * @param handle - the connection
289 * @param errnum - error code from operation that returned an error
290 * @return the database current error message, or message for errnum
291 * (implementation-dependent whether errnum is ignored)
293 APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t
*driver
,
294 apr_dbd_t
*handle
, int errnum
);
296 /** apr_dbd_escape: escape a string so it is safe for use in query/select
298 * @param driver - the driver
299 * @param pool - pool to alloc the result from
300 * @param string - the string to escape
301 * @param handle - the connection
302 * @return the escaped, safe string
304 APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t
*driver
,
305 apr_pool_t
*pool
, const char *string
,
308 /** apr_dbd_prepare: prepare a statement
310 * @param driver - the driver
311 * @param pool - pool to alloc the result from
312 * @param handle - the connection
313 * @param query - the SQL query
314 * @param label - A label for the prepared statement.
315 * use NULL for temporary prepared statements
316 * (eg within a Request in httpd)
317 * @param statement - statement to prepare. May point to null on entry.
318 * @return 0 for success or error code
319 * @remarks To specify parameters of the prepared query, use %s in place of
320 * database specific parameter syntax (e.g. for PostgreSQL, this would be $1,
321 * $2, for SQLite3 this would be ? etc.). For instance: "SELECT name FROM
322 * customers WHERE name=%s" would be a query that this function understands.
323 * Some drivers may support different data types using printf-like format:
324 * for example %d (e.g. PostgreSQL) or %f for numeric data.
326 APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
327 apr_dbd_t
*handle
, const char *query
,
329 apr_dbd_prepared_t
**statement
);
332 /** apr_dbd_pquery: query using a prepared statement + args
334 * @param driver - the driver
335 * @param pool - working pool
336 * @param handle - the connection
337 * @param nrows - number of rows affected.
338 * @param statement - the prepared statement to execute
339 * @param nargs - number of args to prepared statement
340 * @param args - args to prepared statement
341 * @return 0 for success or error code
343 APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
344 apr_dbd_t
*handle
, int *nrows
,
345 apr_dbd_prepared_t
*statement
, int nargs
,
348 /** apr_dbd_pselect: select using a prepared statement + args
350 * @param driver - the driver
351 * @param pool - working pool
352 * @param handle - the connection
353 * @param res - pointer to query results. May point to NULL on entry
354 * @param statement - the prepared statement to execute
355 * @param random - Whether to support random-access to results
356 * @param nargs - number of args to prepared statement
357 * @param args - args to prepared statement
358 * @return 0 for success or error code
360 APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
361 apr_dbd_t
*handle
, apr_dbd_results_t
**res
,
362 apr_dbd_prepared_t
*statement
, int random
,
363 int nargs
, const char **args
);
365 /** apr_dbd_pvquery: query using a prepared statement + args
367 * @param driver - the driver
368 * @param pool - working pool
369 * @param handle - the connection
370 * @param nrows - number of rows affected.
371 * @param statement - the prepared statement to execute
372 * @param ... - varargs list
373 * @return 0 for success or error code
375 APU_DECLARE(int) apr_dbd_pvquery(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
376 apr_dbd_t
*handle
, int *nrows
,
377 apr_dbd_prepared_t
*statement
, ...);
379 /** apr_dbd_pvselect: select using a prepared statement + args
381 * @param driver - the driver
382 * @param pool - working pool
383 * @param handle - the connection
384 * @param res - pointer to query results. May point to NULL on entry
385 * @param statement - the prepared statement to execute
386 * @param random - Whether to support random-access to results
387 * @param ... - varargs list
388 * @return 0 for success or error code
390 APU_DECLARE(int) apr_dbd_pvselect(const apr_dbd_driver_t
*driver
, apr_pool_t
*pool
,
391 apr_dbd_t
*handle
, apr_dbd_results_t
**res
,
392 apr_dbd_prepared_t
*statement
, int random
,