/**
  * 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;
 }
Example #2
0
 /**
  * Repositions the declaration of the name of the constraint.
  *
  * In Informix the name of the constraint is placed at the end of the
  * declaration.
  *
  * @param \Doctrine\DBAL\Schema\Constraint $constraint
  * @param string $sql
  *
  * @return string
  */
 protected function repositionContraintNameSQL(Constraint $constraint, $sql)
 {
     if ($constraintName = $constraint->getName()) {
         if (preg_match("/\\bADD\\s+CONSTRAINT\\b/i", $sql)) {
             $sql = preg_replace("/\\s*\\bADD\\s+CONSTRAINT\\s+{$constraintName}\\b\\s*/i", ' ADD CONSTRAINT ', $sql);
         } else {
             $sql = preg_replace("/\\s*\\bCONSTRAINT\\s+{$constraintName}\\b\\s*/i", '', $sql);
         }
         $sql .= ' CONSTRAINT ' . $constraintName;
     }
     return $sql;
 }