/** * Generate the SQL fragment for a single column in a table. * * @param TableSchema $table The table instance the column is in. * @param string $name The name of the column. * @return string SQL fragment. */ public function columnSql(TableSchema $table, $name) { $data = $table->column($name); $out = $this->_driver->quoteIdentifier($name); $typeMap = ['integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', 'binary' => ' LONGBLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'text' => ' TEXT', 'date' => ' DATE', 'time' => ' TIME', 'datetime' => ' DATETIME', 'timestamp' => ' TIMESTAMP', 'uuid' => ' CHAR(36)']; $specialMap = ['string' => 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; } } $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; }