]> jfr.im git - irc/quakenet/newserv.git/blame - lua/luadb.c
TRUSTS: require sqlite
[irc/quakenet/newserv.git] / lua / luadb.c
CommitLineData
55cee062
CP
1#include "lua.h"
2#include "luabot.h"
d7839cef
CP
3
4#include "../config.h"
5
6#ifdef HAVE_PGSQL
ee8cd7d0
CP
7#include "../dbapi/dbapi.h"
8
9#warning Needs testing with new db API.
55cee062
CP
10
11static int lua_dbcreatequery(lua_State *ps) {
12 char *s = (char *)lua_tostring(ps, 1);
13
14 if(!s)
15 LUA_RETURN(ps, LUA_FAIL);
16
b32ca20f 17 dbcreatequery("%s", s);
55cee062
CP
18 LUA_RETURN(ps, LUA_OK);
19}
20
21/* TODO */
22/*
23static int lua_dbloadtable(lua_State *ps) {
24 lua_list *l = lua_listfromstate(ps);
25 if(!l)
26 LUA_RETURN(ps, LUA_FAIL);
27
28}
29*/
30
31typedef struct lua_callback {
32 lua_list *l;
33 long handler, args;
34} lua_callback;
35
36/* hack */
ee8cd7d0 37DBResult *pgres;
55cee062
CP
38
39static int lua_dbnumfields(lua_State *ps) {
ee8cd7d0 40 lua_pushint(ps, dbnumfields(pgres));
55cee062
CP
41
42 return 1;
43}
44
ee8cd7d0
CP
45/* TODO */
46/*
47static int lua_dbgetrow(lua_State *ps) {
55cee062
CP
48 char *r;
49 int tuple, field, len;
50
ee8cd7d0 51 if(!lua_isint(ps, 1))
55cee062
CP
52 return 0;
53
ee8cd7d0 54 lua_pushstring(ps, dbgetvalue(pgres, lua_toint(ps, 1)), len);
55cee062
CP
55
56 return 1;
57}
ee8cd7d0 58*/
55cee062 59
ee8cd7d0
CP
60void lua_dbcallback(DBConn *dbconn, void *arg) {
61 pgres = dbgetresult(dbconn);
55cee062
CP
62 lua_callback *c = (lua_callback *)arg;
63
64 if(!lua_listexists(c->l)) {
d0e17ef9 65 luafree(c);
55cee062
CP
66 return;
67 }
68
ee8cd7d0 69 if(!dbquerysuccessful(pgres)) {
55cee062
CP
70 _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 0, c->args);
71
72 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
73 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
d0e17ef9 74 luafree(c);
55cee062
CP
75 return;
76 }
77
78 _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 1, c->args);
79
80 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
81 luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
d0e17ef9 82 luafree(c);
ee8cd7d0 83 dbclear(pgres);
55cee062
CP
84 pgres = NULL;
85}
86
87static int lua_dbquery(lua_State *ps) {
88 lua_list *l = lua_listfromstate(ps);
89 char *q = (char *)lua_tostring(ps, 1);
90 lua_callback *cb;
91
92 /* what happens if 3 isn't there? */
93 if(!l || !q)
94 LUA_RETURN(ps, LUA_FAIL);
95
96 if(!lua_isfunction(ps, 2)) {
b32ca20f 97 dbquery("%s", q);
55cee062
CP
98 LUA_RETURN(ps, LUA_OK);
99 }
100
d0e17ef9 101 cb = (lua_callback *)luamalloc(sizeof(lua_callback));
55cee062
CP
102 if(!cb)
103 LUA_RETURN(ps, LUA_FAIL);
104
105 cb->l = l;
106 cb->args = luaL_ref(ps, LUA_REGISTRYINDEX);
107 cb->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
108
b32ca20f 109 dbasyncquery(lua_dbcallback, cb, "%s", q);
55cee062
CP
110
111 LUA_RETURN(ps, LUA_OK);
112}
113
114static int lua_dbescape(lua_State *ps) {
ee8cd7d0 115 char ebuf[8192 * 2 + 1];
55cee062
CP
116 char *s = (char *)lua_tostring(ps, 1);
117 int len;
55cee062
CP
118
119 if(!s)
120 return 0;
121
122 len = lua_strlen(ps, 1);
123 if(len > sizeof(ebuf) / 2 - 5)
124 return 0;
125
ee8cd7d0
CP
126 dbescapestring(ebuf, s, len);
127 lua_pushstring(ps, ebuf);
55cee062
CP
128
129 return 1;
130}
131
132void lua_registerdbcommands(lua_State *l) {
133 lua_register(l, "db_createquery", lua_dbcreatequery);
134
135/* lua_register(l, "db_loadtable", lua_dbloadtable); */
136
137 lua_register(l, "db_query", lua_dbquery);
138 lua_register(l, "db_escape", lua_dbescape);
139 lua_register(l, "db_numfields", lua_dbnumfields);
ee8cd7d0 140/* lua_register(l, "db_getvalue", lua_dbgetvalue);*/
55cee062 141}
d7839cef
CP
142
143#else
144
145void lua_registerdbcommands(lua_State *l) {
146}
147
148#endif