/* ============================================================================= db:close(): Closes an open database connection. ============================================================================= */ int lua_db_close(lua_State *L) { /*~~~~~~~~~~~~~~~~~~~~*/ lua_db_handle *db; apr_status_t rc = 0; /*~~~~~~~~~~~~~~~~~~~~*/ db = lua_get_db_handle(L); if (db && db->alive) { if (db->type == LUA_DBTYPE_APR_DBD) { rc = apr_dbd_close(db->driver, db->handle); if (db->pool) apr_pool_destroy(db->pool); } else { lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close); if (lua_ap_dbd_close != NULL) if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle); } db->driver = NULL; db->handle = NULL; db->alive = 0; db->pool = NULL; } lua_settop(L, 0); lua_pushnumber(L, rc); return 1; }
/* ============================================================================= db:__gc(): Garbage collecting function. ============================================================================= */ int lua_db_gc(lua_State *L) { /*~~~~~~~~~~~~~~~~*/ lua_db_handle *db; /*~~~~~~~~~~~~~~~~~~~~*/ db = lua_touserdata(L, 1); if (db && db->alive) { if (db->type == LUA_DBTYPE_APR_DBD) { apr_dbd_close(db->driver, db->handle); if (db->pool) apr_pool_destroy(db->pool); } else { lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close); if (lua_ap_dbd_close != NULL) if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle); } db->driver = NULL; db->handle = NULL; db->alive = 0; db->pool = NULL; } lua_settop(L, 0); return 0; }
static void test_dbd_generic(abts_case *tc, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { void* native; apr_pool_t *pool = p; apr_status_t rv; native = apr_dbd_native_handle(driver, handle); ABTS_PTR_NOTNULL(tc, native); rv = apr_dbd_check_conn(driver, pool, handle); create_table(tc, handle, driver); select_rows(tc, handle, driver, 0); insert_data(tc, handle, driver, 5); select_rows(tc, handle, driver, 5); delete_rows(tc, handle, driver); select_rows(tc, handle, driver, 0); drop_table(tc, handle, driver); test_escape(tc, handle, driver); rv = apr_dbd_close(driver, handle); ABTS_ASSERT(tc, "failed to close database", rv == 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) { apr_status_t rv; *handle = driver->open(pool, params); if (*handle == NULL) { return APR_EGENERAL; } rv = apr_dbd_check_conn(driver, pool, *handle); if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { apr_dbd_close(driver, *handle); return APR_EGENERAL; } return APR_SUCCESS; }
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; }
int main(int argc, char** argv) { const char *name; const char *params; apr_pool_t *pool = NULL; apr_dbd_t *sql = NULL; const apr_dbd_driver_t *driver = NULL; int rv; apr_initialize(); apr_pool_create(&pool, NULL); if (argc >= 2 && argc <= 3) { name = argv[1]; params = ( argc == 3 ) ? argv[2] : ""; apr_dbd_init(pool); setbuf(stdout,NULL); rv = apr_dbd_get_driver(pool, name, &driver); switch (rv) { case APR_SUCCESS: printf("Loaded %s driver OK.\n", name); break; case APR_EDSOOPEN: printf("Failed to load driver file apr_dbd_%s.so\n", name); goto finish; case APR_ESYMNOTFOUND: printf("Failed to load driver apr_dbd_%s_driver.\n", name); goto finish; case APR_ENOTIMPL: printf("No driver available for %s.\n", name); goto finish; default: /* it's a bug if none of the above happen */ printf("Internal error loading %s.\n", name); goto finish; } rv = apr_dbd_open(driver, pool, params, &sql); switch (rv) { case APR_SUCCESS: printf("Opened %s[%s] OK\n", name, params); break; case APR_EGENERAL: printf("Failed to open %s[%s]\n", name, params); goto finish; default: /* it's a bug if none of the above happen */ printf("Internal error opening %s[%s]\n", name, params); goto finish; } TEST("create table", create_table); TEST("insert rows", insert_rows); TEST("invalid op", invalid_op); TEST("select random", select_random); TEST("select sequential", select_sequential); TEST("transactions", test_transactions); TEST("prepared select", test_pselect); TEST("prepared query", test_pquery); TEST("drop table", drop_table); apr_dbd_close(driver, sql); } else { fprintf(stderr, "Usage: %s driver-name [params]\n", argv[0]); } finish: apr_pool_destroy(pool); apr_terminate(); return 0; }
void AprHandleDeleter::operator()(apr_dbd_t* handle) { if (driver != nullptr && handle != nullptr) { apr_dbd_close(driver, handle); } }