1 #include "dbd_sqlite3.h" |
1 #include "dbd_sqlite3.h" |
2 |
2 |
3 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
3 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
4 |
4 |
|
5 /* |
|
6 * connection = DBD.SQLite3.New(dbfile) |
|
7 */ |
5 static int connection_new(lua_State *L) { |
8 static int connection_new(lua_State *L) { |
6 int n = lua_gettop(L); |
9 int n = lua_gettop(L); |
7 |
10 |
8 const char *db = NULL; |
11 const char *db = NULL; |
9 connection_t *conn = NULL; |
12 connection_t *conn = NULL; |
26 } |
29 } |
27 |
30 |
28 return 1; |
31 return 1; |
29 } |
32 } |
30 |
33 |
|
34 /* |
|
35 * success = connection:close() |
|
36 */ |
31 static int connection_close(lua_State *L) { |
37 static int connection_close(lua_State *L) { |
32 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
38 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
33 int disconnect = 0; |
39 int disconnect = 0; |
34 |
40 |
35 if (conn->sqlite) { |
41 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) { |
42 if (sqlite3_close(conn->sqlite) == SQLITE_OK) { |
44 disconnect = 1; |
43 disconnect = 1; |
45 } |
44 } |
46 } |
45 } |
47 |
46 |
48 lua_pushboolean(L, disconnect); |
47 lua_pushboolean(L, disconnect); |
49 return 1; |
48 return 1; |
50 } |
49 } |
51 |
50 |
|
51 /* |
|
52 * ok = connection:ping() |
|
53 */ |
52 static int connection_ping(lua_State *L) { |
54 static int connection_ping(lua_State *L) { |
53 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
55 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
54 int ok = 0; |
56 int ok = 0; |
55 |
57 |
56 if (conn->sqlite) { |
58 if (conn->sqlite) { |
59 |
61 |
60 lua_pushboolean(L, ok); |
62 lua_pushboolean(L, ok); |
61 return 1; |
63 return 1; |
62 } |
64 } |
63 |
65 |
|
66 /* |
|
67 * statement = connection:prepare(sql_str) |
|
68 */ |
64 static int connection_prepare(lua_State *L) { |
69 static int connection_prepare(lua_State *L) { |
65 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
70 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); |
66 |
71 |
67 if (conn->sqlite) { |
72 if (conn->sqlite) { |
68 return dbd_sqlite3_statement_create(L, conn, luaL_checkstring(L, 2)); |
73 return dbd_sqlite3_statement_create(L, conn, luaL_checkstring(L, 2)); |
70 |
75 |
71 lua_pushnil(L); |
76 lua_pushnil(L); |
72 return 1; |
77 return 1; |
73 } |
78 } |
74 |
79 |
75 |
80 /* |
|
81 * __gc |
|
82 */ |
76 static int connection_gc(lua_State *L) { |
83 static int connection_gc(lua_State *L) { |
77 /* always close the connection */ |
84 /* always close the connection */ |
78 connection_close(L); |
85 connection_close(L); |
79 |
86 |
80 return 0; |
87 return 0; |
81 } |
88 } |
82 |
89 |
83 static const luaL_Reg connection_methods[] = { |
90 int dbd_sqlite3_connection(lua_State *L) { |
84 {"close", connection_close}, |
91 /* |
85 {"ping", connection_ping}, |
92 * instance methods |
86 {"prepare", connection_prepare}, |
93 */ |
87 {NULL, NULL} |
94 static const luaL_Reg connection_methods[] = { |
88 }; |
95 {"close", connection_close}, |
|
96 {"ping", connection_ping}, |
|
97 {"prepare", connection_prepare}, |
|
98 {NULL, NULL} |
|
99 }; |
89 |
100 |
90 static const luaL_Reg connection_class_methods[] = { |
101 /* |
91 {"New", connection_new}, |
102 * class methods |
92 {NULL, NULL} |
103 */ |
93 }; |
104 static const luaL_Reg connection_class_methods[] = { |
|
105 {"New", connection_new}, |
|
106 {NULL, NULL} |
|
107 }; |
94 |
108 |
95 int dbd_sqlite3_connection(lua_State *L) { |
|
96 luaL_newmetatable(L, DBD_SQLITE_CONNECTION); |
109 luaL_newmetatable(L, DBD_SQLITE_CONNECTION); |
97 luaL_register(L, 0, connection_methods); |
110 luaL_register(L, 0, connection_methods); |
98 lua_pushvalue(L,-1); |
111 lua_pushvalue(L,-1); |
99 lua_setfield(L, -2, "__index"); |
112 lua_setfield(L, -2, "__index"); |
100 |
113 |