1
0
mirror of https://github.com/luapower/mysql.git synced 2024-11-05 10:00:32 +01:00

unimportant

This commit is contained in:
Cosmin Apreutesei 2021-11-30 02:15:15 +02:00
parent 7196fc9a77
commit e4f81f4918
3 changed files with 41 additions and 26 deletions

View File

@ -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

View File

@ -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`

View File

@ -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)