Class: LogStash::DrupalDblogJavaMysqlConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/drupal_dblog/jdbcconnection.rb

Overview

A JDBC mysql connection class. The interface is compatible with the mysql2 API.

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, database, port = nil) ⇒ DrupalDblogJavaMysqlConnection

Returns a new instance of DrupalDblogJavaMysqlConnection.



12
13
14
15
16
17
# File 'lib/logstash/inputs/drupal_dblog/jdbcconnection.rb', line 12

def initialize(host, username, password, database, port = nil)
  port ||= 3306

  address = "jdbc:mysql://#{host}:#{port}/#{database}"
  @connection = java.sql.DriverManager.getConnection(address, username, password)
end

Instance Method Details

#closeObject

def update



62
63
64
# File 'lib/logstash/inputs/drupal_dblog/jdbcconnection.rb', line 62

def close
  @connection.close
end

#query(sql) ⇒ Object

def initialize



19
20
21
22
23
24
25
# File 'lib/logstash/inputs/drupal_dblog/jdbcconnection.rb', line 19

def query(sql)
  if sql =~ /select/i
    return select(sql)
  else
    return update(sql)
  end
end

#select(sql) ⇒ Object

def query



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
# File 'lib/logstash/inputs/drupal_dblog/jdbcconnection.rb', line 27

def select(sql)
  stmt = @connection.createStatement
  resultSet = stmt.executeQuery(sql)

  meta = resultSet.
  column_count = meta.getColumnCount

  rows = []

  while resultSet.next
    res = {}

    (1..column_count).each do |i|
      name = meta.getColumnName(i)
      case meta.getColumnType(i)
      when java.sql.Types::INTEGER
        res[name] = resultSet.getInt(name)
      else
        res[name] = resultSet.getString(name)
      end
    end

    rows << res
  end

  stmt.close
  return rows
end

#update(sql) ⇒ Object

def select



56
57
58
59
60
# File 'lib/logstash/inputs/drupal_dblog/jdbcconnection.rb', line 56

def update(sql)
  stmt = @connection.createStatement
  stmt.execute_update(sql)
  stmt.close
end