Software /
code /
prosody
Comparison
util/sqlite3.lua @ 13239:f2578a69ccf4
util.sqlite3: Clean up unused variables
Many leftovers from the earlier version of util.sql this was based on
and cleanup applied there since then.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Jul 2023 14:54:17 +0200 |
parent | 13147:e560f7c691ce |
comparison
equal
deleted
inserted
replaced
13238:26327eac56dc | 13239:f2578a69ccf4 |
---|---|
1 | 1 |
2 -- luacheck: ignore 113/unpack 211 212 411 213 | |
3 local setmetatable, getmetatable = setmetatable, getmetatable; | 2 local setmetatable, getmetatable = setmetatable, getmetatable; |
4 local ipairs, unpack, select = ipairs, table.unpack or unpack, select; | 3 local ipairs, select = ipairs, select; |
5 local tonumber, tostring = tonumber, tostring; | 4 local tostring = tostring; |
6 local assert, xpcall, debug_traceback = assert, xpcall, debug.traceback; | 5 local assert, xpcall, debug_traceback = assert, xpcall, debug.traceback; |
7 local error = error | 6 local error = error |
8 local type = type | 7 local type = type |
9 local t_concat = table.concat; | 8 local t_concat = table.concat; |
10 local t_insert = table.insert; | |
11 local s_char = string.char; | |
12 local array = require "prosody.util.array"; | 9 local array = require "prosody.util.array"; |
13 local log = require "prosody.util.logger".init("sql"); | 10 local log = require "prosody.util.logger".init("sql"); |
14 | 11 |
15 local lsqlite3 = require "lsqlite3"; | 12 local lsqlite3 = require "lsqlite3"; |
16 local build_url = require "socket.url".build; | 13 local build_url = require "socket.url".build; |
17 local ROW, DONE = lsqlite3.ROW, lsqlite3.DONE; | |
18 | 14 |
19 -- from sqlite3.h, no copyright claimed | 15 -- from sqlite3.h, no copyright claimed |
20 local sqlite_errors = require"prosody.util.error".init("util.sqlite3", { | 16 local sqlite_errors = require"prosody.util.error".init("util.sqlite3", { |
21 -- FIXME xmpp error conditions? | 17 -- FIXME xmpp error conditions? |
22 [1] = { code = 1; type = "modify"; condition = "ERROR"; text = "Generic error" }; | 18 [1] = { code = 1; type = "modify"; condition = "ERROR"; text = "Generic error" }; |
49 [28] = { code = 28; type = "continue"; condition = "WARNING"; text = "Warnings from sqlite3_log()" }; | 45 [28] = { code = 28; type = "continue"; condition = "WARNING"; text = "Warnings from sqlite3_log()" }; |
50 [100] = { code = 100; type = "continue"; condition = "ROW"; text = "sqlite3_step() has another row ready" }; | 46 [100] = { code = 100; type = "continue"; condition = "ROW"; text = "sqlite3_step() has another row ready" }; |
51 [101] = { code = 101; type = "continue"; condition = "DONE"; text = "sqlite3_step() has finished executing" }; | 47 [101] = { code = 101; type = "continue"; condition = "DONE"; text = "sqlite3_step() has finished executing" }; |
52 }); | 48 }); |
53 | 49 |
50 -- luacheck: ignore 411/assert | |
54 local assert = function(cond, errno, err) | 51 local assert = function(cond, errno, err) |
55 return assert(sqlite_errors.coerce(cond, err or errno)); | 52 return assert(sqlite_errors.coerce(cond, err or errno)); |
56 end | 53 end |
57 local _ENV = nil; | 54 local _ENV = nil; |
58 -- luacheck: std none | 55 -- luacheck: std none |
65 | 62 |
66 local function is_column(x) return getmetatable(x)==column_mt; end | 63 local function is_column(x) return getmetatable(x)==column_mt; end |
67 local function is_index(x) return getmetatable(x)==index_mt; end | 64 local function is_index(x) return getmetatable(x)==index_mt; end |
68 local function is_table(x) return getmetatable(x)==table_mt; end | 65 local function is_table(x) return getmetatable(x)==table_mt; end |
69 local function is_query(x) return getmetatable(x)==query_mt; end | 66 local function is_query(x) return getmetatable(x)==query_mt; end |
70 local function Integer(n) return "Integer()" end | |
71 local function String(n) return "String()" end | |
72 | 67 |
73 local function Column(definition) | 68 local function Column(definition) |
74 return setmetatable(definition, column_mt); | 69 return setmetatable(definition, column_mt); |
75 end | 70 end |
76 local function Table(definition) | 71 local function Table(definition) |
88 return setmetatable(definition, index_mt); | 83 return setmetatable(definition, index_mt); |
89 end | 84 end |
90 | 85 |
91 function table_mt:__tostring() | 86 function table_mt:__tostring() |
92 local s = { 'name="'..self.__table__.name..'"' } | 87 local s = { 'name="'..self.__table__.name..'"' } |
93 for i,col in ipairs(self.__table__) do | 88 for _, col in ipairs(self.__table__) do |
94 s[#s+1] = tostring(col); | 89 s[#s+1] = tostring(col); |
95 end | 90 end |
96 return 'Table{ '..t_concat(s, ", ")..' }' | 91 return 'Table{ '..t_concat(s, ", ")..' }' |
97 end | 92 end |
98 table_mt.__index = {}; | 93 table_mt.__index = {}; |
99 function table_mt.__index:create(engine) | 94 function table_mt.__index:create(engine) |
100 return engine:_create_table(self); | 95 return engine:_create_table(self); |
101 end | |
102 function table_mt:__call(...) | |
103 -- TODO | |
104 end | 96 end |
105 function column_mt:__tostring() | 97 function column_mt:__tostring() |
106 return 'Column{ name="'..self.name..'", type="'..self.type..'" }' | 98 return 'Column{ name="'..self.name..'", type="'..self.type..'" }' |
107 end | 99 end |
108 function index_mt:__tostring() | 100 function index_mt:__tostring() |
109 local s = 'Index{ name="'..self.name..'"'; | 101 local s = 'Index{ name="'..self.name..'"'; |
110 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end | 102 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end |
111 return s..' }'; | 103 return s..' }'; |
112 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' | 104 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' |
113 end | |
114 | |
115 local function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return s_char(tonumber(c,16)); end)); end | |
116 local function parse_url(url) | |
117 local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)"); | |
118 assert(scheme, "Invalid URL format"); | |
119 local username, password, host, port; | |
120 local authpart, hostpart = secondpart:match("([^@]+)@([^@+])"); | |
121 if not authpart then hostpart = secondpart; end | |
122 if authpart then | |
123 username, password = authpart:match("([^:]*):(.*)"); | |
124 username = username or authpart; | |
125 password = password and urldecode(password); | |
126 end | |
127 if hostpart then | |
128 host, port = hostpart:match("([^:]*):(.*)"); | |
129 host = host or hostpart; | |
130 port = port and assert(tonumber(port), "Invalid URL format"); | |
131 end | |
132 return { | |
133 scheme = scheme:lower(); | |
134 username = username; password = password; | |
135 host = host; port = port; | |
136 database = #database > 0 and database or nil; | |
137 }; | |
138 end | 105 end |
139 | 106 |
140 local engine = {}; | 107 local engine = {}; |
141 function engine:connect() | 108 function engine:connect() |
142 if self.conn then return true; end | 109 if self.conn then return true; end |
155 if ok == false then | 122 if ok == false then |
156 return ok, err; | 123 return ok, err; |
157 end | 124 end |
158 return true; | 125 return true; |
159 end | 126 end |
160 function engine:onconnect() | 127 function engine:onconnect() -- luacheck: ignore 212/self |
161 -- Override from create_engine() | 128 -- Override from create_engine() |
162 end | 129 end |
163 function engine:ondisconnect() -- luacheck: ignore 212/self | 130 function engine:ondisconnect() -- luacheck: ignore 212/self |
164 -- Override from create_engine() | 131 -- Override from create_engine() |
165 end | 132 end |
166 | 133 |
167 function engine:execute(sql, ...) | 134 function engine:execute(sql, ...) |
168 local success, err = self:connect(); | 135 local success, err = self:connect(); |
169 if not success then return success, err; end | 136 if not success then return success, err; end |
170 local prepared = self.prepared; | |
171 | 137 |
172 if select('#', ...) == 0 then | 138 if select('#', ...) == 0 then |
173 local ret = self.conn:exec(sql); | 139 local ret = self.conn:exec(sql); |
174 if ret ~= lsqlite3.OK then | 140 if ret ~= lsqlite3.OK then |
175 local err = sqlite_errors.new(err); | 141 local err = sqlite_errors.new(err); |
354 if self._debug then | 320 if self._debug then |
355 debugquery("create", sql); | 321 debugquery("create", sql); |
356 end | 322 end |
357 local success,err = self:execute(sql); | 323 local success,err = self:execute(sql); |
358 if not success then return success,err; end | 324 if not success then return success,err; end |
359 for i,v in ipairs(table.__table__) do | 325 for _, v in ipairs(table.__table__) do |
360 if is_index(v) then | 326 if is_index(v) then |
361 self:_create_index(v); | 327 self:_create_index(v); |
362 end | 328 end |
363 end | 329 end |
364 return success; | 330 return success; |
394 return { | 360 return { |
395 is_column = is_column; | 361 is_column = is_column; |
396 is_index = is_index; | 362 is_index = is_index; |
397 is_table = is_table; | 363 is_table = is_table; |
398 is_query = is_query; | 364 is_query = is_query; |
399 Integer = Integer; | |
400 String = String; | |
401 Column = Column; | 365 Column = Column; |
402 Table = Table; | 366 Table = Table; |
403 Index = Index; | 367 Index = Index; |
404 create_engine = create_engine; | 368 create_engine = create_engine; |
405 db2uri = db2uri; | 369 db2uri = db2uri; |