mirror of
https://github.com/luapower/mysql.git
synced 2025-01-04 07:10:25 +01:00
unimportant
This commit is contained in:
parent
7e7d7501eb
commit
deeea3a4b4
@ -1,8 +1,8 @@
|
||||
|
||||
-- MySQL client protocol in Lua.
|
||||
-- Written by Yichun Zhang (agentzh). BSD license.
|
||||
-- Modified by Cosmin Apreutesei. Pulbic domain.
|
||||
|
||||
local tcp = require'sock'.tcp
|
||||
local sha1 = require'sha1'.sha1
|
||||
local bit = require'bit'
|
||||
|
||||
@ -26,12 +26,9 @@ local error = error
|
||||
local tonumber = tonumber
|
||||
|
||||
local ok, new_tab = pcall(require, 'table.new')
|
||||
if not ok then
|
||||
new_tab = function (narr, nrec) return {} end
|
||||
end
|
||||
|
||||
local _M = { _VERSION = '0.21' }
|
||||
new_tab = ok and new_tab or function() return {} end
|
||||
|
||||
local mysql = {}
|
||||
|
||||
-- constants
|
||||
|
||||
@ -96,8 +93,8 @@ local CHARSET_MAP = {
|
||||
gb18030 = 248
|
||||
}
|
||||
|
||||
local mt = { __index = _M }
|
||||
|
||||
local conn = {}
|
||||
local mt = {__index = conn}
|
||||
|
||||
-- mysql field value type converters
|
||||
local converters = new_tab(0, 9)
|
||||
@ -239,6 +236,10 @@ local function _send_packet(self, req, size)
|
||||
end
|
||||
|
||||
--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 vla = ffi.typeof(ctype)
|
||||
local buf, len = nil, -1
|
||||
@ -246,7 +247,7 @@ local function grow_buffer(ctype)
|
||||
if minlen == false then
|
||||
buf, len = nil, -1
|
||||
elseif minlen > len then
|
||||
len = glue.nextpow2(minlen)
|
||||
len = nextpow2(minlen)
|
||||
buf = vla(len)
|
||||
end
|
||||
return buf, len
|
||||
@ -271,7 +272,6 @@ local function _recv(self, sz)
|
||||
return ffi.string(buf, offset)
|
||||
end
|
||||
|
||||
|
||||
local function _recv_packet(self)
|
||||
local sock = self.sock
|
||||
|
||||
@ -534,7 +534,8 @@ local function _recv_field_packet(self)
|
||||
end
|
||||
|
||||
|
||||
function _M.new(self)
|
||||
function mysql.new(self, opt)
|
||||
local tcp = opt and opt.tcp or require'sock'.tcp
|
||||
local sock, err = tcp()
|
||||
if not sock then
|
||||
return nil, err
|
||||
@ -543,11 +544,8 @@ function _M.new(self)
|
||||
end
|
||||
|
||||
|
||||
function _M.connect(self, opts)
|
||||
function conn:connect(opts)
|
||||
local sock = self.sock
|
||||
if not sock then
|
||||
return nil, 'not initialized'
|
||||
end
|
||||
|
||||
local max_packet_size = opts.max_packet_size
|
||||
if not max_packet_size then
|
||||
@ -557,8 +555,6 @@ function _M.connect(self, opts)
|
||||
|
||||
local ok, err
|
||||
|
||||
self.compact = opts.compact_arrays
|
||||
|
||||
local database = opts.database or ''
|
||||
local user = opts.user or ''
|
||||
|
||||
@ -724,11 +720,8 @@ function _M.connect(self, opts)
|
||||
return 1
|
||||
end
|
||||
|
||||
function _M.close(self)
|
||||
local sock = self.sock
|
||||
if not sock then
|
||||
return nil, 'not initialized'
|
||||
end
|
||||
function conn:close()
|
||||
local sock = assert(self.sock)
|
||||
|
||||
self.state = nil
|
||||
|
||||
@ -740,20 +733,13 @@ function _M.close(self)
|
||||
return sock:close()
|
||||
end
|
||||
|
||||
function _M.server_ver(self)
|
||||
function conn:server_ver()
|
||||
return self._server_ver
|
||||
end
|
||||
|
||||
local function send_query(self, query)
|
||||
if self.state ~= STATE_CONNECTED then
|
||||
return nil, 'cannot send query in the current context: '
|
||||
.. (self.state or 'nil')
|
||||
end
|
||||
|
||||
local sock = self.sock
|
||||
if not sock then
|
||||
return nil, 'not initialized'
|
||||
end
|
||||
function conn:send_query(query)
|
||||
assert(self.state == STATE_CONNECTED)
|
||||
local sock = assert(self.sock)
|
||||
|
||||
self.packet_no = -1
|
||||
|
||||
@ -771,17 +757,15 @@ local function send_query(self, query)
|
||||
|
||||
return bytes
|
||||
end
|
||||
_M.send_query = send_query
|
||||
|
||||
local function read_result(self, est_nrows)
|
||||
if self.state ~= STATE_COMMAND_SENT then
|
||||
return nil, 'cannot read result in the current context: '
|
||||
.. (self.state or 'nil')
|
||||
end
|
||||
function conn:read_result(est_nrows, compact)
|
||||
assert(self.state == STATE_COMMAND_SENT)
|
||||
local sock = assert(self.sock)
|
||||
|
||||
local sock = self.sock
|
||||
if not sock then
|
||||
return nil, 'not initialized'
|
||||
compact = compact == 'compact'
|
||||
if est_nrows == 'compact' then
|
||||
est_nrows = null
|
||||
compact = true
|
||||
end
|
||||
|
||||
local packet, typ, err = _recv_packet(self)
|
||||
@ -842,8 +826,6 @@ local function read_result(self, est_nrows)
|
||||
|
||||
-- typ == 'EOF'
|
||||
|
||||
local compact = self.compact
|
||||
|
||||
local rows = new_tab(est_nrows or 4, 0)
|
||||
local i = 0
|
||||
while true do
|
||||
@ -881,19 +863,11 @@ local function read_result(self, est_nrows)
|
||||
|
||||
return rows, nil, cols
|
||||
end
|
||||
_M.read_result = read_result
|
||||
|
||||
function _M.query(self, query, est_nrows)
|
||||
local bytes, err = send_query(self, query)
|
||||
if not bytes then
|
||||
return nil, 'failed to send query: ' .. err
|
||||
end
|
||||
|
||||
return read_result(self, est_nrows)
|
||||
end
|
||||
|
||||
function _M.set_compact_arrays(self, value)
|
||||
self.compact = value
|
||||
function conn:query(query, est_nrows)
|
||||
local bytes, err, errcode = self:send_query(query)
|
||||
if not bytes then return nil, err, errcode end
|
||||
return self:read_result(est_nrows)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
return _M
|
||||
return mysql
|
||||
|
@ -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
|
||||
"ssl disabled on server" will be returned.
|
||||
* `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`
|
||||
|
||||
@ -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.
|
||||
|
||||
### `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.
|
||||
|
||||
@ -97,8 +95,17 @@ Each row holds key-value pairs for each data fields. For instance,
|
||||
|
||||
```lua
|
||||
{
|
||||
{ name = "Bob", age = 32, phone = ngx.null },
|
||||
{ name = "Marry", age = 18, phone = "10666372"}
|
||||
{ name = "Bob", age = 32, phone = mysql.null },
|
||||
{ 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,
|
||||
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`
|
||||
|
||||
Quote literal string to be used in queries.
|
||||
|
Loading…
Reference in New Issue
Block a user