summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/database.lua
blob: 3692f841435a50083088a040d0baa16c95c9f173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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