Ejemplo 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;
 }