/
sql.c
175 lines (156 loc) · 4.08 KB
/
sql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "sql.h"
#include "box/sql.h"
#include "lua/msgpack.h"
#include "box/sql/sqliteInt.h"
#include "box/info.h"
#include "lua/utils.h"
#include "lua/luastream.h"
#include "info.h"
#include "box/execute.h"
#include "vstream.h"
static void
lua_push_column_names(struct lua_State *L, struct sqlite3_stmt *stmt)
{
int column_count = sqlite3_column_count(stmt);
lua_createtable(L, column_count, 0);
for (int i = 0; i < column_count; i++) {
const char *name = sqlite3_column_name(stmt, i);
lua_pushstring(L, name == NULL ? "" : name);
lua_rawseti(L, -2, i+1);
}
}
static void
lua_push_row(struct lua_State *L, struct sqlite3_stmt *stmt)
{
int column_count = sqlite3_column_count(stmt);
lua_createtable(L, column_count, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_array_metatable_ref);
lua_setmetatable(L, -2);
for (int i = 0; i < column_count; i++) {
int type = sqlite3_column_type(stmt, i);
switch (type) {
case SQLITE_INTEGER:
luaL_pushint64(L, sqlite3_column_int64(stmt, i));
break;
case SQLITE_FLOAT:
lua_pushnumber(L, sqlite3_column_double(stmt, i));
break;
case SQLITE_TEXT: {
const void *text = sqlite3_column_text(stmt, i);
lua_pushlstring(L, text,
sqlite3_column_bytes(stmt, i));
break;
}
case SQLITE_BLOB: {
const void *blob = sqlite3_column_blob(stmt, i);
if (sql_column_subtype(stmt,i) == SQL_SUBTYPE_MSGPACK) {
luamp_decode(L, luaL_msgpack_default,
(const char **)&blob);
} else {
lua_pushlstring(L, blob,
sqlite3_column_bytes(stmt, i));
}
break;
}
case SQLITE_NULL:
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_nil_ref);
break;
default:
assert(0);
}
lua_rawseti(L, -2, i+1);
}
}
static int
lua_sql_execute(struct lua_State *L)
{
sqlite3 *db = sql_get();
if (db == NULL)
return luaL_error(L, "not ready");
size_t length;
const char *sql = lua_tolstring(L, 1, &length);
if (sql == NULL)
return luaL_error(L, "usage: box.sql.execute(sqlstring)");
struct sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, length, &stmt, &sql) != SQLITE_OK)
goto sqlerror;
assert(stmt != NULL);
int rc;
int retval_count;
if (sqlite3_column_count(stmt) == 0) {
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW);
retval_count = 0;
} else {
lua_newtable(L);
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
lua_push_column_names(L, stmt);
lua_rawseti(L, -2, 0);
int row_count = 0;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
lua_push_row(L, stmt);
lua_rawseti(L, -2, ++row_count);
}
retval_count = 1;
}
if (rc != SQLITE_OK && rc != SQLITE_DONE)
goto sqlerror;
sqlite3_finalize(stmt);
return retval_count;
sqlerror:
lua_pushstring(L, sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return lua_error(L);
}
static int
lbox_execute(struct lua_State *L)
{
struct sqlite3 *db = sql_get();
if (db == NULL)
return luaL_error(L, "not ready");
size_t length;
const char *sql = lua_tolstring(L, 1, &length);
if (sql == NULL)
return luaL_error(L, "usage: box.execute(sqlstring)");
struct sql_request request = {};
request.sql_text = sql;
request.sql_text_len = length;
if (lua_gettop(L) == 2 && lua_sql_bind_list_decode(L, &request, 2) != 0)
return luaT_error(L);
struct sql_response response = {.is_info_flattened = true};
if (sql_prepare_and_execute(&request, &response, &fiber()->gc) != 0)
return luaT_error(L);
int keys;
struct vstream vstream;
luavstream_init(&vstream, L);
lua_newtable(L);
if (sql_response_dump(&response, &keys, &vstream) != 0) {
lua_pop(L, 1);
return luaT_error(L);
}
return 1;
}
static int
lua_sql_debug(struct lua_State *L)
{
struct info_handler info;
luaT_info_handler_create(&info, L);
sql_debug_info(&info);
return 1;
}
void
box_lua_sqlite_init(struct lua_State *L)
{
static const struct luaL_Reg module_funcs [] = {
{"execute", lua_sql_execute},
{"new_execute", lbox_execute},
{"debug", lua_sql_debug},
{NULL, NULL}
};
/* used by lua_sql_execute via upvalue */
lua_createtable(L, 0, 1);
lua_pushstring(L, "sequence");
lua_setfield(L, -2, "__serialize");
luaL_openlib(L, "box.sql", module_funcs, 1);
lua_pop(L, 1);
}