lua_dbd.c revision 822bf3c5fe5ae502a7327369e54805581ce60ba5
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner * Licensed to the Apache Software Foundation (ASF) under one or more
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * contributor license agreements. See the NOTICE file distributed with
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * this work for additional information regarding copyright ownership.
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * The ASF licenses this file to You under the Apache License, Version 2.0
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * (the "License"); you may not use this file except in compliance with
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * the License. You may obtain a copy of the License at
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * http://www.apache.org/licenses/LICENSE-2.0
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova * Unless required by applicable law or agreed to in writing, software
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * distributed under the License is distributed on an "AS IS" BASIS,
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * See the License for the specific language governing permissions and
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova * limitations under the License.
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakovastatic APR_OPTIONAL_FN_TYPE(ap_dbd_close) *lua_ap_dbd_close = NULL;
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakovastatic APR_OPTIONAL_FN_TYPE(ap_dbd_open) *lua_ap_dbd_open = NULL;
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakovastatic request_rec *ap_lua_check_request_rec(lua_State *L, int index)
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova luaL_checkudata(L, index, "Apache2.Request");
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakovastatic lua_db_handle *lua_get_db_handle(lua_State *L)
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova return (lua_db_handle *) lua_topointer(L, -1);
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakovastatic lua_db_result_set *lua_get_result_set(lua_State *L)
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova return (lua_db_result_set *) lua_topointer(L, -1);
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova =============================================================================
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova db:close(): Closes an open database connection.
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova =============================================================================
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~*/
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova /*~~~~~~~~~~~~~~~~~~~~*/
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova rc = apr_dbd_close(db->driver, db->handle);
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close);
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if (db->pool) apr_pool_destroy(db->pool);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder =============================================================================
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova db:__gc(): Garbage collecting function.
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova =============================================================================
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova /*~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder /*~~~~~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder if (db->type == LUA_DBTYPE_APR_DBD) {
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder apr_dbd_close(db->driver, db->handle);
3c0bf20712a0f21aaedc0a9a9c8376bc1e90e799Kristina Sojakova lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close);
3c0bf20712a0f21aaedc0a9a9c8376bc1e90e799Kristina Sojakova if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle);
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova =============================================================================
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova db:active(): Returns true if the connection to the db is still active.
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova =============================================================================
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder /*~~~~~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder rc = apr_dbd_check_conn(db->driver, db->pool, db->handle);
3c0bf20712a0f21aaedc0a9a9c8376bc1e90e799Kristina Sojakova =============================================================================
3c0bf20712a0f21aaedc0a9a9c8376bc1e90e799Kristina Sojakova db:query(statement): Executes the given database query and returns the
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova number of rows affected. If an error is encountered, returns nil as the
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder first parameter and the error message as the second.
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova =============================================================================
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder /*~~~~~~~~~~~~~~~~~~~~~~~*/
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder rc = apr_dbd_query(db->driver, db->handle, &x, statement);
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova const char *err = apr_dbd_error(db->driver, db->handle, rc);
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova =============================================================================
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova db:escape(string): Escapes a string for safe use in the given database type.
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova =============================================================================
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova /*~~~~~~~~~~~~~~~~~~~~~*/
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova const char *escaped = 0;
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova /*~~~~~~~~~~~~~~~~~~~~~*/
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova escaped = apr_dbd_escape(db->driver, r->pool, statement,
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder =============================================================================
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder resultset(N): Fetches one or more rows from a result set.
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova =============================================================================
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova lua_db_result_set *res = lua_get_result_set(L);
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova /* Fetch all rows at once? */
lua_newtable(L);
lua_newtable(L);
if (entry) {
row_no++;
lua_newtable(L);
if (entry) {
const char *statement;
request_rec *r;
int cols;
if (cols > 0) {
lua_newtable(L);
lua_pushnil(L);
if (err) {
lua_pushboolean(L, 0);
const char **vars;
int x, have;
lua_pushboolean(L, 0);
for (x = 0; x < have; x++) {
int cols;
lua_newtable(L);
lua_pushnil(L);
if (err) {
lua_pushboolean(L, 0);
const char **vars;
int x, have;
lua_pushboolean(L, 0);
for (x = 0; x < have; x++) {
int affected = 0;
lua_pushnil(L);
if (err) {
lua_pushboolean(L, 0);
request_rec *r;
int need = 0;
at++;
need++;
lua_pushnil(L);
if (err) {
lua_newtable(L);
const char *tag;
request_rec *r;
lua_pushnil(L);
lua_newtable(L);
lua_newtable(L);
return db;
const char *type;
const char *arguments;
const char *error = 0;
request_rec *r;
lua_settop(L, 0);
if (lua_ap_dbd_open)
if (dbdhandle) {
lua_pushnil(L);
"Could not acquire connection from mod_dbd. If your database is running, this may indicate a permission problem.");
lua_pushnil(L);
lua_settop(L, 0);
lua_pushnil(L);
if (error) {
lua_pushnil(L);
lua_pushnil(L);
type);
lua_pushnil(L);