Skip to content

add resty.mysql:connect support dsn string #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 11 additions & 6 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ Synopsis
-- user = "ngx_test",
-- password = "ngx_test" }

-- local ok, err, errno, sqlstate = db:connect{
-- host = "127.0.0.1",
-- port = 3306,
-- database = "ngx_test",
-- user = "ngx_test",
-- password = "ngx_test",
-- max_packet_size = 1024 * 1024 }

local ok, err, errno, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "ngx_test",
user = "ngx_test",
password = "ngx_test",
max_packet_size = 1024 * 1024 }
dsn="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, ": ", errno, " ", sqlstate)
Expand Down
24 changes: 24 additions & 0 deletions lib/resty/mysql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local strbyte = string.byte
local strchar = string.char
local strfind = string.find
local strrep = string.rep
local strsub = string.sub
local null = ngx.null
local band = bit.band
local bxor = bit.bxor
Expand All @@ -52,6 +53,21 @@ end
converters[0x09] = tonumber -- int24
converters[0x0d] = tonumber -- year

local function _split(str, seq)
local list = {}
local pos = 1
while 1 do
local first, last = strfind(str, seq, pos)
if first then
insert(list, strsub(str, pos, first-1))
pos = last+1
else
insert(list, strsub(str, pos))
break
end
end
return list
end

local function _get_byte2(data, i)
local a, b = strbyte(data, i, i + 1)
Expand Down Expand Up @@ -458,6 +474,14 @@ function connect(self, opts)
return nil, "not initialized"
end

local dsn = opts.dsn
if dsn then
for _, value in pairs(_split(dsn, ";")) do
local _item = _split(value, "=")
opts[_item[1]] = _item[2]
end
end

local max_packet_size = opts.max_packet_size
if not max_packet_size then
max_packet_size = 1024 * 1024 -- default 1 MB
Expand Down