Exemplo n.º 1
0
 public function getColumnSql(Column $column)
 {
     $type = $column->getType();
     $maxLen = $column->getMaxLength();
     $allowed = $column->getAllowedValues();
     $optional = $column->isOptional();
     $default = $column->getDefaultValue();
     $primary = $column->isPrimary();
     $auto = $column->isAutoIncreased();
     if (!$type) {
         throw new Exception("Failed to put together column SQL: Column {$column} has no type specified");
     }
     $type = $this->mapColumnType($type);
     if ($type === 'varchar' && !$maxLen) {
         $type = 'text';
     }
     $sql = $this->quoteName($column);
     $sql .= ' ' . strtoupper($type);
     if ($maxLen) {
         $sql .= "({$maxLen})";
     } else {
         if (!empty($allowed)) {
             $sql .= '(' . implode(',', array_map([$this, 'encode'], $allowed)) . ')';
         }
     }
     if ($optional) {
         $sql .= ' NULL';
     } else {
         $sql .= ' NOT NULL';
     }
     if ($default) {
         if (strtolower($type) === 'timestamp') {
             switch (strtolower($default)) {
                 case 'currenttimestamp':
                 case 'onupdatecurrenttimestamp':
                     $sql .= ' DEFAULT CURRENT_TIMESTAMP';
                     break;
                 default:
                     $sql .= ' DEFAULT ' . $this->encode($default);
                     break;
             }
         } else {
             $sql .= ' DEFAULT ' . $this->encode($default);
         }
     }
     if ($primary) {
         $sql .= ' PRIMARY KEY';
     }
     if ($auto) {
         $sql .= ' AUTO_INCREMENT';
     }
     if (strtolower($type) === 'timestamp' && ($default && strtolower($default) === 'onupdatecurrenttimestamp')) {
         $sql .= ' ON UPDATE CURRENT_TIMESTAMP';
     }
     return $sql;
 }
Exemplo n.º 2
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;
 }