/* ============================================================================= db:escape(string): Escapes a string for safe use in the given database type. ============================================================================= */ int lua_db_escape(lua_State *L) { /*~~~~~~~~~~~~~~~~~~~~~*/ lua_db_handle *db = 0; const char *statement; const char *escaped = 0; request_rec *r; /*~~~~~~~~~~~~~~~~~~~~~*/ r = ap_lua_check_request_rec(L, 2); if (r) { luaL_checktype(L, 3, LUA_TSTRING); statement = lua_tostring(L, 3); db = lua_get_db_handle(L); if (db && db->alive) { apr_dbd_init(r->pool); escaped = apr_dbd_escape(db->driver, r->pool, statement, db->handle); if (escaped) { lua_pushstring(L, escaped); return 1; } } else { lua_pushnil(L); } return (1); } return 0; }
static void test_dbd_init(abts_case *tc, void *data) { apr_pool_t *pool = p; apr_status_t rv; rv = apr_dbd_init(pool); ABTS_ASSERT(tc, "failed to init apr_dbd", rv == APR_SUCCESS); }
int db_manager_init (apr_pool_t* pool, error_messages_t* error_messages, const char* db_xml_config_dir, db_manager_t** db_manager_ptr) { apr_status_t rv; db_manager_t* db_manager = *db_manager_ptr = apr_pcalloc(pool, sizeof(db_manager_t)); //Initialize APR Database system rv = apr_dbd_init(pool); if (rv != APR_SUCCESS){ // ap_log_error(APLOG_MARK, APLOG_ERR, status, s, "Failed to initialize APR DBD."); return rv; } db_manager->db_config_list = apr_pcalloc(pool, sizeof(db_config_list_t)); return 0; }
/* ============================================================================= dbacquire(dbType, dbString): Opens a new connection to a database of type _dbType_ and with the connection parameters _dbString_. If successful, returns a table with functions for using the database handle. If an error occurs, returns nil as the first parameter and the error message as the second. See the APR_DBD for a list of database types and connection strings supported. ============================================================================= */ AP_LUA_DECLARE(int) lua_db_acquire(lua_State *L) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ const char *type; const char *arguments; const char *error = 0; request_rec *r; lua_db_handle *db = 0; apr_status_t rc = 0; ap_dbd_t *dbdhandle = NULL; apr_pool_t *pool = NULL; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ r = ap_lua_check_request_rec(L, 1); if (r) { type = luaL_optstring(L, 2, "mod_dbd"); /* Defaults to mod_dbd */ if (!strcmp(type, "mod_dbd")) { lua_settop(L, 0); lua_ap_dbd_open = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_open); if (lua_ap_dbd_open) dbdhandle = (ap_dbd_t *) lua_ap_dbd_open( r->server->process->pool, r->server); if (dbdhandle) { db = lua_push_db_handle(L, r, LUA_DBTYPE_MOD_DBD, dbdhandle->pool); db->driver = dbdhandle->driver; db->handle = dbdhandle->handle; db->dbdhandle = dbdhandle; return 1; } else { lua_pushnil(L); if ( lua_ap_dbd_open == NULL ) lua_pushliteral(L, "mod_dbd doesn't seem to have been loaded."); else lua_pushliteral( L, "Could not acquire connection from mod_dbd. If your database is running, this may indicate a permission problem."); return 2; } } else { rc = apr_pool_create(&pool, NULL); if (rc != APR_SUCCESS) { lua_pushnil(L); lua_pushliteral(L, "Could not allocate memory for database!"); return 2; } apr_pool_tag(pool, "lua_dbd_pool"); apr_dbd_init(pool); dbdhandle = apr_pcalloc(pool, sizeof(ap_dbd_t)); rc = apr_dbd_get_driver(pool, type, &dbdhandle->driver); if (rc == APR_SUCCESS) { luaL_checktype(L, 3, LUA_TSTRING); arguments = lua_tostring(L, 3); lua_settop(L, 0); if (strlen(arguments)) { rc = apr_dbd_open_ex(dbdhandle->driver, pool, arguments, &dbdhandle->handle, &error); if (rc == APR_SUCCESS) { db = lua_push_db_handle(L, r, LUA_DBTYPE_APR_DBD, pool); db->driver = dbdhandle->driver; db->handle = dbdhandle->handle; db->dbdhandle = dbdhandle; return 1; } else { lua_pushnil(L); if (error) { lua_pushstring(L, error); return 2; } return 1; } } lua_pushnil(L); lua_pushliteral(L, "No database connection string was specified."); apr_pool_destroy(pool); return (2); } else { lua_pushnil(L); if (APR_STATUS_IS_ENOTIMPL(rc)) { lua_pushfstring(L, "driver for %s not available", type); } else if (APR_STATUS_IS_EDSOOPEN(rc)) { lua_pushfstring(L, "can't find driver for %s", type); } else if (APR_STATUS_IS_ESYMNOTFOUND(rc)) { lua_pushfstring(L, "driver for %s is invalid or corrupted", type); } else { lua_pushliteral(L, "mod_lua not compatible with APR in get_driver"); } lua_pushinteger(L, rc); apr_pool_destroy(pool); return 3; } } lua_pushnil(L); return 1; } return 0; }
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; }