Cleaning up foreign key support. Added drop_foreign command.

Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
This commit is contained in:
Taylor Otwell
2012-03-02 10:23:27 -06:00
parent 2f2437a0e3
commit 1d93cab0d0
5 changed files with 101 additions and 111 deletions

View File

@@ -6,14 +6,60 @@ use Laravel\Database\Schema\Table;
abstract class Grammar extends \Laravel\Database\Grammar {
/**
* Get the appropriate data type definition for the column.
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public function foreign(Table $table, Fluent $command)
{
$name = $command->name;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table = $this->wrap($table);
$on = $this->wrap($command->on);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign = $this->columnize($command->columns);
$referenced = $this->columnize((array) $command->references);
$sql = "ALTER TABLE $table ADD CONSTRAINT $name ";
return $sql .= "FOREIGN KEY ($foreign) REFERENCES $on ($referenced)";
}
/**
* Drop a constraint from the table.
*
* @param Table $table
* @param Fluent $fluent
* @return string
*/
protected function drop_constraint(Table $table, Fluent $command)
{
return "ALTER TABLE ".$this->wrap($table)." DROP CONSTRAINT ".$command->name;
}
/**
* Get the SQL syntax for indicating if a column is unsigned.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function type(Fluent $column)
protected function unsigned(Table $table, Fluent $column)
{
return $this->{'type_'.$column->type}($column);
if ($column->type == 'integer' && $column->unsigned)
{
return ' UNSIGNED';
}
}
/**
@@ -40,18 +86,14 @@ abstract class Grammar extends \Laravel\Database\Grammar {
}
/**
* Get the SQL syntax for indicating if a column is unsigned.
* Get the appropriate data type definition for the column.
*
* @param Table $table
* @param Fluent $column
* @return string
*/
protected function unsigned(Table $table, Fluent $column)
protected function type(Fluent $column)
{
if ($column->type == 'integer' && $column->unsigned)
{
return ' UNSIGNED';
}
return $this->{'type_'.$column->type}($column);
}
}