Sun, 23 Nov 2008 01:29:09 +0000
Initial import.
1 | 1 | #include "dbd_sqlite3.h" |
2 | ||
3 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query); | |
4 | ||
5 | static int connection_new(lua_State *L) { | |
6 | int n = lua_gettop(L); | |
7 | ||
8 | const char *db = NULL; | |
9 | connection_t *conn = NULL; | |
10 | ||
11 | /* db */ | |
12 | switch (n) { | |
13 | default: | |
14 | if (lua_isnil(L, 1) == 0) | |
15 | db = luaL_checkstring(L, 1); | |
16 | } | |
17 | ||
18 | conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); | |
19 | ||
20 | if (sqlite3_open(db, &conn->sqlite) == SQLITE_OK) { | |
21 | luaL_getmetatable(L, DBD_SQLITE_CONNECTION); | |
22 | lua_setmetatable(L, -2); | |
23 | } else { | |
24 | luaL_error(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite)); | |
25 | lua_pushnil(L); | |
26 | } | |
27 | ||
28 | return 1; | |
29 | } | |
30 | ||
31 | static int connection_close(lua_State *L) { | |
32 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); | |
33 | int disconnect = 0; | |
34 | ||
35 | if (conn->sqlite) { | |
36 | #if 0 | |
37 | sqlite3_stmt *stmt; | |
38 | ||
39 | while((stmt = sqlite3_next_stmt(conn->sqlite, NULL)) != 0){ | |
40 | sqlite3_finalize(stmt); | |
41 | } | |
42 | #endif | |
43 | if (sqlite3_close(conn->sqlite) == SQLITE_OK) { | |
44 | disconnect = 1; | |
45 | } | |
46 | } | |
47 | ||
48 | lua_pushboolean(L, disconnect); | |
49 | return 1; | |
50 | } | |
51 | ||
52 | static int connection_ping(lua_State *L) { | |
53 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); | |
54 | int ok = 0; | |
55 | ||
56 | if (conn->sqlite) { | |
57 | ok = 1; | |
58 | } | |
59 | ||
60 | lua_pushboolean(L, ok); | |
61 | return 1; | |
62 | } | |
63 | ||
64 | static int connection_prepare(lua_State *L) { | |
65 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); | |
66 | ||
67 | if (conn->sqlite) { | |
68 | return dbd_sqlite3_statement_create(L, conn, luaL_checkstring(L, 2)); | |
69 | } | |
70 | ||
71 | lua_pushnil(L); | |
72 | return 1; | |
73 | } | |
74 | ||
75 | ||
76 | static int connection_gc(lua_State *L) { | |
77 | /* always close the connection */ | |
78 | connection_close(L); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | static const luaL_Reg connection_methods[] = { | |
84 | {"close", connection_close}, | |
85 | {"ping", connection_ping}, | |
86 | {"prepare", connection_prepare}, | |
87 | {NULL, NULL} | |
88 | }; | |
89 | ||
90 | static const luaL_Reg connection_class_methods[] = { | |
91 | {"New", connection_new}, | |
92 | {NULL, NULL} | |
93 | }; | |
94 | ||
95 | int dbd_sqlite3_connection(lua_State *L) { | |
96 | luaL_newmetatable(L, DBD_SQLITE_CONNECTION); | |
97 | luaL_register(L, 0, connection_methods); | |
98 | lua_pushvalue(L,-1); | |
99 | lua_setfield(L, -2, "__index"); | |
100 | ||
101 | lua_pushcfunction(L, connection_gc); | |
102 | lua_setfield(L, -2, "__gc"); | |
103 | ||
104 | luaL_register(L, DBD_SQLITE_CONNECTION, connection_class_methods); | |
105 | ||
106 | return 1; | |
107 | } |