Software / code / prosody
Annotate
util/sqlite3.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parent | 13632:844e7bf7b48a |
| rev | line source |
|---|---|
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local setmetatable, getmetatable = setmetatable, getmetatable; |
|
13239
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
3 local ipairs, select = ipairs, select; |
|
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
4 local tostring = tostring; |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local assert, xpcall, debug_traceback = assert, xpcall, debug.traceback; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local error = error |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local type = type |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local t_concat = table.concat; |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
9 local array = require "prosody.util.array"; |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12872
diff
changeset
|
10 local log = require "prosody.util.logger".init("sql"); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local lsqlite3 = require "lsqlite3"; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local build_url = require "socket.url".build; |
|
12847
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
14 |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
15 -- from sqlite3.h, no copyright claimed |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12872
diff
changeset
|
16 local sqlite_errors = require"prosody.util.error".init("util.sqlite3", { |
|
12847
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
17 -- FIXME xmpp error conditions? |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
18 [1] = { code = 1; type = "modify"; condition = "ERROR"; text = "Generic error" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
19 [2] = { code = 2; type = "cancel"; condition = "INTERNAL"; text = "Internal logic error in SQLite" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
20 [3] = { code = 3; type = "auth"; condition = "PERM"; text = "Access permission denied" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
21 [4] = { code = 4; type = "cancel"; condition = "ABORT"; text = "Callback routine requested an abort" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
22 [5] = { code = 5; type = "wait"; condition = "BUSY"; text = "The database file is locked" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
23 [6] = { code = 6; type = "wait"; condition = "LOCKED"; text = "A table in the database is locked" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
24 [7] = { code = 7; type = "wait"; condition = "NOMEM"; text = "A malloc() failed" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
25 [8] = { code = 8; type = "cancel"; condition = "READONLY"; text = "Attempt to write a readonly database" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
26 [9] = { code = 9; type = "cancel"; condition = "INTERRUPT"; text = "Operation terminated by sqlite3_interrupt()" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
27 [10] = { code = 10; type = "wait"; condition = "IOERR"; text = "Some kind of disk I/O error occurred" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
28 [11] = { code = 11; type = "cancel"; condition = "CORRUPT"; text = "The database disk image is malformed" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
29 [12] = { code = 12; type = "modify"; condition = "NOTFOUND"; text = "Unknown opcode in sqlite3_file_control()" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
30 [13] = { code = 13; type = "wait"; condition = "FULL"; text = "Insertion failed because database is full" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
31 [14] = { code = 14; type = "auth"; condition = "CANTOPEN"; text = "Unable to open the database file" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
32 [15] = { code = 15; type = "cancel"; condition = "PROTOCOL"; text = "Database lock protocol error" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
33 [16] = { code = 16; type = "continue"; condition = "EMPTY"; text = "Internal use only" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
34 [17] = { code = 17; type = "modify"; condition = "SCHEMA"; text = "The database schema changed" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
35 [18] = { code = 18; type = "modify"; condition = "TOOBIG"; text = "String or BLOB exceeds size limit" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
36 [19] = { code = 19; type = "modify"; condition = "CONSTRAINT"; text = "Abort due to constraint violation" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
37 [20] = { code = 20; type = "modify"; condition = "MISMATCH"; text = "Data type mismatch" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
38 [21] = { code = 21; type = "modify"; condition = "MISUSE"; text = "Library used incorrectly" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
39 [22] = { code = 22; type = "cancel"; condition = "NOLFS"; text = "Uses OS features not supported on host" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
40 [23] = { code = 23; type = "auth"; condition = "AUTH"; text = "Authorization denied" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
41 [24] = { code = 24; type = "modify"; condition = "FORMAT"; text = "Not used" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
42 [25] = { code = 25; type = "modify"; condition = "RANGE"; text = "2nd parameter to sqlite3_bind out of range" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
43 [26] = { code = 26; type = "cancel"; condition = "NOTADB"; text = "File opened that is not a database file" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
44 [27] = { code = 27; type = "continue"; condition = "NOTICE"; text = "Notifications from sqlite3_log()" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
45 [28] = { code = 28; type = "continue"; condition = "WARNING"; text = "Warnings from sqlite3_log()" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
46 [100] = { code = 100; type = "continue"; condition = "ROW"; text = "sqlite3_step() has another row ready" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
47 [101] = { code = 101; type = "continue"; condition = "DONE"; text = "sqlite3_step() has finished executing" }; |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
48 }); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
|
13239
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
50 -- luacheck: ignore 411/assert |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 local assert = function(cond, errno, err) |
|
12847
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
52 return assert(sqlite_errors.coerce(cond, err or errno)); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 local _ENV = nil; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 -- luacheck: std none |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 local column_mt = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 local table_mt = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local query_mt = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 --local op_mt = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local index_mt = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 local function is_column(x) return getmetatable(x)==column_mt; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 local function is_index(x) return getmetatable(x)==index_mt; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local function is_table(x) return getmetatable(x)==table_mt; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 local function is_query(x) return getmetatable(x)==query_mt; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 local function Column(definition) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 return setmetatable(definition, column_mt); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local function Table(definition) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 local c = {} |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 for i,col in ipairs(definition) do |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 if is_column(col) then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 c[i], c[col.name] = col, col; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 elseif is_index(col) then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 col.table = definition.name; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 return setmetatable({ __table__ = definition, c = c, name = definition.name }, table_mt); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 local function Index(definition) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return setmetatable(definition, index_mt); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 function table_mt:__tostring() |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 local s = { 'name="'..self.__table__.name..'"' } |
|
13239
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
88 for _, col in ipairs(self.__table__) do |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 s[#s+1] = tostring(col); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 return 'Table{ '..t_concat(s, ", ")..' }' |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 table_mt.__index = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 function table_mt.__index:create(engine) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 return engine:_create_table(self); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 function column_mt:__tostring() |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 return 'Column{ name="'..self.name..'", type="'..self.type..'" }' |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 function index_mt:__tostring() |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local s = 'Index{ name="'..self.name..'"'; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 return s..' }'; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 local engine = {}; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 function engine:connect() |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 if self.conn then return true; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 local params = self.params; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 assert(params.driver == "SQLite3", "Only sqlite3 is supported"); |
|
12847
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
113 local dbh, err = sqlite_errors.coerce(lsqlite3.open(params.database)); |
|
d6cdde74cd9b
util.sqlite3: Create util.error registry from headers
Kim Alvefur <zash@zash.se>
parents:
12845
diff
changeset
|
114 if not dbh then return nil, err; end |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 self.conn = dbh; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 self.prepared = {}; |
| 13632 | 117 if params.password then |
| 118 local ok, err = self:execute(("PRAGMA key='%s'"):format((params.password:gsub("'", "''")))); | |
| 119 if not ok then | |
| 120 return ok, err; | |
| 121 end | |
| 122 end | |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 local ok, err = self:set_encoding(); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 if not ok then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 return ok, err; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 local ok, err = self:onconnect(); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 if ok == false then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 return ok, err; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 return true; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 end |
|
13239
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
133 function engine:onconnect() -- luacheck: ignore 212/self |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 -- Override from create_engine() |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 end |
|
12872
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
136 function engine:ondisconnect() -- luacheck: ignore 212/self |
|
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
137 -- Override from create_engine() |
|
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
138 end |
|
13147
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
139 |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 function engine:execute(sql, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 local success, err = self:connect(); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 if not success then return success, err; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
|
12848
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
144 if select('#', ...) == 0 then |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
145 local ret = self.conn:exec(sql); |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
146 if ret ~= lsqlite3.OK then |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
147 local err = sqlite_errors.new(err); |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
148 err.text = self.conn:errmsg(); |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
149 return err; |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
150 end |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
151 return true; |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
152 end |
|
ccb030d988ac
util.sqlite3: Skip prepared statements when no parameters are given
Kim Alvefur <zash@zash.se>
parents:
12847
diff
changeset
|
153 |
|
13147
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
154 local stmt, err = self.conn:prepare(sql); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 if not stmt then |
|
13147
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
156 err = sqlite_errors.new(err); |
|
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
157 err.text = self.conn:errmsg(); |
|
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
158 return stmt, err; |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 local ret = stmt:bind_values(...); |
|
13147
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
162 if ret ~= lsqlite3.OK then |
|
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
163 return nil, sqlite_errors.new(ret, { message = self.conn:errmsg() }); |
|
e560f7c691ce
util.sqlite3: Don't cache prepared statements for one-off queries
Kim Alvefur <zash@zash.se>
parents:
13146
diff
changeset
|
164 end |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 return stmt; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 local function iterator(table) |
| 13145 | 169 local i = 0; |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 return function() |
| 13145 | 171 i = i + 1; |
| 172 local item = table[i]; | |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 if item ~= nil then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 return item; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
179 local result_mt = { |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
180 __len = function(self) |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
181 return self.__rowcount; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
182 end; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
183 __index = { |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
184 affected = function(self) |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
185 return self.__affected; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
186 end; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
187 rowcount = function(self) |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
188 return self.__rowcount; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
189 end; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
190 }; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
191 __call = function(self) |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
192 return iterator(self.__data); |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
193 end; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
194 }; |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
195 |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 local function debugquery(where, sql, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 local i = 0; local a = {...} |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 sql = sql:gsub("\n?\t+", " "); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 log("debug", "[%s] %s", where, (sql:gsub("%?", function () |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 i = i + 1; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 local v = a[i]; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 if type(v) == "string" then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 v = ("'%s'"):format(v:gsub("'", "''")); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 return tostring(v); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 end))); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
209 function engine:execute_update(sql, ...) |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 local prepared = self.prepared; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 local stmt = prepared[sql]; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 if stmt and stmt:isopen() then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 prepared[sql] = nil; -- Can't be used concurrently |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 else |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 stmt = assert(self.conn:prepare(sql)); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 local ret = stmt:bind_values(...); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 if ret ~= lsqlite3.OK then error(self.conn:errmsg()); end |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
219 local data = array(); |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
220 for row in stmt:rows() do |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
221 data:push(array(row)); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 -- FIXME Error handling, BUSY, ERROR, MISUSE |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 if stmt:reset() == lsqlite3.OK then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 prepared[sql] = stmt; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 end |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
227 local affected = self.conn:changes(); |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
228 return setmetatable({ __affected = affected; __rowcount = #data; __data = data }, result_mt); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 end |
|
13146
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
230 |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
231 function engine:execute_query(sql, ...) |
|
771eb453e03a
util.sqlite3: Deduplicate query methods
Kim Alvefur <zash@zash.se>
parents:
13145
diff
changeset
|
232 return self:execute_update(sql, ...)() |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 end |
| 13145 | 234 |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 engine.insert = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 engine.select = engine.execute_query; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 engine.delete = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 engine.update = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 local function debugwrap(name, f) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 return function (self, sql, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 debugquery(name, sql, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 return f(self, sql, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 function engine:debug(enable) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 self._debug = enable; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 if enable then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 engine.insert = debugwrap("insert", engine.execute_update); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 engine.select = debugwrap("select", engine.execute_query); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 engine.delete = debugwrap("delete", engine.execute_update); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 engine.update = debugwrap("update", engine.execute_update); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 else |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 engine.insert = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 engine.select = engine.execute_query; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 engine.delete = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 engine.update = engine.execute_update; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 function engine:_(word) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 local ret = self.conn:exec(word); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 if ret ~= lsqlite3.OK then return nil, self.conn:errmsg(); end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 return true; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 function engine:_transaction(func, ...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 if not self.conn then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 local a,b = self:connect(); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 if not a then return a,b; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 --assert(not self.__transaction, "Recursive transactions not allowed"); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 local ok, err = self:_"BEGIN"; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 if not ok then return ok, err; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 self.__transaction = true; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 local success, a, b, c = xpcall(func, debug_traceback, ...); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 self.__transaction = nil; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 if success then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 log("debug", "SQL transaction success [%s]", tostring(func)); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 local ok, err = self:_"COMMIT"; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 if not ok then return ok, err; end -- commit failed |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 return success, a, b, c; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 else |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 log("debug", "SQL transaction failure [%s]: %s", tostring(func), a); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 if self.conn then self:_"ROLLBACK"; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 return success, a; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 function engine:transaction(...) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 local ok, ret = self:_transaction(...); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 if not ok then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 local conn = self.conn; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 if not conn or not conn:isopen() then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 self.conn = nil; |
|
12872
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
292 self:ondisconnect(); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 ok, ret = self:_transaction(...); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 return ok, ret; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 function engine:_create_index(index) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 local sql = "CREATE INDEX IF NOT EXISTS \""..index.name.."\" ON \""..index.table.."\" ("; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 for i=1,#index do |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 sql = sql.."\""..index[i].."\""; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 if i ~= #index then sql = sql..", "; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 sql = sql..");" |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 if index.unique then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 sql = sql:gsub("^CREATE", "CREATE UNIQUE"); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 if self._debug then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 debugquery("create", sql); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 return self:execute(sql); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 function engine:_create_table(table) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 local sql = "CREATE TABLE IF NOT EXISTS \""..table.name.."\" ("; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 for i,col in ipairs(table.c) do |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 local col_type = col.type; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 sql = sql.."\""..col.name.."\" "..col_type; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 if col.nullable == false then sql = sql.." NOT NULL"; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 if col.primary_key == true then sql = sql.." PRIMARY KEY"; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 if col.auto_increment == true then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 sql = sql.." AUTOINCREMENT"; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 if i ~= #table.c then sql = sql..", "; end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 sql = sql.. ");" |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 if self._debug then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 debugquery("create", sql); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 local success,err = self:execute(sql); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 if not success then return success,err; end |
|
13239
f2578a69ccf4
util.sqlite3: Clean up unused variables
Kim Alvefur <zash@zash.se>
parents:
13147
diff
changeset
|
331 for _, v in ipairs(table.__table__) do |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 if is_index(v) then |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 self:_create_index(v); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 return success; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 end |
| 13145 | 338 |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 function engine:set_encoding() -- to UTF-8 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 return self:transaction(function() |
| 13145 | 341 for encoding in self:select "PRAGMA encoding;" do |
| 342 if encoding[1] == "UTF-8" then | |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 self.charset = "utf8"; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 end); |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 local engine_mt = { __index = engine }; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 local function db2uri(params) |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 return build_url{ |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 scheme = params.driver, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 user = params.username, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 password = params.password, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 host = params.host, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 port = params.port, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 path = params.database, |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 }; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 |
|
12872
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
361 local function create_engine(_, params, onconnect, ondisconnect) |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 assert(params.driver == "SQLite3", "Only SQLite3 is supported without LuaDBI"); |
|
12872
a20923f7d5fd
mod_storage_sql: Record connection to database as module status
Kim Alvefur <zash@zash.se>
parents:
12848
diff
changeset
|
363 return setmetatable({ url = db2uri(params); params = params; onconnect = onconnect; ondisconnect = ondisconnect }, engine_mt); |
|
12845
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 end |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 return { |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 is_column = is_column; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
368 is_index = is_index; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 is_table = is_table; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 is_query = is_query; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 Column = Column; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 Table = Table; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 Index = Index; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 create_engine = create_engine; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 db2uri = db2uri; |
|
f306336b7e99
util.sqlite3: SQLite3-only variant of util.sql using LuaSQLite3
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 }; |