Module: DataMapper::Constraints::Adapters::DataObjectsAdapter
- Includes:
- SQL
- Included in:
- MysqlAdapter, OracleAdapter, PostgresAdapter, SqlserverAdapter
- Defined in:
- lib/dm-constraints/adapters/dm-do-adapter.rb
Defined Under Namespace
Modules: SQL
Instance Method Summary collapse
-
#constraint_exists?(storage_name, constraint_name) ⇒ Boolean
private
Determine if a constraint exists for a table.
-
#create_relationship_constraint(relationship) ⇒ true, false
Create the constraint for a relationship.
-
#destroy_relationship_constraint(relationship) ⇒ true, false
Remove the constraint for a relationship.
Instance Method Details
#constraint_exists?(storage_name, constraint_name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine if a constraint exists for a table
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dm-constraints/adapters/dm-do-adapter.rb', line 17 def constraint_exists?(storage_name, constraint_name) statement = DataMapper::Ext::String.compress_lines(<<-SQL) SELECT COUNT(*) FROM #{quote_name('information_schema')}.#{quote_name('table_constraints')} WHERE #{quote_name('constraint_type')} = 'FOREIGN KEY' AND #{quote_name('table_schema')} = ? AND #{quote_name('table_name')} = ? AND #{quote_name('constraint_name')} = ? SQL select(statement, schema_name, storage_name, constraint_name).first > 0 end |
#create_relationship_constraint(relationship) ⇒ true, false
Create the constraint for a relationship
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dm-constraints/adapters/dm-do-adapter.rb', line 40 def create_relationship_constraint(relationship) return false unless valid_relationship_for_constraint?(relationship) source_model = relationship.source_model source_table = source_model.storage_name(name) source_key = relationship.source_key constraint_name = constraint_name(source_table, relationship.name) return false if constraint_exists?(source_table, constraint_name) constraint_type = case relationship.inverse.constraint when :protect then 'NO ACTION' when :destroy, :destroy! then 'CASCADE' when :set_nil then 'SET NULL' end return false if constraint_type.nil? storage_name = relationship.source_model.storage_name(name) reference_storage_name = relationship.target_model.storage_name(name) foreign_keys = relationship.source_key.map { |p| property_to_column_name(p, false) } reference_keys = relationship.target_key.map { |p| property_to_column_name(p, false) } execute(create_constraints_statement(storage_name, constraint_name, constraint_type, foreign_keys, reference_storage_name, reference_keys)) end |
#destroy_relationship_constraint(relationship) ⇒ true, false
Remove the constraint for a relationship
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dm-constraints/adapters/dm-do-adapter.rb', line 77 def destroy_relationship_constraint(relationship) return false unless valid_relationship_for_constraint?(relationship) source_model = relationship.source_model source_table = source_model.storage_name(name) constraint_name = constraint_name(source_table, relationship.name) return false unless constraint_exists?(source_table, constraint_name) execute(destroy_constraints_statement(source_table, constraint_name)) end |