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 /* |