From e4f81f4918a765ac8f55bfae3e515d75c4bc420b Mon Sep 17 00:00:00 2001 From: Cosmin Apreutesei Date: Tue, 30 Nov 2021 02:15:15 +0200 Subject: [PATCH] unimportant --- mysql_client.lua | 33 ++++++++++++++++----------------- mysql_client.md | 25 +++++++++++++++++-------- mysql_client_test.lua | 9 ++++++++- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/mysql_client.lua b/mysql_client.lua index ffc335a..628f371 100644 --- a/mysql_client.lua +++ b/mysql_client.lua @@ -482,18 +482,6 @@ function mysql.isconn(x) return getmetatable(x) == conn_mt end -local default_to_lua = { - tinyint = tonumber, - smallint = tonumber, - mediumint = tonumber, - int = tonumber, - bigint = tonumber, - year = tonumber, - float = tonumber, - double = tonumber, - decimal = tonumber, -} - local function return_arg1(v) return v end assert(ffi.abi'le') @@ -898,7 +886,8 @@ end --NOTE: MySQL doesn't give enough metadata to generate a form in a UI, --you'll have to query `information_schema` to get the rest like enum values ---and defaults. So we gather only what we need for display, not for editing. +--and defaults. So we gather only what we need for display and for encoding +--params and decoding values in binary form, but not for editing. local function get_field_packet(buf) local col = {} local _ = get_name(buf) --always "def" @@ -918,7 +907,7 @@ local function get_field_packet(buf) if collation == 63 then --binary mysql_type = bin_types[buf_type] or buf_type local unsigned = band(flags, UNSIGNED_FLAG) ~= 0 or nil --for val decoding - if mysql_type == 'tinyint' or mysql_type == 'smallint' + if mysql_type == 'tinyint' or mysql_type == 'smallint' or mysql_type == 'mediumint' or mysql_type == 'int' or mysql_type == 'bigint' then @@ -926,7 +915,8 @@ local function get_field_packet(buf) col.decimals = 0 col.unsigned = unsigned elseif mysql_type == 'decimal' then - col.type = 'number' + local digits = display_size - (unsigned and 1 or 2) + col.type = digits > 15 and 'decimal' or 'number' col.decimals = decimals col.unsigned = unsigned elseif mysql_type == 'float' then @@ -948,6 +938,7 @@ local function get_field_packet(buf) end col.display_width = display_size else + col.type = 'text' mysql_type = text_types[buf_type] or buf_type local collation = collation_names[collation] local charset = collation and collation:match'^[^_]+' @@ -975,7 +966,7 @@ local function recv_field_packets(self, field_count, field_attrs, to_lua) local typ, buf = recv_packet(self) checkp(self, typ == 'DATA', 'bad packet type') local field = get_field_packet(buf) - field.to_lua = to_lua or default_to_lua[field.mysql_type] + field.to_lua = to_lua or (field.type == 'number' and tonumber or nil) field.index = i fields[i] = field fields[field.name] = field @@ -1126,6 +1117,10 @@ function conn:close() end conn.close = protect(conn.close) +function conn:closed() + return not self.state +end + local function send_query(self, query) mysql.dbg('query', '%s', query) assert(self.state == 'ready') @@ -1228,6 +1223,10 @@ local function read_result(self, opt) else checkp(self, false, 'unsupported param type %s', bt) end + local to_lua = col.to_lua + if to_lua then + v = to_lua(v) + end else v = null_value end @@ -1245,7 +1244,7 @@ local function read_result(self, opt) if v ~= nil then local to_lua = col.to_lua if to_lua then - v = to_lua(v, col) + v = to_lua(v) end else v = null_value diff --git a/mysql_client.md b/mysql_client.md index 8b9018a..9bf6fdc 100644 --- a/mysql_client.md +++ b/mysql_client.md @@ -61,12 +61,16 @@ The `options` argument is a Lua table holding the following keys: "ssl disabled on server" is returned. * `ssl_verify`: if `true`, then verifies the validity of the server SSL certificate (default is `false`). - * `to_lua = f(v, col) -> v` -- custom value converter (defaults to `mysql.to_lua`). + * `to_lua = f(v) -> v` -- custom value converter (defaults to `mysql.to_lua`). -### `cn:close() -> 1 | nil,err` +### `cn:close() -> true | nil,err` Closes the current mysql connection and returns the status. +### `cn:closed() -> true | false` + +Check if the connection was closed. + ### `cn:send_query(query) -> bytes | nil,err` Sends the query to the remote MySQL server without waiting for its replies. @@ -79,10 +83,14 @@ Reads in the next result set returned from the server. The `options` arg can contain: - * `compact = true` -- return an array of arrays instead of an array of `{column->value}` maps. + * `compact = true` -- return an array of arrays instead of an array + of `{column->value}` maps + * __NOTE__: unless you set `null_value`, the rows in compact mode will + be sparse arrays so `ipairs(row)` and `#row` won't work on them + (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. + * `to_lua = f(v) -> v` -- custom value converter (defaults to `cn.to_lua`). * `field_attrs = {name -> attr}` -- extra field attributes. For queries that return a result set, it returns an array of rows. @@ -102,10 +110,11 @@ the `sqlstate` return value contains the standard SQL error code that consists of 5 characters. Note that, the `errcode` and `sqlstate` might be `nil` if MySQL does not return them. -__NOTE:__ decimals and 64 bit integers are converted to Lua numbers by default. -That limits the useful range of number 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`). +__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 atmodule, connection and query level, +and even per field with `field_attrs`). ### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate` diff --git a/mysql_client_test.lua b/mysql_client_test.lua index 116a8e3..d5da66e 100644 --- a/mysql_client_test.lua +++ b/mysql_client_test.lua @@ -99,7 +99,9 @@ sock.run(function() local spp = require'sqlpp'.new() require'sqlpp_mysql' spp.import'mysql' + local cn = spp.connect(conn) + local rows, cols = cn:query({get_table_defs=1}, 'select * from test') print() pr(cols, { @@ -119,11 +121,16 @@ sock.run(function() 'padded', 'enum_values', 'default', + 'mysql_default', 'mysql_display_charset', 'mysql_display_collation', 'mysql_buffer_type', }) - --cn:close() + + pp(rows) + + cn:close() + assert(conn:closed()) end)