From 110241cdb13f8f44f2b17bf63bbd19c12d402f13 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 10 Jan 2013 12:11:36 +0100 Subject: old database class removed --- misc/freeswitch/scripts/common/database.lua | 151 ---------------------------- 1 file changed, 151 deletions(-) delete mode 100644 misc/freeswitch/scripts/common/database.lua diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua deleted file mode 100644 index 3692f84..0000000 --- a/misc/freeswitch/scripts/common/database.lua +++ /dev/null @@ -1,151 +0,0 @@ --- Gemeinschaft 5 module: database class --- (c) AMOOMA GmbH 2012 --- - -module(...,package.seeall) - -Database = {} - -DATABASE_DRIVER = 'mysql' - -function Database.new(self, arg) - arg = arg or {} - object = arg.object or {} - setmetatable(object, self); - self.__index = self; - self.class = 'database'; - self.log = arg.log; - self.conn = nil; - return object; -end - - -function Database.connect(self, database_name, user_name, password, host_name) - local database_driver = nil; - if not (database_name and user_name and password) then - require 'common.configuration_file' - local config = common.configuration_file.get('/opt/freeswitch/scripts/ini/database.ini'); - if config then - database_driver = config[true].driver - database_name = config[database_driver].database - user_name = config[database_driver].user - password = config[database_driver].password - host_name = config[database_driver].host - end - end - - host_name = host_name or 'localhost'; - database_driver = database_driver or DATABASE_DRIVER; - - if database_driver == 'mysql' then - require "luasql.mysql" - self.env = luasql.mysql(); - elseif database_driver == 'odbc' then - require "luasql.odbc" - self.env = luasql.odbc(); - end - - self.conn = self.env:connect(database_name, user_name, password, host_name); - self.conn_id = tostring(self.conn); - self.database_name = database_name; - self.user_name = user_name; - self.password = password; - self.host_name = host_name; - - -- self.log:debug('DATABASE_CONNECT - connection: ', self.conn_id, ', environment: ', self.env); - - return self; -end - - -function Database.reconnect(self) - self.conn = self.env:connect(self.database_name, self.user_name, self.password, self.host_name); - self.conn_id = tostring(self.conn); - - if self.log then - self.log:info('DATABASE_RECONNECT - connection: ', self.conn_id, ', environment: ', self.env); - end - - return self; -end - - -function Database.connected(self) - return self.conn; -end - - -function Database.query(self, sql_query, call_function) - local cursor = self.conn:execute(sql_query); - - if cursor == nil and not self.conn:execute('SELECT @@VERSION') then - if self.log then - self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query); - end - self:reconnect(); - - if call_function then - cursor = self.conn:execute(sql_query); - self.log:notice('DATABASE_QUERY - retry: ', sql_query); - end - end - - if cursor and call_function then - repeat - row = cursor:fetch({}, 'a'); - if row then - call_function(row); - end - until not row; - end - - if type(cursor) == 'userdata' then - cursor:close(); - end - - return cursor; -end - - -function Database.query_return_value(self, sql_query) - local cursor = self.conn:execute(sql_query); - - if cursor == nil and not self.conn:execute('SELECT @@VERSION') then - if self.log then - self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query); - end - self:reconnect(); - cursor = self.conn:execute(sql_query); - self.log:notice('DATABASE_QUERY - retry: ', sql_query); - end - - if type(cursor) == 'userdata' then - local row = cursor:fetch({}, 'n'); - cursor:close(); - - if not row then - return row; - else - return row[1]; - end - end - - return cursor; -end - - -function Database.last_insert_id(self) - return self:query_return_value('SELECT LAST_INSERT_ID()'); -end - - -function Database.release(self, sql_query, call_function) - if self.conn then - self.conn:close(); - end - if self.env then - self.env:close(); - end - - -- self.log:debug('DATABASE_RELEASE - connection: ', self.conn_id, ', status: ', self.env, ', ', self.conn); -end -- cgit v1.2.3 From 48f7c66c5a9447d93a664d9c44fdc47814305493 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 10 Jan 2013 12:12:36 +0100 Subject: odbc database class added --- misc/freeswitch/scripts/common/database.lua | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 misc/freeswitch/scripts/common/database.lua diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua new file mode 100644 index 0000000..1f39135 --- /dev/null +++ b/misc/freeswitch/scripts/common/database.lua @@ -0,0 +1,78 @@ +-- Gemeinschaft 5 module: database class +-- (c) AMOOMA GmbH 2013 +-- + +module(...,package.seeall) + +Database = {} + +DATABASE_DSN = 'gemeinschaft'; + +function Database.new(self, arg) + arg = arg or {} + object = arg.object or {} + setmetatable(object, self); + self.__index = self; + self.class = 'database'; + self.log = arg.log; + self.conn = nil; + return object; +end + + +function Database.connect(self) + self.dsn = DATABASE_DSN; + + require 'common.configuration_file' + local dsn = common.configuration_file.get('/var/lib/freeswitch/.odbc.ini', self.dsn); + + self.database_name = dsn.DATABASE; + self.user_name = dsn.USER; + self.password = dsn.PASSWORD; + self.host_name = dsn.HOST; + + self.conn = freeswitch.Dbh(self.dsn, self.user_name, self.password); + self.conn_id = tostring(self.conn); + + return self; +end + + +function Database.connected(self) + return self.conn:connected(); +end + + +function Database.query(self, sql_query, call_function) + if call_function then + return self.conn:query(sql_query, call_function); + else + return self.conn:query(sql_query); + end +end + + +function Database.query_return_value(self, sql_query) + local result = nil; + + self.conn:query(sql_query, function(row) + for key, value in pairs(row) do + result = value; + return result; + end + end) + + return result; +end + + +function Database.last_insert_id(self) + return self:query_return_value('SELECT LAST_INSERT_ID()'); +end + + +function Database.release(self) + if self.conn then + self.conn:release(); + end +end -- cgit v1.2.3 From aa795ca8c8b37a851315c72b45fcee6f85fc9d55 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 10 Jan 2013 12:24:51 +0100 Subject: Revert "odbc database class added" This reverts commit 48f7c66c5a9447d93a664d9c44fdc47814305493. --- misc/freeswitch/scripts/common/database.lua | 78 ----------------------------- 1 file changed, 78 deletions(-) delete mode 100644 misc/freeswitch/scripts/common/database.lua diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua deleted file mode 100644 index 1f39135..0000000 --- a/misc/freeswitch/scripts/common/database.lua +++ /dev/null @@ -1,78 +0,0 @@ --- Gemeinschaft 5 module: database class --- (c) AMOOMA GmbH 2013 --- - -module(...,package.seeall) - -Database = {} - -DATABASE_DSN = 'gemeinschaft'; - -function Database.new(self, arg) - arg = arg or {} - object = arg.object or {} - setmetatable(object, self); - self.__index = self; - self.class = 'database'; - self.log = arg.log; - self.conn = nil; - return object; -end - - -function Database.connect(self) - self.dsn = DATABASE_DSN; - - require 'common.configuration_file' - local dsn = common.configuration_file.get('/var/lib/freeswitch/.odbc.ini', self.dsn); - - self.database_name = dsn.DATABASE; - self.user_name = dsn.USER; - self.password = dsn.PASSWORD; - self.host_name = dsn.HOST; - - self.conn = freeswitch.Dbh(self.dsn, self.user_name, self.password); - self.conn_id = tostring(self.conn); - - return self; -end - - -function Database.connected(self) - return self.conn:connected(); -end - - -function Database.query(self, sql_query, call_function) - if call_function then - return self.conn:query(sql_query, call_function); - else - return self.conn:query(sql_query); - end -end - - -function Database.query_return_value(self, sql_query) - local result = nil; - - self.conn:query(sql_query, function(row) - for key, value in pairs(row) do - result = value; - return result; - end - end) - - return result; -end - - -function Database.last_insert_id(self) - return self:query_return_value('SELECT LAST_INSERT_ID()'); -end - - -function Database.release(self) - if self.conn then - self.conn:release(); - end -end -- cgit v1.2.3 From 5ad8203ce4f1bfea997960d0b52c626dea24b944 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 10 Jan 2013 12:25:14 +0100 Subject: Revert "old database class removed" This reverts commit 110241cdb13f8f44f2b17bf63bbd19c12d402f13. --- misc/freeswitch/scripts/common/database.lua | 151 ++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 misc/freeswitch/scripts/common/database.lua diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua new file mode 100644 index 0000000..3692f84 --- /dev/null +++ b/misc/freeswitch/scripts/common/database.lua @@ -0,0 +1,151 @@ +-- Gemeinschaft 5 module: database class +-- (c) AMOOMA GmbH 2012 +-- + +module(...,package.seeall) + +Database = {} + +DATABASE_DRIVER = 'mysql' + +function Database.new(self, arg) + arg = arg or {} + object = arg.object or {} + setmetatable(object, self); + self.__index = self; + self.class = 'database'; + self.log = arg.log; + self.conn = nil; + return object; +end + + +function Database.connect(self, database_name, user_name, password, host_name) + local database_driver = nil; + if not (database_name and user_name and password) then + require 'common.configuration_file' + local config = common.configuration_file.get('/opt/freeswitch/scripts/ini/database.ini'); + if config then + database_driver = config[true].driver + database_name = config[database_driver].database + user_name = config[database_driver].user + password = config[database_driver].password + host_name = config[database_driver].host + end + end + + host_name = host_name or 'localhost'; + database_driver = database_driver or DATABASE_DRIVER; + + if database_driver == 'mysql' then + require "luasql.mysql" + self.env = luasql.mysql(); + elseif database_driver == 'odbc' then + require "luasql.odbc" + self.env = luasql.odbc(); + end + + self.conn = self.env:connect(database_name, user_name, password, host_name); + self.conn_id = tostring(self.conn); + self.database_name = database_name; + self.user_name = user_name; + self.password = password; + self.host_name = host_name; + + -- self.log:debug('DATABASE_CONNECT - connection: ', self.conn_id, ', environment: ', self.env); + + return self; +end + + +function Database.reconnect(self) + self.conn = self.env:connect(self.database_name, self.user_name, self.password, self.host_name); + self.conn_id = tostring(self.conn); + + if self.log then + self.log:info('DATABASE_RECONNECT - connection: ', self.conn_id, ', environment: ', self.env); + end + + return self; +end + + +function Database.connected(self) + return self.conn; +end + + +function Database.query(self, sql_query, call_function) + local cursor = self.conn:execute(sql_query); + + if cursor == nil and not self.conn:execute('SELECT @@VERSION') then + if self.log then + self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query); + end + self:reconnect(); + + if call_function then + cursor = self.conn:execute(sql_query); + self.log:notice('DATABASE_QUERY - retry: ', sql_query); + end + end + + if cursor and call_function then + repeat + row = cursor:fetch({}, 'a'); + if row then + call_function(row); + end + until not row; + end + + if type(cursor) == 'userdata' then + cursor:close(); + end + + return cursor; +end + + +function Database.query_return_value(self, sql_query) + local cursor = self.conn:execute(sql_query); + + if cursor == nil and not self.conn:execute('SELECT @@VERSION') then + if self.log then + self.log:error('DATABASE_QUERY - lost connection: ', self.conn_id, ', environment: ', self.env, ', query: ', sql_query); + end + self:reconnect(); + cursor = self.conn:execute(sql_query); + self.log:notice('DATABASE_QUERY - retry: ', sql_query); + end + + if type(cursor) == 'userdata' then + local row = cursor:fetch({}, 'n'); + cursor:close(); + + if not row then + return row; + else + return row[1]; + end + end + + return cursor; +end + + +function Database.last_insert_id(self) + return self:query_return_value('SELECT LAST_INSERT_ID()'); +end + + +function Database.release(self, sql_query, call_function) + if self.conn then + self.conn:close(); + end + if self.env then + self.env:close(); + end + + -- self.log:debug('DATABASE_RELEASE - connection: ', self.conn_id, ', status: ', self.env, ', ', self.conn); +end -- cgit v1.2.3