2025-09-27 15:19:58 +08:00
|
|
|
---
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
2025-10-15 17:05:35 +08:00
|
|
|
--- Created by .
|
2025-09-27 15:19:58 +08:00
|
|
|
--- DateTime: 2025/9/25 08:19
|
2025-10-14 14:45:47 +08:00
|
|
|
--- 业务逻辑
|
2025-10-15 17:05:35 +08:00
|
|
|
local cjson = require('cjson')
|
2025-10-16 16:47:24 +08:00
|
|
|
local pgmoon = require('pgmoon')
|
|
|
|
|
local dbconf = require("config.database")
|
2025-10-15 17:05:35 +08:00
|
|
|
|
|
|
|
|
local _M = {}
|
2025-10-14 14:45:47 +08:00
|
|
|
|
2025-10-16 16:47:24 +08:00
|
|
|
local function get_con(cfg)
|
|
|
|
|
-- 创建一个新的连接
|
|
|
|
|
local conn = pgmoon.new(cfg);
|
|
|
|
|
---- 连接到数据库
|
|
|
|
|
local ok, err = conn:connect()
|
|
|
|
|
if not ok then
|
|
|
|
|
error("Connection failed: " .. err)
|
|
|
|
|
ngx.exit(ngx.HTTP_NOT_FOUND)
|
2025-10-14 14:45:47 +08:00
|
|
|
end
|
2025-10-16 16:47:24 +08:00
|
|
|
--ngx.say("Connection success")
|
|
|
|
|
return conn
|
|
|
|
|
end
|
2025-10-15 17:05:35 +08:00
|
|
|
|
2025-10-16 16:47:24 +08:00
|
|
|
-- 查询数据表中的所有用户信息
|
2025-10-15 17:05:35 +08:00
|
|
|
function _M.getAllUser()
|
2025-10-16 16:47:24 +08:00
|
|
|
--组装sql语句
|
|
|
|
|
local sql = "select * from \"T_Users\""
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
local conn = get_con(dbconf.postgres)
|
|
|
|
|
--设置数据库的编码格式
|
|
|
|
|
--conn:exec("SET NAMES UTF8")
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
local res = conn:query(sql)
|
|
|
|
|
if not res then
|
|
|
|
|
error("Query failed: " .. err)
|
|
|
|
|
--ngx.say(err)
|
|
|
|
|
ngx.exit(ngx.HTTP_NOT_FOUND)
|
|
|
|
|
end
|
|
|
|
|
--整理数据库结果返回值
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-10-15 17:22:04 +08:00
|
|
|
|
2025-10-16 16:47:24 +08:00
|
|
|
--关闭数据库
|
|
|
|
|
conn:disconnect()
|
2025-10-15 17:05:35 +08:00
|
|
|
end
|
|
|
|
|
|
2025-10-16 16:47:24 +08:00
|
|
|
--根据用户id获取用户信息
|
|
|
|
|
function _M.getUser(id)
|
|
|
|
|
--组装sql语句
|
|
|
|
|
local sql = "select * from \"T_Users\" where id="..id
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
local conn = get_con(dbconf.postgres)
|
|
|
|
|
--设置数据库的编码格式
|
|
|
|
|
--conn:exec("SET NAMES UTF8")
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
local res = conn:query(sql)
|
|
|
|
|
if not res then
|
|
|
|
|
error("Query failed: " .. err)
|
|
|
|
|
--ngx.say(err)
|
|
|
|
|
ngx.exit(ngx.HTTP_NOT_FOUND)
|
|
|
|
|
end
|
|
|
|
|
--整理数据库结果返回值
|
2025-10-16 20:08:20 +08:00
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
-- end
|
|
|
|
|
--end
|
|
|
|
|
ngx.say(cjson.encode(res))
|
2025-10-16 16:47:24 +08:00
|
|
|
--关闭数据库
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
end
|
|
|
|
|
return _M
|