]>
jfr.im git - irc/quakenet/newserv.git/blob - lua/luadb.c
4 #include "../dbapi2/dbapi2.h"
6 static DBAPIConn
*luadb
;
8 typedef struct lua_callback
{
13 static int lua_dbcreatequery(lua_State
*ps
) {
14 char *s
= (char *)lua_tostring(ps
, 1);
17 LUA_RETURN(ps
, LUA_FAIL
);
20 LUA_RETURN(ps
, LUA_FAIL
);
22 luadb
->unsafecreatetable(luadb
, NULL
, NULL
, "%s", s
);
23 LUA_RETURN(ps
, LUA_OK
);
26 static int lua_dbtablename(lua_State
*ps
) {
27 char *s
= (char *)lua_tostring(ps
, 1);
30 LUA_RETURN(ps
, LUA_FAIL
);
33 LUA_RETURN(ps
, LUA_FAIL
);
35 char *tablename
= luadb
->tablename(luadb
, s
);
37 LUA_RETURN(ps
, LUA_FAIL
);
39 lua_pushstring(ps
, tablename
);
43 const static DBAPIResult
*active_result
;
44 static void lua_dbcallback(const DBAPIResult
*result
, void *tag
) {
45 lua_callback
*c
= (lua_callback
*)tag
;
47 if(!lua_listexists(c
->l
)) {
52 active_result
= result
;
53 _lua_vpcall(c
->l
->l
, (void *)c
->handler
, LUA_POINTERMODE
, "bR", result
&& result
->success
? 1 : 0, c
->args
);
57 result
->clear(result
);
59 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->handler
);
60 luaL_unref(c
->l
->l
, LUA_REGISTRYINDEX
, c
->args
);
64 static int lua_dbnumfields(lua_State
*ps
) {
71 lua_pushint(ps
, active_result
->fields
);
75 static int lua_dbgetvalue(lua_State
*ps
) {
85 int field
= lua_toint(ps
, 1);
86 if(field
< 0 || field
>= active_result
->fields
)
89 char *value
= active_result
->get(active_result
, field
);
90 /* bit rubbish, no way to get the length... binary data is out then */
91 lua_pushstring(ps
, value
);
96 static int lua_dbnextrow(lua_State
*ps
) {
103 int r
= active_result
->next(active_result
);
104 lua_pushboolean(ps
, r
?1:0);
108 static int lua_dbquery(lua_State
*ps
) {
109 lua_list
*l
= lua_listfromstate(ps
);
110 char *q
= (char *)lua_tostring(ps
, 1);
114 LUA_RETURN(ps
, LUA_FAIL
);
116 /* what happens if 3 isn't there? */
118 LUA_RETURN(ps
, LUA_FAIL
);
120 if(!lua_isfunction(ps
, 2)) {
121 luadb
->unsafesquery(luadb
, "%s", q
);
122 LUA_RETURN(ps
, LUA_OK
);
125 cb
= (lua_callback
*)luamalloc(sizeof(lua_callback
));
127 LUA_RETURN(ps
, LUA_FAIL
);
130 cb
->args
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
131 cb
->handler
= luaL_ref(ps
, LUA_REGISTRYINDEX
);
133 luadb
->unsafequery(luadb
, lua_dbcallback
, cb
, "%s", q
);
135 LUA_RETURN(ps
, LUA_OK
);
138 static int lua_dbescape(lua_State
*ps
) {
139 char ebuf
[8192 * 2 + 1];
140 char *s
= (char *)lua_tostring(ps
, 1);
149 len
= lua_strlen(ps
, 1);
150 if(len
> sizeof(ebuf
) / 2 - 5)
153 luadb
->escapestring(luadb
, ebuf
, s
, len
);
154 lua_pushstring(ps
, ebuf
);
159 void lua_registerdbcommands(lua_State
*l
) {
160 lua_register(l
, "db_tablename", lua_dbtablename
);
162 lua_register(l
, "db_createquery", lua_dbcreatequery
);
163 lua_register(l
, "db_query", lua_dbquery
);
164 lua_register(l
, "db_escape", lua_dbescape
);
166 lua_register(l
, "db_numfields", lua_dbnumfields
);
167 lua_register(l
, "db_getvalue", lua_dbgetvalue
);
168 lua_register(l
, "db_nextrow", lua_dbnextrow
);
170 luadb
= dbapi2open(NULL
, "lua");
173 /* open gets you a unique db */
174 /* parameterised queries (huge pain due to no va_args in dbapi2) */
179 void lua_destroydb(void) {