/usr/share/tarantool/box/net/sql.lua is in tarantool-lts-modules 1.5.5.37.g1687c02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | -- sql.lua (internal file)
box.net.sql = {
-- constructor
-- box.net.sql.connect(
-- 'pg', -- @driver ('pg' or 'mysql')
-- 'my.host', -- @host
-- 5432, -- @port
-- 'user', -- @username
-- 'SECRET', -- @password
-- 'DB', -- @database name
-- { raise = false }, -- @config options
-- { sql1, var, ... var }, -- @startup SQL statements
-- ...
-- )
--
-- @return connector to database or throw error
-- if option raise set in 'false' and an error will be happened
-- the function will return 'nil' as the first variable and
-- text of error as the second
connect = function(driver, host, port, user, password, db, cfg, ...)
if type(driver) == 'table' then
driver = driver.driver
end
if type(box.net.sql.connectors[driver]) ~= 'function' then
error(string.format("Unknown driver '%s'", driver))
end
local self = {
-- connection variables
driver = driver,
host = host,
port = port,
user = user,
password = password,
db = db,
-- private variables
queue = {},
processing = false,
-- throw exception if error
raise = true
}
-- config parameters
if type(cfg) == 'table' then
if type(cfg.raise) == 'boolean' then
self.raise = cfg.raise
end
end
local init = { ... }
setmetatable(self, box.net.sql)
-- it add 'raw' field in the table
local s, c = pcall(box.net.sql.connectors[driver], self)
if not s then
if self.raise then
error(c)
end
return nil, c
end
-- perform init statements
for i, s in pairs(init) do
c:execute(unpack(s))
end
return c
end,
connectors = { },
__index = {
-- base method
-- example:
-- local tuples, arows, txtst = db:execute(sql, args)
-- tuples - a table of tuples (tables)
-- arows - count of affected rows
-- txtst - text status (Postgresql specific)
-- the method throws exception by default.
-- user can change the behaviour by set 'connection.raise'
-- attribute to 'false'
-- in the case it will return negative arows if error and
-- txtst will contain text of error
execute = function(self, sql, ...)
-- waits until connection will be free
while self.processing do
self.queue[ box.fiber.id() ] = box.ipc.channel()
self.queue[ box.fiber.id() ]:get()
self.queue[ box.fiber.id() ] = nil
end
self.processing = true
local res = { pcall(self.raw.execute, self, sql, ...) }
self.processing = false
if not res[1] then
if self.raise then
error(res[2])
end
return {}, -1, res[2]
end
-- wakeup one waiter
for fid, ch in pairs(self.queue) do
ch:put(true, 0)
self.queue[ fid ] = nil
break
end
table.remove(res, 1)
return unpack(res)
end,
-- pings database
-- returns true if success. doesn't throw any errors
ping = function(self)
local pf = function()
local res = self:execute('SELECT 1 AS code')
if type(res) ~= 'table' then
return false
end
if type(res[1]) ~= 'table' then
return false
end
return res[1].code == 1
end
local res, code = pcall(pf)
if res == true then
return code
else
return false
end
end,
-- select rows
-- returns table of rows
select = function(self, sql, ...)
local res = self:execute(sql, ...)
return res
end,
-- select one row
single = function(self, sql, ...)
local res = self:execute(sql, ...)
if #res > 1 then
error("SQL request returned multiply rows")
end
return res[1]
end,
-- perform request. returns count of affected rows
perform = function(self, sql, ...)
local res, affected, status = self:execute(sql, ...)
return affected
end,
-- quote variable
quote = function(self, variable)
return self.raw:quote(variable)
end,
-- quote identifier
quote_ident = function(self, variable)
return self.raw:quote_ident(variable)
end,
-- begin transaction
begin_work = function(self)
return self:perform('BEGIN')
end,
-- commit transaction
commit = function(self)
return self:perform('COMMIT')
end,
-- rollback transaction
rollback = function(self)
return self:perform('ROLLBACK')
end,
-- transaction
txn = function(self, proc)
local raise = self.raise
self.raise = true
self:begin_work()
local res = { pcall(proc, self) }
-- commit transaction
if res[1] then
res = { pcall(function() self:commit() end) }
self.raise = raise
if res[1] then
return true
end
return res[1], res[2]
end
local res_txn = { pcall(function() self:rollback() end) }
if not res_txn[1] then
res[2] = res[2] .. "\n" .. res_txn[2]
end
self.raise = raise
return res[1], res[2]
end
}
}
|