]>
jfr.im git - irc/quakenet/newserv.git/blob - lua/luadb.c
7 #include "../dbapi/dbapi.h"
9 #warning Needs testing with new db API.
11 static int lua_dbcreatequery(lua_State
*ps
) {
12 char *s
= (char *)lua_tostring(ps
, 1);
15 LUA_RETURN(ps
, LUA_FAIL
);
17 dbcreatequery("%s", s
);
18 LUA_RETURN(ps
, LUA_OK
);
23 static int lua_dbloadtable(lua_State *ps) {
24 lua_list *l = lua_listfromstate(ps);
26 LUA_RETURN(ps, LUA_FAIL);
31 typedef struct lua_callback
{
39 static int lua_dbnumfields(lua_State
*ps
) {
40 lua_pushint(ps
, dbnumfields(pgres
));
47 static int lua_dbgetrow(lua_State *ps) {
49 int tuple, field, len;
54 lua_pushstring(ps, dbgetvalue(pgres, lua_toint(ps, 1)), len);
60 void lua_dbcallback(DBConn
*dbconn
, void *arg
) {
61 pgres
= dbgetresult(dbconn
);
62 lua_callback
*c
= (lua_callback
*)arg
;
64 if(!lua_listexists(c
->l
)) {
69 if(!dbquerysuccessful(pgres
)) {
70 _lua_vpcall(c
->l
->l
, (void *)c
->handler
, LUA_POINTERMODE
, "bR", 0, c
->args
);
72 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->handler
);
73 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->args
);
78 _lua_vpcall(c
->l
->l
, (void *)c
->handler
, LUA_POINTERMODE
, "bR", 1, c
->args
);
80 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->handler
);
81 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->args
);
87 static int lua_dbquery(lua_State
*ps
) {
88 lua_list
*l
= lua_listfromstate(ps
);
89 char *q
= (char *)lua_tostring(ps
, 1);
92 /* what happens if 3 isn't there? */
94 LUA_RETURN(ps
, LUA_FAIL
);
96 if(!lua_isfunction(ps
, 2)) {
98 LUA_RETURN(ps
, LUA_OK
);
101 cb
= (lua_callback
*)luamalloc(sizeof(lua_callback
));
103 LUA_RETURN(ps
, LUA_FAIL
);
106 cb
->args
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
107 cb
->handler
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
109 dbasyncquery(lua_dbcallback
, cb
, "%s", q
);
111 LUA_RETURN(ps
, LUA_OK
);
114 static int lua_dbescape(lua_State
*ps
) {
115 char ebuf
[8192 * 2 + 1];
116 char *s
= (char *)lua_tostring(ps
, 1);
122 len
= lua_strlen(ps
, 1);
123 if(len
> sizeof(ebuf
) / 2 - 5)
126 dbescapestring(ebuf
, s
, len
);
127 lua_pushstring(ps
, ebuf
);
132 void lua_registerdbcommands(lua_State
*l
) {
133 lua_register(l
, "db_createquery", lua_dbcreatequery
);
135 /* lua_register(l, "db_loadtable", lua_dbloadtable); */
137 lua_register(l
, "db_query", lua_dbquery
);
138 lua_register(l
, "db_escape", lua_dbescape
);
139 lua_register(l
, "db_numfields", lua_dbnumfields
);
140 /* lua_register(l, "db_getvalue", lua_dbgetvalue);*/
145 void lua_registerdbcommands(lua_State
*l
) {