From a96be3800f5ea96160ec24e42106b88a1a3207ca Mon Sep 17 00:00:00 2001 From: Cosmin Apreutesei Date: Tue, 7 Dec 2021 20:30:35 +0200 Subject: [PATCH] unimportant --- mysql_client.lua | 23 +++++++++++++++-------- mysql_client.md | 10 ++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/mysql_client.lua b/mysql_client.lua index a4039be..17ee73d 100644 --- a/mysql_client.lua +++ b/mysql_client.lua @@ -964,7 +964,7 @@ 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 = {} to_lua = to_lua or self.to_lua 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 fields[i] = field fields[field.name] = field - if field_attrs then - update(field, field_attrs[field.name]) - end end if field_count > 0 then local typ, buf = recv_packet(self) checkp(self, typ == 'EOF', 'bad packet type') get_eof_packet(buf) 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 end @@ -1157,9 +1164,9 @@ local function read_result(self, opt) checkp(self, typ == 'DATA', 'bad packet type') 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 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) buf(1) --filler 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.cols = recv_field_packets(self, col_count , opt and opt.field_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) stmt.cursor = assert(cursor_types[opt and opt.cursor or 'none']) return stmt end diff --git a/mysql_client.md b/mysql_client.md index 975e515..ea3577b 100644 --- a/mysql_client.md +++ b/mysql_client.md @@ -90,8 +90,10 @@ The `options` arg can contain: (but `#cols` will). * `to_array = true` -- return an array of values for single-column results. * `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. + * `field_attrs = {name -> attr}` -- extra field attributes. can also be + 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 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 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 -your own `to_lua` (which you can set at module, connection and query level, -and even per field with `field_attrs`). +your own `to_lua` (which you can set at module or connection level, and even +per query with `field_attrs`). ### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`