mirror of
https://github.com/luapower/mysql.git
synced 2025-01-06 08:10:24 +01:00
unimportant
This commit is contained in:
parent
5612b97801
commit
cb7ae6e888
@ -510,16 +510,6 @@ function _M.new(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function _M.set_timeout(self, timeout)
|
|
||||||
local sock = self.sock
|
|
||||||
if not sock then
|
|
||||||
return nil, "not initialized"
|
|
||||||
end
|
|
||||||
|
|
||||||
return sock:settimeout(timeout)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function _M.connect(self, opts)
|
function _M.connect(self, opts)
|
||||||
local sock = self.sock
|
local sock = self.sock
|
||||||
if not sock then
|
if not sock then
|
||||||
|
112
mysql_client.md
112
mysql_client.md
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
## `local mysql = require'mysql_client'`
|
## `local mysql = require'mysql_client'`
|
||||||
|
|
||||||
MySQL client protocol
|
MySQL client protocol in Lua. Stolen from OpenResty and modified to work standalone.
|
||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|
||||||
@ -10,96 +10,37 @@ This library is considered production ready.
|
|||||||
## Example
|
## Example
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local mysql = require "resty.mysql"
|
local mysql = require'mysql_client'
|
||||||
local db, err = mysql:new()
|
local db = assert(mysql:new())
|
||||||
if not db then
|
|
||||||
ngx.say("failed to instantiate mysql: ", err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
db:set_timeout(1000) -- 1 sec
|
assert(db:connect{
|
||||||
|
host = '127.0.0.1',
|
||||||
|
port = 3306,
|
||||||
|
database = 'ngx_test',
|
||||||
|
user = 'ngx_test',
|
||||||
|
password = 'ngx_test',
|
||||||
|
charset = 'utf8',
|
||||||
|
max_packet_size = 1024 * 1024,
|
||||||
|
})
|
||||||
|
|
||||||
-- or connect to a unix domain socket file listened
|
assert(db:query('drop table if exists cats'))
|
||||||
-- by a mysql server:
|
|
||||||
-- local ok, err, errcode, sqlstate =
|
|
||||||
-- db:connect{
|
|
||||||
-- path = "/path/to/mysql.sock",
|
|
||||||
-- database = "ngx_test",
|
|
||||||
-- user = "ngx_test",
|
|
||||||
-- password = "ngx_test" }
|
|
||||||
|
|
||||||
local ok, err, errcode, sqlstate = db:connect{
|
local res = assert(db:query('create table cats '
|
||||||
host = "127.0.0.1",
|
.. '(id serial primary key, '
|
||||||
port = 3306,
|
.. 'name varchar(5))'))
|
||||||
database = "ngx_test",
|
|
||||||
user = "ngx_test",
|
|
||||||
password = "ngx_test",
|
|
||||||
charset = "utf8",
|
|
||||||
max_packet_size = 1024 * 1024,
|
|
||||||
}
|
|
||||||
|
|
||||||
if not ok then
|
local res = assert(db:query('insert into cats (name) '
|
||||||
ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
|
.. 'values (\'Bob\'),(\'\'),(null)'))
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
ngx.say("connected to mysql.")
|
print(res.affected_rows, ' rows inserted into table cats ',
|
||||||
|
'(last insert id: ', res.insert_id, ')')
|
||||||
|
|
||||||
local res, err, errcode, sqlstate =
|
local res = assert(db:query('select * from cats order by id asc', 10))
|
||||||
db:query("drop table if exists cats")
|
|
||||||
if not res then
|
|
||||||
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
res, err, errcode, sqlstate =
|
local cjson = require'cjson'
|
||||||
db:query("create table cats "
|
print(cjson.encode(res))
|
||||||
.. "(id serial primary key, "
|
|
||||||
.. "name varchar(5))")
|
|
||||||
if not res then
|
|
||||||
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
ngx.say("table cats created.")
|
assert(db:close())
|
||||||
|
|
||||||
res, err, errcode, sqlstate =
|
|
||||||
db:query("insert into cats (name) "
|
|
||||||
.. "values (\'Bob\'),(\'\'),(null)")
|
|
||||||
if not res then
|
|
||||||
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
ngx.say(res.affected_rows, " rows inserted into table cats ",
|
|
||||||
"(last insert id: ", res.insert_id, ")")
|
|
||||||
|
|
||||||
-- run a select query, expected about 10 rows in
|
|
||||||
-- the result set:
|
|
||||||
res, err, errcode, sqlstate =
|
|
||||||
db:query("select * from cats order by id asc", 10)
|
|
||||||
if not res then
|
|
||||||
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cjson = require "cjson"
|
|
||||||
ngx.say("result: ", cjson.encode(res))
|
|
||||||
|
|
||||||
-- put it into the connection pool of size 100,
|
|
||||||
-- with 10 seconds max idle timeout
|
|
||||||
local ok, err = db:set_keepalive(10000, 100)
|
|
||||||
if not ok then
|
|
||||||
ngx.say("failed to set keepalive: ", err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- or just close the connection right away:
|
|
||||||
-- local ok, err = db:close()
|
|
||||||
-- if not ok then
|
|
||||||
-- ngx.say("failed to close: ", err)
|
|
||||||
-- return
|
|
||||||
-- end
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
@ -134,11 +75,6 @@ The `options` argument is a Lua table holding the following keys:
|
|||||||
* `compact_arrays`: `true` to use array-of-arrays structure for the result set,
|
* `compact_arrays`: `true` to use array-of-arrays structure for the result set,
|
||||||
rather than the default array-of-hashes structure.
|
rather than the default array-of-hashes structure.
|
||||||
|
|
||||||
### `db:set_timeout(time)`
|
|
||||||
|
|
||||||
Sets the timeout (in ms) protection for subsequent operations,
|
|
||||||
including the `connect` method.
|
|
||||||
|
|
||||||
### `db:close() -> 1 | nil,err`
|
### `db:close() -> 1 | nil,err`
|
||||||
|
|
||||||
Closes the current mysql connection and returns the status.
|
Closes the current mysql connection and returns the status.
|
||||||
|
Loading…
Reference in New Issue
Block a user