From 48d0133a79bff71ada92dff809d65d0bdae91d16 Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Sat, 25 Oct 2025 14:33:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8orm=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A1=A8=E5=B0=81=E8=A3=85=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E7=BC=96=E5=86=99=E5=92=8C=E8=B0=83=E8=AF=95=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=87=BD=E6=95=B0=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=92=8C=E7=9B=B8=E5=85=B3=E5=86=85=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/test.lua | 21 ++++++-- src/util/database.lua | 30 +++++------ src/util/helpers.lua | 6 +-- src/util/model.lua | 121 ++++++++++++++++++++++-------------------- 4 files changed, 97 insertions(+), 81 deletions(-) diff --git a/src/test/test.lua b/src/test/test.lua index ac8f2d0..3b309fd 100644 --- a/src/test/test.lua +++ b/src/test/test.lua @@ -15,10 +15,23 @@ local User = require("model.user") -local user = User.find("1") -ngx.say(user) -local userPages = User:paginate(1) -ngx.say(userPages) +--获取数据表中的记录数 +local code, res = User:count() +--ngx.say(res) + +--查询表中id为1的数据记录 +code, res = User:find("1") + +--查询表中的所有记录 +code, res = User:all() +for _, row in ipairs(res) do + for key, value in pairs(row) do + ngx.say(key .. ":" .. tostring(value)) + end +end + +--local code, userPages = User:paginate(1) +--ngx.say(userPages) --获取请求参数的键值和数据值 --local cjson = require("cjson") diff --git a/src/util/database.lua b/src/util/database.lua index 97c9d79..2e12cb3 100644 --- a/src/util/database.lua +++ b/src/util/database.lua @@ -12,15 +12,14 @@ local mt = { __index = _M } @return bool, db_context, err --]] function _M:get_connection() + local code = 0 if ngx.ctx[self.db_type] then -- if write before read, make sure write read connection the same if ngx.ctx[WRITE] then - return ngx.ctx[WRITE], nil + return code, ngx.ctx[WRITE] end - return ngx.ctx[self.db_type], nil + return code, ngx.ctx[self.db_type] end - - local code = 0 -- 创建一个新的连接 local conn = pgmoon.new({ host = self.host, -- postgres host @@ -29,13 +28,14 @@ function _M:get_connection() password = self.password, -- postgres password database = self.database }); - conn.set_timeout(self.timeout or 1000) + --conn.set_timeout(self.timeout or 1000) ---- 连接到数据库 local ok, err = conn:connect() if not ok then print("Connection failed: " .. err) code = 0x000002 end + ngx.log(ngx.INFO, 'Connection success') --ngx.say("Connection success") ngx.ctx[self.db_type] = conn return code,conn @@ -63,18 +63,18 @@ end @return bool, data, err --]] function _M.db_query(self, sql) - local db, err = self:get_connection() - if err ~= nil then - return nil, err + local code, conn = self:get_connection() + if code ~= 0 then + return code,nil end - local res, errcode, sqlstate - res, err, errcode, sqlstate = db:query(sql) + ngx.log(ngx.INFO, 'begin db query :' .. sql) + -- 执行查询 + local res, err = conn:query(sql) if not res then - ngx.log(ngx.ERR, err, errcode, sqlstate) - return nil, err + ngx.log(ngx.ERR, 'Query failed:' .. sql) + return 2,nil end - - return res, nil + return 0, res end function _M.new(self, opts) @@ -85,7 +85,7 @@ function _M.new(self, opts) password = opts.password or '', database = opts.database or 'postgres', --charset = opts.charset or 'utf8mb4', - --timeout = opts.timeout, + --timeout = opts.timeout or 1000, --max_packet_size = 1024 * 1024, db_pool_timeout = opts.pool_timeout or 1000, db_pool_size = opts.pool_size or 1000, diff --git a/src/util/helpers.lua b/src/util/helpers.lua index bf5ce1a..43b89c1 100644 --- a/src/util/helpers.lua +++ b/src/util/helpers.lua @@ -94,7 +94,7 @@ end local function set_cookie(key, value, expires) local config = require("config.app") - local cookie, err = require("lib.cookie"):new() + local cookie, err = require("util.cookie"):new() if not cookie then ngx.log(ngx.ERR, err) return false, err @@ -118,7 +118,7 @@ local function set_cookie(key, value, expires) end local function get_cookie(key) - local cookie, err = require("lib.cookie"):new() + local cookie, err = require("util.cookie"):new() if not cookie then ngx.log(ngx.ERR, err) return false @@ -127,7 +127,7 @@ local function get_cookie(key) end local function get_local_time() - local config = require("config.app") + local config = require("config.config") local time_zone = ngx.re.match(config.time_zone, "[0-9]+") if time_zone == nil then local err = "not set time zone or format error, time zone should look like `+8:00` current is: " .. config.time_zone diff --git a/src/util/model.lua b/src/util/model.lua index 03b39dc..d1dcfc2 100644 --- a/src/util/model.lua +++ b/src/util/model.lua @@ -8,8 +8,6 @@ local table_remove = helpers.table_remove local _M = {} -local mt = { __index = _M } - local WRITE = 'WRITE' local READ = 'READ' @@ -125,27 +123,29 @@ end function _M:find(id,column) if self.query_sql ~= nil then ngx.log(ngx.ERR, 'cannot use find() with other query sql') - return nil + return 1, nil end column = column or 'id' id = transform_value(id) + ngx.log(ngx.INFO, 'table name :' .. self.table) local sql = 'select * from '..self.table..' where '..column..'='..id..' limit 1' - local res = self:query(sql, READ) + ngx.log(ngx.INFO, 'query sql:', sql) + local code, res = self:query(sql, READ) if table.getn(res) > 0 then + ngx.log(ngx.INFO, 'query record count:', table.getn(res)) res = self:make_relations(res) - return res[1] - else - return false + return code, res end + return code, nil end function _M:all() if self.query_sql ~= nil then ngx.log(ngx.ERR, 'cannot use all() with other query sql ', self.query_sql) - return nil + return 2, nil end - local res = self:query('select * from '..self.table, READ) - return self:make_relations(res) + local code, res = self:query('select * from \"'..self.table..'\"', READ) + return code, self:make_relations(res) end --组装sql语句条件为and相关的字段 @@ -187,18 +187,17 @@ end --获取数据表中记录的数量 function _M:count() - local sql = self.query_sql - if not sql then - sql = 'select count(*) from '..self.table - else - sql = 'select count(*) from '..self.table..' '..self.query_sql - end - local res = self:query(sql, READ) - if table.getn(res) > 0 then - return tonumber(res[1]['count(*)']) - else - return 0 + local sql = "" + if self.query_sql ~= nil then + sql = 'select count(*) from \"'..self.table..'\" '..self.query_sql + else + sql = 'select count(*) from \"'..self.table.."\"" end + local code, res = self:query(sql, READ) + if table.getn(res) > 0 then + return code, tonumber(res[1]['count']) + end + return code, nil end -- params: (option)int num @@ -222,10 +221,10 @@ function _M:get(num) end --根据数据模型中的 -function _M:paginate(page_num, per_page) +function _M:paginate(page_num, page_size) page_num = page_num or 1 - per_page = per_page or config.per_page - local sql, count_sql, total + page_size = page_size or 10 + local sql, count_sql, total, code local data={ data = {}, next_page = 1, @@ -233,25 +232,24 @@ function _M:paginate(page_num, per_page) total = 0 } if not self.query_sql then - sql = 'select * from '..self.table..' limit '..per_page*page_num..','..per_page - count_sql = 'select count(*) from '..self.table + sql = 'select * from \"'..self.table..'\" limit '..page_num..' offset '..page_size + count_sql = 'select count(*) from \"'..self.table..'\"' else - sql = 'select * from '..self.table .. ' '..self.query_sql .. ' limit '..per_page*(page_num-1)..','..per_page - count_sql = 'select count(*) from '..self.table..' '..self.query_sql + sql = 'select * from \"'..self.table .. '\" '..self.query_sql .. ' limit '..page_num..' offset '..page_size + count_sql = 'select count(*) from \"'..self.table..'\" '..self.query_sql end - total = self:query(count_sql, READ) - if not total then - else - data['total'] = tonumber(total[1]['count(*)']) + code, total = self:query(count_sql, READ) + if code == 0 then + data['total'] = tonumber(total[1]['count']) data['data'] = self:make_relations(self:query(sql, READ)) end + --暂时没有理清下面的业务逻辑 if (table.getn(data['data']) + ((page_num - 1)* per_page)) < data['total'] then data['next_page'] = page_num + 1 end if tonumber(page_num) ~= 1 then data['prev_page'] = page_num - 1 end - return data end @@ -365,30 +363,29 @@ end --查询数据表中的数据记录 function _M:query(sql, type) if not sql then - return ngx.log(ngx.ERR,'query() function need sql to query') + ngx.log(ngx.ERR,'query() function need sql to query') + return 2, nil end self.query_sql = nil self.has_order_by = false if type == READ then - local result, err = database_read:db_query(sql) - if err ~= nil then - ngx.log(ngx.ERR, "read db error. res: " .. (err or "no reason")) - ngx.exit(500) - return + local code, result = database_read:db_query(sql) + if code ~= 0 then + ngx.log(ngx.ERR, "read db error. res: " .. tostring(code).." or no reason") + return code, nil end - return result + return code, result elseif type == WRITE then - local result, err = database_write:db_query(sql) - if err ~= nil then - ngx.log(ngx.ERR, "write db error. res: " .. (err or "no reason")) - ngx.exit(500) - return + local code, result = database_write:db_query(sql) + if code ~= 0 then + ngx.log(ngx.ERR, "write db error. res: " .. (code or "no reason")) + return code, nil end - return result.affected_rows + return code, result.affected_rows else ngx.log(ngx.ERR, 'type invalid, need ' .. READ .. ' or '..WRITE) ngx.exit(500) - return + return 2, nil end end @@ -397,20 +394,26 @@ function _M:get_hidden() return self.hidden end +function _M:debug() + ngx.log(ngx.INFO, self.table.." ") +end + --初始化数据表中的字段 function _M:new(table, attributes, hidden) - return setmetatable({ - table = table, - attributes = attributes or {}, - hidden = hidden or {}, - query_sql = nil, - has_order_by = false, - relation = { - mode = 0 - }, - relation_sql = nil, - --soft_delete_column = 'deleted_at' - }, mt) + local obj = { + table = table, + attributes = attributes or {}, + hidden = hidden or {}, + query_sql = nil, + has_order_by = false, + relation = { + mode = 0 + }, + relation_sql = nil, + --soft_delete_column = 'deleted_at' + } + setmetatable(obj, {__index = _M}) + return obj end return _M \ No newline at end of file