Sha256: 421b83b98386ae590417036ffb546fcccc7403e7c6cddd3f64eb3f2dd491f568
Contents?: true
Size: 1.09 KB
Versions: 6
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 @connection ||= WrappedJDBCConnection.new(@port) end end end
Version data entries
6 entries across 6 versions & 1 rubygems