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

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