dbd/postgresql/connection.c

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

mercurial