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.
20 #include "apu_config.h"
23 #include "apr_pools.h"
25 #include "apr_strings.h"
27 #include "apr_thread_mutex.h"
29 #include "apr_atomic.h"
31 #include "apu_internal.h"
32 #include "apr_dbd_internal.h"
34 #include "apu_version.h"
36 static apr_hash_t *drivers = NULL;
37 static apr_uint32_t initialised = 0, in_init = 1;
39 #define CLEANUP_CAST (apr_status_t (*)(void*))
42 /* deprecated, but required for existing providers. Existing and new
43 * providers should be refactored to use a provider-specific mutex so
44 * that different providers do not block one another.
45 * In APR 1.3 this is no longer used for dso module loading, and
46 * apu_dso_mutex_[un]lock is used instead.
47 * In APR 2.0 this should become entirely local to libaprutil-2.so and
48 * no longer be exported.
50 static apr_thread_mutex_t* mutex = NULL;
51 APU_DECLARE(apr_status_t) apr_dbd_mutex_lock()
53 return apr_thread_mutex_lock(mutex);
55 APU_DECLARE(apr_status_t) apr_dbd_mutex_unlock()
57 return apr_thread_mutex_unlock(mutex);
60 APU_DECLARE(apr_status_t) apr_dbd_mutex_lock() {
63 APU_DECLARE(apr_status_t) apr_dbd_mutex_unlock() {
69 #define DRIVER_LOAD(name,driver,pool) \
71 extern const apr_dbd_driver_t driver; \
72 apr_hash_set(drivers,name,APR_HASH_KEY_STRING,&driver); \
79 static apr_status_t apr_dbd_term(void *ptr)
81 /* set drivers to NULL so init can work again */
84 /* Everything else we need is handled by cleanups registered
85 * when we created mutexes and loaded DSOs
90 APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool)
92 apr_status_t ret = APR_SUCCESS;
95 if (apr_atomic_inc32(&initialised)) {
96 apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
98 while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
104 /* Top level pool scope, need process-scope lifetime */
105 for (parent = pool; parent; parent = apr_pool_parent_get(pool))
108 /* deprecate in 2.0 - permit implicit initialization */
112 drivers = apr_hash_make(pool);
115 ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
116 /* This already registers a pool cleanup */
121 /* Load statically-linked drivers: */
123 DRIVER_LOAD("mysql", apr_dbd_mysql_driver, pool);
126 DRIVER_LOAD("pgsql", apr_dbd_pgsql_driver, pool);
129 DRIVER_LOAD("sqlite3", apr_dbd_sqlite3_driver, pool);
132 DRIVER_LOAD("sqlite2", apr_dbd_sqlite2_driver, pool);
135 DRIVER_LOAD("oracle", apr_dbd_oracle_driver, pool);
138 DRIVER_LOAD("freetds", apr_dbd_freetds_driver, pool);
141 DRIVER_LOAD("odbc", apr_dbd_odbc_driver, pool);
143 #if APU_HAVE_SOME_OTHER_BACKEND
144 DRIVER_LOAD("firebird", apr_dbd_other_driver, pool);
146 #endif /* APU_DSO_BUILD */
148 apr_pool_cleanup_register(pool, NULL, apr_dbd_term,
149 apr_pool_cleanup_null);
151 apr_atomic_dec32(&in_init);
156 APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name,
157 const apr_dbd_driver_t **driver)
162 apr_dso_handle_sym_t symbol;
167 rv = apu_dso_mutex_lock();
172 *driver = apr_hash_get(drivers, name, APR_HASH_KEY_STRING);
175 apu_dso_mutex_unlock();
181 /* The driver DSO must have exactly the same lifetime as the
182 * drivers hash table; ignore the passed-in pool */
183 pool = apr_hash_pool_get(drivers);
186 apr_snprintf(modname, sizeof(modname), "dbd%s.nlm", name);
188 apr_snprintf(modname, sizeof(modname),
189 "apr_dbd_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".dll", name);
191 apr_snprintf(modname, sizeof(modname),
192 "apr_dbd_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".so", name);
194 apr_snprintf(symname, sizeof(symname), "apr_dbd_%s_driver", name);
195 rv = apu_dso_load(NULL, &symbol, modname, symname, pool);
196 if (rv == APR_SUCCESS || rv == APR_EINIT) { /* previously loaded?!? */
198 name = apr_pstrdup(pool, name);
199 apr_hash_set(drivers, name, APR_HASH_KEY_STRING, *driver);
201 if ((*driver)->init) {
202 (*driver)->init(pool);
205 apu_dso_mutex_unlock();
207 #else /* not builtin and !APR_HAS_DSO => not implemented */
214 APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver,
215 apr_pool_t *pool, const char *params,
220 *handle = (driver->open)(pool, params, error);
221 if (*handle == NULL) {
224 rv = apr_dbd_check_conn(driver, pool, *handle);
225 if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
226 /* XXX: rv is APR error code, but apr_dbd_error() takes int! */
228 *error = apr_dbd_error(driver, *handle, rv);
230 apr_dbd_close(driver, *handle);
236 APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver,
237 apr_pool_t *pool, const char *params,
240 return apr_dbd_open_ex(driver,pool,params,handle,NULL);
243 APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver,
244 apr_pool_t *pool, apr_dbd_t *handle,
245 apr_dbd_transaction_t **trans)
247 int ret = driver->start_transaction(pool, handle, trans);
249 apr_pool_cleanup_register(pool, *trans,
250 CLEANUP_CAST driver->end_transaction,
251 apr_pool_cleanup_null);
256 APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver,
258 apr_dbd_transaction_t *trans)
260 apr_pool_cleanup_kill(pool, trans, CLEANUP_CAST driver->end_transaction);
261 return driver->end_transaction(trans);
264 APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver,
265 apr_dbd_transaction_t *trans)
267 return driver->transaction_mode_get(trans);
270 APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver,
271 apr_dbd_transaction_t *trans,
274 return driver->transaction_mode_set(trans, mode);
277 APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver,
280 return driver->close(handle);
283 APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver)
288 APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver,
291 return driver->native_handle(handle);
294 APU_DECLARE(int) apr_dbd_check_conn(const apr_dbd_driver_t *driver,
298 return driver->check_conn(pool, handle);
301 APU_DECLARE(int) apr_dbd_set_dbname(const apr_dbd_driver_t *driver,
303 apr_dbd_t *handle, const char *name)
305 return driver->set_dbname(pool,handle,name);
308 APU_DECLARE(int) apr_dbd_query(const apr_dbd_driver_t *driver,
310 int *nrows, const char *statement)
312 return driver->query(handle,nrows,statement);
315 APU_DECLARE(int) apr_dbd_select(const apr_dbd_driver_t *driver,
317 apr_dbd_t *handle, apr_dbd_results_t **res,
318 const char *statement, int random)
320 return driver->select(pool,handle,res,statement,random);
323 APU_DECLARE(int) apr_dbd_num_cols(const apr_dbd_driver_t *driver,
324 apr_dbd_results_t *res)
326 return driver->num_cols(res);
329 APU_DECLARE(int) apr_dbd_num_tuples(const apr_dbd_driver_t *driver,
330 apr_dbd_results_t *res)
332 return driver->num_tuples(res);
335 APU_DECLARE(int) apr_dbd_get_row(const apr_dbd_driver_t *driver,
337 apr_dbd_results_t *res, apr_dbd_row_t **row,
340 return driver->get_row(pool,res,row,rownum);
343 APU_DECLARE(const char*) apr_dbd_get_entry(const apr_dbd_driver_t *driver,
344 apr_dbd_row_t *row, int col)
346 return driver->get_entry(row,col);
349 APU_DECLARE(const char*) apr_dbd_get_name(const apr_dbd_driver_t *driver,
350 apr_dbd_results_t *res, int col)
352 return driver->get_name(res,col);
355 APU_DECLARE(const char*) apr_dbd_error(const apr_dbd_driver_t *driver,
356 apr_dbd_t *handle, int errnum)
358 return driver->error(handle,errnum);
361 APU_DECLARE(const char*) apr_dbd_escape(const apr_dbd_driver_t *driver,
362 apr_pool_t *pool, const char *string,
365 return driver->escape(pool,string,handle);
368 APU_DECLARE(int) apr_dbd_prepare(const apr_dbd_driver_t *driver,
370 apr_dbd_t *handle, const char *query,
372 apr_dbd_prepared_t **statement)
375 int i, nargs = 0, nvals = 0;
380 if (!driver->pformat) {
384 /* find the number of parameters in the query */
385 for (q = query; *q; q++) {
387 if (apr_isalpha(q[1])) {
389 } else if (q[1] == '%') {
396 qlen = strlen(query) +
397 nargs * (strlen(driver->pformat) + sizeof(nargs) * 3 + 2) + 1;
398 pq = apr_palloc(pool, qlen);
399 t = apr_pcalloc(pool, sizeof(*t) * nargs);
401 for (p = pq, q = query, i = 0; *q; q++) {
403 if (apr_isalpha(q[1])) {
405 case 'd': t[i] = APR_DBD_TYPE_INT; break;
406 case 'u': t[i] = APR_DBD_TYPE_UINT; break;
407 case 'f': t[i] = APR_DBD_TYPE_FLOAT; break;
412 case 'd': t[i] = APR_DBD_TYPE_TINY; q += 2; break;
413 case 'u': t[i] = APR_DBD_TYPE_UTINY; q += 2; break;
416 case 'd': t[i] = APR_DBD_TYPE_SHORT; q++; break;
417 case 'u': t[i] = APR_DBD_TYPE_USHORT; q++; break;
424 case 'd': t[i] = APR_DBD_TYPE_LONGLONG; q += 2; break;
425 case 'u': t[i] = APR_DBD_TYPE_ULONGLONG; q += 2; break;
428 case 'd': t[i] = APR_DBD_TYPE_LONG; q++; break;
429 case 'u': t[i] = APR_DBD_TYPE_ULONG; q++; break;
430 case 'f': t[i] = APR_DBD_TYPE_DOUBLE; q++; break;
436 case 't': t[i] = APR_DBD_TYPE_TEXT; q += 2; break;
437 case 'i': t[i] = APR_DBD_TYPE_TIME; q += 2; break;
438 case 'd': t[i] = APR_DBD_TYPE_DATE; q += 2; break;
439 case 'a': t[i] = APR_DBD_TYPE_DATETIME; q += 2; break;
440 case 's': t[i] = APR_DBD_TYPE_TIMESTAMP; q += 2; break;
441 case 'z': t[i] = APR_DBD_TYPE_ZTIMESTAMP; q += 2; break;
442 case 'b': t[i] = APR_DBD_TYPE_BLOB; q += 2; break;
443 case 'c': t[i] = APR_DBD_TYPE_CLOB; q += 2; break;
444 case 'n': t[i] = APR_DBD_TYPE_NULL; q += 2; break;
452 case APR_DBD_TYPE_NONE: /* by default, we expect strings */
453 t[i] = APR_DBD_TYPE_STRING;
455 case APR_DBD_TYPE_BLOB:
456 case APR_DBD_TYPE_CLOB: /* three (3) more values passed in */
463 /* insert database specific parameter reference */
464 p += apr_snprintf(p, qlen - (p - pq), driver->pformat, ++i);
465 } else if (q[1] == '%') { /* reduce %% to % */
476 return driver->prepare(pool,handle,pq,label,nargs,nvals,t,statement);
479 APU_DECLARE(int) apr_dbd_pquery(const apr_dbd_driver_t *driver,
481 apr_dbd_t *handle, int *nrows,
482 apr_dbd_prepared_t *statement,
483 int nargs, const char **args)
485 return driver->pquery(pool,handle,nrows,statement,args);
488 APU_DECLARE(int) apr_dbd_pselect(const apr_dbd_driver_t *driver,
490 apr_dbd_t *handle, apr_dbd_results_t **res,
491 apr_dbd_prepared_t *statement, int random,
492 int nargs, const char **args)
494 return driver->pselect(pool,handle,res,statement,random,args);
497 APU_DECLARE_NONSTD(int) apr_dbd_pvquery(const apr_dbd_driver_t *driver,
499 apr_dbd_t *handle, int *nrows,
500 apr_dbd_prepared_t *statement, ...)
504 va_start(args, statement);
505 ret = driver->pvquery(pool,handle,nrows,statement,args);
510 APU_DECLARE_NONSTD(int) apr_dbd_pvselect(const apr_dbd_driver_t *driver,
511 apr_pool_t *pool, apr_dbd_t *handle,
512 apr_dbd_results_t **res,
513 apr_dbd_prepared_t *statement,
518 va_start(args, random);
519 ret = driver->pvselect(pool,handle,res,statement,random,args);
524 APU_DECLARE(int) apr_dbd_pbquery(const apr_dbd_driver_t *driver,
526 apr_dbd_t *handle, int *nrows,
527 apr_dbd_prepared_t *statement,
530 return driver->pbquery(pool,handle,nrows,statement,args);
533 APU_DECLARE(int) apr_dbd_pbselect(const apr_dbd_driver_t *driver,
535 apr_dbd_t *handle, apr_dbd_results_t **res,
536 apr_dbd_prepared_t *statement, int random,
539 return driver->pbselect(pool,handle,res,statement,random,args);
542 APU_DECLARE_NONSTD(int) apr_dbd_pvbquery(const apr_dbd_driver_t *driver,
544 apr_dbd_t *handle, int *nrows,
545 apr_dbd_prepared_t *statement, ...)
549 va_start(args, statement);
550 ret = driver->pvbquery(pool,handle,nrows,statement,args);
555 APU_DECLARE_NONSTD(int) apr_dbd_pvbselect(const apr_dbd_driver_t *driver,
556 apr_pool_t *pool, apr_dbd_t *handle,
557 apr_dbd_results_t **res,
558 apr_dbd_prepared_t *statement,
563 va_start(args, random);
564 ret = driver->pvbselect(pool,handle,res,statement,random,args);
569 APU_DECLARE(apr_status_t) apr_dbd_datum_get(const apr_dbd_driver_t *driver,
570 apr_dbd_row_t *row, int col,
571 apr_dbd_type_e type, void *data)
573 return driver->datum_get(row,col,type,data);