修改用户接口,编写例子并进行分页、排序、删除等数据表操作功能测试
This commit is contained in:
parent
6581489a07
commit
a786ad1c6f
|
|
@ -61,7 +61,7 @@ end
|
||||||
--根据用户id删除用户信息
|
--根据用户id删除用户信息
|
||||||
function _M.delete_user(m)
|
function _M.delete_user(m)
|
||||||
local id = m.id
|
local id = m.id
|
||||||
local code, ret = dao.delete_user(id)
|
local code, ret = dao.deleteUser(id)
|
||||||
local result = resp:json(code, ret)
|
local result = resp:json(code, ret)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
end
|
end
|
||||||
|
|
@ -79,7 +79,7 @@ function _M.update_user(m)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local code, ret = dao.update_user(id, body_data)
|
local code, ret = dao.updateUser(id, body_data)
|
||||||
local result = resp:json(code, ret)
|
local result = resp:json(code, ret)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -31,46 +31,64 @@ code, res = User:all()
|
||||||
-- end
|
-- end
|
||||||
--end
|
--end
|
||||||
|
|
||||||
|
--ngx.say("----begin where and query---")
|
||||||
-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据
|
-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据
|
||||||
code, res = User:where('username','=','zhangsan'):where('password','=','111111'):get()
|
code, res = User:where('name','=','zhangsan'):where('password','=','111111'):get()
|
||||||
for _, row in ipairs(res) do
|
--for _, row in ipairs(res) do
|
||||||
for key, value in pairs(row) do
|
-- for key, value in pairs(row) do
|
||||||
ngx.say(key .. ":" .. tostring(value))
|
-- ngx.say(key .. ":" .. tostring(value))
|
||||||
end
|
-- end
|
||||||
end
|
--end
|
||||||
|
|
||||||
|
--ngx.say("----begin where or query---")
|
||||||
-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组
|
-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组
|
||||||
code, res = User:where('name','=','zhangsan'):orwhere('name','=','admin'):get()
|
code, res = User:where('name','=','zhangsan'):orwhere('name','=','admin'):get()
|
||||||
for _, row in ipairs(res) do
|
--for _, row in ipairs(res) do
|
||||||
|
-- for key, value in pairs(row) do
|
||||||
|
-- ngx.say(key .. ":" .. tostring(value))
|
||||||
|
-- end
|
||||||
|
--end
|
||||||
|
|
||||||
|
--orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写),
|
||||||
|
code, res = User:orderby('created_time'):get()
|
||||||
|
--for _, row in ipairs(res) do
|
||||||
|
-- for key, value in pairs(row) do
|
||||||
|
-- ngx.say(key .. ":" .. tostring(value))
|
||||||
|
-- end
|
||||||
|
--end
|
||||||
|
|
||||||
|
-- 创建一个用户
|
||||||
|
code, res = User:create({
|
||||||
|
id='3',
|
||||||
|
password='22222',
|
||||||
|
name='lisi',
|
||||||
|
email='lisi@gmail.com',
|
||||||
|
})
|
||||||
|
|
||||||
|
-- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL
|
||||||
|
code, res = User:where('id', '=', '3'):update({
|
||||||
|
phone='666666',
|
||||||
|
email='zhangsan@qq.com'
|
||||||
|
})
|
||||||
|
--输出更新后影响的行总数
|
||||||
|
ngx.say("update affected_rows: ", res.affected_rows)
|
||||||
|
|
||||||
|
-- 删除 id = 1 的用户
|
||||||
|
code, res = User:where('id','=','3'):delete()
|
||||||
|
ngx.say("delete affected_rows: ", res.affected_rows)
|
||||||
|
|
||||||
|
--分页 获取数据表中的记录
|
||||||
|
local data = nil
|
||||||
|
code, data = User:paginate(1)
|
||||||
|
local count = data.total
|
||||||
|
ngx.say("data total:", count)
|
||||||
|
for _, row in ipairs(data.data) do
|
||||||
|
ngx.say("begin show data:")
|
||||||
for key, value in pairs(row) do
|
for key, value in pairs(row) do
|
||||||
ngx.say(key .. ":" .. tostring(value))
|
ngx.say(key .. ":" .. tostring(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写),
|
|
||||||
--code, res = User:orderby('created_at'):get()
|
|
||||||
|
|
||||||
-- 创建一个用户
|
|
||||||
--code, res = User:create({
|
|
||||||
-- id=3,
|
|
||||||
-- password='22222',
|
|
||||||
-- name='lisi',
|
|
||||||
-- email='lisi@gmail.com',
|
|
||||||
--})
|
|
||||||
|
|
||||||
-- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL
|
|
||||||
--code, res = User:where('id', '=', '1'):update({
|
|
||||||
-- phone='666666',
|
|
||||||
-- email='zhangsan@qq.com'
|
|
||||||
--})
|
|
||||||
|
|
||||||
-- 删除 id = 1 的用户
|
|
||||||
--code, res = User:where('id','=','1'):delete()
|
|
||||||
|
|
||||||
--分页 获取数据表中的记录
|
|
||||||
--local code, userPages = User:paginate(1)
|
|
||||||
--ngx.say(userPages)
|
|
||||||
|
|
||||||
--获取请求参数的键值和数据值
|
--获取请求参数的键值和数据值
|
||||||
--local cjson = require("cjson")
|
--local cjson = require("cjson")
|
||||||
--local header = ngx.req.get_headers()
|
--local header = ngx.req.get_headers()
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ function _M:retrieve_relations(ids)
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
local ids_str = implode(unique(ids))
|
local ids_str = implode(unique(ids))
|
||||||
self.relation_sql = 'select * from '..self.relation.model.table..' where ' .. self.relation.foreign_key .. ' in (' .. ids_str .. ')'
|
self.relation_sql = 'select * from \"'..self.relation.model.table..'\" where ' .. self.relation.foreign_key .. ' in (' .. ids_str .. ')'
|
||||||
return table_remove(self:query(self.relation_sql, READ), self.relation.model:get_hidden())
|
return table_remove(self:query(self.relation_sql, READ), self.relation.model:get_hidden())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -128,7 +128,7 @@ function _M:find(id,column)
|
||||||
column = column or 'id'
|
column = column or 'id'
|
||||||
id = transform_value(id)
|
id = transform_value(id)
|
||||||
ngx.log(ngx.INFO, 'table name :' .. self.table)
|
ngx.log(ngx.INFO, 'table name :' .. self.table)
|
||||||
local sql = 'select * from '..self.table..' where '..column..'='..id..' limit 1'
|
local sql = 'select * from \"'..self.table..'\" where '..column..'='..id..' limit 1'
|
||||||
ngx.log(ngx.INFO, 'query sql:', sql)
|
ngx.log(ngx.INFO, 'query sql:', sql)
|
||||||
local code, res = self:query(sql, READ)
|
local code, res = self:query(sql, READ)
|
||||||
if table.getn(res) > 0 then
|
if table.getn(res) > 0 then
|
||||||
|
|
@ -210,63 +210,56 @@ function _M:get(num)
|
||||||
end
|
end
|
||||||
if not self.query_sql then
|
if not self.query_sql then
|
||||||
ngx.log(ngx.ERR,'do not have query sql str')
|
ngx.log(ngx.ERR,'do not have query sql str')
|
||||||
return
|
return 2,nil
|
||||||
end
|
end
|
||||||
local sql = 'select * from '..self.table..' '..self.query_sql .. ' ' .. limit_sql
|
local sql = 'select * from \"'..self.table..'\" '..self.query_sql .. ' ' .. limit_sql
|
||||||
local res = self:query(sql, READ)
|
local code,res = self:query(sql, READ)
|
||||||
if self.relation.local_key ~= nil then
|
if self.relation.local_key ~= nil then
|
||||||
return self:make_relations(res)
|
return code, self:make_relations(res)
|
||||||
end
|
end
|
||||||
return res
|
return code, res
|
||||||
end
|
end
|
||||||
|
|
||||||
--根据数据模型中的
|
--根据数据模型中的
|
||||||
function _M:paginate(page_num, page_size)
|
function _M:paginate(page_num, page_size)
|
||||||
page_num = page_num or 1
|
page_num = page_num or 1
|
||||||
page_size = page_size or 10
|
page_size = page_size or 10
|
||||||
local sql, count_sql, total, code
|
local sql, count_sql, total, code, res
|
||||||
local data={
|
local data={
|
||||||
data = {},
|
data = {},
|
||||||
next_page = 1,
|
|
||||||
prev_page = 1,
|
|
||||||
total = 0
|
total = 0
|
||||||
}
|
}
|
||||||
if not self.query_sql then
|
if not self.query_sql then
|
||||||
sql = 'select * from \"'..self.table..'\" limit '..page_num..' offset '..page_size
|
sql = 'select * from \"'..self.table..'\" limit '..page_size..' offset '..(page_num - 1) * page_size
|
||||||
count_sql = 'select count(*) from \"'..self.table..'\"'
|
count_sql = 'select count(*) from \"'..self.table..'\"'
|
||||||
else
|
else
|
||||||
sql = 'select * from \"'..self.table .. '\" '..self.query_sql .. ' limit '..page_num..' offset '..page_size
|
sql = 'select * from \"'..self.table .. '\" '..self.query_sql .. ' limit '..page_size..' offset '..(page_num - 1) * page_size
|
||||||
count_sql = 'select count(*) from \"'..self.table..'\" '..self.query_sql
|
count_sql = 'select count(*) from \"'..self.table..'\" '..self.query_sql
|
||||||
end
|
end
|
||||||
code, total = self:query(count_sql, READ)
|
code, total = self:query(count_sql, READ)
|
||||||
if code == 0 then
|
if code == 0 then
|
||||||
data['total'] = tonumber(total[1]['count'])
|
data['total'] = tonumber(total[1]['count'])
|
||||||
data['data'] = self:make_relations(self:query(sql, READ))
|
|
||||||
end
|
end
|
||||||
--暂时没有理清下面的业务逻辑
|
code, res = self:query(sql, READ)
|
||||||
if (table.getn(data['data']) + ((page_num - 1)* per_page)) < data['total'] then
|
if code == 0 then
|
||||||
data['next_page'] = page_num + 1
|
data['data'] = self:make_relations(res)
|
||||||
end
|
end
|
||||||
if tonumber(page_num) ~= 1 then
|
return code, data
|
||||||
data['prev_page'] = page_num - 1
|
|
||||||
end
|
|
||||||
return data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--返回数据表中的第一条数据记录
|
--返回数据表中的第一条数据记录
|
||||||
function _M:first()
|
function _M:first()
|
||||||
if not self.query_sql then
|
if not self.query_sql then
|
||||||
ngx.log(ngx.ERR,'do not have query sql str')
|
ngx.log(ngx.ERR,'do not have query sql str')
|
||||||
return
|
return 2,nil
|
||||||
end
|
end
|
||||||
local sql = 'select * from '..self.table..' '..self.query_sql..' limit 1'
|
local sql = 'select * from \"'..self.table..'\" '..self.query_sql..' limit 1'
|
||||||
local res = self:query(sql, READ)
|
local code, res = self:query(sql, READ)
|
||||||
if next(res) ~= nil then
|
if next(res) ~= nil then
|
||||||
res = self:make_relations(res)
|
res = self:make_relations(res)
|
||||||
return res[1]
|
return code, res[1]
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
return code, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--插入数据内容到数据表中
|
--插入数据内容到数据表中
|
||||||
|
|
@ -282,7 +275,7 @@ function _M:create(data)
|
||||||
values = values..','..value
|
values = values..','..value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return self:query('insert into '..self.table..'('..columns..') values('..values..')', WRITE)
|
return self:query('insert into \"'..self.table..'\"('..columns..') values('..values..')', WRITE)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M:with(relation)
|
function _M:with(relation)
|
||||||
|
|
@ -313,18 +306,19 @@ end
|
||||||
--根据id删除数据库中的数据记录
|
--根据id删除数据库中的数据记录
|
||||||
function _M:delete(id)
|
function _M:delete(id)
|
||||||
id = id or nil
|
id = id or nil
|
||||||
if not id then
|
if not id then
|
||||||
-- 拼接需要delete的字段
|
-- 拼接需要delete的字段
|
||||||
if self.query_sql then
|
if self.query_sql then
|
||||||
local sql = 'delete from '..self.table..' '..self.query_sql..' limit 1'
|
--postgres 数据库使用delete语句时不支持limit
|
||||||
return self:query(sql, WRITE)
|
--local sql = 'delete from \"'..self.table..'\" '..self.query_sql..' limit 1'
|
||||||
end
|
local sql = 'delete from \"'..self.table..'\" '..self.query_sql
|
||||||
ngx.log(ngx.ERR,'delete function need prefix sql')
|
return self:query(sql, WRITE)
|
||||||
ngx.exit(500)
|
end
|
||||||
else
|
ngx.log(ngx.ERR,'delete function need prefix sql')
|
||||||
return self:query('delete from '..self.table..' where id=' .. id .. ' limit 1', WRITE)
|
return 2, nil
|
||||||
end
|
end
|
||||||
return false
|
--return self:query('delete from '..self.table..' where id=' .. id .. ' limit 1', WRITE)
|
||||||
|
return self:query('delete from \"'..self.table..'\" where id='.. id, WRITE)
|
||||||
end
|
end
|
||||||
|
|
||||||
--软删除数据,不从数据表中删除记录,只将定义表中的字段值进行处理
|
--软删除数据,不从数据表中删除记录,只将定义表中的字段值进行处理
|
||||||
|
|
@ -343,7 +337,7 @@ function _M:update(data)
|
||||||
-- 拼接需要update的字段
|
-- 拼接需要update的字段
|
||||||
local str = nil
|
local str = nil
|
||||||
for column,value in pairs(data) do
|
for column,value in pairs(data) do
|
||||||
clean_value = transform_value(value)
|
local clean_value = transform_value(value)
|
||||||
if not str then
|
if not str then
|
||||||
str = column..'='..clean_value
|
str = column..'='..clean_value
|
||||||
else
|
else
|
||||||
|
|
@ -352,12 +346,13 @@ function _M:update(data)
|
||||||
end
|
end
|
||||||
-- 判断是模型自身执行update还是数据库where限定执行
|
-- 判断是模型自身执行update还是数据库where限定执行
|
||||||
if self.query_sql then
|
if self.query_sql then
|
||||||
local sql = 'update '..self.table..' set '..str..' '..self.query_sql..' limit 1'
|
--postgres 数据库使用update语句时不支持limit
|
||||||
|
--local sql = 'update \"'..self.table..'\" set '..str..' '..self.query_sql..' limit 1'
|
||||||
|
local sql = 'update \"'..self.table..'\" set '..str..' '..self.query_sql
|
||||||
return self:query(sql, WRITE)
|
return self:query(sql, WRITE)
|
||||||
end
|
end
|
||||||
ngx.log(ngx.ERR,'update function cannot called without restriction')
|
ngx.log(ngx.ERR,'update function cannot called without restriction')
|
||||||
ngx.exit(500)
|
return 2, nil
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--查询数据表中的数据记录
|
--查询数据表中的数据记录
|
||||||
|
|
@ -381,12 +376,10 @@ function _M:query(sql, type)
|
||||||
ngx.log(ngx.ERR, "write db error. res: " .. (code or "no reason"))
|
ngx.log(ngx.ERR, "write db error. res: " .. (code or "no reason"))
|
||||||
return code, nil
|
return code, nil
|
||||||
end
|
end
|
||||||
return code, result.affected_rows
|
return code, result
|
||||||
else
|
|
||||||
ngx.log(ngx.ERR, 'type invalid, need ' .. READ .. ' or '..WRITE)
|
|
||||||
ngx.exit(500)
|
|
||||||
return 2, nil
|
|
||||||
end
|
end
|
||||||
|
ngx.log(ngx.ERR, 'type invalid, need ' .. READ .. ' or '..WRITE)
|
||||||
|
return 2, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--获取需要隐藏的列
|
--获取需要隐藏的列
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user