29 |
29 |
30 if (pport >= 1 && pport <= 65535) { |
30 if (pport >= 1 && pport <= 65535) { |
31 snprintf(portbuf, sizeof(portbuf), "%d", pport); |
31 snprintf(portbuf, sizeof(portbuf), "%d", pport); |
32 port = portbuf; |
32 port = portbuf; |
33 } else { |
33 } else { |
34 luaL_error(L, "Invalid port %d", pport); |
34 luaL_error(L, DBI_ERR_INVALID_PORT, pport); |
35 } |
35 } |
36 } |
36 } |
37 case 4: |
37 case 4: |
38 if (lua_isnil(L, 4) == 0) |
38 if (lua_isnil(L, 4) == 0) |
39 host = luaL_checkstring(L, 4); |
39 host = luaL_checkstring(L, 4); |
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 lua_pushnil(L); |
57 lua_pushnil(L); |
58 lua_pushfstring(L, "Failed to connect to database: %s", PQerrorMessage(conn->postgresql)); |
58 lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, PQerrorMessage(conn->postgresql)); |
59 return 2; |
59 return 2; |
60 } |
60 } |
61 |
61 |
62 luaL_getmetatable(L, DBD_POSTGRESQL_CONNECTION); |
62 luaL_getmetatable(L, DBD_POSTGRESQL_CONNECTION); |
63 lua_setmetatable(L, -2); |
63 lua_setmetatable(L, -2); |