module DataMapper::Constraints::Adapters::DataObjectsAdapter::SQL

Private Instance Methods

constraint_name(storage_name, relationship_name) click to toggle source

generates a unique constraint name given a table and a relationships

@param [String] storage_name

name of table to constrain

@param [String] relationship_name

name of the relationship to constrain

@return [String]

name of the constraint

@api private

# File lib/data_mapper/constraints/adapters/do_adapter.rb, line 186
def constraint_name(storage_name, relationship_name)
  identifier = "#{storage_name}_#{relationship_name}"[0, self.class::IDENTIFIER_MAX_LENGTH - 3]
  "#{identifier}_fk"
end
create_constraints_statement(constraint_name, constraint_type, source_storage_name, source_keys, target_storage_name, target_keys) click to toggle source

Generates the SQL statement to create a constraint

@param [String] #constraint_name

name of the foreign key constraint

@param [String] constraint_type

type of constraint to ALTER source_storage_name with

@param [String] source_storage_name

name of table to ALTER with constraint

@param [Array(String)] source_keys

columns in source_storage_name that refer to foreign table

@param [String] target_storage_name

target table of the constraint

@param [Array(String)] target_keys

columns the target table that are referred to

@return [String]

SQL DDL Statement to create a constraint

@api private

# File lib/data_mapper/constraints/adapters/do_adapter.rb, line 144
          def create_constraints_statement(constraint_name, constraint_type, source_storage_name, source_keys, target_storage_name, target_keys)
            DataMapper::Ext::String.compress_lines("              ALTER TABLE #{quote_name(source_storage_name)}
              ADD CONSTRAINT #{quote_name(constraint_name)}
              FOREIGN KEY (#{source_keys.join(', ')})
              REFERENCES #{quote_name(target_storage_name)} (#{target_keys.join(', ')})
              ON DELETE #{constraint_type}
              ON UPDATE #{constraint_type}
")
          end
destroy_constraints_statement(storage_name, constraint_name) click to toggle source

Generates the SQL statement to destroy a constraint

@param [String] storage_name

name of table to constrain

@param [String] #constraint_name

name of foreign key constraint

@return [String]

SQL DDL Statement to destroy a constraint

@api private

# File lib/data_mapper/constraints/adapters/do_adapter.rb, line 167
          def destroy_constraints_statement(storage_name, constraint_name)
            DataMapper::Ext::String.compress_lines("              ALTER TABLE #{quote_name(storage_name)}
              DROP CONSTRAINT #{quote_name(constraint_name)}
")
          end