Skip to content
Permalink
Browse files

Make the foreign key constraints columns references resolution DRY

Also uses the assertQuotedQuery method in the TableTest class
  • Loading branch information...
HavokInspiration committed Jun 15, 2015
1 parent 0c8ecd0 commit 0aea497e2da88e8c7e08fb7e8d476602ef876d19
@@ -89,6 +89,24 @@ protected function _convertOnClause($clause)
return Table::ACTION_SET_NULL;
}
/**
* Convert foreign key constraints references to a valid
* stringified list
*
* @param string|array $references The referenced columns of a foreign key constraint statement
* @return string
*/
protected function _convertFkColumnsReference($references) {
if (is_string($references)) {
return $this->_driver->quoteIdentifier($references);
}
return implode(', ', array_map(
[$this->_driver, 'quoteIdentifier'],
$references
));
}
/**
* Generate the SQL to drop a table.
*
@@ -418,20 +418,11 @@ protected function _keySql($prefix, $data)
}
}
if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
if (!is_array($data['references'][1])) {
$data['references'][1] = [$data['references'][1]];
}
$columnsReference = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['references'][1]
);
return $prefix . sprintf(
' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
implode(', ', $columns),
$this->_driver->quoteIdentifier($data['references'][0]),
implode(', ', $columnsReference),
$this->_convertFkColumnsReference($data['references'][1]),
$this->_foreignOnClause($data['update']),
$this->_foreignOnClause($data['delete'])
);
@@ -463,20 +463,11 @@ protected function _keySql($prefix, $data)
$data['columns']
);
if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
if (!is_array($data['references'][1])) {
$data['references'][1] = [$data['references'][1]];
}
$columnsReference = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['references'][1]
);
return $prefix . sprintf(
' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s DEFERRABLE INITIALLY IMMEDIATE',
implode(', ', $columns),
$this->_driver->quoteIdentifier($data['references'][0]),
implode(', ', $columnsReference),
$this->_convertFkColumnsReference($data['references'][1]),
$this->_foreignOnClause($data['update']),
$this->_foreignOnClause($data['delete'])
);
@@ -332,19 +332,10 @@ public function constraintSql(Table $table, $name)
if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
$type = 'FOREIGN KEY';
if (!is_array($data['references'][1])) {
$data['references'][1] = [$data['references'][1]];
}
$columnsReference = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['references'][1]
);
$clause = sprintf(
' REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
$this->_driver->quoteIdentifier($data['references'][0]),
implode(', ', $columnsReference),
$this->_convertFkColumnsReference($data['references'][1]),
$this->_foreignOnClause($data['update']),
$this->_foreignOnClause($data['delete'])
);
@@ -415,20 +415,11 @@ protected function _keySql($prefix, $data)
$data['columns']
);
if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
if (!is_array($data['references'][1])) {
$data['references'][1] = [$data['references'][1]];
}
$columnsReference = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['references'][1]
);
return $prefix . sprintf(
' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
implode(', ', $columns),
$this->_driver->quoteIdentifier($data['references'][0]),
implode(', ', $columnsReference),
$this->_convertFkColumnsReference($data['references'][1]),
$this->_foreignOnClause($data['update']),
$this->_foreignOnClause($data['delete'])
);
@@ -436,11 +436,10 @@ public function testAddConstraintForeignKeyTwoColumns()
$this->assertEquals($expected, $compositeConstraint);
$expectedSubstring = '#CONSTRAINT <product_id_fk> FOREIGN KEY \(<product_id>, <product_category>\)' .
' REFERENCES <products> \(<id>, <category>\)#';
$expectedSubstring = str_replace(['<', '>'], ['[`"\[]', '[`"\]]'], $expectedSubstring);
$expectedSubstring = 'CONSTRAINT <product_id_fk> FOREIGN KEY \(<product_id>, <product_category>\)' .
' REFERENCES <products> \(<id>, <category>\)';
$this->assertRegExp($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('default'))[0]);
$this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('default'))[0]);
}
/**

0 comments on commit 0aea497

Please sign in to comment.
You can’t perform that action at this time.