Example #1
0
 /**
  * {@inheritDoc}
  */
 public function columnSql(Table $table, $name)
 {
     $data = $table->column($name);
     $out = $this->_driver->quoteIdentifier($name);
     $typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)'];
     $specialMap = ['string' => true, 'text' => true, 'binary' => true];
     if (isset($typeMap[$data['type']])) {
         $out .= $typeMap[$data['type']];
     }
     if (isset($specialMap[$data['type']])) {
         switch ($data['type']) {
             case 'string':
                 $out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
                 if (!isset($data['length'])) {
                     $data['length'] = 255;
                 }
                 break;
             case 'text':
                 $isKnownLength = in_array($data['length'], Table::$columnLengths);
                 if (empty($data['length']) || !$isKnownLength) {
                     $out .= ' TEXT';
                     break;
                 }
                 if ($isKnownLength) {
                     $length = array_search($data['length'], Table::$columnLengths);
                     $out .= ' ' . strtoupper($length) . 'TEXT';
                 }
                 break;
             case 'binary':
                 $isKnownLength = in_array($data['length'], Table::$columnLengths);
                 if (empty($data['length']) || !$isKnownLength) {
                     $out .= ' BLOB';
                     break;
                 }
                 if ($isKnownLength) {
                     $length = array_search($data['length'], Table::$columnLengths);
                     $out .= ' ' . strtoupper($length) . 'BLOB';
                 }
                 break;
         }
     }
     $hasLength = ['integer', 'string'];
     if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
         $out .= '(' . (int) $data['length'] . ')';
     }
     $hasPrecision = ['float', 'decimal'];
     if (in_array($data['type'], $hasPrecision, true) && (isset($data['length']) || isset($data['precision']))) {
         $out .= '(' . (int) $data['length'] . ',' . (int) $data['precision'] . ')';
     }
     $hasUnsigned = ['float', 'decimal', 'integer', 'biginteger'];
     if (in_array($data['type'], $hasUnsigned, true) && isset($data['unsigned']) && $data['unsigned'] === true) {
         $out .= ' UNSIGNED';
     }
     if (isset($data['null']) && $data['null'] === false) {
         $out .= ' NOT NULL';
     }
     $addAutoIncrement = [$name] == (array) $table->primaryKey() && !$table->hasAutoIncrement();
     if (in_array($data['type'], ['integer', 'biginteger']) && ($data['autoIncrement'] === true || $addAutoIncrement)) {
         $out .= ' AUTO_INCREMENT';
     }
     if (isset($data['null']) && $data['null'] === true) {
         $out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
         unset($data['default']);
     }
     if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) {
         $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
         unset($data['default']);
     }
     if (isset($data['default']) && in_array($data['type'], ['timestamp', 'datetime']) && strtolower($data['default']) === 'current_timestamp') {
         $out .= ' DEFAULT CURRENT_TIMESTAMP';
         unset($data['default']);
     }
     if (isset($data['comment']) && $data['comment'] !== '') {
         $out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
     }
     return $out;
 }