Included Modules

DataMapper::Constraints::Adapters::DataObjectsAdapter

Public Instance Methods

constraint_exists?(storage_name, constraint_name) click to toggle source

Determine if a constraint exists for a table

@param storage_name [Symbol]

  name of table to check constraint on

@param constraint_name [~String]

  name of constraint to check for

@return [Boolean]

@api private

    # File lib/data_mapper/constraints/adapters/do_adapter.rb, line 17
17:         def constraint_exists?(storage_name, constraint_name)
18:           statement = DataMapper::Ext::String.compress_lines(            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')} = ?)
19: 
20:           select(statement, schema_name, storage_name, constraint_name).first > 0
21:         end
create_relationship_constraint(relationship) click to toggle source

Create the constraint for a relationship

@param relationship [Relationship]

  the relationship to create the constraint for

@return [true, false]

  true if creating the constraints was successful

@api semipublic

    # File lib/data_mapper/constraints/adapters/do_adapter.rb, line 40
40:         def create_relationship_constraint(relationship)
41:           return false unless valid_relationship_for_constraint?(relationship)
42: 
43:           source_storage_name = relationship.source_model.storage_name(name)
44:           target_storage_name = relationship.target_model.storage_name(name)
45:           constraint_name     = constraint_name(source_storage_name, relationship.name)
46: 
47:           return false if constraint_exists?(source_storage_name, constraint_name)
48: 
49:           constraint_type =
50:             case relationship.inverse.constraint
51:             when :protect            then 'NO ACTION'
52:             # TODO: support :cascade as an option:
53:             #   (destroy doesn't communicate the UPDATE constraint)
54:             when :destroy, :destroy! then 'CASCADE'
55:             when :set_nil            then 'SET NULL'
56:             end
57: 
58:           return false if constraint_type.nil?
59: 
60:           source_keys = relationship.source_key.map { |p| property_to_column_name(p, false) }
61:           target_keys = relationship.target_key.map { |p| property_to_column_name(p, false) }
62: 
63:           create_constraints_statement = create_constraints_statement(
64:             constraint_name,
65:             constraint_type,
66:             source_storage_name,
67:             source_keys,
68:             target_storage_name,
69:             target_keys)
70: 
71:           execute(create_constraints_statement)
72:         end
destroy_relationship_constraint(relationship) click to toggle source

Remove the constraint for a relationship

@param relationship [Relationship]

  the relationship to remove the constraint for

@return [true, false]

  true if destroying the constraint was successful

@api semipublic

    # File lib/data_mapper/constraints/adapters/do_adapter.rb, line 84
84:         def destroy_relationship_constraint(relationship)
85:           return false unless valid_relationship_for_constraint?(relationship)
86: 
87:           storage_name    = relationship.source_model.storage_name(name)
88:           constraint_name = constraint_name(storage_name, relationship.name)
89: 
90:           return false unless constraint_exists?(storage_name, constraint_name)
91: 
92:           destroy_constraints_statement =
93:             destroy_constraints_statement(storage_name, constraint_name)
94: 
95:           execute(destroy_constraints_statement)
96:         end

Private Instance Methods

valid_relationship_for_constraint?(relationship) click to toggle source

Check to see if the relationship’s constraints can be used

Only one-to-one, one-to-many, and many-to-many relationships can be used for constraints. They must also be in the same repository as the adapter is connected to.

@param relationship [Relationship]

  the relationship to check

@return [true, false]

  true if a constraint can be established for relationship

@api private

     # File lib/data_mapper/constraints/adapters/do_adapter.rb, line 114
114:         def valid_relationship_for_constraint?(relationship)
115:           return false unless relationship.source_repository_name == name || relationship.source_repository_name.nil?
116:           return false unless relationship.target_repository_name == name || relationship.target_repository_name.nil?
117:           return false unless relationship.kind_of?(Associations::ManyToOne::Relationship)
118:           true
119:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.