Add. role to be able select database backend in constructor.
To configure use `database.backend` option
It can be a string value like `database.backend = 'native'`.
So it will always use same backend.
Or it can be a table value like
```Lua
database.backend = {
main = 'native';
base64 = '';
}
```
Role `database.backend.main` is predefined and it equal to `native` if not set.
If there no role when Database class creates or role unknown role `main` is used
```Lua
dbh = Database.new('system') -- uses role `main`
dbh = Database.new('system', 'main') -- same as previews
dbh = Database.new('system', 'base64') -- uses role `base64`
dbh = Database.new('system', 'test') -- uses role `main`
```
This commit is contained in:
@@ -13,14 +13,16 @@ require 'resources.functions.config'
|
|||||||
|
|
||||||
local log = require "resources.functions.log".database
|
local log = require "resources.functions.log".database
|
||||||
|
|
||||||
local BACKEND = xml_handler and xml_handler.db_backend or 'native'
|
local BACKEND = database and database.backend
|
||||||
|
if type(BACKEND) ~= 'table' then BACKEND = {main = BACKEND} end
|
||||||
|
BACKEND.main = BACKEND.main or 'native'
|
||||||
|
|
||||||
local unpack = unpack or table.unpack
|
local unpack = unpack or table.unpack
|
||||||
|
|
||||||
-----------------------------------------------------------
|
-----------------------------------------------------------
|
||||||
local installed_classes = {}
|
local installed_classes = {}
|
||||||
local default_backend = FsDatabase
|
local default_backend = FsDatabase
|
||||||
local function new_database(backend)
|
local function new_database(backend, backend_name)
|
||||||
local class = installed_classes[backend]
|
local class = installed_classes[backend]
|
||||||
if class then return class end
|
if class then return class end
|
||||||
|
|
||||||
@@ -35,6 +37,10 @@ local function new_database(backend)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Database:backend_name()
|
||||||
|
return backend_name
|
||||||
|
end
|
||||||
|
|
||||||
function Database:first_row(sql)
|
function Database:first_row(sql)
|
||||||
local result
|
local result
|
||||||
local ok, err = self:query(sql, function(row)
|
local ok, err = self:query(sql, function(row)
|
||||||
@@ -167,7 +173,7 @@ local Database = {} do
|
|||||||
|
|
||||||
local backend_loader = setmetatable({}, {__index = function(self, backend)
|
local backend_loader = setmetatable({}, {__index = function(self, backend)
|
||||||
local class = require("resources.functions.database." .. backend)
|
local class = require("resources.functions.database." .. backend)
|
||||||
local database = new_database(class)
|
local database = new_database(class, backend)
|
||||||
self[backend] = function(...)
|
self[backend] = function(...)
|
||||||
return database.new(...)
|
return database.new(...)
|
||||||
end
|
end
|
||||||
@@ -176,7 +182,10 @@ end})
|
|||||||
|
|
||||||
Database.backend = backend_loader
|
Database.backend = backend_loader
|
||||||
|
|
||||||
Database.new = Database.backend[BACKEND]
|
function Database.new(dbname, role)
|
||||||
|
local backend = role and BACKEND[role] or BACKEND.main
|
||||||
|
return Database.backend[backend](dbname)
|
||||||
|
end
|
||||||
|
|
||||||
Database.__self_test__ = function(backends, ...)
|
Database.__self_test__ = function(backends, ...)
|
||||||
for _, backend in ipairs(backends) do
|
for _, backend in ipairs(backends) do
|
||||||
|
|||||||
@@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
local log = require "resources.functions.log".database
|
local log = require "resources.functions.log".database
|
||||||
|
|
||||||
|
assert(freeswitch, "Require FreeSWITCH environment")
|
||||||
|
|
||||||
-----------------------------------------------------------
|
-----------------------------------------------------------
|
||||||
local FsDatabase = {} if freeswitch then
|
local FsDatabase = {} do
|
||||||
|
|
||||||
require "resources.functions.file_exists"
|
require "resources.functions.file_exists"
|
||||||
require "resources.functions.database_handle"
|
require "resources.functions.database_handle"
|
||||||
|
|||||||
Reference in New Issue
Block a user