dbd/postgresql/connection.c

changeset 3
b61020ca4753
parent 2
c4f02fc67e5a
child 4
c50b0e6f25d6
equal deleted inserted replaced
2:c4f02fc67e5a 3:b61020ca4753
51 conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); 51 conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t));
52 52
53 conn->postgresql = PQsetdbLogin(host, port, options, tty, db, user, password); 53 conn->postgresql = PQsetdbLogin(host, port, options, tty, db, user, password);
54 conn->statement_id = 0; 54 conn->statement_id = 0;
55 55
56 if (PQstatus(conn->postgresql) == CONNECTION_OK) { 56 if (PQstatus(conn->postgresql) != CONNECTION_OK) {
57 luaL_getmetatable(L, DBD_POSTGRESQL_CONNECTION);
58 lua_setmetatable(L, -2);
59 } else {
60 luaL_error(L, "Failed to connect to database: %s", PQerrorMessage(conn->postgresql));
61 lua_pushnil(L); 57 lua_pushnil(L);
58 lua_pushfstring(L, "Failed to connect to database: %s", PQerrorMessage(conn->postgresql));
59 return 2;
62 } 60 }
61
62 luaL_getmetatable(L, DBD_POSTGRESQL_CONNECTION);
63 lua_setmetatable(L, -2);
63 64
64 return 1; 65 return 1;
65 } 66 }
66 67
67 /* 68 /*
72 int disconnect = 0; 73 int disconnect = 0;
73 74
74 if (conn->postgresql) { 75 if (conn->postgresql) {
75 PQfinish(conn->postgresql); 76 PQfinish(conn->postgresql);
76 disconnect = 1; 77 disconnect = 1;
78 conn->postgresql = NULL;
77 } 79 }
78 80
79 lua_pushboolean(L, disconnect); 81 lua_pushboolean(L, disconnect);
80 return 1; 82 return 1;
81 } 83 }
107 if (conn->postgresql) { 109 if (conn->postgresql) {
108 return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2)); 110 return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2));
109 } 111 }
110 112
111 lua_pushnil(L); 113 lua_pushnil(L);
112 return 1; 114 lua_pushstring(L, "Database not available");
115 return 2;
113 } 116 }
114 117
115 /* 118 /*
116 * __gc 119 * __gc
117 */ 120 */

mercurial