unimportant

This commit is contained in:
Cosmin Apreutesei 2021-12-07 20:30:35 +02:00
parent 1a27dad1a6
commit a96be3800f
2 changed files with 21 additions and 12 deletions

View File

@ -964,7 +964,7 @@ end
local function tonum(x) return tonumber(x) end local function tonum(x) return tonumber(x) end
local function recv_field_packets(self, field_count, field_attrs, to_lua) local function recv_field_packets(self, field_count, field_attrs, opt)
local fields = {} local fields = {}
to_lua = to_lua or self.to_lua to_lua = to_lua or self.to_lua
for i = 1, field_count do for i = 1, field_count do
@ -975,15 +975,22 @@ local function recv_field_packets(self, field_count, field_attrs, to_lua)
field.index = i field.index = i
fields[i] = field fields[i] = field
fields[field.name] = field fields[field.name] = field
if field_attrs then
update(field, field_attrs[field.name])
end
end end
if field_count > 0 then if field_count > 0 then
local typ, buf = recv_packet(self) local typ, buf = recv_packet(self)
checkp(self, typ == 'EOF', 'bad packet type') checkp(self, typ == 'EOF', 'bad packet type')
get_eof_packet(buf) get_eof_packet(buf)
end end
if type(field_attrs) == 'function' then
field_attrs = field_attrs(self, fields, opt)
end
if field_attrs then
for name, attrs in pairs(field_attrs) do
if cols[name] then
update(cols[name], attrs)
end
end
end
return fields return fields
end end
@ -1157,9 +1164,9 @@ local function read_result(self, opt)
checkp(self, typ == 'DATA', 'bad packet type') checkp(self, typ == 'DATA', 'bad packet type')
local field_count = get_uint(buf) local field_count = get_uint(buf)
local extra = buf_len(buf) > 0 and get_uint(buf) or nil local extra = buf_len(buf) > 0 and get_uint(buf) or nil --not used.
local cols = recv_field_packets(self, field_count, opt and opt.field_attrs, opt and opt.to_lua) local cols = recv_field_packets(self, field_count, opt and opt.field_attrs, opt)
local compact = opt and opt.compact local compact = opt and opt.compact
local to_array = opt and opt.to_array and #cols == 1 local to_array = opt and opt.to_array and #cols == 1
@ -1312,8 +1319,8 @@ function conn:prepare(query, opt)
local param_count = get_u16(buf) local param_count = get_u16(buf)
buf(1) --filler buf(1) --filler
stmt.warning_count = get_u16(buf) stmt.warning_count = get_u16(buf)
stmt.params = recv_field_packets(self, param_count, opt and opt.param_attrs, opt and opt.to_lua) stmt.params = recv_field_packets(self, param_count, opt and opt.param_attrs, opt)
stmt.cols = recv_field_packets(self, col_count , opt and opt.field_attrs, opt and opt.to_lua) stmt.cols = recv_field_packets(self, col_count , opt and opt.field_attrs, opt)
stmt.cursor = assert(cursor_types[opt and opt.cursor or 'none']) stmt.cursor = assert(cursor_types[opt and opt.cursor or 'none'])
return stmt return stmt
end end

View File

@ -90,8 +90,10 @@ The `options` arg can contain:
(but `#cols` will). (but `#cols` will).
* `to_array = true` -- return an array of values for single-column results. * `to_array = true` -- return an array of values for single-column results.
* `null_value = val` -- value to use for `null` (defaults to `nil`). * `null_value = val` -- value to use for `null` (defaults to `nil`).
* `to_lua = f(v, col) -> v` -- custom value converter (defaults to `cn.to_lua`). * `field_attrs = {name -> attr}` -- extra field attributes. can also be
* `field_attrs = {name -> attr}` -- extra field attributes. a function which will be called with `field_attrs(cn, fields, opt)`
as soon as field metadata is received but before rows are received
(so you can even set a custom `to_lua` to certain 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
@ -113,8 +115,8 @@ 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, connection and query level, your own `to_lua` (which you can set at module or connection level, and even
and even per field with `field_attrs`). per query with `field_attrs`).
### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate` ### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`