Sha256: 053b8991885f655537561d0427767eb8498025c79a804bb3185d3f3db4361513

Contents?: true

Size: 1.39 KB

Versions: 15

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8
require "java"
require "rubygems"
require "jdbc/mysql"

java_import "com.mysql.jdbc.Driver"

# A JDBC mysql connection class.
# The interface is compatible with the mysql2 API.
class LogStash::DrupalDblogJavaMysqlConnection

  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 # def initialize

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

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

    meta = resultSet.getMetaData
    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 # def select

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

  def close
    @connection.close
  end # def close

end # class LogStash::DrupalDblogJavaMysqlConnection

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
logstash-input-drupal_dblog-2.0.7-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.6-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.5-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.4-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.3-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.2-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-2.0.1-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.7-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.6-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.5-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.4-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.3-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.2-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-input-drupal_dblog-0.1.1-java lib/logstash/inputs/drupal_dblog/jdbcconnection.rb
logstash-lib-1.3.2 lib/logstash/inputs/drupal_dblog/jdbcconnection.rb