dbd/sqlite3/connection.c

changeset 3
b61020ca4753
parent 2
c4f02fc67e5a
child 4
c50b0e6f25d6
equal deleted inserted replaced
2:c4f02fc67e5a 3:b61020ca4753
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 /* 5 /*
6 * connection = DBD.SQLite3.New(dbfile) 6 * connection,err = DBD.SQLite3.New(dbfile)
7 */ 7 */
8 static int connection_new(lua_State *L) { 8 static int connection_new(lua_State *L) {
9 int n = lua_gettop(L); 9 int n = lua_gettop(L);
10 10
11 const char *db = NULL; 11 const char *db = NULL;
12 connection_t *conn = NULL; 12 connection_t *conn = NULL;
13 13
14 /* db */ 14 /* db */
15 switch (n) { 15 switch (n) {
16 default: 16 case 1:
17 if (lua_isnil(L, 1) == 0) 17 if (lua_isnil(L, 1) == 0)
18 db = luaL_checkstring(L, 1); 18 db = luaL_checkstring(L, 1);
19 } 19 }
20 20
21 conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); 21 conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t));
22 22
23 if (sqlite3_open(db, &conn->sqlite) == SQLITE_OK) { 23 if (sqlite3_open(db, &conn->sqlite) != SQLITE_OK) {
24 luaL_getmetatable(L, DBD_SQLITE_CONNECTION);
25 lua_setmetatable(L, -2);
26 } else {
27 luaL_error(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite));
28 lua_pushnil(L); 24 lua_pushnil(L);
25 lua_pushfstring(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite));
26 return 2;
29 } 27 }
28
29 luaL_getmetatable(L, DBD_SQLITE_CONNECTION);
30 lua_setmetatable(L, -2);
30 31
31 return 1; 32 return 1;
32 } 33 }
33 34
34 /* 35 /*
37 static int connection_close(lua_State *L) { 38 static int connection_close(lua_State *L) {
38 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); 39 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION);
39 int disconnect = 0; 40 int disconnect = 0;
40 41
41 if (conn->sqlite) { 42 if (conn->sqlite) {
42 if (sqlite3_close(conn->sqlite) == SQLITE_OK) { 43 sqlite3_close(conn->sqlite);
43 disconnect = 1; 44 disconnect = 1;
44 } 45 conn->sqlite = NULL;
45 } 46 }
46 47
47 lua_pushboolean(L, disconnect); 48 lua_pushboolean(L, disconnect);
48 return 1; 49 return 1;
49 } 50 }
51
50 52
51 /* 53 /*
52 * ok = connection:ping() 54 * ok = connection:ping()
53 */ 55 */
54 static int connection_ping(lua_State *L) { 56 static int connection_ping(lua_State *L) {
62 lua_pushboolean(L, ok); 64 lua_pushboolean(L, ok);
63 return 1; 65 return 1;
64 } 66 }
65 67
66 /* 68 /*
67 * statement = connection:prepare(sql_str) 69 * statement,err = connection:prepare(sql_str)
68 */ 70 */
69 static int connection_prepare(lua_State *L) { 71 static int connection_prepare(lua_State *L) {
70 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION); 72 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION);
71 73
72 if (conn->sqlite) { 74 if (conn->sqlite) {
73 return dbd_sqlite3_statement_create(L, conn, luaL_checkstring(L, 2)); 75 return dbd_sqlite3_statement_create(L, conn, luaL_checkstring(L, 2));
74 } 76 }
75 77
76 lua_pushnil(L); 78 lua_pushnil(L);
77 return 1; 79 lua_pushstring(L, "Connection not available");
80 return 2;
78 } 81 }
79 82
80 /* 83 /*
81 * __gc 84 * __gc
82 */ 85 */

mercurial