Module: Foreigner::ConnectionAdapters::Sql2003

Included in:
Mysql2Adapter, PostgreSQLAdapter
Defined in:
lib/foreigner/connection_adapters/sql2003.rb

Instance Method Summary collapse

Instance Method Details

#add_foreign_key(from_table, to_table, options = {}) ⇒ Object



12
13
14
15
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 12

def add_foreign_key(from_table, to_table, options = {})
  sql = "ALTER TABLE #{quote_table_name(from_table)} #{add_foreign_key_sql(from_table, to_table, options)}"
  execute(sql)
end

#add_foreign_key_sql(from_table, to_table, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 17

def add_foreign_key_sql(from_table, to_table, options = {})
  column  = options[:column] || "#{to_table.to_s.singularize}_id"
  foreign_key_name = foreign_key_name(from_table, column, options)
  primary_key = options[:primary_key] || "id"
  dependency = dependency_sql(options[:dependent])

  sql =
    "ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
    "FOREIGN KEY (#{quote_column_name(column)}) " +
    "REFERENCES #{quote_table_name(ActiveRecord::Migrator.proper_table_name(to_table))}(#{primary_key})"
  sql << " #{dependency}" if dependency.present?
  sql << " #{options[:options]}" if options[:options]

  sql
end

#drop_table(*args) ⇒ Object



8
9
10
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 8

def drop_table(*args)
  disable_referential_integrity { super }
end

#remove_foreign_key(table, options) ⇒ Object



33
34
35
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 33

def remove_foreign_key(table, options)
  execute "ALTER TABLE #{quote_table_name(table)} #{remove_foreign_key_sql(table, options)}"
end

#remove_foreign_key_sql(table, options) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 37

def remove_foreign_key_sql(table, options)
  if Hash === options
    foreign_key_name = foreign_key_name(table, options[:column], options)
  else
    foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
  end

  "DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 4

def supports_foreign_keys?
  true
end