2025-10-22 08:46:41 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by admin.
|
|
|
|
|
|
--- DateTime: 2025/10/15 09:12
|
|
|
|
|
|
---
|
2025-10-24 14:58:03 +08:00
|
|
|
|
--local snowflake = require("util.snowflake")
|
|
|
|
|
|
--
|
|
|
|
|
|
--local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
|
|
|
|
|
--local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
|
|
|
|
|
--local snow = snowflake.new(workerId, datacenterId)
|
|
|
|
|
|
--local id = snow:generateUniqueId()-- 生成ID
|
|
|
|
|
|
--ngx.say("Generated ID:"..snow.int64_to_string(id))
|
2025-10-22 08:46:41 +08:00
|
|
|
|
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--max =a and b or c--a?b:c
|
|
|
|
|
|
|
2025-10-24 22:49:40 +08:00
|
|
|
|
local User = require("model.user")
|
2025-10-24 15:15:45 +08:00
|
|
|
|
|
2025-10-25 14:33:27 +08:00
|
|
|
|
--获取数据表中的记录数
|
|
|
|
|
|
local code, res = User:count()
|
|
|
|
|
|
--ngx.say(res)
|
|
|
|
|
|
|
|
|
|
|
|
--查询表中id为1的数据记录
|
|
|
|
|
|
code, res = User:find("1")
|
|
|
|
|
|
|
|
|
|
|
|
--查询表中的所有记录
|
|
|
|
|
|
code, res = User:all()
|
2025-10-25 14:46:10 +08:00
|
|
|
|
--显示查询到的数据记录
|
|
|
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
2025-10-25 16:27:52 +08:00
|
|
|
|
--ngx.say("----begin where and query---")
|
2025-10-25 14:46:10 +08:00
|
|
|
|
-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据
|
2025-10-25 16:27:52 +08:00
|
|
|
|
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
|
2025-10-25 14:46:10 +08:00
|
|
|
|
|
2025-10-25 16:27:52 +08:00
|
|
|
|
--ngx.say("----begin where or query---")
|
2025-10-25 14:46:10 +08:00
|
|
|
|
-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组
|
|
|
|
|
|
code, res = User:where('name','=','zhangsan'):orwhere('name','=','admin'):get()
|
2025-10-25 16:27:52 +08:00
|
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
2025-10-25 14:33:27 +08:00
|
|
|
|
|
2025-10-25 14:46:10 +08:00
|
|
|
|
--orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写),
|
2025-10-25 16:27:52 +08:00
|
|
|
|
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
|
2025-10-25 14:46:10 +08:00
|
|
|
|
|
|
|
|
|
|
-- 创建一个用户
|
2025-10-25 16:27:52 +08:00
|
|
|
|
code, res = User:create({
|
|
|
|
|
|
id='3',
|
|
|
|
|
|
password='22222',
|
|
|
|
|
|
name='lisi',
|
|
|
|
|
|
email='lisi@gmail.com',
|
|
|
|
|
|
})
|
2025-10-25 14:46:10 +08:00
|
|
|
|
|
|
|
|
|
|
-- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL
|
2025-10-25 16:27:52 +08:00
|
|
|
|
code, res = User:where('id', '=', '3'):update({
|
|
|
|
|
|
phone='666666',
|
|
|
|
|
|
email='zhangsan@qq.com'
|
|
|
|
|
|
})
|
|
|
|
|
|
--输出更新后影响的行总数
|
|
|
|
|
|
ngx.say("update affected_rows: ", res.affected_rows)
|
2025-10-25 14:46:10 +08:00
|
|
|
|
|
|
|
|
|
|
-- 删除 id = 1 的用户
|
2025-10-25 16:27:52 +08:00
|
|
|
|
code, res = User:where('id','=','3'):delete()
|
|
|
|
|
|
ngx.say("delete affected_rows: ", res.affected_rows)
|
2025-10-25 14:46:10 +08:00
|
|
|
|
|
|
|
|
|
|
--分页 获取数据表中的记录
|
2025-10-25 16:27:52 +08:00
|
|
|
|
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
|
2025-10-24 22:49:40 +08:00
|
|
|
|
|
|
|
|
|
|
--获取请求参数的键值和数据值
|
|
|
|
|
|
--local cjson = require("cjson")
|
|
|
|
|
|
--local header = ngx.req.get_headers()
|
|
|
|
|
|
--for k,v in pairs(header) do
|
|
|
|
|
|
-- ngx.say("[header] name:", k, "value:", v)
|
|
|
|
|
|
--end
|
|
|
|
|
|
--
|
|
|
|
|
|
--local payloads = ngx.req.get_uri_args()
|
|
|
|
|
|
--for k,v in pairs(payloads) do
|
|
|
|
|
|
-- ngx.say("[params] name:", k, " value:", v)
|
|
|
|
|
|
--end
|
2025-10-24 14:58:03 +08:00
|
|
|
|
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--去掉组装最后一位逗号(,)
|
|
|
|
|
|
--local newKeys = keys:sub(1, #keys -1)
|
|
|
|
|
|
--local newValues = values:sub(1, #values -1)
|
|
|
|
|
|
|
2025-10-22 08:46:41 +08:00
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
--ngx.req.read_body()
|
|
|
|
|
|
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
--local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
|
|
|
|
|
|
--if not body_data then
|
|
|
|
|
|
-- ngx.say("read file error:", err)
|
|
|
|
|
|
-- return ngx.exit(400)
|
|
|
|
|
|
--end
|
|
|
|
|
|
--local len = #body_data
|
|
|
|
|
|
|
|
|
|
|
|
--local file_name = ngx.req.get_body_file()
|
|
|
|
|
|
--ngx.say("file length:", len)
|
|
|
|
|
|
|
|
|
|
|
|
--ngx.req.read_body_in_buffer(ngx.var.request_body_file)
|
|
|
|
|
|
|
|
|
|
|
|
--ngx.say(body_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--local cjson = require("cjson")
|
|
|
|
|
|
--local file_path = "/home/frankly/work/test.dat"
|
|
|
|
|
|
--local file_length = 1024 * 1024 * 400
|
|
|
|
|
|
--local f, err = io.input(file_path, "r")
|
|
|
|
|
|
--if not f then
|
|
|
|
|
|
-- ngx.say("read file error:"..err)
|
|
|
|
|
|
-- return
|
|
|
|
|
|
--end
|
|
|
|
|
|
--local content = f:read(file_length) --读取文件内容
|
|
|
|
|
|
--f:close() --关闭文件
|
|
|
|
|
|
----ngx.say(content)
|
|
|
|
|
|
--local res = {
|
|
|
|
|
|
-- key = "data",
|
|
|
|
|
|
-- value = content
|
|
|
|
|
|
--}
|
|
|
|
|
|
----ngx.header["Length"] = #content
|
|
|
|
|
|
--ngx.header["Content-Type"] = 'application/json; charset=UTF-8'
|
|
|
|
|
|
--ngx.say(cjson.encode(res))
|
2025-10-17 15:43:19 +08:00
|
|
|
|
--ngx.log(ngx.INFO, "send data success")
|