Module: RR::ConnectionExtenders::JdbcSQLExtender

Defined in:
lib/rubyrep/connection_extenders/jdbc_extender.rb

Overview

Provides various JDBC specific functionality required by Rubyrep.

Instance Method Summary collapse

Instance Method Details

#disconnect!Object

Monkey patch for activerecord-jdbc-adapter-0.7.2 as it doesn’t set the @active flag to false, thus ActiveRecord#active? incorrectly confirms the connection to still be active.



13
14
15
16
# File 'lib/rubyrep/connection_extenders/jdbc_extender.rb', line 13

def disconnect!
  super
  @active = false
end

#primary_key_names(table) ⇒ Object

Returns an ordered list of primary key column names of the given table



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyrep/connection_extenders/jdbc_extender.rb', line 19

def primary_key_names(table)
  if tables.grep(/^#{table}$/i).empty?
    # Note: Cannot use tables.include? as returned tables are made lowercase under JRuby MySQL
    raise "table '#{table}' does not exist"
  end
  columns = []
  result_set = @connection.connection..getPrimaryKeys(nil, nil, table);
  while result_set.next
    column_name = result_set.getString("COLUMN_NAME")
    key_seq = result_set.getShort("KEY_SEQ")
    columns << {:column_name => column_name, :key_seq => key_seq}
  end
  columns.sort! {|a, b| a[:key_seq] <=> b[:key_seq]}
  key_names = columns.map {|column| column[:column_name]}
  key_names
end

#referenced_tables(tables) ⇒ Object

Returns for each given table, which other tables it references via foreign key constraints.

  • tables: an array of table names

  • returns: a hash with

    • key: name of the referencing table

    • value: an array of names of referenced tables



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubyrep/connection_extenders/jdbc_extender.rb', line 42

def referenced_tables(tables)
  result = {}
  tables.each do |table|
    references_of_this_table = []
    result_set = @connection.connection..getImportedKeys(nil, nil, table)
    while result_set.next
      referenced_table = result_set.getString("PKTABLE_NAME")
      unless references_of_this_table.include? referenced_table
        references_of_this_table << referenced_table
      end
    end
    result[table] = references_of_this_table
  end
  result
end