Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. local mysql = require "resty.mysql"
  2.  
  3. -- 数据库配置
  4. local st = require "settings"
  5.  
  6. local _M = { _version = "0.1" }
  7.  
  8. local mt = { __index = _M }
  9.  
  10.  
  11. function _M.new(self, o)
  12. o = o or { _database = st.mysql.database }
  13. setmetatable(o, mt)
  14. return o
  15. end
  16.  
  17. -- 获取数据库连接
  18. function _M.get_conn(self, conn_info)
  19. conn_info = conn_info or {}
  20. local db, err = mysql:new()
  21. if not db then
  22. ngx.log(ngx.ERR, "failed to instantiate mysql: " .. err)
  23. return nil
  24. end
  25. -- 默认超时3000ms
  26. local timeout = conn_info.timeout or 3000
  27. db:set_timeout(timeout)
  28. local mysql_st = st.mysql
  29. local database = conn_info.database or self._database
  30. local host = mysql_st.host
  31. local port = mysql_st.port
  32. local user = mysql_st.user
  33. local password = mysql_st.password
  34. local ok, err, errcode, sqlstate = db:connect{
  35. host = host,
  36. port = port,
  37. database = database,
  38. user = user,
  39. password = password,
  40. }
  41. if not ok then
  42. ngx.log(ngx.ERR, "failed to connect: " .. err .. ": " .. errcode .. " " .. sqlstate)
  43. return nil
  44. end
  45. if db:get_reused_times() == 0 then
  46. -- 第一次使用,设置编码utf8,避免获取到的中文数据出现乱码
  47. db:query("SET NAMES utf8")
  48. end
  49. return db
  50. end
  51.  
  52.  
  53. -- 插入封装
  54. -- table_name: 数据库表
  55. -- item: 插入的内容
  56. function _M.insert(self, table_name, item)
  57. local fields_item = {}
  58. local values_item = {}
  59. local index = 1
  60. for field, value in pairs(item) do
  61. fields_item[index] = "`" .. field .. "`"
  62. values_item[index] = ngx.quote_sql_str(value)
  63. index = index + 1
  64. end
  65. local sql = string.format("INSERT INTO %s (%s) VALUES (%s)", table_name,
  66. table.concat(fields_item, ","),
  67. table.concat(values_item, ","))
  68. local db = self:get_conn()
  69. local res, err, errcode, sqlstate = db:query(sql)
  70. if not res then
  71. ngx.log(ngx.ERR, "bad result: " .. err .. ": " .. errcode .. ": " .. sqlstate)
  72. return nil, err, errcode, sqlstate
  73. end
  74. db:set_keepalive(10000, 100)
  75. return res
  76. end
  77.  
  78.  
  79. -- 查询封装
  80. -- table_name: 数据库表
  81. -- query_table: 查询内容
  82. function _M.find(self, table_name, query_table)
  83. local columns = query_table.columns or "*"
  84. local conditions = query_table.conditions or ""
  85. local bind = query_table.bind or {}
  86. local order = query_table.order or ""
  87. local limit = tonumber(query_table.limit) or -1
  88. local offset = tonumber(query_table.offset) or -1
  89.  
  90. for k, v in pairs(bind) do
  91. local regex = ":" .. k .. ":"
  92. conditions, n, err = ngx.re.sub(conditions, regex, ngx.quote_sql_str(v))
  93. if not conditions then
  94. ngx.log(ngx.ERR, err)
  95. return nil
  96. end
  97. end
  98.  
  99. local sql = string.format("SELECT %s FROM %s", columns, table_name)
  100.  
  101. if #conditions ~= 0 then
  102. sql = string.format("%s WHERE %s", sql, conditions)
  103. end
  104.  
  105. if #order ~= 0 then
  106. sql = string.format("%s %s", sql, order)
  107. end
  108.  
  109. if offset > 0 then
  110. sql = string.format("%s OFFSET %s", sql, offset)
  111. end
  112.  
  113. if limit > 0 then
  114. sql = string.format("%s LIMIT %s", sql, limit)
  115. end
  116.  
  117. local db = self:get_conn()
  118. local res, err, errcode, sqlstate = db:query(sql)
  119. if not res then
  120. ngx.log(ngx.ERR, "bad result: " .. err .. ": " .. errcode .. ": " .. sqlstate)
  121. return nil, err, errcode, sqlstate
  122. end
  123. db:set_keepalive(10000, 100)
  124. return res
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement