add foreign_key_constraint
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / DDL / SQLServer.pm
CommitLineData
1ea76bff 1package SQL::Translator::Generator::DDL::SQLServer;
2
3use Moo;
4use SQL::Translator::Generator::Utils;
5
6with 'SQL::Translator::Generator::Role::DDL';
7
8sub _build_shim { SQL::Translator::Generator::Utils->new( quote_chars => [qw( [ ] )] ) }
9
f699ffaa 10sub _build_numeric_types {
11 +{
12 int => 1,
13 }
14}
15
16sub _build_unquoted_defaults {
17 +{
18 NULL => 1,
19 }
20}
21
1ea76bff 22sub _build_type_map {
23 +{
24 date => 'datetime',
25 'time' => 'datetime',
26 }
27}
28
29has sizeless_types => (
30 is => 'ro',
31 builder => '_build_sizeless_types',
32);
33
34sub _build_sizeless_types {
35 +{ map { $_ => 1 }
36 qw( tinyint smallint int integer bigint text bit image datetime ) }
37}
38
39sub 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
48sub field_type_size {
49 my ($self, $field) = @_;
50
51 ($field->size && !$self->sizeless_types->{$field->data_type}
52 ? '(' . $field->size . ')'
53 : ''
54 )
55}
56
57sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) }
58
e6fcfabf 59sub 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
67sub 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 74sub 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
82sub unique_constraint_name {
83 my ($self, $constraint) = @_;
38d0ddf0 84 $self->quote($constraint->name || $constraint->table->name . '_uc' )
2363a629 85}
86
87sub 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 99sub 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
1ea76bff 1291;
130