Compare commits
No commits in common. "5ebf2812e13676afd773ab91747af414c7856ea2" and "4c18c0bdac2d7d9499c989f8cf8b2eae0d55f319" have entirely different histories.
5ebf2812e1
...
4c18c0bdac
|
|
@ -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;/home/frankly/work/AuthPlatform/src/share/?/?.lub;/home/frankly/work/AuthPlatform/src/share/?.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;;';
|
||||||
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.
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,12 @@ 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 --
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
@ -29,32 +35,56 @@ 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),
|
||||||
name = DB.name,
|
-- database settings
|
||||||
host = DB.host,
|
type = DB.type or "sqlite3",
|
||||||
port = DB.port,
|
-- if you use sqlite set database path value
|
||||||
username = DB.username,
|
-- if not set a database name
|
||||||
password = DB.password,
|
name = DB.name or "database.db",
|
||||||
|
-- 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
|
||||||
--local luasql = require("luasql.postgres")
|
if DB.type == SQLITE then
|
||||||
--sql = luasql.postgres()
|
local luasql = require("luasql.sqlite3")
|
||||||
--print(DB.name, DB.username, DB.password, DB.host, DB.port)
|
sql = luasql.sqlite3()
|
||||||
--_connect = sql:connect(DB.name, DB.username, DB.password, DB.host, DB.port)
|
_connect = sql:connect(DB.name)
|
||||||
|
|
||||||
luasql = require "luasql.postgres"
|
elseif DB.type == MYSQL then
|
||||||
env = luasql.postgres()
|
local luasql = require("luasql.mysql")
|
||||||
-- database user pwd host port
|
sql = luasql.mysql()
|
||||||
_connect = env:connect("postgres","postgres","1qaz2wsx",'127.0.0.1',5432)
|
print(DB.name, DB.username, DB.password, DB.host, DB.port)
|
||||||
|
_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(DEBUG, "Connect error!")
|
BACKTRACE(ERROR, "Connect problem!")
|
||||||
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 --
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
@ -67,9 +97,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
|
||||||
|
|
|
||||||
|
|
@ -4,23 +4,16 @@ DB = {
|
||||||
DEBUG = true,
|
DEBUG = true,
|
||||||
new = true,
|
new = true,
|
||||||
backtrace = true,
|
backtrace = true,
|
||||||
name = "postgres",
|
name = "AuthDB",
|
||||||
username = "postgres",
|
type = "postgresql",
|
||||||
password = "1qaz2wsx",
|
username = "postgresql",
|
||||||
|
password = "Admin123",
|
||||||
host = "127.0.0.1",
|
host = "127.0.0.1",
|
||||||
port = 5432
|
port = 5432
|
||||||
}
|
}
|
||||||
|
|
||||||
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 --------------------------------
|
----------------------------- REQUIRE --------------------------------
|
||||||
|
|
||||||
local Table = require("orm.model")
|
local Table = require("orm.model")
|
||||||
local fields = require("orm.tools.fields")
|
local fields = require("orm.tools.fields")
|
||||||
|
|
||||||
|
|
@ -36,22 +29,23 @@ 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()
|
||||||
ngx.say("User " .. user.username .. " has id " .. user.id)
|
print("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()
|
||||||
ngx.print("First user name is: " .. first_user.username)
|
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()
|
||||||
ngx.print("We get " .. users:count() .. " users")
|
print("We get " .. users:count() .. " users")
|
||||||
-- We get 5 users
|
-- We get 5 users
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user