Module: ArJdbc::MissingFunctionalityHelper

Included in:
Derby, SQLite3
Defined in:
lib/arjdbc/jdbc/missing_functionality_helper.rb

Instance Method Summary collapse

Instance Method Details

#alter_table(table_name, options = {}) ⇒ Object

Taken from SQLite adapter



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/arjdbc/jdbc/missing_functionality_helper.rb', line 5

def alter_table(table_name, options = {}) #:nodoc:
  table_name = table_name.to_s.downcase
  altered_table_name = "altered_#{table_name}"
  caller = lambda {|definition| yield definition if block_given?}

  transaction do
    # A temporary table might improve performance here, but
    # it doesn't seem to maintain indices across the whole move.
    move_table(table_name, altered_table_name,
      options)
    move_table(altered_table_name, table_name, &caller)
  end
end

#copy_table(from, to, options = {}) ⇒ Object

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/arjdbc/jdbc/missing_functionality_helper.rb', line 24

def copy_table(from, to, options = {}) #:nodoc:
  options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
  create_table(to, options) do |definition|
    @definition = definition
    columns(from).each do |column|
      column_name = options[:rename] ?
        (options[:rename][column.name] ||
         options[:rename][column.name.to_sym] ||
         column.name) : column.name

      @definition.column(column_name, column.type,
        :limit => column.limit, :default => column.default,
        :precision => column.precision, :scale => column.scale,
        :null => column.null)
    end
    @definition.primary_key(primary_key(from)) if primary_key(from)
    yield @definition if block_given?
  end

  copy_table_indexes(from, to, options[:rename] || {})
  copy_table_contents(from, to,
    @definition.columns.map {|column| column.name},
    options[:rename] || {})
end

#copy_table_contents(from, to, columns, rename = {}) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/arjdbc/jdbc/missing_functionality_helper.rb', line 72

def copy_table_contents(from, to, columns, rename = {}) #:nodoc:
  column_mappings = Hash[*columns.map {|name| [name, name]}.flatten]
  rename.inject(column_mappings) {|map, a| map[a.last] = a.first; map}
  from_columns = columns(from).collect {|col| col.name}
  columns = columns.find_all{|col| from_columns.include?(column_mappings[col])}
  quoted_columns = columns.map { |col| quote_column_name(col) } * ','

  quoted_to = quote_table_name(to)
  execute("SELECT * FROM #{quote_table_name(from)}").each do |row|
    sql = "INSERT INTO #{quoted_to} (#{quoted_columns}) VALUES ("
    sql << columns.map {|col| quote row[column_mappings[col]]} * ', '
    sql << ')'
    execute sql
  end
end

#copy_table_indexes(from, to, rename = {}) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/arjdbc/jdbc/missing_functionality_helper.rb', line 49

def copy_table_indexes(from, to, rename = {}) #:nodoc:
  indexes(from).each do |index|
    name = index.name.downcase
    if to == "altered_#{from}"
      name = "temp_#{name}"
    elsif from == "altered_#{to}"
      name = name[5..-1]
    end

    to_column_names = columns(to).map(&:name)
    columns = index.columns.map {|c| rename[c] || c }.select do |column|
      to_column_names.include?(column)
    end

    unless columns.empty?
      # index name can't be the same
      opts = { :name => name.gsub(/(_?)(#{from})_/, "\\1#{to}_") }
      opts[:unique] = true if index.unique
      add_index(to, columns, opts)
    end
  end
end

#move_table(from, to, options = {}, &block) ⇒ Object

:nodoc:



19
20
21
22
# File 'lib/arjdbc/jdbc/missing_functionality_helper.rb', line 19

def move_table(from, to, options = {}, &block) #:nodoc:
  copy_table(from, to, options, &block)
  drop_table(from)
end