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



22
23
24
25
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 22

def add_foreign_key(from_table, to_table, options = {})
  sql = "ALTER TABLE #{quote_proper_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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 27

def add_foreign_key_sql(from_table, to_table, options = {})
  column  = options[:column] || "#{to_table.to_s.singularize}_id"
  foreign_key_name = options.key?(:name) ? options[:name].to_s : foreign_key_name(from_table, column)
  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_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
11
12
13
14
15
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 8

def drop_table(*args)
  options = args.extract_options!
  if options[:force]
    disable_referential_integrity { super(*(args.dup << options)) }
  else
    super(*(args.dup << options))
  end
end

#foreign_key_exists?(from_table, options) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 17

def foreign_key_exists?(from_table, options)
  foreign_key_name = decipher_foreign_key_name(from_table, options)
  foreign_keys(from_table).any? { |fk| fk.name == foreign_key_name }
end

#proper_table_name(table) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 47

def proper_table_name(table)
  # This will normally be a no-op, but prevents the table from being wrapped twice:
  table = Foreigner::SchemaDumper::ClassMethods.remove_prefix_and_suffix(table)
  if ActiveRecord::Migration.instance_methods(false).include? :proper_table_name
    ActiveRecord::Migration.new.proper_table_name(table, options = {
      table_name_prefix: ActiveRecord::Base.table_name_prefix,
      table_name_suffix: ActiveRecord::Base.table_name_suffix
    })
  else
    ActiveRecord::Migrator.proper_table_name(table)
  end
end

#quote_proper_table_name(table) ⇒ Object



43
44
45
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 43

def quote_proper_table_name(table)
  quote_table_name(proper_table_name(table))
end

#remove_foreign_key(table, options) ⇒ Object



60
61
62
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 60

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

#remove_foreign_key_sql(table, options) ⇒ Object



64
65
66
67
# File 'lib/foreigner/connection_adapters/sql2003.rb', line 64

def remove_foreign_key_sql(table, options)
  foreign_key_name = decipher_foreign_key_name(table, options)
  "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