Sun, 23 Nov 2008 04:12:04 +0000
Cleanup and commenting
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 { | |
34 | luaL_error(L, "Invalid port %d", pport); | |
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 | ||
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); | |
62 | } | |
63 | ||
64 | return 1; | |
65 | } | |
66 | ||
2 | 67 | /* |
68 | * success = connection:close() | |
69 | */ | |
1 | 70 | static int connection_close(lua_State *L) { |
71 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
72 | int disconnect = 0; | |
73 | ||
74 | if (conn->postgresql) { | |
75 | PQfinish(conn->postgresql); | |
76 | disconnect = 1; | |
77 | } | |
78 | ||
79 | lua_pushboolean(L, disconnect); | |
80 | return 1; | |
81 | } | |
82 | ||
2 | 83 | /* |
84 | * ok = connection:ping() | |
85 | */ | |
1 | 86 | static int connection_ping(lua_State *L) { |
87 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
88 | int ok = 0; | |
89 | ||
90 | if (conn->postgresql) { | |
91 | ConnStatusType status = PQstatus(conn->postgresql); | |
92 | ||
93 | if (status == CONNECTION_OK) | |
94 | ok = 1; | |
95 | } | |
96 | ||
97 | lua_pushboolean(L, ok); | |
98 | return 1; | |
99 | } | |
100 | ||
2 | 101 | /* |
102 | * statement = connection:prepare(sql_string) | |
103 | */ | |
1 | 104 | static int connection_prepare(lua_State *L) { |
105 | connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); | |
106 | ||
107 | if (conn->postgresql) { | |
108 | return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2)); | |
109 | } | |
110 | ||
111 | lua_pushnil(L); | |
112 | return 1; | |
113 | } | |
114 | ||
2 | 115 | /* |
116 | * __gc | |
117 | */ | |
1 | 118 | static int connection_gc(lua_State *L) { |
119 | /* always close the connection */ | |
120 | connection_close(L); | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
2 | 125 | int dbd_postgresql_connection(lua_State *L) { |
126 | static const luaL_Reg connection_methods[] = { | |
127 | {"close", connection_close}, | |
128 | {"ping", connection_ping}, | |
129 | {"prepare", connection_prepare}, | |
130 | {NULL, NULL} | |
131 | }; | |
1 | 132 | |
2 | 133 | static const luaL_Reg connection_class_methods[] = { |
134 | {"New", connection_new}, | |
135 | {NULL, NULL} | |
136 | }; | |
1 | 137 | |
138 | luaL_newmetatable(L, DBD_POSTGRESQL_CONNECTION); | |
139 | luaL_register(L, 0, connection_methods); | |
140 | lua_pushvalue(L,-1); | |
141 | lua_setfield(L, -2, "__index"); | |
142 | ||
143 | lua_pushcfunction(L, connection_gc); | |
144 | lua_setfield(L, -2, "__gc"); | |
145 | ||
146 | luaL_register(L, DBD_POSTGRESQL_CONNECTION, connection_class_methods); | |
147 | ||
148 | return 1; | |
149 | } |