Class: Mysqlcollector::Mysql

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlcollector/mysql.rb

Instance Method Summary collapse

Constructor Details

#initializeMysql

Returns a new instance of Mysql.



5
6
7
8
9
10
11
12
13
14
# File 'lib/mysqlcollector/mysql.rb', line 5

def initialize
  @conn = Mysql2::Client.new(host: $config[:mysql_host],
                             port: $config[:mysql_port],
                             username: $config[:mysql_username],
                             password: $config[:mysql_password])

  Mysqlcollector::Log.new.debug("Connected on MySQL server: #{$config[:mysql_host]}:#{$config[:mysql_port]}");
rescue Exception => error
  Mysqlcollector::Log.new.error(error.message)
end

Instance Method Details

#closeObject



26
27
28
29
30
# File 'lib/mysqlcollector/mysql.rb', line 26

def close
  @conn.close
rescue
  exit 1
end

#execute(sql) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/mysqlcollector/mysql.rb', line 16

def execute(sql)
  begin
    Mysqlcollector::Log.new.debug("MySQL Query: #{sql}")

    @conn.query(sql).each(:as => :hash)
  rescue Exception => error
    Mysqlcollector::Log.new.sql_error(error, sql);
  end
end