diff options
-rw-r--r-- | misc/freeswitch/scripts/common/database.lua | 123 |
1 files changed, 25 insertions, 98 deletions
diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua index 22a68fb..1f39135 100644 --- a/misc/freeswitch/scripts/common/database.lua +++ b/misc/freeswitch/scripts/common/database.lua @@ -1,12 +1,12 @@ -- Gemeinschaft 5 module: database class --- (c) AMOOMA GmbH 2012 +-- (c) AMOOMA GmbH 2013 -- module(...,package.seeall) Database = {} -DATABASE_DRIVER = 'mysql' +DATABASE_DSN = 'gemeinschaft'; function Database.new(self, arg) arg = arg or {} @@ -20,117 +20,49 @@ function Database.new(self, arg) 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('/var/lib/freeswitch/.odbc.ini'); - if config then - database_driver = config.gemeinschaft.driver - database_name = config.gemeinschaft.DATABASE - user_name = config.gemeinschaft.USER - password = config.gemeinschaft.PASSWORD - host_name = config.gemeinschaft.HOST - end - end +function Database.connect(self) + self.dsn = DATABASE_DSN; - host_name = host_name or 'localhost'; - database_driver = database_driver or DATABASE_DRIVER; + require 'common.configuration_file' + local dsn = common.configuration_file.get('/var/lib/freeswitch/.odbc.ini', self.dsn); - 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.database_name = dsn.DATABASE; + self.user_name = dsn.USER; + self.password = dsn.PASSWORD; + self.host_name = dsn.HOST; - self.conn = self.env:connect(database_name, user_name, password, host_name); + self.conn = freeswitch.Dbh(self.dsn, self.user_name, self.password); 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; + return self.conn:connected(); 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; + if call_function then + return self.conn:query(sql_query, call_function); + else + return self.conn:query(sql_query); 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); + local result = nil; - 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); + self.conn:query(sql_query, function(row) + for key, value in pairs(row) do + result = value; + return result; 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 + end) - return cursor; + return result; end @@ -139,13 +71,8 @@ function Database.last_insert_id(self) end -function Database.release(self, sql_query, call_function) +function Database.release(self) if self.conn then - self.conn:close(); - end - if self.env then - self.env:close(); + self.conn:release(); end - - -- self.log:debug('DATABASE_RELEASE - connection: ', self.conn_id, ', status: ', self.env, ', ', self.conn); end |