/** * 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.
*/
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;
}
/* ============================================================================= db:active(): Returns true if the connection to the db is still active. =============================================================================
*/ int lua_db_active(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~*/
lua_db_handle *db = 0;
apr_status_t rc = 0; /*~~~~~~~~~~~~~~~~~~~~*/
db = lua_get_db_handle(L); if (db && db->alive) {
rc = apr_dbd_check_conn(db->driver, db->pool, db->handle); if (rc == APR_SUCCESS) {
lua_pushboolean(L, 1); return 1;
}
}
lua_pushboolean(L, 0); return 1;
}
/* ============================================================================= db:query(statement): Executes the given database query and returns the number of rows affected. If an error is encountered, returns nil as the first parameter and the error message as the second. =============================================================================
*/ int lua_db_query(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~*/
lua_db_handle *db = 0;
apr_status_t rc = 0; int x = 0; constchar *statement; /*~~~~~~~~~~~~~~~~~~~~~~~*/
luaL_checktype(L, 3, LUA_TSTRING);
statement = lua_tostring(L, 3);
db = lua_get_db_handle(L); if (db && db->alive)
rc = apr_dbd_query(db->driver, db->handle, &x, statement); else {
rc = 0;
x = -1;
}
if (rc == APR_SUCCESS)
lua_pushnumber(L, x); else {
lua_pushnil(L); if (err) {
lua_pushstring(L, err); return 2;
}
}
return 1;
}
/* ============================================================================= 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; constchar *statement; constchar *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;
}
/* ============================================================================= resultset(N): Fetches one or more rows from a result set. =============================================================================
*/ int lua_db_get_row(lua_State *L)
{ int row_no,x,alpha = 0; constchar *entry, *rowname;
apr_dbd_row_t *row = 0;
lua_db_result_set *res = lua_get_result_set(L);
/* ============================================================================= db:select(statement): Queries the database for the given statement and returns the rows/columns found as a table. If an error is encountered, returns nil as the first parameter and the error message as the second. =============================================================================
*/ int lua_db_select(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~*/
lua_db_handle *db = 0;
apr_status_t rc = 0; constchar *statement;
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) {
/* ============================================================================= statement:select(var1, var2, var3...): Injects variables into a prepared statement and returns the number of rows matching the query. =============================================================================
*/ int lua_db_prepared_select(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
lua_db_prepared_statement *st = 0;
apr_status_t rc = 0; constchar **vars; int x, have; /*~~~~~~~~~~~~~~~~~~~~~~~*/
/* Fetch the prepared statement and the vars passed */
luaL_checktype(L, 1, LUA_TTABLE);
lua_rawgeti(L, 1, 0);
luaL_checktype(L, -1, LUA_TUSERDATA);
st = (lua_db_prepared_statement*) lua_topointer(L, -1);
/* Check if we got enough variables passed on to us.
* This, of course, only works for prepared statements made through lua. */
have = lua_gettop(L) - 2; if (st->variables != -1 && have < st->variables ) {
lua_pushboolean(L, 0);
lua_pushfstring(L, "Error in executing prepared statement: Expected %d arguments, got %d.",
st->variables, have); return 2;
}
vars = apr_pcalloc(st->db->pool, have*sizeof(char *)); for (x = 0; x < have; x++) {
vars[x] = lua_tostring(L, x + 2);
}
/* Fire off the query */ if (st->db && st->db->alive) {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ int cols;
apr_dbd_results_t *results = 0; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
lua_pushboolean(L, 0);
lua_pushliteral(L, "Database connection seems to be closed, please reacquire it."); return (2);
}
/* ============================================================================= statement:query(var1, var2, var3...): Injects variables into a prepared statement and returns the number of rows affected. =============================================================================
*/ int lua_db_prepared_query(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
lua_db_prepared_statement *st = 0;
apr_status_t rc = 0; constchar **vars; int x, have; /*~~~~~~~~~~~~~~~~~~~~~~~*/
/* Fetch the prepared statement and the vars passed */
luaL_checktype(L, 1, LUA_TTABLE);
lua_rawgeti(L, 1, 0);
luaL_checktype(L, -1, LUA_TUSERDATA);
st = (lua_db_prepared_statement*) lua_topointer(L, -1);
/* Check if we got enough variables passed on to us.
* This, of course, only works for prepared statements made through lua. */
have = lua_gettop(L) - 2; if (st->variables != -1 && have < st->variables ) {
lua_pushboolean(L, 0);
lua_pushfstring(L, "Error in executing prepared statement: Expected %d arguments, got %d.",
st->variables, have); return 2;
}
vars = apr_pcalloc(st->db->pool, have*sizeof(char *)); for (x = 0; x < have; x++) {
vars[x] = lua_tostring(L, x + 2);
}
/* Fire off the query */ if (st->db && st->db->alive) {
/*~~~~~~~~~~~~~~*/ int affected = 0; /*~~~~~~~~~~~~~~*/
lua_pushboolean(L, 0);
lua_pushliteral(L, "Database connection seems to be closed, please reacquire it."); return (2);
}
/* ============================================================================= db:prepare(statement): Prepares a statement for later query/select. Returns a table with a :query and :select function, same as the db funcs. =============================================================================
*/ int lua_db_prepare(lua_State* L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
lua_db_handle *db = 0;
apr_status_t rc = 0; constchar *statement, *at;
request_rec *r;
lua_db_prepared_statement* st; int need = 0; /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
r = ap_lua_check_request_rec(L, 2); if (r) {
apr_dbd_prepared_t *pstatement = NULL;
luaL_checktype(L, 3, LUA_TSTRING);
statement = lua_tostring(L, 3);
/* Count number of variables in statement */
at = ap_strchr_c(statement,'%'); while (at != NULL) { if (at[1] == '%') {
at++;
} else {
need++;
}
at = ap_strchr_c(at+1,'%');
}
/* ============================================================================= 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. =============================================================================
*/ int lua_db_acquire(lua_State *L)
{ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ constchar *type; constchar *arguments; constchar *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 */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.