/** * Gets the SQL to create a constraint on a table on this platform. * * @param Constraint $constraint * @param string|Table $table * @return string */ public function getCreateConstraintSQL(\Doctrine\DBAL\Schema\Constraint $constraint, $table) { if ($table instanceof \Doctrine\DBAL\Schema\Table) { $table = $table->getQuotedName($this); } $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this); $columns = array(); foreach ($constraint->getColumns() as $column) { $columns[] = $column; } $columnList = '(' . implode(', ', $columns) . ')'; $referencesClause = ''; if ($constraint instanceof \Doctrine\DBAL\Schema\Index) { if ($constraint->isPrimary()) { $query .= ' PRIMARY KEY'; } elseif ($constraint->isUnique()) { $query .= ' UNIQUE'; } else { throw new \InvalidArgumentException('Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'); } } else { if ($constraint instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) { $query .= ' FOREIGN KEY'; $foreignColumns = array(); foreach ($constraint->getForeignColumns() as $column) { $foreignColumns[] = $column; } $referencesClause = ' REFERENCES ' . $constraint->getForeignTableName() . ' (' . implode(', ', $foreignColumns) . ')'; } } $query .= ' ' . $columnList . $referencesClause; return $query; }