Wed, 26 Nov 2008 10:43:45 +0000
Clean up error messages for consistency.
1 | 1 | #include "dbd_postgresql.h" |
2 | ||
3 | int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); | |
4 | ||
2 | 5 | /* |
6 | * connection = DBD.PostgreSQL.New(dbname, user, password, host, port) | |
7 | */ | |
1 | 8 | static int connection_new(lua_State *L) { |
9 | int n = lua_gettop(L); | |
10 | connection_t *conn = NULL; | |
11 | ||
12 | const char *host = NULL; | |
13 | const char *user = NULL; | |
14 | const char *password = NULL; | |
15 | const char *db = NULL; | |
16 | const char *port = NULL; | |
17 | ||
18 | const char *options = NULL; /* TODO always NULL */ | |
19 | const char *tty = NULL; /* TODO always NULL */ | |
20 | ||
21 | char portbuf[18]; | |
22 | ||
23 | /* db, user, password, host, port */ | |
24 | switch (n) { | |
25 | case 5: | |
26 | if (lua_isnil(L, 5) == 0) | |
27 | { | |
28 | int pport = luaL_checkint(L, 5); | |
29 | ||
30 | if (pport >= 1 && pport <= 65535) { | |
31 | snprintf(portbuf, sizeof(portbuf), "%d", pport); | |
32 | port = portbuf; | |
33 | } else { | |
4 | 34 | luaL_error(L, DBI_ERR_INVALID_PORT, pport); |
1 | 35 | } |
36 | } | |
37 | case 4: | |
38 | if (lua_isnil(L, 4) == 0) | |
39 | host = luaL_checkstring(L, 4); | |
40 | case 3: | |
41 | if (lua_isnil(L, 3) == 0) | |
42 | password = luaL_checkstring(L, 3); | |
43 | case 2: | |
44 | if (lua_isnil(L, 2) == 0) | |
45 | user = luaL_checkstring(L, 2); | |
46 | case 1: | |
47 | if (lua_isnil(L, 1) == 0) | |
48 | db = luaL_checkstring(L, 1); | |
49 | } | |
50 | ||
51 | conn = (connection_t *)lua_newuserdata(L, sizeof(connection_t)); | |
52 | ||
53 | conn->postgresql = PQsetdbLogin(host, port, options, tty, db, user, password); | |
54 | conn->statement_id = 0; | |
55 | ||
3 | 56 | if (PQstatus(conn->postgresql) != CONNECTION_OK) { |
1 | 57 | lua_pushnil(L); |
4 | 58 | lua_pushfstring(L, DBI_ERR_CONNECTION_FAILED, PQerrorMessage(conn->postgresql)); |
3 | 59 | return 2; |
1 | 60 | } |
61 | ||
3 | 62 | luaL_getmetatable(L, DBD_POSTGRESQL_CONNECTION); |
63 | lua_setmetatable(L, -2); | |
64 | ||
1 | 65 | return 1; |
66 | } | |
67 | ||
2 | 68 | /* |
69 | * success = connection:close() | |
70 | */ | |
1 | 71 | static int connection_close(lua_State *L) { |
72 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
73 | int disconnect = 0; | |
74 | ||
75 | if (conn->postgresql) { | |
76 | PQfinish(conn->postgresql); | |
77 | disconnect = 1; | |
3 | 78 | conn->postgresql = NULL; |
1 | 79 | } |
80 | ||
81 | lua_pushboolean(L, disconnect); | |
82 | return 1; | |
83 | } | |
84 | ||
2 | 85 | /* |
86 | * ok = connection:ping() | |
87 | */ | |
1 | 88 | static int connection_ping(lua_State *L) { |
89 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
90 | int ok = 0; | |
91 | ||
92 | if (conn->postgresql) { | |
93 | ConnStatusType status = PQstatus(conn->postgresql); | |
94 | ||
95 | if (status == CONNECTION_OK) | |
96 | ok = 1; | |
97 | } | |
98 | ||
99 | lua_pushboolean(L, ok); | |
100 | return 1; | |
101 | } | |
102 | ||
2 | 103 | /* |
104 | * statement = connection:prepare(sql_string) | |
105 | */ | |
1 | 106 | static int connection_prepare(lua_State *L) { |
107 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
108 | ||
109 | if (conn->postgresql) { | |
110 | return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2)); | |
111 | } | |
112 | ||
113 | lua_pushnil(L); | |
4 | 114 | lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE); |
3 | 115 | return 2; |
1 | 116 | } |
117 | ||
2 | 118 | /* |
119 | * __gc | |
120 | */ | |
1 | 121 | static int connection_gc(lua_State *L) { |
122 | /* always close the connection */ | |
123 | connection_close(L); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
2 | 128 | int dbd_postgresql_connection(lua_State *L) { |
129 | static const luaL_Reg connection_methods[] = { | |
130 | {"close", connection_close}, | |
131 | {"ping", connection_ping}, | |
132 | {"prepare", connection_prepare}, | |
133 | {NULL, NULL} | |
134 | }; | |
1 | 135 | |
2 | 136 | static const luaL_Reg connection_class_methods[] = { |
137 | {"New", connection_new}, | |
138 | {NULL, NULL} | |
139 | }; | |
1 | 140 | |
141 | luaL_newmetatable(L, DBD_POSTGRESQL_CONNECTION); | |
142 | luaL_register(L, 0, connection_methods); | |
143 | lua_pushvalue(L,-1); | |
144 | lua_setfield(L, -2, "__index"); | |
145 | ||
146 | lua_pushcfunction(L, connection_gc); | |
147 | lua_setfield(L, -2, "__gc"); | |
148 | ||
149 | luaL_register(L, DBD_POSTGRESQL_CONNECTION, connection_class_methods); | |
150 | ||
151 | return 1; | |
152 | } |