blib/lib/SQL/Translator/Generator/DDL/SQLServer.pm

filter: Statementstmt 95.7% 68 / 71 Branchbran 64.2% 9 / 14 Conditioncond 53.3% 16 / 30 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 20 totaltotal 66.8% 119 / 178 CC 22 SCAR 18.2
CC
22
Cov
81%
foreign_key_constraint
20.2
drop_tables
4.1
constraints
3.1
CRAP 6.2
« blib/lib/SQL/Translator/Generator/Role/Quote.pm ↑ Summary blib/lib/SQL/Translator/Schema/Trigger.pm »
1 package SQL::Translator::Generator::DDL::SQLServer;
2  
3 =head1 NAME
4  
5 SQL::Translator::Generator::DDL::SQLServer - A Moo based MS SQL Server DDL
6 generation engine.
7  
8 =head1 DESCRIPTION
9  
10 I<documentation volunteers needed>
11  
12 =cut
13  
14 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 with 'SQL::Translator::Generator::Role::Quote';
18 3 with 'SQL::Translator::Generator::Role::DDL';
19  
20 120 ▶sub quote_chars { [qw([ ])] }
Subroutine quote_chars: called CC=1 SCAR=0.0
Pod undocumented
21 116 ▶sub name_sep {q(.)}
Subroutine name_sep: called CC=1 SCAR=0.0
Pod undocumented
22  
23 2 ▶sub _build_numeric_types {
Subroutine _build_numeric_types: called CC=1 SCAR=0.0
24 2 +{ int => 1, };
25 }
26  
27 0 ▶sub _build_unquoted_defaults {
Subroutine _build_unquoted_defaults: not called CC=1 SCAR=6.9
28 0 +{ NULL => 1, };
29 }
30  
31 4 ▶sub _build_type_map {
Subroutine _build_type_map: called CC=1 SCAR=0.0
32 +{
33 4 date => 'datetime',
34 'time' => 'datetime',
35 };
36 }
37  
38 3 ▶sub _build_sizeless_types {
Subroutine _build_sizeless_types: called CC=1 SCAR=0.0
39 3 +{ map { $_ => 1 } qw( tinyint smallint int integer bigint text bit image datetime ) };
40 }
41  
42 35 ▶sub field {
Subroutine field: called CC=2 SCAR=7.2
Pod undocumented
43 35 my ($self, $field) = @_;
44  
45 35 ▶ return join ' ', $self->field_name($field),
Condition50%1/2
$self->field_type($field) || die('type is required')
l!l
350
MC/DC0%0/1
$self->field_type($field) || die('type is required')
$self->field_type($field)
Truth table50%1/2
A
A= $self->field_type($field)
Aresult
11
00
46 ($self->field_type($field) || die 'type is required'),
47 $self->field_autoinc($field),
48 $self->field_nullable($field),
49 $self->field_default($field),;
50 }
51  
52 35 ▶sub field_autoinc { ($_[1]->is_auto_increment ? 'IDENTITY' : ()) }
Subroutine field_autoinc: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->is_auto_increment ? : 6 29 35
53  
54 7 ▶sub primary_key_constraint {
Subroutine primary_key_constraint: called CC=2 SCAR=6.9
Pod undocumented
55 7 ▶ 'CONSTRAINT '
Condition100%2/2
$_[1]->name || $_[1]->table->name . '_pk'
l!l
34
MC/DC100%1/1
$_[1]->name || $_[1]->table->name . '_pk'
$_[1]->name
Truth table100%2/2
A
A= $_[1]->name
Aresult
11
00
56 . $_[0]->quote($_[1]->name || $_[1]->table->name . '_pk')
57 . ' PRIMARY KEY ('
58 . join(', ', map $_[0]->quote($_), $_[1]->fields) . ')';
59 }
60  
61 4 ▶sub index {
Subroutine index: called CC=2 SCAR=7.6
Pod undocumented
62 4 ▶ 'CREATE INDEX '
Condition50%1/2
$_[1]->name || $_[1]->table->name . '_idx'
l!l
40
MC/DC0%0/1
$_[1]->name || $_[1]->table->name . '_idx'
$_[1]->name
Truth table50%1/2
A
A= $_[1]->name
Aresult
11
00
63 . $_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') . ' ON '
64 . $_[0]->quote($_[1]->table->name) . ' ('
65 . join(', ', map $_[0]->quote($_), $_[1]->fields) . ');';
66 }
67  
68 0 ▶sub unique_constraint_single {
Subroutine unique_constraint_single: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my ($self, $constraint) = @_;
70  
71 0 'CONSTRAINT '
72 . $self->unique_constraint_name($constraint)
73 . ' UNIQUE ('
74 . join(', ', map $self->quote($_), $constraint->fields) . ')';
75 }
76  
77 2 ▶sub unique_constraint_name {
Subroutine unique_constraint_name: called CC=2 SCAR=7.2
Pod undocumented
78 2 my ($self, $constraint) = @_;
79 2 ▶ $self->quote($constraint->name || $constraint->table->name . '_uc');
Condition50%1/2
$constraint->name || $constraint->table->name . '_uc'
l!l
20
MC/DC0%0/1
$constraint->name || $constraint->table->name . '_uc'
$constraint->name
Truth table50%1/2
A
A= $constraint->name
Aresult
11
00
80 }
81  
82 2 ▶sub unique_constraint_multiple {
Subroutine unique_constraint_multiple: called CC=1 SCAR=0.0
Pod undocumented
83 2 my ($self, $constraint) = @_;
84  
85 'CREATE UNIQUE NONCLUSTERED INDEX '
86 . $self->unique_constraint_name($constraint) . ' ON '
87 . $self->quote($constraint->table->name) . ' ('
88 . join(', ', map $self->quote($_), $constraint->fields) . ')'
89 . ' WHERE '
90 2 . join(' AND ', map $self->quote($_->name) . ' IS NOT NULL', grep { $_->is_nullable } $constraint->fields) . ';';
91 }
92  
93 4 ▶sub foreign_key_constraint {
Subroutine foreign_key_constraint: called CC=11 SCAR=30.0
Pod undocumented
94 4 my ($self, $constraint) = @_;
95  
96 4 ▶ my $on_delete = uc($constraint->on_delete || '');
Condition50%1/2
$constraint->on_delete || ''
l!l
04
MC/DC0%0/1
$constraint->on_delete || ''
$constraint->on_delete
Truth table50%1/2
A
A= $constraint->on_delete
Aresult
11
00
97 4 ▶ my $on_update = uc($constraint->on_update || '');
Condition50%1/2
$constraint->on_update || ''
l!l
04
MC/DC0%0/1
$constraint->on_update || ''
$constraint->on_update
Truth table50%1/2
A
A= $constraint->on_update
Aresult
11
00
98  
99 # The default implicit constraint action in MSSQL is RESTRICT
100 # but you can not specify it explicitly. Go figure :)
101 4 ▶ for (map uc $_ || '', $on_delete, $on_update) {
Condition50%1/2
uc $_ || ''
l!l
08
MC/DC0%0/1
uc $_ || ''
uc $_
Truth table50%1/2
A
A= uc $_
Aresult
11
00
102 8 ▶ undef $_ if $_ eq 'RESTRICT';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "RESTRICT" 0 8 8
103 }
104  
105 'ALTER TABLE '
106 4 ▶ . $self->quote($constraint->table->name)
Branch: 2 branches
BranchTrueFalseTotal
$on_delete && $on_delete ne 'NO ACTION' ? : 0 4 4
$on_update && $on_update ne 'NO ACTION' ? : 0 4 4
Condition37%3/8
$constraint->name || $constraint->table->name . '_fk'
l!l
40
$on_delete and $on_delete ne "NO ACTION"
!ll&&!rl&&r
400
$on_update and $on_update ne "NO ACTION"
!ll&&!rl&&r
400
MC/DC0%0/1
$constraint->name || $constraint->table->name . '_fk'
$constraint->name
MC/DC0%0/2
$on_delete and $on_delete ne "NO ACTION"
$on_delete $on_delete ne "NO ACTION"
MC/DC0%0/2
$on_update and $on_update ne "NO ACTION"
$on_update $on_update ne "NO ACTION"
Truth table50%1/2
A
A= $constraint->name
Aresult
11
00
Truth table33%1/3
A and B
A= $on_deleteB= $on_delete ne "NO ACTION"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $on_updateB= $on_update ne "NO ACTION"
ABresult
0X0
100
111
107 . ' ADD CONSTRAINT '
108 . $self->quote($constraint->name || $constraint->table->name . '_fk')
109 . ' FOREIGN KEY' . ' ('
110 . join(', ', map $self->quote($_), $constraint->fields)
111 . ') REFERENCES '
112 . $self->quote($constraint->reference_table) . ' ('
113 . join(', ', map $self->quote($_), $constraint->reference_fields) . ')'
114 . (
115 $on_delete && $on_delete ne "NO ACTION" ? ' ON DELETE ' . $on_delete
116 : ''
117 )
118 . (
119 $on_update && $on_update ne "NO ACTION" ? ' ON UPDATE ' . $on_update
120 : ''
121 ) . ';';
122 }
123  
124 1 ▶sub enum_constraint_name {
Subroutine enum_constraint_name: called CC=1 SCAR=0.0
Pod undocumented
125 1 my ($self, $field_name) = @_;
126 1 $self->quote($field_name . '_chk');
127 }
128  
129 1 ▶sub enum_constraint {
Subroutine enum_constraint: called CC=1 SCAR=0.0
Pod undocumented
130 1 my ($self, $field_name, $vals) = @_;
131  
132 1 return ('CONSTRAINT '
133 . $self->enum_constraint_name($field_name)
134 . ' CHECK ('
135 . $self->quote($field_name) . ' IN ('
136 . join(',', map $self->quote_string($_), @$vals)
137 . '))');
138 }
139  
140 10 ▶sub constraints {
Subroutine constraints: called CC=3 SCAR=11.2
Pod undocumented
141 10 my ($self, $table) = @_;
142  
143 (
144 map $self->enum_constraint($_->name, { $_->extra }->{list} || []),
145 33 grep { 'enum' eq lc $_->data_type } $table->get_fields
146 ),
147  
148 13 (map $self->primary_key_constraint($_), grep { $_->type eq PRIMARY_KEY } $table->get_constraints),
149  
150 (
151 map $self->unique_constraint_single($_),
152 grep {
153 10 ▶ $_->type eq UNIQUE
Condition50%1/2
{$_->extra}->{'list'} || []
l!l
10
MC/DC0%0/1
{$_->extra}->{'list'} || []
{$_->extra}->{'list'}
Truth table50%1/2
A
A= {$_->extra}->{'list'}
Aresult
11
00
154 13 ▶ && !grep { $_->is_nullable }
Condition66%2/3
$_->type eq 'UNIQUE' && !grep({$_->is_nullable;} $_->fields)
!ll&&!rl&&r
1120
MC/DC0%0/2
$_->type eq 'UNIQUE' && !grep({$_->is_nullable;} $_->fields)
$_->type eq 'UNIQUE' !grep({$_->is_nullable;} $_->fields)
Truth table66%2/3
A && not(B)
A= $_->type eq 'UNIQUE'B= !grep({$_->is_nullable;} $_->fields)
ABresult
0X0
100
111
155 $_->fields
156 } $table->get_constraints
157 ),
158 ;
159 }
160  
161 10 ▶sub table {
Subroutine table: called CC=1 SCAR=0.0
Pod undocumented
162 10 my ($self, $table) = @_;
163 join("\n", $self->table_comments($table), '')
164 . join("\n\n",
165 'CREATE TABLE '
166 . $self->quote($table->name) . " (\n"
167 10 . join(",\n", map {" $_"} $self->fields($table), $self->constraints($table),) . "\n);",
168 $self->unique_constraints_multiple($table), $self->indices($table),);
169 }
170  
171 10 ▶sub unique_constraints_multiple {
Subroutine unique_constraints_multiple: called CC=2 SCAR=7.0
Pod undocumented
172 10 my ($self, $table) = @_;
173 (
174 map $self->unique_constraint_multiple($_),
175 grep {
176 10 $_->type eq UNIQUE
177 13 ▶ && grep { $_->is_nullable }
Condition66%2/3
$_->type eq 'UNIQUE' && grep({$_->is_nullable;} $_->fields)
!ll&&!rl&&r
1102
MC/DC50%1/2
$_->type eq 'UNIQUE' && grep({$_->is_nullable;} $_->fields)
$_->type eq 'UNIQUE' grep({$_->is_nullable;} $_->fields)
Truth table66%2/3
A && B
A= $_->type eq 'UNIQUE'B= grep({$_->is_nullable;} $_->fields)
ABresult
0X0
100
111
178 $_->fields
179 } $table->get_constraints
180 );
181 }
182  
183 8 ▶sub drop_table {
Subroutine drop_table: called CC=1 SCAR=0.0
Pod undocumented
184 8 my ($self, $table) = @_;
185 8 my $name = $table->name;
186 8 my $q_name = $self->quote($name);
187 8 "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U')" . " DROP TABLE $q_name;";
188 }
189  
190 8 ▶sub remove_table_constraints {
Subroutine remove_table_constraints: called CC=1 SCAR=0.0
Pod undocumented
191 8 my ($self, $table) = @_;
192 8 my $name = $table->name;
193 8 my $q_name = $self->quote($name);
194 8 "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U')"
195 . " ALTER TABLE $q_name NOCHECK CONSTRAINT all;";
196 }
197  
198 3 ▶sub drop_tables {
Subroutine drop_tables: called CC=4 SCAR=14.0
Pod undocumented
199 3 my ($self, $schema) = @_;
200  
201 3 ▶ if ($self->add_drop_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add_drop_table) 2 1 3
202 2 my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
203 2 ▶ return join "\n",
Branch: 2 branches
BranchTrueFalseTotal
$self->add_comments ? : 0 2 2
$self->add_comments ? : 0 2 2
204 (
205 (
206 $self->add_comments
207 ? ('--', '-- Turn off constraints', '--', '',)
208 : ()
209 ),
210 (map $self->remove_table_constraints($_), @tables),
211 ($self->add_comments ? ('--', '-- Drop tables', '--', '',) : ()),
212 (map $self->drop_table($_), @tables),
213 );
214 }
215 1 return '';
216 }
217  
218 3 ▶sub foreign_key_constraints {
Subroutine foreign_key_constraints: called CC=1 SCAR=0.0
Pod undocumented
219 3 my ($self, $schema) = @_;
220 (
221 3 map $self->foreign_key_constraint($_), grep { $_->type eq FOREIGN_KEY }
222 map $_->get_constraints, $schema->get_tables
223 );
224 }
225  
226 3 ▶sub schema {
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
227 3 my ($self, $schema) = @_;
228  
229 $self->header_comments
230 . $self->drop_tables($schema)
231 3 . join("\n\n", map $self->table($_), grep { $_->name } $schema->get_tables) . "\n"
232 . join "\n", $self->foreign_key_constraints($schema);
233 }
234  
235 3 1;
236  
237 =head1 AUTHORS
238  
239 See the included AUTHORS file:
240 L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
241  
242 =head1 COPYRIGHT
243  
244 Copyright (c) 2012 the SQL::Translator L</AUTHORS> as listed above.
245  
246 =head1 LICENSE
247  
248 This code is free software and may be distributed under the same terms as Perl
249 itself.
250  
251 =cut
« blib/lib/SQL/Translator/Generator/Role/Quote.pm ↑ Summary blib/lib/SQL/Translator/Schema/Trigger.pm »