Compare commits

...

2 Commits

Author SHA1 Message Date
wanglei
5ebf2812e1 修改orm测试 2025-10-14 23:22:23 +08:00
wanglei
2801aa9647 修改测试例子 2025-10-14 17:16:16 +08:00
4 changed files with 36 additions and 60 deletions

View File

@ -7,7 +7,7 @@ events {
} }
http { http {
lua_package_path 'src/?/?.lua;src/?.lua;src/share/?/?.lua;src/share/?.lua;/home/frankly/work/AuthPlatform/src/share/lib/?/?.lub;/home/frankly/work/AuthPlatform/src/share/lib/ngx-oauth/?.lua;;'; lua_package_path 'src/?/?.lua;src/?.lua;src/share/?/?.lua;src/share/?.lua;/home/frankly/work/AuthPlatform/src/share/lib/?/?.lub;/home/frankly/work/AuthPlatform/src/share/lib/ngx-oauth/?.lua;/home/frankly/work/AuthPlatform/src/share/?/?.lub;/home/frankly/work/AuthPlatform/src/share/?.lua;;';
lua_package_cpath 'src/share/lib/?.so;;'; lua_package_cpath 'src/share/lib/?.so;;';
# Path of the file with trusted CA certificates. # Path of the file with trusted CA certificates.

View File

@ -126,7 +126,7 @@ function Query(own_table, data)
end end
insert = insert .. ") \n\t VALUES (" .. values .. ")" insert = insert .. ") \n\t VALUES (" .. values .. ")"
print(insert)
-- TODO: return valid ID -- TODO: return valid ID
_connect = db:insert(insert) _connect = db:insert(insert)

View File

@ -15,12 +15,6 @@ ID = "id"
AGGREGATOR = "aggregator" AGGREGATOR = "aggregator"
QUERY_LIST = "query_list" QUERY_LIST = "query_list"
-- databases types
SQLITE = "sqlite3"
ORACLE = "oracle"
MYSQL = "mysql"
POSTGRESQL = "postgresql"
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Model Settings -- -- Model Settings --
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@ -35,56 +29,32 @@ DB = {
new = (DB.new == true), new = (DB.new == true),
DEBUG = (DB.DEBUG == true), DEBUG = (DB.DEBUG == true),
backtrace = (DB.backtrace == true), backtrace = (DB.backtrace == true),
-- database settings name = DB.name,
type = DB.type or "sqlite3", host = DB.host,
-- if you use sqlite set database path value port = DB.port,
-- if not set a database name username = DB.username,
name = DB.name or "database.db", password = DB.password,
-- not sqlite db settings
host = DB.host or nil,
port = DB.port or nil,
username = DB.username or nil,
password = DB.password or nil
} }
print(DB)
local sql, _connect local sql, _connect
-- Get database by settings -- Get database by settings
if DB.type == SQLITE then --local luasql = require("luasql.postgres")
local luasql = require("luasql.sqlite3") --sql = luasql.postgres()
sql = luasql.sqlite3() --print(DB.name, DB.username, DB.password, DB.host, DB.port)
_connect = sql:connect(DB.name) --_connect = sql:connect(DB.name, DB.username, DB.password, DB.host, DB.port)
elseif DB.type == MYSQL then luasql = require "luasql.postgres"
local luasql = require("luasql.mysql") env = luasql.postgres()
sql = luasql.mysql() -- database user pwd host port
print(DB.name, DB.username, DB.password, DB.host, DB.port) _connect = env:connect("postgres","postgres","1qaz2wsx",'127.0.0.1',5432)
_connect = sql:connect(DB.name, DB.username, DB.password, DB.host, DB.port)
elseif DB.type == POSTGRESQL then
local luasql = require("luasql.postgres")
sql = luasql.postgres()
print(DB.name, DB.username, DB.password, DB.host, DB.port)
_connect = sql:connect(DB.name, DB.username, DB.password, DB.host, DB.port)
else
BACKTRACE(ERROR, "Database type not suported '" .. tostring(DB.type) .. "'")
end
if not _connect then if not _connect then
BACKTRACE(ERROR, "Connect problem!") BACKTRACE(DEBUG, "Connect error!")
end end
-- if DB.new then
-- BACKTRACE(INFO, "Remove old database")
-- if DB.type == SQLITE then
-- os.remove(DB.name)
-- else
-- _connect:execute('DROP DATABASE `' .. DB.name .. '`')
-- end
-- end
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Database -- -- Database --
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@ -97,9 +67,9 @@ db = {
-- Execute SQL query -- Execute SQL query
execute = function (self, query) execute = function (self, query)
BACKTRACE(DEBUG, query) BACKTRACE(DEBUG, query)
BACKTRACE(INFO, query)
local result = self.connect:execute(query) local result = self.connect:execute(query)
if result then if result then
return result return result
else else

View File

@ -4,16 +4,23 @@ DB = {
DEBUG = true, DEBUG = true,
new = true, new = true,
backtrace = true, backtrace = true,
name = "AuthDB", name = "postgres",
type = "postgresql", username = "postgres",
username = "postgresql", password = "1qaz2wsx",
password = "Admin123",
host = "127.0.0.1", host = "127.0.0.1",
port = 5432 port = 5432
} }
----------------------------- REQUIRE -------------------------------- luasql = require "luasql.postgres"
env = assert(luasql.postgres())
-- database user pwd host port
conn = assert(env:connect("postgres","postgres","1qaz2wsx",'127.0.0.1',5432))
-- version
cur = conn:execute("SELECT version();")
ngx.say(cur:fetch())
----------------------------- REQUIRE --------------------------------
local Table = require("orm.model") local Table = require("orm.model")
local fields = require("orm.tools.fields") local fields = require("orm.tools.fields")
@ -29,23 +36,22 @@ local User = Table({
}) })
----------------------------- CREATE DATA -------------------------------- ----------------------------- CREATE DATA --------------------------------
local user = User({ local user = User({
id = 1,
username = "zhangsan", username = "zhangsan",
password = "123456", password = "123456",
time_create = os.time() time_create = os.time()
}) })
user:save() --user:save()
print("User " .. user.username .. " has id " .. user.id) ngx.say("User " .. user.username .. " has id " .. user.id)
-- User Bob Smith has id 1 -- User Bob Smith has id 1
----------------------------- GET DATA -------------------------------- ----------------------------- GET DATA --------------------------------
local first_user = User.get:first() local first_user = User.get:first()
print("First user name is: " .. first_user.username) ngx.print("First user name is: " .. first_user.username)
-- First user name is: First user -- First user name is: First user
local users = User.get:all() local users = User.get:all()
print("We get " .. users:count() .. " users") ngx.print("We get " .. users:count() .. " users")
-- We get 5 users -- We get 5 users