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 ' . |
38d0ddf0 |
61 | $_[0]->quote($_[1]->name || $_[1]->table->name . '_pk') . |
e6fcfabf |
62 | ' PRIMARY KEY (' . |
38d0ddf0 |
63 | join( ', ', map $_[0]->quote($_), $_[1]->fields ) . |
e6fcfabf |
64 | ')' |
65 | } |
66 | |
67 | sub index { |
68 | 'CREATE INDEX ' . |
38d0ddf0 |
69 | $_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') . |
70 | ' ON ' . $_[0]->quote($_[1]->table->name) . |
71 | ' (' . join( ', ', map $_[0]->quote($_), $_[1]->fields ) . ');' |
e6fcfabf |
72 | } |
73 | |
2363a629 |
74 | sub unique_constraint_single { |
75 | my ($self, $constraint) = @_; |
76 | |
77 | 'CONSTRAINT ' . |
78 | $self->unique_constraint_name($constraint) . |
38d0ddf0 |
79 | ' UNIQUE (' . join( ', ', map $self->quote($_), $constraint->fields ) . ')' |
2363a629 |
80 | } |
81 | |
82 | sub unique_constraint_name { |
83 | my ($self, $constraint) = @_; |
38d0ddf0 |
84 | $self->quote($constraint->name || $constraint->table->name . '_uc' ) |
2363a629 |
85 | } |
86 | |
87 | sub unique_constraint_multiple { |
88 | my ($self, $constraint) = @_; |
89 | |
90 | 'CREATE UNIQUE NONCLUSTERED INDEX ' . |
91 | $self->unique_constraint_name($constraint) . |
38d0ddf0 |
92 | ' ON ' . $self->quote($constraint->table->name) . ' (' . |
2363a629 |
93 | join( ', ', $constraint->fields ) . ')' . |
94 | ' WHERE ' . join( ' AND ', |
38d0ddf0 |
95 | map $self->quote($_->name) . ' IS NOT NULL', |
2363a629 |
96 | grep { $_->is_nullable } $constraint->fields ) . ';' |
97 | } |
98 | |
38d0ddf0 |
99 | sub foreign_key_constraint { |
100 | my ($self, $constraint) = @_; |
101 | |
102 | my $on_delete = uc ($constraint->on_delete || ''); |
103 | my $on_update = uc ($constraint->on_update || ''); |
104 | |
105 | # The default implicit constraint action in MSSQL is RESTRICT |
106 | # but you can not specify it explicitly. Go figure :) |
107 | for (map uc $_ || '', $on_delete, $on_update) { |
108 | undef $_ if $_ eq 'RESTRICT' |
109 | } |
110 | |
111 | 'ALTER TABLE ' . $self->quote($constraint->table->name) . |
112 | ' ADD CONSTRAINT ' . |
113 | $self->quote($constraint->name || $constraint->table->name . '_fk') . |
114 | ' FOREIGN KEY' . |
115 | ' (' . join( ', ', map $self->quote($_), $constraint->fields ) . ') REFERENCES '. |
116 | $self->quote($constraint->reference_table) . |
117 | ' (' . join( ', ', map $self->quote($_), $constraint->reference_fields ) . ')' |
118 | . ( |
119 | $on_delete && $on_delete ne "NO ACTION" |
120 | ? ' ON DELETE ' . $on_delete |
121 | : '' |
122 | ) . ( |
123 | $on_update && $on_update ne "NO ACTION" |
124 | ? ' ON UPDATE ' . $on_update |
125 | : '' |
126 | ) . ';'; |
127 | } |
128 | |
280d92ba |
129 | sub enum_constraint_name { |
130 | my ($self, $field_name) = @_; |
131 | $self->quote($field_name . '_chk' ) |
132 | } |
133 | |
134 | sub enum_constraint { |
135 | my ( $self, $field_name, $vals ) = @_; |
136 | |
137 | return ( |
138 | 'CONSTRAINT ' . $self->enum_constraint_name($field_name) . |
139 | ' CHECK (' . $self->quote($field_name) . |
140 | ' IN (' . join( ',', map qq('$_'), @$vals ) . '))' |
141 | ) |
142 | } |
143 | |
1ea76bff |
144 | 1; |
145 | |