📄 apr_dbd.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include <ctype.h>#include <stdio.h>#include "apu_config.h"#include "apu.h"#include "apr_pools.h"#include "apr_dso.h"#include "apr_strings.h"#include "apr_hash.h"#include "apr_thread_mutex.h"#include "apr_lib.h"#include "apu_internal.h"#include "apr_dbd_internal.h"#include "apr_dbd.h"#include "apu_version.h"static apr_hash_t *drivers = NULL;#define CLEANUP_CAST (apr_status_t (*)(void*))#if APR_HAS_THREADS/* deprecated, but required for existing providers. Existing and new * providers should be refactored to use a provider-specific mutex so * that different providers do not block one another. * In APR 1.3 this is no longer used for dso module loading, and * apu_dso_mutex_[un]lock is used instead. * In APR 2.0 this should become entirely local to libaprutil-2.so and * no longer be exported. */static apr_thread_mutex_t* mutex = NULL;APU_DECLARE(apr_status_t) apr_dbd_mutex_lock(){ return apr_thread_mutex_lock(mutex);}APU_DECLARE(apr_status_t) apr_dbd_mutex_unlock(){ return apr_thread_mutex_unlock(mutex);}#elseAPU_DECLARE(apr_status_t) apr_dbd_mutex_lock() { return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_dbd_mutex_unlock() { return APR_SUCCESS;}#endif#if !APU_DSO_BUILD#define DRIVER_LOAD(name,driver,pool) \ { \ extern const apr_dbd_driver_t driver; \ apr_hash_set(drivers,name,APR_HASH_KEY_STRING,&driver); \ if (driver.init) { \ driver.init(pool); \ } \ }#endifstatic apr_status_t apr_dbd_term(void *ptr){ /* set drivers to NULL so init can work again */ drivers = NULL; /* Everything else we need is handled by cleanups registered * when we created mutexes and loaded DSOs */ return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_dbd_init(apr_pool_t *pool){ apr_status_t ret = APR_SUCCESS; apr_pool_t *parent; if (drivers != NULL) { return APR_SUCCESS; } /* Top level pool scope, need process-scope lifetime */ for (parent = pool; parent; parent = apr_pool_parent_get(pool)) pool = parent;#if APU_DSO_BUILD /* deprecate in 2.0 - permit implicit initialization */ apu_dso_init(pool);#endif drivers = apr_hash_make(pool);#if APR_HAS_THREADS ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool); /* This already registers a pool cleanup */#endif#if !APU_DSO_BUILD /* Load statically-linked drivers: */#if APU_HAVE_MYSQL DRIVER_LOAD("mysql", apr_dbd_mysql_driver, pool);#endif#if APU_HAVE_PGSQL DRIVER_LOAD("pgsql", apr_dbd_pgsql_driver, pool);#endif#if APU_HAVE_SQLITE3 DRIVER_LOAD("sqlite3", apr_dbd_sqlite3_driver, pool);#endif#if APU_HAVE_SQLITE2 DRIVER_LOAD("sqlite2", apr_dbd_sqlite2_driver, pool);#endif#if APU_HAVE_ORACLE DRIVER_LOAD("oracle", apr_dbd_oracle_driver, pool);#endif#if APU_HAVE_FREETDS DRIVER_LOAD("freetds", apr_dbd_freetds_driver, pool);#endif#if APU_HAVE_ODBC DRIVER_LOAD("odbc", apr_dbd_odbc_driver, pool);#endif#if APU_HAVE_SOME_OTHER_BACKEND DRIVER_LOAD("firebird", apr_dbd_other_driver, pool);#endif#endif /* APU_DSO_BUILD */ apr_pool_cleanup_register(pool, NULL, apr_dbd_term, apr_pool_cleanup_null); return ret;}APU_DECLARE(apr_status_t) apr_dbd_get_driver(apr_pool_t *pool, const char *name, const apr_dbd_driver_t **driver){#if APU_DSO_BUILD char modname[32]; char symname[34]; apr_dso_handle_sym_t symbol;#endif apr_status_t rv;#if APU_DSO_BUILD rv = apu_dso_mutex_lock(); if (rv) { return rv; }#endif *driver = apr_hash_get(drivers, name, APR_HASH_KEY_STRING); if (*driver) {#if APU_DSO_BUILD apu_dso_mutex_unlock();#endif return APR_SUCCESS; }#if APU_DSO_BUILD /* The driver DSO must have exactly the same lifetime as the * drivers hash table; ignore the passed-in pool */ pool = apr_hash_pool_get(drivers);#if defined(NETWARE) apr_snprintf(modname, sizeof(modname), "dbd%s.nlm", name);#elif defined(WIN32) apr_snprintf(modname, sizeof(modname), "apr_dbd_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".dll", name);#else apr_snprintf(modname, sizeof(modname), "apr_dbd_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".so", name);#endif apr_snprintf(symname, sizeof(symname), "apr_dbd_%s_driver", name); rv = apu_dso_load(&symbol, modname, symname, pool); if (rv != APR_SUCCESS) { /* APR_EDSOOPEN or APR_ESYMNOTFOUND? */ if (rv == APR_EINIT) { /* previously loaded?!? */ name = apr_pstrdup(pool, name); apr_hash_set(drivers, name, APR_HASH_KEY_STRING, *driver); rv = APR_SUCCESS; } goto unlock; } *driver = symbol; if ((*driver)->init) { (*driver)->init(pool); } name = apr_pstrdup(pool, name); apr_hash_set(drivers, name, APR_HASH_KEY_STRING, *driver);unlock: apu_dso_mutex_unlock();#else /* not builtin and !APR_HAS_DSO => not implemented */ rv = APR_ENOTIMPL;#endif return rv;}APU_DECLARE(apr_status_t) apr_dbd_open_ex(const apr_dbd_driver_t *driver, apr_pool_t *pool, const char *params, apr_dbd_t **handle, const char **error){ apr_status_t rv; *handle = (driver->open)(pool, params, error); if (*handle == NULL) { return APR_EGENERAL; } rv = apr_dbd_check_conn(driver, pool, *handle); if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { /* XXX: rv is APR error code, but apr_dbd_error() takes int! */ if (error) { *error = apr_dbd_error(driver, *handle, rv); } apr_dbd_close(driver, *handle); return APR_EGENERAL; } return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver, apr_pool_t *pool, const char *params, apr_dbd_t **handle){ return apr_dbd_open_ex(driver,pool,params,handle,NULL);}APU_DECLARE(int) apr_dbd_transaction_start(const apr_dbd_driver_t *driver, apr_pool_t *pool, apr_dbd_t *handle, apr_dbd_transaction_t **trans){ int ret = driver->start_transaction(pool, handle, trans); if (*trans) { apr_pool_cleanup_register(pool, *trans, CLEANUP_CAST driver->end_transaction, apr_pool_cleanup_null); } return ret;}APU_DECLARE(int) apr_dbd_transaction_end(const apr_dbd_driver_t *driver, apr_pool_t *pool, apr_dbd_transaction_t *trans){ apr_pool_cleanup_kill(pool, trans, CLEANUP_CAST driver->end_transaction); return driver->end_transaction(trans);}APU_DECLARE(int) apr_dbd_transaction_mode_get(const apr_dbd_driver_t *driver, apr_dbd_transaction_t *trans){ return driver->transaction_mode_get(trans);}APU_DECLARE(int) apr_dbd_transaction_mode_set(const apr_dbd_driver_t *driver, apr_dbd_transaction_t *trans, int mode){ return driver->transaction_mode_set(trans, mode);}APU_DECLARE(apr_status_t) apr_dbd_close(const apr_dbd_driver_t *driver, apr_dbd_t *handle){ return driver->close(handle);}APU_DECLARE(const char*) apr_dbd_name(const apr_dbd_driver_t *driver){ return driver->name;}APU_DECLARE(void*) apr_dbd_native_handle(const apr_dbd_driver_t *driver, apr_dbd_t *handle){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -