3
namespace Upside\Db\Schema\Compiler;
5
use Upside\Db\Schema\AlterTable;
6
use Upside\Db\Schema\BaseColumn;
7
use Upside\Db\Schema\Compiler;
8
use Upside\Db\Schema\CreateTable;
10
class SQLServer extends Compiler
12
protected string $wrapper = '[%s]';
15
protected array $modifiers = ['nullable', 'default', 'autoincrement'];
16
protected string $autoincrement = 'IDENTITY';
18
protected function handle_type_integer(BaseColumn $column): string
20
return match ($column->get('size', 'normal')) {
22
'small' => 'SMALLINT',
23
'medium' => 'INTEGER',
29
protected function handle_type_decimal(BaseColumn $column): string
31
if (null !== $l = $column->get('length')) {
32
if (null === $p = $column->get('precision')) {
33
return 'DECIMAL (' . $this->value($l) . ')';
36
return 'DECIMAL (' . $this->value($l) . ', ' . $this->value($p) . ')';
42
protected function handle_type_boolean(BaseColumn $column): string
47
protected function handle_type_string(BaseColumn $column): string
49
return 'NVARCHAR(' . $this->value($column->get('length', 255)) . ')';
52
protected function handle_type_fixed(BaseColumn $column): string
54
return 'NCHAR(' . $this->value($column->get('length', 255)) . ')';
57
protected function handle_type_text(BaseColumn $column): string
59
return 'NVARCHAR(max)';
62
protected function handle_type_binary(BaseColumn $column): string
64
return 'VARBINARY(max)';
67
protected function handle_type_timestamp(BaseColumn $column): string
72
protected function handle_rename_column(AlterTable $table, $data): string
74
/** @var BaseColumn $column */
75
$column = $data['column'];
77
return 'sp_rename ' . $this->wrap($table->get_table_name()) . '.' . $this->wrap($data['from']) . ', '
78
. $this->wrap($column->get_name()) . ', COLUMN';
81
protected function handle_engine(CreateTable $schema): string
86
public function rename_table(string $current, string $new): array
89
'sql' => 'sp_rename ' . $this->wrap($current) . ', ' . $this->wrap($new),
94
public function current_database(string $dsn): array
97
'sql' => 'SELECT SCHEMA_NAME()',
102
public function get_columns(string $database, string $table): array
104
$sql = 'SELECT ' . $this->wrap('column_name') . ' AS ' . $this->wrap('name')
105
. ', ' . $this->wrap('data_type') . ' AS ' . $this->wrap('type')
106
. ' FROM ' . $this->wrap('information_schema') . '.' . $this->wrap('columns')
107
. ' WHERE ' . $this->wrap('table_schema') . ' = ? AND ' . $this->wrap('table_name') . ' = ? '
108
. ' ORDER BY ' . $this->wrap('ordinal_position') . ' ASC';
112
'params' => [$database, $table],