-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
leandre
committed
Feb 22, 2016
1 parent
355081a
commit 3bbda4a
Showing
11 changed files
with
177 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-- | ||
-- mysql config | ||
-- User: leandre | ||
-- Date: 16/2/1 | ||
-- Time: 上午11:21 | ||
-- | ||
|
||
local _P = { | ||
alpha = { | ||
master = { | ||
host = "127.0.0.1", | ||
port = "3306", | ||
user = "root", | ||
password = "root", | ||
database = "test" | ||
}, | ||
slave = { | ||
host = "127.0.0.1", | ||
port = "3306", | ||
user = "root", | ||
password = "root", | ||
database = "test" | ||
} | ||
} | ||
} | ||
return _P |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,4 +65,4 @@ function _CTR:error(code, msg, data) | |
return true | ||
end | ||
|
||
return _CTR; | ||
return _CTR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- | ||
-- mysql test | ||
-- User: leandre | ||
-- Date: 16/2/22 | ||
-- Time: 下午3:22 | ||
-- | ||
|
||
local CTR = require(app_config["ctr_path"] .. "abstract") | ||
local _M = CTR | ||
|
||
--- test index action | ||
-- | ||
function _M:index() | ||
local model = require(app_path .. "model.test") | ||
local result = model:getTest() | ||
self:succ("", "", result) | ||
end | ||
|
||
return _M | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,5 @@ | |
-- | ||
local _C = {} | ||
|
||
return _C; | ||
return _C | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
-- | ||
-- mysql | ||
-- User: leandre | ||
-- Date: 16/2/1 | ||
-- Time: 上午11:18 | ||
-- | ||
|
||
local _M = {} | ||
local _config = {} | ||
|
||
--- init db config | ||
-- | ||
function _M:init() | ||
_config = require(app_path .. "config.mysql") | ||
return _config | ||
end | ||
|
||
function _M:connect(pool_name, is_slave) | ||
local mysql = require("resty.mysql") | ||
local db, err = mysql:new() | ||
if not db then | ||
ngx.say("failed to instantiate mysql: ", err) | ||
return | ||
end | ||
|
||
db:set_timeout(1000) -- 1 sec | ||
|
||
local conf = {} | ||
if is_slave == true then | ||
conf = _config[pool_name]["slave"] | ||
else | ||
conf = _config[pool_name]["master"] | ||
end | ||
|
||
local ok, err, errno, sqlstate = db:connect { | ||
host = conf["host"], | ||
port = conf["port"], | ||
database = conf["database"], | ||
user = conf["user"], | ||
password = conf["password"], | ||
max_packet_size = 1024 * 1024 | ||
} | ||
|
||
if not ok then | ||
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate) | ||
return false | ||
end | ||
|
||
return db | ||
end | ||
|
||
function _M:query(poolname, sql) | ||
if poolname == "" or sql == "" then | ||
return false | ||
end | ||
|
||
local db | ||
if string.find(sql, "select") == 1 then | ||
db = self:connect(poolname, true) -- slave db | ||
else | ||
db = self:connect(poolname, false) -- master db | ||
end | ||
|
||
if not db then | ||
return false | ||
end | ||
|
||
|
||
local res, err, errno, sqlstate = db:query(sql) | ||
if not res then | ||
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") | ||
return false | ||
end | ||
|
||
-- 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 false | ||
end | ||
|
||
return res | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- | ||
-- test model | ||
-- User: leandre | ||
-- Date: 16/2/22 | ||
-- Time: 下午3:24 | ||
-- | ||
|
||
local _M = {} | ||
local mysql = container["mysql"]; | ||
|
||
function _M:getTest() | ||
local res = mysql:query("alpha", "select * from users;") | ||
if res then | ||
return res | ||
end | ||
return false | ||
end | ||
|
||
return _M |