summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/database.lua
blob: 1f391358c3afbdbe5c6f6b117645bf39be458e8a (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
-- 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