Sha256: 95e3eed840a2795c99bbfd0f05d1b859e7dc2d9a995274e207f4af1e15df2574

Contents?: true

Size: 1.18 KB

Versions: 11

Compression:

Stored size: 1.18 KB

Contents

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

module IsolatedServer
  class Mysql < Base
    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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
isolated_server-0.4.12 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.11 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.10 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.9 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.8 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.7 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.6 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.5 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.4 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.3 lib/isolated_server/mysql/jdbc_connection.rb
isolated_server-0.4.2 lib/isolated_server/mysql/jdbc_connection.rb