mirror of
https://github.com/luapower/mysql.git
synced 2025-02-08 16:20:31 +01:00
unimportant
This commit is contained in:
parent
5d6d31aac3
commit
9372919199
@ -463,7 +463,7 @@ local int_ranges = {
|
|||||||
local conn = {}
|
local conn = {}
|
||||||
local conn_mt = {__index = conn}
|
local conn_mt = {__index = conn}
|
||||||
|
|
||||||
local from_text_converters = {
|
local to_lua = {
|
||||||
tinyint = tonumber,
|
tinyint = tonumber,
|
||||||
shortint = tonumber,
|
shortint = tonumber,
|
||||||
mediumint = tonumber,
|
mediumint = tonumber,
|
||||||
@ -472,7 +472,15 @@ local from_text_converters = {
|
|||||||
year = tonumber,
|
year = tonumber,
|
||||||
float = tonumber,
|
float = tonumber,
|
||||||
double = tonumber,
|
double = tonumber,
|
||||||
|
decimal = tonumber,
|
||||||
}
|
}
|
||||||
|
function mysql.to_lua(v, col)
|
||||||
|
local to_lua = to_lua[col.type]
|
||||||
|
if to_lua then
|
||||||
|
v = to_lua(v)
|
||||||
|
end
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
|
||||||
local function return_arg1(v) return v end
|
local function return_arg1(v) return v end
|
||||||
|
|
||||||
@ -888,10 +896,10 @@ local function get_field_packet(buf)
|
|||||||
local col = {}
|
local col = {}
|
||||||
local _ = get_name(buf) --always "def"
|
local _ = get_name(buf) --always "def"
|
||||||
col.schema = get_name(buf)
|
col.schema = get_name(buf)
|
||||||
col.table = get_name(buf)
|
col.table_alias = get_name(buf)
|
||||||
col.origin_table = get_name(buf)
|
col.table = get_name(buf) --name of origin table
|
||||||
col.name = get_name(buf)
|
col.name = get_name(buf) --alias column name
|
||||||
col.origin_name = get_name(buf)
|
col.col = get_name(buf) --name of column in origin table
|
||||||
local _ = get_uint(buf) --0x0c
|
local _ = get_uint(buf) --0x0c
|
||||||
local collation = get_u16(buf)
|
local collation = get_u16(buf)
|
||||||
col.max_char_w = get_u32(buf)
|
col.max_char_w = get_u32(buf)
|
||||||
@ -918,7 +926,7 @@ local function get_field_packet(buf)
|
|||||||
return col
|
return col
|
||||||
end
|
end
|
||||||
|
|
||||||
local function recv_field_packets(self, field_count)
|
local function recv_field_packets(self, field_count, field_attrs)
|
||||||
local fields = {}
|
local fields = {}
|
||||||
for i = 1, field_count do
|
for i = 1, field_count do
|
||||||
local typ, buf = recv_packet(self)
|
local typ, buf = recv_packet(self)
|
||||||
@ -927,6 +935,9 @@ local function recv_field_packets(self, field_count)
|
|||||||
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)
|
||||||
@ -1026,6 +1037,7 @@ function mysql.connect(opt)
|
|||||||
end
|
end
|
||||||
check(self, typ == 'OK', 'bad packet type')
|
check(self, typ == 'OK', 'bad packet type')
|
||||||
|
|
||||||
|
self.to_lua = mysql.to_lua
|
||||||
self.state = 'ready'
|
self.state = 'ready'
|
||||||
|
|
||||||
if opt.collation == 'server' then
|
if opt.collation == 'server' then
|
||||||
@ -1035,6 +1047,7 @@ function mysql.connect(opt)
|
|||||||
self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset]
|
self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset]
|
||||||
self.schema = opt.schema
|
self.schema = opt.schema
|
||||||
self.user = opt.user
|
self.user = opt.user
|
||||||
|
self.host_port = host .. ':' .. port
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@ -1090,14 +1103,16 @@ local function read_result(self, opt)
|
|||||||
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
|
||||||
|
|
||||||
local cols = recv_field_packets(self, field_count)
|
local cols = recv_field_packets(self, field_count, opt and opt.field_attrs)
|
||||||
|
|
||||||
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
|
||||||
local null_value = opt and opt.null_value
|
local sopt = opt or self
|
||||||
local datetime_format = opt and opt.datetime_format
|
local null_value = sopt.null_value
|
||||||
local date_format = opt and opt.date_format
|
local datetime_format = sopt.datetime_format
|
||||||
local time_format = opt and opt.time_format
|
local date_format = sopt.date_format
|
||||||
|
local time_format = sopt.time_format
|
||||||
|
local to_lua = sopt.to_lua
|
||||||
|
|
||||||
local rows = {}
|
local rows = {}
|
||||||
local i = 0
|
local i = 0
|
||||||
@ -1162,10 +1177,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 convert = from_text_converters[col.type]
|
v = to_lua(v, col)
|
||||||
if convert then
|
|
||||||
v = convert(v)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
v = null_value
|
v = null_value
|
||||||
end
|
end
|
||||||
@ -1229,8 +1241,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)
|
stmt.params = recv_field_packets(self, param_count, opt and opt.param_attrs)
|
||||||
stmt.cols = recv_field_packets(self, col_count)
|
stmt.cols = recv_field_packets(self, col_count, opt and opt.field_attrs)
|
||||||
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
|
||||||
|
@ -59,6 +59,7 @@ The `options` argument is a Lua table holding the following keys:
|
|||||||
"ssl disabled on server" is returned.
|
"ssl disabled on server" is returned.
|
||||||
* `ssl_verify`: if `true`, then verifies the validity of the server SSL
|
* `ssl_verify`: if `true`, then verifies the validity of the server SSL
|
||||||
certificate (default is `false`).
|
certificate (default is `false`).
|
||||||
|
* `to_lua = f(v, col) -> v` -- custom value converter (defaults to `mysql.to_lua`).
|
||||||
|
|
||||||
### `cn:close() -> 1 | nil,err`
|
### `cn:close() -> 1 | nil,err`
|
||||||
|
|
||||||
@ -72,37 +73,24 @@ Returns the bytes successfully sent out. Use `read_result()` to read the replies
|
|||||||
|
|
||||||
### `cn:read_result([options]) -> res,nil|'again',cols | nil,err,errcode,sqlstate`
|
### `cn:read_result([options]) -> res,nil|'again',cols | nil,err,errcode,sqlstate`
|
||||||
|
|
||||||
Reads in one result returned from the server.
|
Reads in the next result set returned from the server.
|
||||||
|
|
||||||
It returns a Lua table (`res`) describing the MySQL `OK packet`
|
|
||||||
or `result set packet` for the query result.
|
|
||||||
|
|
||||||
For queries corresponding to a result set, it returns an array holding all the rows.
|
|
||||||
|
|
||||||
The `options` arg can contain:
|
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.
|
||||||
* `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.
|
||||||
|
|
||||||
For queries that do not correspond to a result set, it returns a Lua table like this:
|
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
|
||||||
|
the autoincrement value if any and the affected rows.
|
||||||
|
|
||||||
```lua
|
If more results are following the current result, a second return value
|
||||||
{
|
`'again'` is returned. One should always check this value and call this
|
||||||
insert_id = 0,
|
method again to retrieve more results. This usually happens when the original
|
||||||
server_status = 2,
|
query contains multiple statements (separated by semicolon in the same
|
||||||
warning_count = 1,
|
query string) or calling a stored procedure.
|
||||||
affected_rows = 32,
|
|
||||||
message = nil
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
If more results are following the current result, a second `err` return value
|
|
||||||
will be given the string `again`. One should always check this (second) return
|
|
||||||
value and if it is `again`, then she should call this method again to retrieve
|
|
||||||
more results. This usually happens when the original query contains multiple
|
|
||||||
statements (separated by semicolon in the same query string) or calling a
|
|
||||||
MySQL procedure.
|
|
||||||
|
|
||||||
In case of errors, this method returns at most 4 values: `nil`, `err`, `errcode`, and `sqlstate`.
|
In case of errors, this method returns at most 4 values: `nil`, `err`, `errcode`, and `sqlstate`.
|
||||||
The `err` return value contains a string describing the error, the `errcode`
|
The `err` return value contains a string describing the error, the `errcode`
|
||||||
@ -111,6 +99,9 @@ the `sqlstate` return value contains the standard SQL error code that consists
|
|||||||
of 5 characters. Note that, the `errcode` and `sqlstate` might be `nil`
|
of 5 characters. Note that, the `errcode` and `sqlstate` might be `nil`
|
||||||
if MySQL does not return them.
|
if MySQL does not return them.
|
||||||
|
|
||||||
|
NOTE: 64 bit integers and decimals are converted to Lua numbers by default.
|
||||||
|
That limits the useful integer range of number types to ±2^51 or 15 digits.
|
||||||
|
|
||||||
### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`
|
### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`
|
||||||
|
|
||||||
This is a shortcut for combining the [send_query](#send_query) call
|
This is a shortcut for combining the [send_query](#send_query) call
|
||||||
|
Loading…
Reference in New Issue
Block a user