Ejemplo n.º 1
0
 public function equals(Column $otherColumn, $namesOnly = true)
 {
     if (!$this->_table->equals($otherColumn->getTable()) || $this->getName() !== $otherColumn->getName()) {
         return false;
     }
     if ($namesOnly) {
         return true;
     }
     $otherRef = $otherColumn->getReference();
     if ($this->_type !== $otherColumn->getType() || $this->_maxLength && $this->_maxLength !== $otherColumn->getMaxLength() || $this->_allowedValues != $otherColumn->getAllowedValues() || $this->_keyType !== $otherColumn->getKeyType() || $this->_autoIncreased !== $otherColumn->isAutoIncreased() || $this->_optional !== $otherColumn->isOptional() || $this->_defaultValue !== $otherColumn->getDefaultValue() || $this->_reference && !$otherRef || !$this->_reference && $otherRef || $this->_reference && $otherRef && !$this->_reference->equals($otherRef)) {
         return false;
     }
     return true;
 }
Ejemplo n.º 2
0
 public function saveColumn(Column $column)
 {
     if ($column->isSynced()) {
         return $this;
     }
     $syncedCol = $column->getTable()->getColumn($column->getName())->load();
     if ($column->equals($syncedCol, false)) {
         return $this;
     }
     $sql = $this->getColumnSql($column);
     $name = $this->quoteName($column->getDatabase(), $column->getTable());
     if ($syncedCol->isUnique() && !$column->isUnique()) {
         $this->dropUniqueIndex($syncedCol);
     }
     $this->query("ALTER TABLE {$name} MODIFY {$sql}");
     /* Drop/Add UNIQUE if needed */
     if (!$syncedCol->isUnique() && $column->isUnique()) {
         $this->addUniqueIndex($column);
     }
     /* Drop PRIMARY if needed (It gets added through getColumnSql() and the PRIMARY KEY addon) */
     if ($syncedCol->isPrimary() && !$column->isPrimary()) {
         $this->query("ALTER TABLE {$name} DROP PRIMARY KEY");
     }
     /* Add INDEX if needed */
     if (!$syncedCol->isIndex() && $column->isIndex()) {
         $this->addIndex($column);
     }
     /* Drop/Add CONSTRAINTS if needed */
     $syncedRef = $syncedCol->getReference();
     $colRef = $column->getReference();
     $refChanged = $colRef && $syncedRef && !$colRef->equals($syncedRef);
     if ($syncedRef && !$colRef || $colRef && !$syncedRef || $refChanged) {
         if ($syncedRef && !$colRef || $refChanged) {
             $this->dropConstraint($syncedCol);
         }
         if ($colRef && !$syncedRef || $refChanged) {
             $this->addConstraint($column);
         }
     }
     /* Drop INDEX if needed */
     if ($syncedCol->isIndex() && !$column->isIndex()) {
         $this->dropIndex($column);
     }
     return $this;
 }