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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
See license.txt in the root of this project.
*/
# include "luametatex.h"
# include "lmtoptional.h"
typedef void mysql_instance;
typedef void mysql_result;
typedef char **mysql_row;
typedef unsigned int mysql_offset;
typedef struct mysql_field {
char *name;
char *org_name;
char *table;
char *org_table;
char *db;
char *catalog;
char *def;
unsigned long length;
unsigned long max_length;
unsigned int name_length;
unsigned int org_name_length;
unsigned int table_length;
unsigned int org_table_length;
unsigned int db_length;
unsigned int catalog_length;
unsigned int def_length;
unsigned int flags;
unsigned int decimals;
unsigned int charsetnr;
int type;
void *extension;
} mysql_field;
# define MYSQLLIB_METATABLE "luatex.mysqllib"
typedef struct mysqllib_data {
/*tex There is not much more than a pointer currently. */
mysql_instance * db;
} mysqllib_data ;
typedef struct mysqllib_state_info {
int initialized;
int padding;
mysql_instance * (*mysql_init) (
mysql_instance *mysql
);
mysql_instance * (*mysql_real_connect) (
mysql_instance *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag
);
unsigned int (*mysql_errno) (
mysql_instance *mysql
);
const char * (*mysql_error) (
mysql_instance *mysql
);
int (*mysql_real_query) (
mysql_instance *mysql,
const char *q,
unsigned long length
);
mysql_result * (*mysql_store_result) (
mysql_instance *mysql
);
void (*mysql_free_result) (
mysql_result *result
);
unsigned long long (*mysql_num_rows) (
mysql_result *res
);
mysql_row (*mysql_fetch_row) (
mysql_result *result
);
unsigned int (*mysql_affected_rows) (
mysql_instance *mysql
);
unsigned int (*mysql_field_count) (
mysql_instance *mysql
);
unsigned int (*mysql_num_fields) (
mysql_result *res
);
mysql_field * (*mysql_fetch_fields) (
mysql_result *res
);
mysql_offset (*mysql_field_seek) (
mysql_result *result,
mysql_offset offset
);
void (*mysql_close) (
mysql_instance *sock
);
} mysqllib_state_info;
static mysqllib_state_info mysqllib_state = {
.initialized = 0,
.padding = 0,
.mysql_init = NULL,
.mysql_real_connect = NULL,
.mysql_errno = NULL,
.mysql_error = NULL,
.mysql_real_query = NULL,
.mysql_store_result = NULL,
.mysql_free_result = NULL,
.mysql_num_rows = NULL,
.mysql_fetch_row = NULL,
.mysql_affected_rows = NULL,
.mysql_field_count = NULL,
.mysql_num_fields = NULL,
.mysql_fetch_fields = NULL,
.mysql_field_seek = NULL,
.mysql_close = NULL,
};
static int mysqllib_initialize(lua_State * L)
{
if (! mysqllib_state.initialized) {
const char *filename = lua_tostring(L, 1);
if (filename != NULL) {
lmt_library lib = lmt_library_load(filename);
mysqllib_state.mysql_init = lmt_library_find(lib, "mysql_init" );
mysqllib_state.mysql_real_connect = lmt_library_find(lib, "mysql_real_connect" );
mysqllib_state.mysql_errno = lmt_library_find(lib, "mysql_errno" );
mysqllib_state.mysql_error = lmt_library_find(lib, "mysql_error" );
mysqllib_state.mysql_real_query = lmt_library_find(lib, "mysql_real_query" );
mysqllib_state.mysql_store_result = lmt_library_find(lib, "mysql_store_result" );
mysqllib_state.mysql_free_result = lmt_library_find(lib, "mysql_free_result" );
mysqllib_state.mysql_num_rows = lmt_library_find(lib, "mysql_num_rows" );
mysqllib_state.mysql_fetch_row = lmt_library_find(lib, "mysql_fetch_row" );
mysqllib_state.mysql_affected_rows = lmt_library_find(lib, "mysql_affected_rows" );
mysqllib_state.mysql_field_count = lmt_library_find(lib, "mysql_field_count" );
mysqllib_state.mysql_num_fields = lmt_library_find(lib, "mysql_num_fields" );
mysqllib_state.mysql_fetch_fields = lmt_library_find(lib, "mysql_fetch_fields" );
mysqllib_state.mysql_field_seek = lmt_library_find(lib, "mysql_field_seek" );
mysqllib_state.mysql_close = lmt_library_find(lib, "mysql_close" );
mysqllib_state.initialized = lmt_library_okay(lib);
}
}
lua_pushboolean(L, mysqllib_state.initialized);
return 1;
}
static int mysqllib_open(lua_State * L)
{
if (mysqllib_state.initialized) {
const char * database = luaL_checkstring(L, 1);
const char * username = luaL_optstring(L, 2, NULL);
const char * password = luaL_optstring(L, 3, NULL);
const char * host = luaL_optstring(L, 4, NULL);
int port = lmt_optinteger(L, 5, 0);
const char * socket = NULL; /* luaL_optstring(L, 6, NULL); */
int flag = 0; /* luaL_optinteger(L, 7, 0); */
mysql_instance * db = mysqllib_state.mysql_init(NULL);
if (db != NULL) {
if (mysqllib_state.mysql_real_connect(db, host, username, password, database, port, socket, flag)) {
mysqllib_data *data = lua_newuserdatauv(L, sizeof(data), 0);
data->db = db ;
luaL_getmetatable(L, MYSQLLIB_METATABLE);
lua_setmetatable(L, -2);
return 1;
} else {
mysqllib_state.mysql_close(db);
}
}
}
return 0;
}
static int mysqllib_close(lua_State * L)
{
if (mysqllib_state.initialized) {
mysqllib_data * data = luaL_checkudata(L, 1, MYSQLLIB_METATABLE);
if (data != NULL) {
mysqllib_state.mysql_close(data->db);
data->db = NULL;
}
}
return 0;
}
/* execute(database,querystring,callback) : callback(nofcolumns,values,fields) */
static int mysqllib_execute(lua_State * L)
{
if (mysqllib_state.initialized) {
mysqllib_data * data = luaL_checkudata(L, 1, MYSQLLIB_METATABLE);
if (data != NULL) {
size_t length = 0;
const char *query = lua_tolstring(L, 2, &length);
if (query != NULL) {
int error = mysqllib_state.mysql_real_query(data->db, query, (int) length);
if (!error) {
mysql_result * result = mysqllib_state.mysql_store_result(data->db);
if (result != NULL) {
int nofrows = 0;
int nofcolumns = 0;
mysqllib_state.mysql_field_seek(result, 0);
nofrows = (int) mysqllib_state.mysql_num_rows(result);
nofcolumns = mysqllib_state.mysql_num_fields(result);
/* This is similar to sqlite but there the callback is more indirect. */
if (nofcolumns > 0 && nofrows > 0) {
for (int r = 0; r < nofrows; r++) {
mysql_row row = mysqllib_state.mysql_fetch_row(result);
lua_pushvalue(L, -1);
lua_pushinteger(L, nofcolumns);
lua_createtable(L, nofcolumns, 0);
for (int c = 0; c < nofcolumns; c++) {
lua_pushstring(L, row[c]);
lua_rawseti(L, -2, (lua_Integer)c + 1);
}
if (r) {
lua_call(L, 2, 0);
} else {
mysql_field * fields = mysqllib_state.mysql_fetch_fields(result);
lua_createtable(L, nofcolumns, 0);
for (int c = 0; c < nofcolumns; c++) {
lua_pushstring(L, fields[c].name);
lua_rawseti(L, -2, (lua_Integer)c + 1);
}
lua_call(L, 3, 0);
}
}
}
mysqllib_state.mysql_free_result(result);
}
lua_pushboolean(L, 1);
return 1;
}
}
}
}
lua_pushboolean(L, 0);
return 1;
}
static int mysqllib_getmessage(lua_State * L)
{
if (mysqllib_state.initialized) {
mysqllib_data * data = luaL_checkudata(L, 1, MYSQLLIB_METATABLE);
if (data != NULL) {
lua_pushstring(L, mysqllib_state.mysql_error(data->db));
return 1;
}
}
return 0;
}
/* private */
static int mysqllib_free(lua_State * L)
{
return mysqllib_close(L);
}
/* <string> = tostring(instance) */
static int mysqllib_tostring(lua_State * L)
{
if (mysqllib_state.initialized) {
mysqllib_data * data = luaL_checkudata(L, 1, MYSQLLIB_METATABLE);
if (data != NULL) {
(void) lua_pushfstring(L, "<mysqllib-instance %p>", data);
} else {
lua_pushnil(L);
}
return 1;
} else {
return 0;
}
}
static const struct luaL_Reg mysqllib_metatable[] = {
{ "__tostring", mysqllib_tostring },
{ "__gc", mysqllib_free },
{ NULL, NULL },
};
static struct luaL_Reg mysqllib_function_list[] = {
{ "initialize", mysqllib_initialize },
{ "open", mysqllib_open },
{ "close", mysqllib_close },
{ "execute", mysqllib_execute },
{ "getmessage", mysqllib_getmessage },
{ NULL, NULL },
};
int luaopen_mysql(lua_State * L)
{
luaL_newmetatable(L, MYSQLLIB_METATABLE);
luaL_setfuncs(L, mysqllib_metatable, 0);
lmt_library_register(L, "mysql", mysqllib_function_list);
return 0;
}
|