Compare commits
2 Commits
4c18c0bdac
...
5ebf2812e1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ebf2812e1 | ||
|
|
2801aa9647 |
|
|
@ -7,7 +7,7 @@ events {
|
|||
}
|
||||
|
||||
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;;';
|
||||
|
||||
# Path of the file with trusted CA certificates.
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ function Query(own_table, data)
|
|||
end
|
||||
|
||||
insert = insert .. ") \n\t VALUES (" .. values .. ")"
|
||||
|
||||
print(insert)
|
||||
-- TODO: return valid ID
|
||||
_connect = db:insert(insert)
|
||||
|
||||
|
|
|
|||
|
|
@ -15,12 +15,6 @@ ID = "id"
|
|||
AGGREGATOR = "aggregator"
|
||||
QUERY_LIST = "query_list"
|
||||
|
||||
-- databases types
|
||||
SQLITE = "sqlite3"
|
||||
ORACLE = "oracle"
|
||||
MYSQL = "mysql"
|
||||
POSTGRESQL = "postgresql"
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Model Settings --
|
||||
------------------------------------------------------------------------------
|
||||
|
|
@ -35,56 +29,32 @@ DB = {
|
|||
new = (DB.new == true),
|
||||
DEBUG = (DB.DEBUG == true),
|
||||
backtrace = (DB.backtrace == true),
|
||||
-- database settings
|
||||
type = DB.type or "sqlite3",
|
||||
-- if you use sqlite set database path value
|
||||
-- if not set a database name
|
||||
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
|
||||
name = DB.name,
|
||||
host = DB.host,
|
||||
port = DB.port,
|
||||
username = DB.username,
|
||||
password = DB.password,
|
||||
}
|
||||
|
||||
print(DB)
|
||||
|
||||
local sql, _connect
|
||||
|
||||
-- Get database by settings
|
||||
if DB.type == SQLITE then
|
||||
local luasql = require("luasql.sqlite3")
|
||||
sql = luasql.sqlite3()
|
||||
_connect = sql:connect(DB.name)
|
||||
--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)
|
||||
|
||||
elseif DB.type == MYSQL then
|
||||
local luasql = require("luasql.mysql")
|
||||
sql = luasql.mysql()
|
||||
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
|
||||
luasql = require "luasql.postgres"
|
||||
env = luasql.postgres()
|
||||
-- database user pwd host port
|
||||
_connect = env:connect("postgres","postgres","1qaz2wsx",'127.0.0.1',5432)
|
||||
|
||||
if not _connect then
|
||||
BACKTRACE(ERROR, "Connect problem!")
|
||||
BACKTRACE(DEBUG, "Connect error!")
|
||||
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 --
|
||||
------------------------------------------------------------------------------
|
||||
|
|
@ -97,9 +67,9 @@ db = {
|
|||
-- Execute SQL query
|
||||
execute = function (self, query)
|
||||
BACKTRACE(DEBUG, query)
|
||||
BACKTRACE(INFO, query)
|
||||
|
||||
local result = self.connect:execute(query)
|
||||
|
||||
if result then
|
||||
return result
|
||||
else
|
||||
|
|
|
|||
|
|
@ -4,16 +4,23 @@ DB = {
|
|||
DEBUG = true,
|
||||
new = true,
|
||||
backtrace = true,
|
||||
name = "AuthDB",
|
||||
type = "postgresql",
|
||||
username = "postgresql",
|
||||
password = "Admin123",
|
||||
name = "postgres",
|
||||
username = "postgres",
|
||||
password = "1qaz2wsx",
|
||||
host = "127.0.0.1",
|
||||
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 fields = require("orm.tools.fields")
|
||||
|
||||
|
|
@ -29,23 +36,22 @@ local User = Table({
|
|||
})
|
||||
|
||||
----------------------------- CREATE DATA --------------------------------
|
||||
|
||||
local user = User({
|
||||
id = 1,
|
||||
username = "zhangsan",
|
||||
password = "123456",
|
||||
time_create = os.time()
|
||||
})
|
||||
|
||||
user:save()
|
||||
print("User " .. user.username .. " has id " .. user.id)
|
||||
--user:save()
|
||||
ngx.say("User " .. user.username .. " has id " .. user.id)
|
||||
-- User Bob Smith has id 1
|
||||
|
||||
----------------------------- GET DATA --------------------------------
|
||||
|
||||
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
|
||||
|
||||
local users = User.get:all()
|
||||
print("We get " .. users:count() .. " users")
|
||||
ngx.print("We get " .. users:count() .. " users")
|
||||
-- We get 5 users
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user