Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8abb2d7
copy the code from old project
Aug 16, 2016
9cbe7c2
first test case for prepare
Aug 16, 2016
4721da8
添加 markdown 示例
Aug 16, 2016
1a8cdd0
modify the connect address
Aug 16, 2016
d2caf7c
modify the connect param
Aug 16, 2016
3ef50fa
support the float and double type for response
membphis Aug 16, 2016
b7fe001
change the input param
membphis Aug 16, 2016
b0e7738
change lua code style
Aug 17, 2016
7e1c9a0
remove unused variable
Aug 17, 2016
993c539
change the test case env variable
Aug 17, 2016
d98be1f
change and test case and main example
Aug 17, 2016
4bf423c
change the prepare funtion return object
Aug 17, 2016
94aaf9d
specified the query sql type
Aug 17, 2016
2995802
_read_eof_packet function return two variable
Aug 17, 2016
bdf0415
merge the similar code
Aug 19, 2016
f11a58f
In the correct position to modify the state
Aug 19, 2016
7e8641d
add comment and change code style
Aug 19, 2016
b68402c
Merge branch 'feature/prepare_statement_internal' into feature/prepar…
Aug 19, 2016
f417b35
support all of the number type
membphis Aug 22, 2016
6e56e71
Merge branch 'feature/prepare_statement_internal' into feature/prepar…
membphis Aug 22, 2016
ea2e006
add test case for string type
membphis Aug 22, 2016
f80ba73
fully support Date and Time Data Type
membphis Aug 23, 2016
526cc69
Merge branch 'feature/prepare_statement_internal' into feature/prepar…
Aug 23, 2016
167fc0a
change code style
Aug 23, 2016
191e989
change Lua code style
Aug 23, 2016
276ed59
Merge branch 'feature/prepare_statement' into feature/prepare_stateme…
membphis Aug 23, 2016
d18e794
share the cdata on the Lua VM level
membphis Aug 24, 2016
129606f
Merge branch 'feature/prepare_statement_internal' into feature/prepar…
membphis Aug 24, 2016
97c15ee
avoid the table lookup overhead in the `mysql_data_type` variable
membphis Aug 25, 2016
5ac515e
Merge branch 'feature/prepare_statement_internal' into feature/prepar…
membphis Aug 25, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,118 @@ Synopsis
}
```


The example for using mysql's prepare-statement:

```nginx

# you do not need the following line if you are using
# the ngx_openresty bundle:
lua_package_path "/path/to/lua-resty-mysql/lib/?.lua;;";

server {
location /test {
content_by_lua_block {
local mysql = require "resty.mysql"
local json = require "cjson"

local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end

db:set_timeout(1000) -- 1 sec

-- or connect to a unix domain socket file listened
-- 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{
host = "127.0.0.1",
port = 3306,
database = "ngx_test",
user = "ngx_test",
password = "ngx_test",
max_packet_size = 1024 * 1024 }

if not ok then
ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end

ngx.say("connected to mysql.")

local res, err, errcode, sqlstate =
db:query("drop table if exists cats")
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end

res, err, errcode, sqlstate =
db:query("create table cats "
.. "(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.")

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, ")")

local statement_id, err = db:prepare([[SELECT id
FROM cats WHERE id = ? OR id = ?]])
if err then
ngx.say("prepare failed:", err)
return
end

ngx.say("prepare success:", json.encode(statement_id))

local res, err = db:execute(statement_id, 1, 2)
if err then
ngx.say("execute failed.", err)
return
end

ngx.say("execute success:", json.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
}
}
}
```

[Back to TOC](#table-of-contents)

Methods
Expand Down
Loading