add foreign_key_constraint
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / DDL / SQLServer.pm
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
10 sub _build_numeric_types {
11    +{
12       int => 1,
13    }
14 }
15
16 sub _build_unquoted_defaults {
17    +{
18       NULL => 1,
19    }
20 }
21
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
59 sub primary_key_constraint {
60   'CONSTRAINT ' .
61     $_[0]->quote($_[1]->name || $_[1]->table->name . '_pk') .
62     ' PRIMARY KEY (' .
63     join( ', ', map $_[0]->quote($_), $_[1]->fields ) .
64     ')'
65 }
66
67 sub index {
68   'CREATE INDEX ' .
69    $_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') .
70    ' ON ' . $_[0]->quote($_[1]->table->name) .
71    ' (' . join( ', ', map $_[0]->quote($_), $_[1]->fields ) . ');'
72 }
73
74 sub unique_constraint_single {
75   my ($self, $constraint) = @_;
76
77   'CONSTRAINT ' .
78    $self->unique_constraint_name($constraint) .
79    ' UNIQUE (' . join( ', ', map $self->quote($_), $constraint->fields ) . ')'
80 }
81
82 sub unique_constraint_name {
83   my ($self, $constraint) = @_;
84   $self->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->quote($constraint->table->name) . ' (' .
93    join( ', ', $constraint->fields ) . ')' .
94    ' WHERE ' . join( ' AND ',
95     map $self->quote($_->name) . ' IS NOT NULL',
96     grep { $_->is_nullable } $constraint->fields ) . ';'
97 }
98
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
129 1;
130