public function getDropForeignKeyDDL(ForeignKey $fk)
 {
     if ($fk->isSkipSql()) {
         return;
     }
     $pattern = "\nALTER TABLE %s DROP FOREIGN KEY %s;\n";
     return sprintf($pattern, $this->quoteIdentifier($fk->getTable()->getName()), $this->quoteIdentifier($fk->getName()));
 }
 public function getForeignKeyDDL(ForeignKey $fk)
 {
     if ($fk->isSkipSql()) {
         return;
     }
     $pattern = 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)';
     $script = sprintf($pattern, $this->quoteIdentifier($fk->getName()), $this->getColumnListDDL($fk->getLocalColumns()), $this->quoteIdentifier($fk->getForeignTableName()), $this->getColumnListDDL($fk->getForeignColumns()));
     if ($fk->hasOnUpdate() && $fk->getOnUpdate() != ForeignKey::SETNULL) {
         $script .= ' ON UPDATE ' . $fk->getOnUpdate();
     }
     if ($fk->hasOnDelete() && $fk->getOnDelete() != ForeignKey::SETNULL) {
         $script .= ' ON DELETE ' . $fk->getOnDelete();
     }
     return $script;
 }
Example #3
0
 /**
  * @param string $stmt
  * @return $this
  */
 public function parse($stmt)
 {
     $lines = array_map('trim', explode(PHP_EOL, $stmt));
     $this->first = array_shift($lines);
     $last = array_pop($lines);
     //remove AUTO_INCREMENT bit from raw statement
     $this->last = preg_replace('/AUTO_INCREMENT=\\d+ ?/', '', $last);
     $this->statement = str_replace($last, $this->last, $stmt);
     if ($this->name === null) {
         if (!preg_match('/`([^`]+)/', $this->first, $match)) {
             throw new \RuntimeException(sprintf('Unable to extract name from %s (looked at line %s)', $stmt, $lines[0]));
         }
         $this->name = $match[1];
     }
     foreach ($lines as $ln) {
         if (mb_substr($ln, -1) == ',') {
             $ln = mb_substr($ln, 0, -1);
         }
         //field lines start with back-tick
         switch ($ln[0]) {
             case '`':
                 $field = new Field($ln);
                 $this->fields[$field->getName()] = $field;
                 break;
             case 'P':
                 $this->primary = new Primary($ln);
                 break;
             case 'S':
                 //spatial
             //spatial
             case 'U':
                 //unique
             //unique
             case 'F':
                 //fulltext
             //fulltext
             case 'I':
                 //index
             //index
             case 'K':
                 //Key
                 $idx = new Index($ln);
                 $this->indexes[$idx->getName()] = $idx;
                 break;
             case 'C':
                 $constraint = new ForeignKey($ln);
                 $this->constraints[$constraint->getName()] = $constraint;
                 break;
             default:
                 throw new \LogicException(sprintf('Unable to parse line %s in %s', $ln, $stmt));
         }
     }
     return $this;
 }
Example #4
0
 public function getForeignKeyDDL(ForeignKey $fk)
 {
     if ($fk->isSkipSql()) {
         return;
     }
     $pattern = "CONSTRAINT %s\r\n    FOREIGN KEY (%s) REFERENCES %s (%s)";
     $script = sprintf($pattern, $this->quoteIdentifier($fk->getName()), $this->getColumnListDDL($fk->getLocalColumns()), $this->quoteIdentifier($fk->getForeignTableName()), $this->getColumnListDDL($fk->getForeignColumns()));
     if ($fk->hasOnDelete()) {
         $script .= "\r\n    ON DELETE " . $fk->getOnDelete();
     }
     return $script;
 }
Example #5
0
 /**
  * @param ForeignKey $foreignKey
  */
 public function addForeignKey(ForeignKey $foreignKey)
 {
     $foreignKey->setParentTable($this);
     $this->foreignKeys[$foreignKey->getName()] = $foreignKey;
 }
Example #6
0
 /**
  * @param ForeignKey $changedForeignKey
  */
 public function addChangedForeignKey(ForeignKey $changedForeignKey)
 {
     $this->changedForeignKeys[$changedForeignKey->getName()] = $changedForeignKey;
 }