Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/lua/sqlite A Lua module to access SQLite databases.
details: https://anonhg.NetBSD.org/src/rev/299037b282fe
branches: trunk
changeset: 770385:299037b282fe
user: mbalmer <mbalmer%NetBSD.org@localhost>
date: Sat Oct 15 10:26:30 2011 +0000
description:
A Lua module to access SQLite databases.
diffstat:
lib/lua/sqlite/Makefile | 8 +
lib/lua/sqlite/sqlite.c | 481 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 489 insertions(+), 0 deletions(-)
diffs (truncated from 497 to 300 lines):
diff -r 3eb1fbfda595 -r 299037b282fe lib/lua/sqlite/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/lua/sqlite/Makefile Sat Oct 15 10:26:30 2011 +0000
@@ -0,0 +1,8 @@
+# $NetBSD: Makefile,v 1.1 2011/10/15 10:26:30 mbalmer Exp $
+
+LUA_MODULES= sqlite
+LUA_SRCS.sqlite= sqlite.c
+
+LDADD+= -lsqlite3
+
+.include <bsd.lua.mk>
diff -r 3eb1fbfda595 -r 299037b282fe lib/lua/sqlite/sqlite.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/lua/sqlite/sqlite.c Sat Oct 15 10:26:30 2011 +0000
@@ -0,0 +1,481 @@
+/* $NetBSD: sqlite.c,v 1.1 2011/10/15 10:26:30 mbalmer Exp $ */
+
+/*
+ * Copyright (c) 2011 Marc Balmer <marc%msys.ch@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* SQLite interface for Lua */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <sqlite3.h>
+#include <unistd.h>
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+#define SQLITE_DB_METATABLE "SQLite database connection methods"
+#define SQLITE_STMT_METATABLE "SQLite statement methods"
+
+int luaopen_sqlite(lua_State*);
+
+static void
+sqlite_error(lua_State *L, const char *fmt, ...)
+{
+ va_list ap;
+ int len;
+ char *msg;
+
+ va_start(ap, fmt);
+ len = vasprintf(&msg, fmt, ap);
+ va_end(ap);
+
+ if (len != -1) {
+ lua_pushstring(L, msg);
+ free(msg);
+ } else
+ lua_pushstring(L, "vasprintf failed");
+ lua_error(L);
+}
+
+static int
+sqlite_initialize(lua_State *L)
+{
+ lua_pushinteger(L, sqlite3_initialize());
+ return 1;
+}
+
+static int
+sqlite_shutdown(lua_State *L)
+{
+ lua_pushinteger(L, sqlite3_shutdown());
+ return 1;
+}
+
+static int
+sqlite_open(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = lua_newuserdata(L, sizeof(sqlite3 *));
+ lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db,
+ (int)luaL_checkinteger(L, -2), NULL));
+
+ luaL_getmetatable(L, SQLITE_DB_METATABLE);
+ lua_setmetatable(L, -3);
+ return 2;
+
+}
+
+static int
+sqlite_libversion(lua_State *L)
+{
+ lua_pushstring(L, sqlite3_libversion());
+ return 1;
+}
+
+static int
+sqlite_libversion_number(lua_State *L)
+{
+ lua_pushinteger(L, sqlite3_libversion_number());
+ return 1;
+}
+
+static int
+sqlite_sourceid(lua_State *L)
+{
+ lua_pushstring(L, sqlite3_sourceid());
+ return 1;
+}
+
+static int
+db_close(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ lua_pushinteger(L, sqlite3_close(*db));
+ return 1;
+
+}
+
+static int
+db_prepare(lua_State *L)
+{
+ sqlite3 **db;
+ sqlite3_stmt **stmt;
+ const char *sql;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *));
+ sql = luaL_checkstring(L, 2);
+ lua_pushinteger(L, sqlite3_prepare_v2(*db, sql,
+ (int)strlen(sql) + 1, stmt, NULL));
+ luaL_getmetatable(L, SQLITE_STMT_METATABLE);
+ lua_setmetatable(L, -3);
+ return 2;
+
+}
+
+static int
+db_exec(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL,
+ NULL, NULL));
+ return 1;
+}
+
+static int
+db_errcode(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ lua_pushinteger(L, sqlite3_errcode(*db));
+ return 1;
+}
+
+static int
+db_errmsg(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ lua_pushstring(L, sqlite3_errmsg(*db));
+ return 1;
+}
+
+static int
+db_get_autocommit(lua_State *L)
+{
+ sqlite3 **db;
+
+ db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
+ lua_pushboolean(L, sqlite3_get_autocommit(*db));
+ return 1;
+}
+
+static int
+stmt_bind(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+ int pidx;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ pidx = (int)luaL_checkinteger(L, 2);
+
+ switch (lua_type(L, 3)) {
+ case LUA_TNUMBER:
+ lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx,
+ lua_tonumber(L, 3)));
+ break;
+ case LUA_TSTRING:
+ lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx,
+ lua_tostring(L, 3), -1, NULL));
+ break;
+ case LUA_TNIL:
+ lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx));
+ break;
+ default:
+ sqlite_error(L, "unsupported data type %s",
+ luaL_typename(L, 3));
+ }
+ return 1;
+}
+
+static int
+stmt_bind_parameter_count(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
+ return 1;
+}
+
+static int
+stmt_bind_parameter_index(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt,
+ lua_tostring(L, 2)));
+ return 1;
+}
+
+static int
+stmt_bind_parameter_name(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+ int pidx;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ pidx = (int)luaL_checkinteger(L, 2);
+ lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx));
+ return 1;
+}
+
+static int
+stmt_step(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ lua_pushinteger(L, sqlite3_step(*stmt));
+ return 1;
+}
+
+static int
+stmt_column_name(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+ int cidx;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ cidx = (int)luaL_checkinteger(L, 2) - 1;
+
+ lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
+ return 1;
+}
+
+static int
+stmt_column_count(lua_State *L)
+{
+ sqlite3_stmt **stmt;
+
+ stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
+ lua_pushinteger(L, sqlite3_column_count(*stmt));
+ return 1;
+}
+
+static int
+stmt_column(lua_State *L)
+{
Home |
Main Index |
Thread Index |
Old Index