Skip to content

Commit

Permalink
Improving schema generation for SQLServer
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 12, 2014
1 parent 1d201c6 commit 24e3623
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Database/Schema/SqlserverSchema.php
Expand Up @@ -296,40 +296,44 @@ public function columnSql(Table $table, $name) {
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = [
'biginteger' => ' BIGINT',
'integer' => 'INTEGER',
'biginteger' => 'BIGINT',
'boolean' => ' BIT',
'binary' => ' BINARY',
'float' => ' FLOAT',
'decimal' => ' DECIMAL',
'text' => ' TEXT',
'text' => ' NVARCHAR(MAX)',
'date' => ' DATE',
'time' => ' TIME',
'datetime' => ' DATETIME',
'timestamp' => ' DATETIME',
'uuid' => 'UNIQUEIDENTIFIER'
];

if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}

if ($data['type'] === 'integer') {
$type = ' INTEGER';
if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
$out .= $type;
if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
unset($data['null'], $data['default']);
$out .= ' IDENTITY(1, 1)';
}
}

if ($data['type'] === 'string') {
$isFixed = !empty($data['fixed']);
$type = ' VARCHAR';
if ($isFixed) {
$type = ' CHAR';
}
if ($isFixed && isset($data['length']) && $data['length'] == 36) {
$type = ' UNIQUEIDENTIFIER';
$type = ' NVARCHAR';

if (!empty($data['fixed'])) {
$type = ' NCHAR';
}
$out .= $type;
if (isset($data['length']) && $data['length'] != 36) {
$out .= '(' . (int)$data['length'] . ')';

if (!isset($data['length'])) {
$data['length'] = 255;
}

$out .= sprintf('%s(%d)', $type, $data['length']);
}

if ($data['type'] === 'float' && isset($data['precision'])) {
Expand All @@ -345,14 +349,17 @@ public function columnSql(Table $table, $name) {
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}

if (isset($data['null']) && $data['null'] === true) {
$out .= ' DEFAULT NULL';
unset($data['default']);
}

if (isset($data['default']) && $data['type'] !== 'datetime') {
$default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']);
$out .= ' DEFAULT ' . $default;
}

return $out;
}

Expand Down

0 comments on commit 24e3623

Please sign in to comment.