1 #include "dbd_postgresql.h" |
1 #include "dbd_postgresql.h" |
2 |
2 |
3 int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
3 int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query); |
4 |
4 |
|
5 /* |
|
6 * connection = DBD.PostgreSQL.New(dbname, user, password, host, port) |
|
7 */ |
5 static int connection_new(lua_State *L) { |
8 static int connection_new(lua_State *L) { |
6 int n = lua_gettop(L); |
9 int n = lua_gettop(L); |
7 connection_t *conn = NULL; |
10 connection_t *conn = NULL; |
8 |
11 |
9 const char *host = NULL; |
12 const char *host = NULL; |
72 |
78 |
73 lua_pushboolean(L, disconnect); |
79 lua_pushboolean(L, disconnect); |
74 return 1; |
80 return 1; |
75 } |
81 } |
76 |
82 |
|
83 /* |
|
84 * ok = connection:ping() |
|
85 */ |
77 static int connection_ping(lua_State *L) { |
86 static int connection_ping(lua_State *L) { |
78 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); |
87 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); |
79 int ok = 0; |
88 int ok = 0; |
80 |
89 |
81 if (conn->postgresql) { |
90 if (conn->postgresql) { |
87 |
96 |
88 lua_pushboolean(L, ok); |
97 lua_pushboolean(L, ok); |
89 return 1; |
98 return 1; |
90 } |
99 } |
91 |
100 |
|
101 /* |
|
102 * statement = connection:prepare(sql_string) |
|
103 */ |
92 static int connection_prepare(lua_State *L) { |
104 static int connection_prepare(lua_State *L) { |
93 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); |
105 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); |
94 |
106 |
95 if (conn->postgresql) { |
107 if (conn->postgresql) { |
96 return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2)); |
108 return dbd_postgresql_statement_create(L, conn, luaL_checkstring(L, 2)); |
98 |
110 |
99 lua_pushnil(L); |
111 lua_pushnil(L); |
100 return 1; |
112 return 1; |
101 } |
113 } |
102 |
114 |
103 |
115 /* |
|
116 * __gc |
|
117 */ |
104 static int connection_gc(lua_State *L) { |
118 static int connection_gc(lua_State *L) { |
105 /* always close the connection */ |
119 /* always close the connection */ |
106 connection_close(L); |
120 connection_close(L); |
107 |
121 |
108 return 0; |
122 return 0; |
109 } |
123 } |
110 |
124 |
111 static const luaL_Reg connection_methods[] = { |
125 int dbd_postgresql_connection(lua_State *L) { |
112 {"close", connection_close}, |
126 static const luaL_Reg connection_methods[] = { |
113 {"ping", connection_ping}, |
127 {"close", connection_close}, |
114 {"prepare", connection_prepare}, |
128 {"ping", connection_ping}, |
115 {NULL, NULL} |
129 {"prepare", connection_prepare}, |
116 }; |
130 {NULL, NULL} |
|
131 }; |
117 |
132 |
118 static const luaL_Reg connection_class_methods[] = { |
133 static const luaL_Reg connection_class_methods[] = { |
119 {"New", connection_new}, |
134 {"New", connection_new}, |
120 {NULL, NULL} |
135 {NULL, NULL} |
121 }; |
136 }; |
122 |
137 |
123 int dbd_postgresql_connection(lua_State *L) { |
|
124 luaL_newmetatable(L, DBD_POSTGRESQL_CONNECTION); |
138 luaL_newmetatable(L, DBD_POSTGRESQL_CONNECTION); |
125 luaL_register(L, 0, connection_methods); |
139 luaL_register(L, 0, connection_methods); |
126 lua_pushvalue(L,-1); |
140 lua_pushvalue(L,-1); |
127 lua_setfield(L, -2, "__index"); |
141 lua_setfield(L, -2, "__index"); |
128 |
142 |