修改用户接口,编写例子并进行分页、排序、删除等数据表操作功能测试
This commit is contained in:
parent
6581489a07
commit
a786ad1c6f
|
|
@ -61,7 +61,7 @@ end
|
|||
--根据用户id删除用户信息
|
||||
function _M.delete_user(m)
|
||||
local id = m.id
|
||||
local code, ret = dao.delete_user(id)
|
||||
local code, ret = dao.deleteUser(id)
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -79,7 +79,7 @@ function _M.update_user(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = dao.update_user(id, body_data)
|
||||
local code, ret = dao.updateUser(id, body_data)
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -31,46 +31,64 @@ code, res = User:all()
|
|||
-- end
|
||||
--end
|
||||
|
||||
--ngx.say("----begin where and query---")
|
||||
-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据
|
||||
code, res = User:where('username','=','zhangsan'):where('password','=','111111'):get()
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
ngx.say(key .. ":" .. tostring(value))
|
||||
end
|
||||
end
|
||||
code, res = User:where('name','=','zhangsan'):where('password','=','111111'):get()
|
||||
--for _, row in ipairs(res) do
|
||||
-- for key, value in pairs(row) do
|
||||
-- ngx.say(key .. ":" .. tostring(value))
|
||||
-- end
|
||||
--end
|
||||
|
||||
--ngx.say("----begin where or query---")
|
||||
-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组
|
||||
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
|
||||
ngx.say(key .. ":" .. tostring(value))
|
||||
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 header = ngx.req.get_headers()
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ function _M:retrieve_relations(ids)
|
|||
return {}
|
||||
end
|
||||
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())
|
||||
end
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ function _M:find(id,column)
|
|||
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 sql = 'select * from \"'..self.table..'\" where '..column..'='..id..' limit 1'
|
||||
ngx.log(ngx.INFO, 'query sql:', sql)
|
||||
local code, res = self:query(sql, READ)
|
||||
if table.getn(res) > 0 then
|
||||
|
|
@ -210,63 +210,56 @@ function _M:get(num)
|
|||
end
|
||||
if not self.query_sql then
|
||||
ngx.log(ngx.ERR,'do not have query sql str')
|
||||
return
|
||||
return 2,nil
|
||||
end
|
||||
local sql = 'select * from '..self.table..' '..self.query_sql .. ' ' .. limit_sql
|
||||
local res = self:query(sql, READ)
|
||||
local sql = 'select * from \"'..self.table..'\" '..self.query_sql .. ' ' .. limit_sql
|
||||
local code,res = self:query(sql, READ)
|
||||
if self.relation.local_key ~= nil then
|
||||
return self:make_relations(res)
|
||||
return code, self:make_relations(res)
|
||||
end
|
||||
return res
|
||||
return code, res
|
||||
end
|
||||
|
||||
--根据数据模型中的
|
||||
function _M:paginate(page_num, page_size)
|
||||
page_num = page_num or 1
|
||||
page_size = page_size or 10
|
||||
local sql, count_sql, total, code
|
||||
local sql, count_sql, total, code, res
|
||||
local data={
|
||||
data = {},
|
||||
next_page = 1,
|
||||
prev_page = 1,
|
||||
total = 0
|
||||
}
|
||||
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..'\"'
|
||||
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
|
||||
end
|
||||
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
|
||||
code, res = self:query(sql, READ)
|
||||
if code == 0 then
|
||||
data['data'] = self:make_relations(res)
|
||||
end
|
||||
return code, data
|
||||
end
|
||||
|
||||
--返回数据表中的第一条数据记录
|
||||
function _M:first()
|
||||
if not self.query_sql then
|
||||
ngx.log(ngx.ERR,'do not have query sql str')
|
||||
return
|
||||
return 2,nil
|
||||
end
|
||||
local sql = 'select * from '..self.table..' '..self.query_sql..' limit 1'
|
||||
local res = self:query(sql, READ)
|
||||
local sql = 'select * from \"'..self.table..'\" '..self.query_sql..' limit 1'
|
||||
local code, res = self:query(sql, READ)
|
||||
if next(res) ~= nil then
|
||||
res = self:make_relations(res)
|
||||
return res[1]
|
||||
else
|
||||
return false
|
||||
return code, res[1]
|
||||
end
|
||||
return code, nil
|
||||
end
|
||||
|
||||
--插入数据内容到数据表中
|
||||
|
|
@ -282,7 +275,7 @@ function _M:create(data)
|
|||
values = values..','..value
|
||||
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
|
||||
|
||||
function _M:with(relation)
|
||||
|
|
@ -313,18 +306,19 @@ end
|
|||
--根据id删除数据库中的数据记录
|
||||
function _M:delete(id)
|
||||
id = id or nil
|
||||
if not id then
|
||||
-- 拼接需要delete的字段
|
||||
if self.query_sql then
|
||||
local sql = 'delete from '..self.table..' '..self.query_sql..' limit 1'
|
||||
return self:query(sql, WRITE)
|
||||
end
|
||||
ngx.log(ngx.ERR,'delete function need prefix sql')
|
||||
ngx.exit(500)
|
||||
else
|
||||
return self:query('delete from '..self.table..' where id=' .. id .. ' limit 1', WRITE)
|
||||
end
|
||||
return false
|
||||
if not id then
|
||||
-- 拼接需要delete的字段
|
||||
if self.query_sql then
|
||||
--postgres 数据库使用delete语句时不支持limit
|
||||
--local sql = 'delete from \"'..self.table..'\" '..self.query_sql..' limit 1'
|
||||
local sql = 'delete from \"'..self.table..'\" '..self.query_sql
|
||||
return self:query(sql, WRITE)
|
||||
end
|
||||
ngx.log(ngx.ERR,'delete function need prefix sql')
|
||||
return 2, nil
|
||||
end
|
||||
--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
|
||||
|
||||
--软删除数据,不从数据表中删除记录,只将定义表中的字段值进行处理
|
||||
|
|
@ -343,7 +337,7 @@ function _M:update(data)
|
|||
-- 拼接需要update的字段
|
||||
local str = nil
|
||||
for column,value in pairs(data) do
|
||||
clean_value = transform_value(value)
|
||||
local clean_value = transform_value(value)
|
||||
if not str then
|
||||
str = column..'='..clean_value
|
||||
else
|
||||
|
|
@ -352,12 +346,13 @@ function _M:update(data)
|
|||
end
|
||||
-- 判断是模型自身执行update还是数据库where限定执行
|
||||
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)
|
||||
end
|
||||
ngx.log(ngx.ERR,'update function cannot called without restriction')
|
||||
ngx.exit(500)
|
||||
return false
|
||||
return 2, nil
|
||||
end
|
||||
|
||||
--查询数据表中的数据记录
|
||||
|
|
@ -381,12 +376,10 @@ function _M:query(sql, type)
|
|||
ngx.log(ngx.ERR, "write db error. res: " .. (code or "no reason"))
|
||||
return code, nil
|
||||
end
|
||||
return code, result.affected_rows
|
||||
else
|
||||
ngx.log(ngx.ERR, 'type invalid, need ' .. READ .. ' or '..WRITE)
|
||||
ngx.exit(500)
|
||||
return 2, nil
|
||||
return code, result
|
||||
end
|
||||
ngx.log(ngx.ERR, 'type invalid, need ' .. READ .. ' or '..WRITE)
|
||||
return 2, nil
|
||||
end
|
||||
|
||||
--获取需要隐藏的列
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user