unimportant

This commit is contained in:
Cosmin Apreutesei 2021-04-30 09:59:49 +03:00
parent 7e7d7501eb
commit deeea3a4b4
2 changed files with 583 additions and 608 deletions

View File

@ -1,8 +1,8 @@
-- MySQL client protocol in Lua. -- MySQL client protocol in Lua.
-- Written by Yichun Zhang (agentzh). BSD license. -- Written by Yichun Zhang (agentzh). BSD license.
-- Modified by Cosmin Apreutesei. Pulbic domain.
local tcp = require'sock'.tcp
local sha1 = require'sha1'.sha1 local sha1 = require'sha1'.sha1
local bit = require'bit' local bit = require'bit'
@ -26,12 +26,9 @@ local error = error
local tonumber = tonumber local tonumber = tonumber
local ok, new_tab = pcall(require, 'table.new') local ok, new_tab = pcall(require, 'table.new')
if not ok then new_tab = ok and new_tab or function() return {} end
new_tab = function (narr, nrec) return {} end
end
local _M = { _VERSION = '0.21' }
local mysql = {}
-- constants -- constants
@ -96,8 +93,8 @@ local CHARSET_MAP = {
gb18030 = 248 gb18030 = 248
} }
local mt = { __index = _M } local conn = {}
local mt = {__index = conn}
-- mysql field value type converters -- mysql field value type converters
local converters = new_tab(0, 9) local converters = new_tab(0, 9)
@ -239,6 +236,10 @@ local function _send_packet(self, req, size)
end end
--static, auto-growing buffer allocation pattern (ctype must be vla). --static, auto-growing buffer allocation pattern (ctype must be vla).
local max, ceil, log = math.max, math.ceil, math.log
local function nextpow2(x)
return max(0, 2^(ceil(log(x) / log(2))))
end
local function grow_buffer(ctype) local function grow_buffer(ctype)
local vla = ffi.typeof(ctype) local vla = ffi.typeof(ctype)
local buf, len = nil, -1 local buf, len = nil, -1
@ -246,7 +247,7 @@ local function grow_buffer(ctype)
if minlen == false then if minlen == false then
buf, len = nil, -1 buf, len = nil, -1
elseif minlen > len then elseif minlen > len then
len = glue.nextpow2(minlen) len = nextpow2(minlen)
buf = vla(len) buf = vla(len)
end end
return buf, len return buf, len
@ -271,7 +272,6 @@ local function _recv(self, sz)
return ffi.string(buf, offset) return ffi.string(buf, offset)
end end
local function _recv_packet(self) local function _recv_packet(self)
local sock = self.sock local sock = self.sock
@ -534,7 +534,8 @@ local function _recv_field_packet(self)
end end
function _M.new(self) function mysql.new(self, opt)
local tcp = opt and opt.tcp or require'sock'.tcp
local sock, err = tcp() local sock, err = tcp()
if not sock then if not sock then
return nil, err return nil, err
@ -543,11 +544,8 @@ function _M.new(self)
end end
function _M.connect(self, opts) function conn:connect(opts)
local sock = self.sock local sock = self.sock
if not sock then
return nil, 'not initialized'
end
local max_packet_size = opts.max_packet_size local max_packet_size = opts.max_packet_size
if not max_packet_size then if not max_packet_size then
@ -557,8 +555,6 @@ function _M.connect(self, opts)
local ok, err local ok, err
self.compact = opts.compact_arrays
local database = opts.database or '' local database = opts.database or ''
local user = opts.user or '' local user = opts.user or ''
@ -724,11 +720,8 @@ function _M.connect(self, opts)
return 1 return 1
end end
function _M.close(self) function conn:close()
local sock = self.sock local sock = assert(self.sock)
if not sock then
return nil, 'not initialized'
end
self.state = nil self.state = nil
@ -740,20 +733,13 @@ function _M.close(self)
return sock:close() return sock:close()
end end
function _M.server_ver(self) function conn:server_ver()
return self._server_ver return self._server_ver
end end
local function send_query(self, query) function conn:send_query(query)
if self.state ~= STATE_CONNECTED then assert(self.state == STATE_CONNECTED)
return nil, 'cannot send query in the current context: ' local sock = assert(self.sock)
.. (self.state or 'nil')
end
local sock = self.sock
if not sock then
return nil, 'not initialized'
end
self.packet_no = -1 self.packet_no = -1
@ -771,17 +757,15 @@ local function send_query(self, query)
return bytes return bytes
end end
_M.send_query = send_query
local function read_result(self, est_nrows) function conn:read_result(est_nrows, compact)
if self.state ~= STATE_COMMAND_SENT then assert(self.state == STATE_COMMAND_SENT)
return nil, 'cannot read result in the current context: ' local sock = assert(self.sock)
.. (self.state or 'nil')
end
local sock = self.sock compact = compact == 'compact'
if not sock then if est_nrows == 'compact' then
return nil, 'not initialized' est_nrows = null
compact = true
end end
local packet, typ, err = _recv_packet(self) local packet, typ, err = _recv_packet(self)
@ -842,8 +826,6 @@ local function read_result(self, est_nrows)
-- typ == 'EOF' -- typ == 'EOF'
local compact = self.compact
local rows = new_tab(est_nrows or 4, 0) local rows = new_tab(est_nrows or 4, 0)
local i = 0 local i = 0
while true do while true do
@ -881,19 +863,11 @@ local function read_result(self, est_nrows)
return rows, nil, cols return rows, nil, cols
end end
_M.read_result = read_result
function _M.query(self, query, est_nrows) function conn:query(query, est_nrows)
local bytes, err = send_query(self, query) local bytes, err, errcode = self:send_query(query)
if not bytes then if not bytes then return nil, err, errcode end
return nil, 'failed to send query: ' .. err return self:read_result(est_nrows)
end
return read_result(self, est_nrows)
end
function _M.set_compact_arrays(self, value)
self.compact = value
end end
local qmap = { local qmap = {
@ -907,8 +881,8 @@ local qmap = {
['\'' ] = '\\\'', ['\'' ] = '\\\'',
['\"' ] = '\\"', ['\"' ] = '\\"',
} }
function _M.quote(s) function mysql.quote(s)
return s:gsub('[%z\b\n\r\t\26\\\'\"]', qmap) return s:gsub('[%z\b\n\r\t\26\\\'\"]', qmap)
end end
return _M return mysql

View File

@ -72,8 +72,6 @@ The `options` argument is a Lua table holding the following keys:
If the server does not have SSL support (or just disabled), the error string If the server does not have SSL support (or just disabled), the error string
"ssl disabled on server" will be returned. "ssl disabled on server" will be returned.
* `ssl_verify`: if `true`, then verifies the validity of the server SSL certificate (default to `false`). * `ssl_verify`: if `true`, then verifies the validity of the server SSL certificate (default to `false`).
* `compact_arrays`: `true` to use array-of-arrays structure for the result set,
rather than the default array-of-hashes structure.
### `db:close() -> 1 | nil,err` ### `db:close() -> 1 | nil,err`
@ -85,7 +83,7 @@ Sends the query to the remote MySQL server without waiting for its replies.
Returns the bytes successfully sent out. Use `read_result()` to read the replies. Returns the bytes successfully sent out. Use `read_result()` to read the replies.
### `db:read_result([nrows]) -> res | nil,err,errcode,sqlstate` ### `db:read_result([nrows,]['compact']) -> res | nil,err,errcode,sqlstate`
Reads in one result returned from the server. Reads in one result returned from the server.
@ -97,8 +95,17 @@ Each row holds key-value pairs for each data fields. For instance,
```lua ```lua
{ {
{ name = "Bob", age = 32, phone = ngx.null }, { name = "Bob", age = 32, phone = mysql.null },
{ name = "Marry", age = 18, phone = "10666372"} { name = "Marry", age = 18, phone = "10666372" }
}
```
If `'compact'` given, it returns an array-of-arrays instead:
```lua
{
{ "Bob", 32, null },
{ "Marry", 18, "10666372" }
} }
``` ```
@ -148,12 +155,6 @@ Returns the MySQL server version string, like `"5.1.64"`.
You should only call this method after successfully connecting to a MySQL server, You should only call this method after successfully connecting to a MySQL server,
otherwise `nil` will be returned. otherwise `nil` will be returned.
### `db:set_compact_arrays(true|false)`
Sets whether to use the "compact-arrays" structure for the resultsets returned
by subsequent queries. See the `compact_arrays` option for the `connect`
method for more details.
### `mysql.quote(s) -> s` ### `mysql.quote(s) -> s`
Quote literal string to be used in queries. Quote literal string to be used in queries.