mysql/mysql_client.md

167 lines
5.4 KiB
Markdown
Raw Normal View History

2021-04-29 21:47:39 +02:00
## `local mysql = require'mysql_client'`
MySQL client protocol in Lua.
2021-08-23 21:54:29 +02:00
Stolen from OpenResty, modified to work with [sock] and added prepared statements.
2021-04-29 21:47:39 +02:00
## Example
```lua
2021-04-29 22:06:07 +02:00
local mysql = require'mysql_client'
assert(mysql.connect{
2021-04-29 22:06:07 +02:00
host = '127.0.0.1',
port = 3306,
user = 'bar',
password = 'baz',
2021-09-06 11:28:55 +02:00
schema = 'foo',
charset = 'utf8mb4',
2021-04-29 22:06:07 +02:00
max_packet_size = 1024 * 1024,
})
2021-07-12 01:45:24 +02:00
assert(cn:query('drop table if exists cats'))
2021-04-29 22:06:07 +02:00
2021-07-12 01:45:24 +02:00
local res = assert(cn:query('create table cats '
2021-04-29 22:06:07 +02:00
.. '(id serial primary key, '
.. 'name varchar(5))'))
2021-07-12 01:45:24 +02:00
local res = assert(cn:query('insert into cats (name) '
2021-08-18 19:36:08 +02:00
.. "values ('Bob'),(''),(null)"))
2021-04-29 22:06:07 +02:00
print(res.affected_rows, ' rows inserted into table cats ',
'(last insert id: ', res.insert_id, ')')
require'pp'(assert(cn:query('select * from cats order by id asc', 10)))
2021-04-29 22:06:07 +02:00
2021-07-12 01:45:24 +02:00
assert(cn:close())
2021-04-29 21:47:39 +02:00
```
## API
### `mysql.connect(options) -> ok | nil,err,errcode,sqlstate`
2021-04-29 21:47:39 +02:00
Connect to a MySQL server.
The `options` argument is a Lua table holding the following keys:
2021-09-06 11:28:55 +02:00
* `host`: server's IP address (required).
* `port`: server's port (optional, defaults to 3306).
* `user`: user name.
* `password`: password (optional).
* `schema`: the schema to set as current schema (optional).
* `collation`: the collation used for the connection (`charset` is implied by this).
* use `'server'` to get the server's collation and charset for the connection.
* `charset`: the character set used for the connection.
* if `collation` not set, the default collation for the charset is selected.
* `max_packet_size`: the upper limit for the reply packets sent from the server (defaults to 16 MB).
* `ssl`: if `true`, then uses SSL to connect to MySQL (defaults to `false`).
2021-04-29 21:47:39 +02:00
If the server does not have SSL support (or just disabled), the error string
2021-09-06 11:28:55 +02:00
"ssl disabled on server" is returned.
* `ssl_verify`: if `true`, then verifies the validity of the server SSL
certificate (default is `false`).
2021-04-29 21:47:39 +02:00
2021-07-12 01:45:24 +02:00
### `cn:close() -> 1 | nil,err`
2021-04-29 21:47:39 +02:00
Closes the current mysql connection and returns the status.
2021-07-12 01:45:24 +02:00
### `cn:send_query(query) -> bytes | nil,err`
2021-04-29 21:47:39 +02:00
Sends the query to the remote MySQL server without waiting for its replies.
Returns the bytes successfully sent out. Use `read_result()` to read the replies.
2021-08-18 19:36:08 +02:00
### `cn:read_result([options]) -> res,nil|'again',cols | nil,err,errcode,sqlstate`
2021-04-29 21:47:39 +02:00
Reads in one result 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.
2021-08-18 19:36:08 +02:00
The `options` arg can contain:
2021-04-30 08:59:49 +02:00
2021-08-18 19:36:08 +02:00
* `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.
* `null_value = val` -- value to use for `null` (defaults to `nil`).
2021-04-29 21:47:39 +02:00
For queries that do not correspond to a result set, it returns a Lua table like this:
```lua
{
insert_id = 0,
server_status = 2,
warning_count = 1,
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
2021-08-18 19:36:08 +02:00
MySQL procedure.
2021-04-29 21:47:39 +02:00
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`
return value holds the MySQL error code (a numerical value), and finally,
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.
2021-08-18 19:36:08 +02:00
### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate`
2021-04-29 21:47:39 +02:00
This is a shortcut for combining the [send_query](#send_query) call
and the first [read_result](#read_result) call.
You should always check if the `err` return value is `again` in case of
success because this method will only call [read_result](#read_result)
2021-08-18 19:36:08 +02:00
once for you.
2021-04-29 21:47:39 +02:00
2021-08-23 21:54:29 +02:00
### `cn:prepare(query, [opt]) -> stmt`
2021-08-23 21:54:29 +02:00
Prepare a statement. Options can contain:
* `cursor`: 'read_only', 'update', 'scrollabe', 'none' (default: 'none').
### `stmt:exec(params...)`
Execute a statement. Use `cn:read_result()` to get the results.
### `stmt:free()`
Free statement.
### `cn.server_ver`
2021-04-29 21:47:39 +02:00
The MySQL server version string.
2021-04-29 21:47:39 +02:00
2021-09-01 21:51:56 +02:00
### `cn:quote(s) -> s`
2021-04-29 21:47:39 +02:00
2021-09-06 11:28:55 +02:00
Quote literal string to be used in queries. Quoting only works if current
collation is known (ses `collation` arg on `connect()`).
2021-04-29 21:47:39 +02:00
### Multiple result set support
For a SQL query that produces multiple result-sets, it is always your duty to
check the 'again' error message returned by the query, and keep pulling more
result sets by calling the `read_result()` until no 'again' error message
returned (or some other errors happen).
## Limitations
### Authentication
By default, of all authentication methods, only
[Old Password Authentication(mysql_old_password)](https://dev.mysql.com/doc/internals/en/old-password-authentication.html)
and [Secure Password Authentication(mysql_native_password)](https://dev.mysql.com/doc/internals/en/secure-password-authentication.html)
are suppored.
## TODO
* implement the data compression support in the protocol.