Commit | Line | Data |
1ea76bff |
1 | package SQL::Translator::Generator::DDL::SQLServer; |
2 | |
3 | use Moo; |
4 | use SQL::Translator::Generator::Utils; |
5 | |
6 | with 'SQL::Translator::Generator::Role::DDL'; |
7 | |
8 | sub _build_shim { SQL::Translator::Generator::Utils->new( quote_chars => [qw( [ ] )] ) } |
9 | |
f699ffaa |
10 | sub _build_numeric_types { |
11 | +{ |
12 | int => 1, |
13 | } |
14 | } |
15 | |
16 | sub _build_unquoted_defaults { |
17 | +{ |
18 | NULL => 1, |
19 | } |
20 | } |
21 | |
1ea76bff |
22 | sub _build_type_map { |
23 | +{ |
24 | date => 'datetime', |
25 | 'time' => 'datetime', |
26 | } |
27 | } |
28 | |
29 | has sizeless_types => ( |
30 | is => 'ro', |
31 | builder => '_build_sizeless_types', |
32 | ); |
33 | |
34 | sub _build_sizeless_types { |
35 | +{ map { $_ => 1 } |
36 | qw( tinyint smallint int integer bigint text bit image datetime ) } |
37 | } |
38 | |
39 | sub field { |
40 | my ($self, $field) = @_; |
41 | |
42 | return join ' ', $self->field_name($field), ($self->field_type($field)||die 'type is required'), |
43 | $self->field_autoinc($field), |
44 | $self->field_nullable($field), |
45 | $self->field_default($field), |
46 | } |
47 | |
48 | sub field_type_size { |
49 | my ($self, $field) = @_; |
50 | |
51 | ($field->size && !$self->sizeless_types->{$field->data_type} |
52 | ? '(' . $field->size . ')' |
53 | : '' |
54 | ) |
55 | } |
56 | |
57 | sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) } |
58 | |
e6fcfabf |
59 | sub primary_key_constraint { |
60 | 'CONSTRAINT ' . |
61 | $_[0]->shim->quote($_[1]->name || $_[1]->table->name . '_pk') . |
62 | ' PRIMARY KEY (' . |
63 | join( ', ', map $_[0]->shim->quote($_), $_[1]->fields ) . |
64 | ')' |
65 | } |
66 | |
67 | sub index { |
68 | 'CREATE INDEX ' . |
69 | $_[0]->shim->quote($_[1]->name || $_[1]->table->name . '_idx') . |
70 | ' ON ' . $_[0]->shim->quote($_[1]->table->name) . |
71 | ' (' . join( ', ', map $_[0]->shim->quote($_), $_[1]->fields ) . ');' |
72 | } |
73 | |
2363a629 |
74 | sub unique_constraint_single { |
75 | my ($self, $constraint) = @_; |
76 | |
77 | 'CONSTRAINT ' . |
78 | $self->unique_constraint_name($constraint) . |
79 | ' UNIQUE (' . join( ', ', $constraint->fields ) . ')' |
80 | } |
81 | |
82 | sub unique_constraint_name { |
83 | my ($self, $constraint) = @_; |
84 | $self->shim->quote($constraint->name || $constraint->table->name . '_uc' ) |
85 | } |
86 | |
87 | sub unique_constraint_multiple { |
88 | my ($self, $constraint) = @_; |
89 | |
90 | 'CREATE UNIQUE NONCLUSTERED INDEX ' . |
91 | $self->unique_constraint_name($constraint) . |
92 | ' ON ' . $self->shim->quote($constraint->table->name) . ' (' . |
93 | join( ', ', $constraint->fields ) . ')' . |
94 | ' WHERE ' . join( ' AND ', |
95 | map $self->shim->quote($_->name) . ' IS NOT NULL', |
96 | grep { $_->is_nullable } $constraint->fields ) . ';' |
97 | } |
98 | |
1ea76bff |
99 | 1; |
100 | |