Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*	$NetBSD: sqlite.c,v 1.10 2019/05/16 12:42:35 tpaul Exp $ */

/*
 * Copyright (c) 2011, 2013, 2016, 2017 Marc Balmer <marc@msys.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* SQLite interface for Lua */

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#define SQLITE_DB_METATABLE "SQLite database connection methods"
#define SQLITE_STMT_METATABLE "SQLite statement methods"

int luaopen_sqlite(lua_State*);

static __printflike(2, 3) void
sqlite_error(lua_State *L, const char *fmt, ...)
{
	va_list ap;
	int len;
	char *msg;

	va_start(ap, fmt);
	len = vasprintf(&msg, fmt, ap);
	va_end(ap);

	if (len != -1) {
		lua_pushstring(L, msg);
		free(msg);
	} else
		lua_pushstring(L, "vasprintf failed");
	lua_error(L);
}

static int
sqlite_initialize(lua_State *L)
{
	lua_pushinteger(L, sqlite3_initialize());
	return 1;
}

static int
sqlite_shutdown(lua_State *L)
{
	lua_pushinteger(L, sqlite3_shutdown());
	return 1;
}

static int
sqlite_open(lua_State *L)
{
	sqlite3 **db;

	db = lua_newuserdata(L, sizeof(sqlite3 *));
	luaL_getmetatable(L, SQLITE_DB_METATABLE);
	lua_setmetatable(L, -2);

	if (lua_gettop(L) > 2)
		lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db,
		    (int)luaL_checkinteger(L, -2), NULL));
	else
		lua_pushinteger(L, sqlite3_open(luaL_checkstring(L, -2), db));
	return 2;

}

static int
sqlite_libversion(lua_State *L)
{
	lua_pushstring(L, sqlite3_libversion());
	return 1;
}

static int
sqlite_libversion_number(lua_State *L)
{
	lua_pushinteger(L, sqlite3_libversion_number());
	return 1;
}

static int
sqlite_sourceid(lua_State *L)
{
	lua_pushstring(L, sqlite3_sourceid());
	return 1;
}

static int
db_close(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	if (*db) {
		lua_pushinteger(L, sqlite3_close(*db));
		*db = NULL;
	} else
		lua_pushnil(L);
	return 1;

}

static int
db_prepare(lua_State *L)
{
	sqlite3 **db;
	sqlite3_stmt **stmt;
	const char *sql;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *));
	sql = luaL_checkstring(L, 2);
	lua_pushinteger(L, sqlite3_prepare_v2(*db, sql,
	    (int)strlen(sql) + 1, stmt, NULL));
	luaL_getmetatable(L, SQLITE_STMT_METATABLE);
	lua_setmetatable(L, -3);
	return 2;

}

static int
db_exec(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL,
	    NULL, NULL));
	return 1;
}

static int
db_errcode(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	lua_pushinteger(L, sqlite3_errcode(*db));
	return 1;
}

static int
db_errmsg(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	lua_pushstring(L, sqlite3_errmsg(*db));
	return 1;
}

static int
db_get_autocommit(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	lua_pushboolean(L, sqlite3_get_autocommit(*db));
	return 1;
}

static int
db_changes(lua_State *L)
{
	sqlite3 **db;

	db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
	lua_pushinteger(L, sqlite3_changes(*db));
	return 1;
}

static int
stmt_bind(lua_State *L)
{
	sqlite3_stmt **stmt;
	int pidx;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	pidx = (int)luaL_checkinteger(L, 2);

	switch (lua_type(L, 3)) {
	case LUA_TNUMBER:
		lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx,
		    lua_tonumber(L, 3)));
		break;
	case LUA_TSTRING:
		lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx,
		    lua_tostring(L, 3), -1, SQLITE_TRANSIENT));
		break;
	case LUA_TNIL:
		lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx));
		break;
	default:
		sqlite_error(L, "unsupported data type %s",
		    luaL_typename(L, 3));
	}
	return 1;
}

static int
stmt_bind_parameter_count(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
	return 1;
}

static int
stmt_bind_parameter_index(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt,
	    lua_tostring(L, 2)));
	return 1;
}

static int
stmt_bind_parameter_name(lua_State *L)
{
	sqlite3_stmt **stmt;
	int pidx;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	pidx = (int)luaL_checkinteger(L, 2);
	lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx));
	return 1;
}

static int
stmt_step(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	lua_pushinteger(L, sqlite3_step(*stmt));
	return 1;
}

static int
stmt_column_name(lua_State *L)
{
	sqlite3_stmt **stmt;
	int cidx;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	cidx = (int)luaL_checkinteger(L, 2) - 1;

	lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
	return 1;
}

static int
stmt_column_count(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	lua_pushinteger(L, sqlite3_column_count(*stmt));
	return 1;
}

static int
stmt_column(lua_State *L)
{
	sqlite3_stmt **stmt;
	int cidx;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	cidx = (int)luaL_checkinteger(L, 2) - 1;

	switch (sqlite3_column_type(*stmt, cidx)) {
	case SQLITE_INTEGER:
		lua_pushinteger(L, sqlite3_column_int(*stmt, cidx));
		break;
	case SQLITE_FLOAT:
		lua_pushnumber(L, sqlite3_column_double(*stmt, cidx));
		break;
	case SQLITE_TEXT:
		lua_pushstring(L, (const char *)sqlite3_column_text(*stmt,
		    cidx));
		break;
	case SQLITE_BLOB:
	case SQLITE_NULL:
		lua_pushnil(L);
		break;
	}
	return 1;
}

static int
stmt_reset(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	sqlite3_reset(*stmt);
	return 0;
}

static int
stmt_clear_bindings(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	if (*stmt) {
		sqlite3_clear_bindings(*stmt);
		*stmt = NULL;
	}
	return 0;
}

static int
stmt_finalize(lua_State *L)
{
	sqlite3_stmt **stmt;

	stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
	if (*stmt) {
		sqlite3_finalize(*stmt);
		*stmt = NULL;
	}
	return 0;
}

struct constant {
	const char *name;
	int value;
};

static const struct constant sqlite_constant[] = {
	/* SQLite return codes */
	{ "OK",			SQLITE_OK },
	{ "ERROR",		SQLITE_ERROR },
	{ "INTERNAL",		SQLITE_INTERNAL },
	{ "PERM",		SQLITE_PERM },
	{ "ABORT",		SQLITE_ABORT },
	{ "BUSY",		SQLITE_BUSY },
	{ "LOCKED",		SQLITE_LOCKED },
	{ "NOMEM",		SQLITE_NOMEM },
	{ "READONLY",		SQLITE_READONLY },
	{ "INTERRUPT",		SQLITE_INTERRUPT },
	{ "IOERR",		SQLITE_IOERR },
	{ "CORRUPT",		SQLITE_CORRUPT },
	{ "NOTFOUND",		SQLITE_NOTFOUND },
	{ "FULL",		SQLITE_FULL },
	{ "CANTOPEN",		SQLITE_CANTOPEN },
	{ "PROTOCOL",		SQLITE_PROTOCOL },
	{ "EMPTY",		SQLITE_EMPTY },
	{ "SCHEMA",		SQLITE_SCHEMA },
	{ "TOOBIG",		SQLITE_TOOBIG },
	{ "CONSTRAINT",		SQLITE_CONSTRAINT },
	{ "MISMATCH",		SQLITE_MISMATCH },
	{ "MISUSE",		SQLITE_MISUSE },
	{ "NOLFS",		SQLITE_NOLFS },
	{ "AUTH",		SQLITE_AUTH },
	{ "FORMAT",		SQLITE_FORMAT },
	{ "RANGE",		SQLITE_RANGE },
	{ "NOTADB",		SQLITE_NOTADB },
	{ "ROW",		SQLITE_ROW },
	{ "DONE",		SQLITE_DONE },

	/* File modes */
	{ "OPEN_READONLY",	SQLITE_OPEN_READONLY },
	{ "OPEN_READWRITE",	SQLITE_OPEN_READWRITE },
	{ "OPEN_CREATE",	SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },

	{ NULL,			0 }
};

static void
sqlite_set_info(lua_State *L)
{
	lua_pushliteral(L, "_COPYRIGHT");
	lua_pushliteral(L, "Copyright (C) 2011, 2012, 2013 by "
	    "Marc Balmer <marc@msys.ch>");
	lua_settable(L, -3);
	lua_pushliteral(L, "_DESCRIPTION");
	lua_pushliteral(L, "SQLite interface for Lua");
	lua_settable(L, -3);
	lua_pushliteral(L, "_VERSION");
	lua_pushliteral(L, "sqlite 1.0.3");
	lua_settable(L, -3);
}

int
luaopen_sqlite(lua_State* L)
{
	static const struct luaL_Reg sqlite_methods[] = {
		{ "initialize",			sqlite_initialize },
		{ "shutdown",			sqlite_shutdown },
		{ "open",			sqlite_open },
		{ "libversion",			sqlite_libversion },
		{ "libversion_number",		sqlite_libversion_number },
		{ "sourceid",			sqlite_sourceid },
		{ NULL,				NULL }
	};
	static const struct luaL_Reg db_methods[] = {
		{ "close",			db_close },
		{ "prepare",			db_prepare },
		{ "exec",			db_exec },
		{ "errcode",			db_errcode },
		{ "errmsg",			db_errmsg },
		{ "get_autocommit",		db_get_autocommit },
		{ "changes",			db_changes },
		{ NULL,				NULL }
	};
	static const struct luaL_Reg stmt_methods[] = {
		{ "bind",			stmt_bind },
		{ "bind_parameter_count",	stmt_bind_parameter_count },
		{ "bind_parameter_index",	stmt_bind_parameter_index },
		{ "bind_parameter_name",	stmt_bind_parameter_name },
		{ "step",			stmt_step },
		{ "column",			stmt_column },
		{ "reset",			stmt_reset },
		{ "clear_bindings",		stmt_clear_bindings },
		{ "finalize",			stmt_finalize },
		{ "column_name",		stmt_column_name },
		{ "column_count",		stmt_column_count },
		{ NULL,		NULL }
	};
	int n;

	sqlite3_initialize();

	luaL_newlib(L, sqlite_methods);
	luaL_setfuncs(L, db_methods, 0);
	luaL_setfuncs(L, stmt_methods, 0);
	sqlite_set_info(L);

	/* The database connection metatable */
	if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) {
		luaL_setfuncs(L, db_methods, 0);

		lua_pushliteral(L, "__gc");
		lua_pushcfunction(L, db_close);
		lua_settable(L, -3);

		lua_pushliteral(L, "__index");
		lua_pushvalue(L, -2);
		lua_settable(L, -3);

		lua_pushliteral(L, "__metatable");
		lua_pushliteral(L, "must not access this metatable");
		lua_settable(L, -3);
	}
	lua_pop(L, 1);

	/* The statement metatable */
	if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) {
		luaL_setfuncs(L, stmt_methods, 0);

		lua_pushliteral(L, "__gc");
		lua_pushcfunction(L, stmt_finalize);
		lua_settable(L, -3);

		lua_pushliteral(L, "__index");
		lua_pushvalue(L, -2);
		lua_settable(L, -3);

		lua_pushliteral(L, "__metatable");
		lua_pushliteral(L, "must not access this metatable");
		lua_settable(L, -3);
	}
	lua_pop(L, 1);

	for (n = 0; sqlite_constant[n].name != NULL; n++) {
		lua_pushinteger(L, sqlite_constant[n].value);
		lua_setfield(L, -2, sqlite_constant[n].name);
	};
	return 1;
}