mirror of
https://github.com/luapower/mysql.git
synced 2025-01-01 14:00:27 +01:00
switched to a binary protocol; added prepared statements
This commit is contained in:
parent
adde594b24
commit
27382e197d
1310
mysql_client.lua
1310
mysql_client.lua
File diff suppressed because it is too large
Load Diff
@ -1,25 +1,21 @@
|
|||||||
|
|
||||||
## `local mysql = require'mysql_client'`
|
## `local mysql = require'mysql_client'`
|
||||||
|
|
||||||
MySQL client protocol in Lua. Stolen from OpenResty and modified to work standalone.
|
MySQL client protocol in Lua.
|
||||||
|
Stolen from OpenResty and modified to work standalone.
|
||||||
## Status
|
|
||||||
|
|
||||||
This library is considered production ready.
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local mysql = require'mysql_client'
|
local mysql = require'mysql_client'
|
||||||
local cn = assert(mysql:new())
|
|
||||||
|
|
||||||
assert(cn:connect{
|
assert(mysql.connect{
|
||||||
host = '127.0.0.1',
|
host = '127.0.0.1',
|
||||||
port = 3306,
|
port = 3306,
|
||||||
database = 'ngx_test',
|
database = 'foo',
|
||||||
user = 'ngx_test',
|
user = 'bar',
|
||||||
password = 'ngx_test',
|
password = 'baz',
|
||||||
charset = 'utf8',
|
charset = 'utf8mb4',
|
||||||
max_packet_size = 1024 * 1024,
|
max_packet_size = 1024 * 1024,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -35,21 +31,14 @@ local res = assert(cn:query('insert into cats (name) '
|
|||||||
print(res.affected_rows, ' rows inserted into table cats ',
|
print(res.affected_rows, ' rows inserted into table cats ',
|
||||||
'(last insert id: ', res.insert_id, ')')
|
'(last insert id: ', res.insert_id, ')')
|
||||||
|
|
||||||
local res = assert(cn:query('select * from cats order by id asc', 10))
|
require'pp'(assert(cn:query('select * from cats order by id asc', 10)))
|
||||||
|
|
||||||
local cjson = require'cjson'
|
|
||||||
print(cjson.encode(res))
|
|
||||||
|
|
||||||
assert(cn:close())
|
assert(cn:close())
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### `mysql:new() -> cn | nil,err`
|
### `mysql.connect(options) -> ok | nil,err,errcode,sqlstate`
|
||||||
|
|
||||||
Creates a MySQL connection object.
|
|
||||||
|
|
||||||
### `cn:connect(options) -> ok | nil,err,errcode,sqlstate`
|
|
||||||
|
|
||||||
Connect to a MySQL server.
|
Connect to a MySQL server.
|
||||||
|
|
||||||
@ -129,12 +118,22 @@ 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)
|
success because this method will only call [read_result](#read_result)
|
||||||
once for you.
|
once for you.
|
||||||
|
|
||||||
### `cn:server_ver() -> s`
|
|
||||||
|
|
||||||
Returns the MySQL server version string, like `"5.1.64"`.
|
### `cn:prepare(query) -> stmt`
|
||||||
|
|
||||||
You should only call this method after successfully connecting to a MySQL server,
|
Prepare a statement.
|
||||||
otherwise `nil` will be returned.
|
|
||||||
|
### `stmt:exec(params...)`
|
||||||
|
|
||||||
|
Execute a statement. Use `cn:read_result()` to get the results.
|
||||||
|
|
||||||
|
### `stmt:free()`
|
||||||
|
|
||||||
|
Free statement.
|
||||||
|
|
||||||
|
### `cn.server_ver`
|
||||||
|
|
||||||
|
The MySQL server version string.
|
||||||
|
|
||||||
### `mysql.quote(s) -> s`
|
### `mysql.quote(s) -> s`
|
||||||
|
|
||||||
@ -158,7 +157,5 @@ are suppored.
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
* implement the MySQL binary row data packets.
|
|
||||||
* implement MySQL server prepare and execute packets.
|
|
||||||
* implement the data compression support in the protocol.
|
* implement the data compression support in the protocol.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user