Sha256: 21151949432379623228d671963b531bd9d664ff3e3c6cbe7289f96d6d618d75

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require "java"
java_import "java.sql.DriverManager"
java_import "org.apache.commons.lang.StringEscapeUtils"

class MysqlIsolatedServer
  class WrappedJDBCConnection
    def initialize(port)
      @cx ||= DriverManager.get_connection("jdbc:mysql://127.0.0.1:#{port}/mysql", "root", "")
    end

    def query(sql)
      stmt = @cx.create_statement
      if sql !~ /^select/i && sql !~ /^show/i
        return stmt.execute(sql)
      end

      rs = stmt.execute_query(sql)

      rows = []
      while (rs.next)
        meta_data = rs.get_meta_data
        num_cols = meta_data.get_column_count

        row = {}
        1.upto(num_cols) do |col|
          col_name = meta_data.get_column_label(col)
          col_value = rs.get_object(col) # of meta_data.get_column_type(col)

          row[col_name] = col_value
        end

        rows << row
      end
      rows
    ensure
      stmt.close if stmt
      rs.close if rs
    end

    def escape(str)
      StringEscapeUtils.escapeSql(str)
    end
  end

  module DBConnection
    def connection
      @cx ||= WrappedJDBCConnection.new(@port)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mysql_isolated_server-0.5.4 lib/mysql_isolated_server/jdbc_connection.rb
mysql_isolated_server-0.5.3 lib/mysql_isolated_server/jdbc_connection.rb
mysql_isolated_server-0.5.2 lib/mysql_isolated_server/jdbc_connection.rb