unimportant

This commit is contained in:
Cosmin Apreutesei 2021-12-15 17:05:02 +02:00
parent 9353aba013
commit b6fce28d11
4 changed files with 14 additions and 13 deletions

View File

@ -3,7 +3,7 @@
--Written by Cosmin Apreutesei. Public domain. --Written by Cosmin Apreutesei. Public domain.
--Original code by Yichun Zhang (agentzh). BSD license. --Original code by Yichun Zhang (agentzh). BSD license.
if not ... then require'mysql_client_test'; return end if not ... then require'mysql_test'; return end
local ffi = require'ffi' local ffi = require'ffi'
local bit = require'bit' local bit = require'bit'
@ -973,7 +973,7 @@ local function recv_field_packets(self, field_count, field_attrs, opt)
local typ, buf = recv_packet(self) local typ, buf = recv_packet(self)
checkp(self, typ == 'DATA', 'bad packet type') checkp(self, typ == 'DATA', 'bad packet type')
local field = get_field_packet(buf) local field = get_field_packet(buf)
field.to_lua = to_lua or (field.type == 'number' and tonum or nil) field.mysql_to_lua = to_lua or (field.type == 'number' and tonum or nil)
field.index = i field.index = i
fields[i] = field fields[i] = field
fields[field.name] = field fields[field.name] = field
@ -1023,10 +1023,10 @@ function mysql.connect(opt)
return opt return opt
end end
local host = opt.host local host = opt.host or '127.0.0.1'
local port = opt.port or 3306 local port = opt.port or 3306
mysql.note('connect', 'host=%s:%s user=%s db=%s', mysql.note('connect', '%s:%s user=%s db=%s',
host, port, opt.user or '', opt.db or '') host, port, opt.user or '', opt.db or '')
local tcp = opt and opt.tcp or require'sock'.tcp local tcp = opt and opt.tcp or require'sock'.tcp
@ -1113,7 +1113,7 @@ conn.connect = protect(conn.connect)
function conn:close() function conn:close()
if self.state then if self.state then
mysql.note('close', 'host=%s:%s', self.host, self.port) mysql.note('close', '%s:%s', self.host, self.port)
local buf = send_buffer(1) local buf = send_buffer(1)
set_u8(buf, COM_QUIT) set_u8(buf, COM_QUIT)
send_packet(self, buf) send_packet(self, buf)
@ -1230,7 +1230,7 @@ local function read_result(self, opt)
else else
checkp(self, false, 'unsupported param type %s', bt) checkp(self, false, 'unsupported param type %s', bt)
end end
local to_lua = col.to_lua local to_lua = col.mysql_to_lua
if to_lua then if to_lua then
v = to_lua(v, col) v = to_lua(v, col)
end end
@ -1249,7 +1249,7 @@ local function read_result(self, opt)
for i, col in ipairs(cols) do for i, col in ipairs(cols) do
local v = get_str(buf) local v = get_str(buf)
if v ~= nil then if v ~= nil then
local to_lua = col.to_lua local to_lua = col.mysql_to_lua
if to_lua then if to_lua then
v = to_lua(v, col) v = to_lua(v, col)
end end

View File

@ -1,5 +1,5 @@
## `local mysql = require'mysql_client'` ## `local mysql = require'mysql'`
MySQL client protocol in Lua. Ripped from OpenResty, modified to work with MySQL client protocol in Lua. Ripped from OpenResty, modified to work with
[sock], added prepared statements, better interpretation of field metadata [sock], added prepared statements, better interpretation of field metadata
@ -9,7 +9,7 @@ changes.
## Example ## Example
```lua ```lua
local mysql = require'mysql_client' local mysql = require'mysql'
assert(mysql.connect{ assert(mysql.connect{
host = '127.0.0.1', host = '127.0.0.1',
@ -93,7 +93,7 @@ The `options` arg can contain:
* `field_attrs = {name -> attr}` -- extra field attributes. It can also be * `field_attrs = {name -> attr}` -- extra field attributes. It can also be
a function which will be called as `field_attrs(cn, fields, opt)` a function which will be called as `field_attrs(cn, fields, opt)`
as soon as field metadata is received but before rows are received as soon as field metadata is received but before rows are received
(so you can even set a custom `to_lua` for particular fields). (so you can even set a custom `mysql_to_lua` for particular fields).
For queries that return a result set, it returns an array of rows. For queries that return a result set, it returns an array of rows.
For other queries it returns a Lua table with information such as For other queries it returns a Lua table with information such as
@ -115,8 +115,9 @@ if MySQL does not return them.
__NOTE:__ Decimals with up to 15 digits of precision and 64 bit integers __NOTE:__ Decimals with up to 15 digits of precision and 64 bit integers
are converted to Lua numbers by default. That limits the useful range of are converted to Lua numbers by default. That limits the useful range of
integer types to 15 significant digits. If you have other needs, provide integer types to 15 significant digits. If you have other needs, provide
your own `to_lua` (which you can set at module or connection level, and even your own `to_lua` (which you can set at module or connection level,
per query with `field_attrs`). per query in `field_attrs`, or in a [schema] column definition with
the `mysql_to_lua` attribute).
### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate` ### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`

View File

@ -1,5 +1,5 @@
local mysql = require'mysql_client' local mysql = require'mysql'
local sock = require'sock' local sock = require'sock'
local pp = require'pp' local pp = require'pp'
require'$' require'$'