dbd/sqlite3/connection.c

changeset 3
b61020ca4753
parent 2
c4f02fc67e5a
child 4
c50b0e6f25d6
--- a/dbd/sqlite3/connection.c	Sun Nov 23 04:12:04 2008 +0000
+++ b/dbd/sqlite3/connection.c	Wed Nov 26 10:01:03 2008 +0000
@@ -3,7 +3,7 @@
 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query);
 
 /* 
- * connection = DBD.SQLite3.New(dbfile)
+ * connection,err = DBD.SQLite3.New(dbfile)
  */
 static int connection_new(lua_State *L) {
     int n = lua_gettop(L);
@@ -13,21 +13,22 @@
 
     /* db */
     switch (n) {
-    default:
+    case 1:
 	if (lua_isnil(L, 1) == 0) 
 	    db = luaL_checkstring(L, 1);
     }
 
     conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t));
 
-    if (sqlite3_open(db, &conn->sqlite) == SQLITE_OK) {
-        luaL_getmetatable(L, DBD_SQLITE_CONNECTION);
-        lua_setmetatable(L, -2);
-    } else {
-	luaL_error(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite));
+    if (sqlite3_open(db, &conn->sqlite) != SQLITE_OK) {
 	lua_pushnil(L);
+	lua_pushfstring(L, "Failed to connect to database: %s", sqlite3_errmsg(conn->sqlite));
+	return 2;
     }
 
+    luaL_getmetatable(L, DBD_SQLITE_CONNECTION);
+    lua_setmetatable(L, -2);
+
     return 1;
 }
 
@@ -39,15 +40,16 @@
     int disconnect = 0;   
 
     if (conn->sqlite) {
-	if (sqlite3_close(conn->sqlite) == SQLITE_OK) {
-	    disconnect = 1;
-	}
+	sqlite3_close(conn->sqlite);
+	disconnect = 1;
+	conn->sqlite = NULL;
     }
 
     lua_pushboolean(L, disconnect);
     return 1;
 }
 
+
 /*
  * ok = connection:ping()
  */
@@ -64,7 +66,7 @@
 }
 
 /*
- * statement = connection:prepare(sql_str)
+ * statement,err = connection:prepare(sql_str)
  */
 static int connection_prepare(lua_State *L) {
     connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_SQLITE_CONNECTION);
@@ -74,7 +76,8 @@
     }
 
     lua_pushnil(L);    
-    return 1;
+    lua_pushstring(L, "Connection not available");
+    return 2;
 }
 
 /*

mercurial