From 423e0ab503cdc72a9900cd87e8a591ef4dadfc65 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Fri, 25 Mar 2016 10:04:48 +0300 Subject: [PATCH] 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` ``` --- .../scripts/resources/functions/database.lua | 17 +++++++++++++---- .../resources/functions/database/native.lua | 4 +++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/resources/install/scripts/resources/functions/database.lua b/resources/install/scripts/resources/functions/database.lua index 518dde3bd9..34989f62aa 100644 --- a/resources/install/scripts/resources/functions/database.lua +++ b/resources/install/scripts/resources/functions/database.lua @@ -13,14 +13,16 @@ require 'resources.functions.config' 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 installed_classes = {} local default_backend = FsDatabase -local function new_database(backend) +local function new_database(backend, backend_name) local class = installed_classes[backend] if class then return class end @@ -35,6 +37,10 @@ local function new_database(backend) return self end + function Database:backend_name() + return backend_name + end + function Database:first_row(sql) local result local ok, err = self:query(sql, function(row) @@ -167,7 +173,7 @@ local Database = {} do local backend_loader = setmetatable({}, {__index = function(self, backend) local class = require("resources.functions.database." .. backend) - local database = new_database(class) + local database = new_database(class, backend) self[backend] = function(...) return database.new(...) end @@ -176,7 +182,10 @@ end}) 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, ...) for _, backend in ipairs(backends) do diff --git a/resources/install/scripts/resources/functions/database/native.lua b/resources/install/scripts/resources/functions/database/native.lua index d427f47e3d..ce16b3baeb 100644 --- a/resources/install/scripts/resources/functions/database/native.lua +++ b/resources/install/scripts/resources/functions/database/native.lua @@ -4,8 +4,10 @@ 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.database_handle"