Class: DBLeftovers::GenericDatabaseInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/db_leftovers/generic_database_interface.rb

Instance Method Summary collapse

Instance Method Details

#execute_add_constraint(chk) ⇒ Object



53
54
55
56
57
58
# File 'lib/db_leftovers/generic_database_interface.rb', line 53

def execute_add_constraint(chk)
  sql = <<-EOQ
      ALTER TABLE #{chk.on_table} ADD CONSTRAINT #{chk.constraint_name} CHECK (#{chk.check})
  EOQ
  execute_sql sql
end

#execute_add_foreign_key(fk) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/db_leftovers/generic_database_interface.rb', line 39

def execute_add_foreign_key(fk)
  on_delete = "ON DELETE CASCADE" if fk.cascade
  on_delete = "ON DELETE SET NULL" if fk.set_null
  execute_sql %{ALTER TABLE #{fk.from_table}
            ADD CONSTRAINT #{fk.constraint_name}
            FOREIGN KEY (#{fk.from_column})
            REFERENCES #{fk.to_table} (#{fk.to_column})
            #{on_delete}}
end

#execute_add_index(idx) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/db_leftovers/generic_database_interface.rb', line 17

def execute_add_index(idx)
  unique = idx.unique? ? 'UNIQUE' : ''
  where = idx.where_clause.present? ? "WHERE #{idx.where_clause}" : ''
  using = idx.using_clause.present? ? "USING #{idx.using_clause}" : ''

  sql = <<-EOQ
    CREATE #{unique} INDEX #{idx.index_name}
    ON #{idx.table_name}
    #{using}
    (#{idx.index_function || idx.column_names.join(', ')})
    #{where}
  EOQ
  execute_sql(sql)
end

#execute_drop_constraint(constraint_name, on_table) ⇒ Object



60
61
62
# File 'lib/db_leftovers/generic_database_interface.rb', line 60

def execute_drop_constraint(constraint_name, on_table)
  execute_sql %{ALTER TABLE #{on_table} DROP CONSTRAINT #{constraint_name}}
end

#execute_drop_foreign_key(constraint_name, from_table, from_column) ⇒ Object



49
50
51
# File 'lib/db_leftovers/generic_database_interface.rb', line 49

def execute_drop_foreign_key(constraint_name, from_table, from_column)
  execute_sql %{ALTER TABLE #{from_table} DROP CONSTRAINT #{constraint_name}}
end

#execute_drop_index(table_name, index_name) ⇒ Object



32
33
34
35
36
37
# File 'lib/db_leftovers/generic_database_interface.rb', line 32

def execute_drop_index(table_name, index_name)
  sql = <<-EOQ
      DROP INDEX #{index_name}
  EOQ
  execute_sql(sql)
end

#execute_sql(sql) ⇒ Object



64
65
66
# File 'lib/db_leftovers/generic_database_interface.rb', line 64

def execute_sql(sql)
  @conn.execute(sql)
end

#lookup_all_constraintsObject



13
14
15
# File 'lib/db_leftovers/generic_database_interface.rb', line 13

def lookup_all_constraints
  raise "Should be overriden by a database-specific interface"
end

#lookup_all_foreign_keysObject



9
10
11
# File 'lib/db_leftovers/generic_database_interface.rb', line 9

def lookup_all_foreign_keys
  raise "Should be overriden by a database-specific interface"
end

#lookup_all_indexesObject



5
6
7
# File 'lib/db_leftovers/generic_database_interface.rb', line 5

def lookup_all_indexes
  raise "Should be overriden by a database-specific interface"
end