Class: IsolatedServer::Mysql::WrappedJDBCConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/isolated_server/mysql/jdbc_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ WrappedJDBCConnection

Returns a new instance of WrappedJDBCConnection.



8
9
10
# File 'lib/isolated_server/mysql/jdbc_connection.rb', line 8

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

Instance Method Details

#escape(str) ⇒ Object



41
42
43
# File 'lib/isolated_server/mysql/jdbc_connection.rb', line 41

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

#query(sql) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/isolated_server/mysql/jdbc_connection.rb', line 12

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)
     = rs.
    num_cols = .get_column_count

    row = {}
    1.upto(num_cols) do |col|
      col_name = .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