MSSQL/DDL.pm

filter: Statementstmt 22.9% 31 / 135 Branchbran 6.9% 5 / 72 Conditioncond 7.5% 3 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 63.6% 7 / 11 Podpod 100% 1 / 1 totaltotal 16.6% 47 / 283 CC 61 SCAR 61.6
CC
61
Cov
16%
install_ddl
930.0
_topo_sort_sources
306.0
_resolve_table_name
30.0
CRAP 471.4
↑ Summary MSSQL/Introspect/Columns.pm »
1 package DBIO::MSSQL::DDL;
2 # ABSTRACT: Generate MSSQL DDL from DBIO Result classes
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our @EXPORT_OK = qw(_mssql_column_type _quote_ident);
9  
10 3 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DBIO::Schema::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DBIO::MSSQL::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 my $ADAPTER = DBIO::MSSQL::Adapter->new;
15  
16  
17  
18 0 ▶sub install_ddl {
Subroutine install_ddl: not called CC=30 SCAR=68.4
Pod documented
19 0 my ($class, $schema) = @_;
20  
21 0 my @stmts;
22 my %seen_table;
23 0 my @view_stmts;
24 0 my %seen_view;
25  
26 0 for my $source_name (_topo_sort_sources($schema)) {
27 0 my $source = $schema->source($source_name);
28 0 my $result_class = $source->result_class;
29 # Views: emit CREATE VIEW after all tables; skip virtual views.
30 0 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 0 0 0
31 0 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 0 0 0
Condition0%0/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
000
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table0%0/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
32 0 my $vname = _resolve_table_name($source->name);
33 0 ▶ next if !defined $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $vname or $seen_view{$vname}++ 0 0 0
Condition0%0/3
not defined $vname or $seen_view{$vname}++
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $vname or $seen_view{$vname}++
not defined $vname $seen_view{$vname}++
Truth table0%0/3
not(A) or B
A= not defined $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
34 0 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 0 0 0
35 0 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 0 0
Condition0%0/3
defined $def and length $def
!ll&&!rl&&r
000
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table0%0/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
36 0 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _quote_ident($vname), $def;
37 0 next;
38 }
39  
40 0 my $table_name = _resolve_table_name($source->name);
41  
42 # Virtual / view source whose name is inline SQL: skip here.
43 0 ▶ next unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 0 0
44  
45 # Multiple Result classes may share one physical table.
46 0 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 0 0
47  
48 0 my @col_defs;
49 my %is_pk;
50 0 my @pk_cols = $source->primary_columns;
51 0 @is_pk{@pk_cols} = (1) x @pk_cols;
52  
53 0 for my $col_name ($source->columns) {
54 0 my $info = $source->column_info($col_name);
55 0 my $type = _mssql_column_type($info);
56  
57 0 my $def = sprintf ' %s %s', _quote_ident($col_name), $type;
58  
59 0 ▶ $def .= ' NOT NULL' if defined $info->{is_nullable} && !$info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"is_nullable"} and not $info->{"is_nullable"} 0 0 0
Condition0%0/3
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
defined $info->{"is_nullable"} not $info->{"is_nullable"}
Truth table0%0/3
A and not(B)
A= defined $info->{"is_nullable"}B= not $info->{"is_nullable"}
ABresult
0X0
100
111
60  
61 0 ▶ if ($info->{is_auto_increment}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'is_auto_increment'}) { } 0 0 0
elsif (defined $info->{'default_value'}) { } 0 0 0
62 0 $def .= ' IDENTITY(1,1)';
63 } elsif (defined $info->{default_value}) {
64 0 my $dv = $info->{default_value};
65 0 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 0 0 0
66 0 $def .= " DEFAULT $$dv";
67 } else {
68 0 $def .= " DEFAULT $dv";
69 }
70 }
71  
72 0 push @col_defs, $def;
73 }
74  
75 0 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 0 0 0
76 push @col_defs, sprintf ' PRIMARY KEY (%s)',
77 0 join(', ', map { _quote_ident($_) } @pk_cols);
78 }
79  
80 0 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 0 0 0
81 0 my %uniques = $source->unique_constraints;
82 0 for my $uname (sort keys %uniques) {
83 0 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 0 0 0
84 0 my $cols = $uniques{$uname};
85 push @col_defs, sprintf ' UNIQUE (%s)',
86 0 join(', ', map { _quote_ident($_) } @$cols);
87 }
88 }
89  
90 0 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);",
91 _quote_ident($table_name), join(",\n", @col_defs);
92  
93 # Standalone indexes declared via mssql_indexes class method.
94 0 ▶ if ($result_class->can('mssql_indexes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("mssql_indexes")) 0 0 0
95 0 my $indexes = $result_class->mssql_indexes;
96 0 for my $idx_name (sort keys %$indexes) {
97 0 my $idx = $indexes->{$idx_name};
98 0 ▶ my $unique = $idx->{unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'unique'} ? : 0 0 0
99 0 ▶ my $kind = $idx->{kind} || '';
Condition0%0/2
$idx->{'kind'} || ''
l!l
00
MC/DC0%0/1
$idx->{'kind'} || ''
$idx->{'kind'}
Truth table0%0/2
A
A= $idx->{'kind'}
Aresult
11
00
100 0 ▶ my $kind_sql = $kind eq 'clustered' ? 'CLUSTERED' : $kind eq 'nonclustered' ? 'NONCLUSTERED' : '';
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'clustered' ? : 0 0 0
$kind eq 'nonclustered' ? : 0 0 0
101 my $columns = join ', ',
102 0 ▶ map { _quote_ident($_) } @{ $idx->{columns} // [] };
Condition0%0/2
$idx->{"columns"} // []
l!l
00
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table0%0/2
A
A= $idx->{"columns"}
Aresult
11
00
103 0 ▶ my $sql = sprintf 'CREATE %sINDEX %s ON %s %s(%s)',
Branch: 1 branch
BranchTrueFalseTotal
$kind_sql ? : 0 0 0
104 $unique, _quote_ident($idx_name),
105 _quote_ident($table_name), $kind_sql ? "$kind_sql " : '', $columns;
106 0 push @stmts, "$sql;";
107 }
108 }
109 }
110  
111 0 return join "
112  
113 ", @stmts, @view_stmts;
114 }
115  
116 0 ▶sub _resolve_table_name {
Subroutine _resolve_table_name: not called CC=5 SCAR=34.0
117 0 my ($name) = @_;
118 0 ▶ return $name unless ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name 0 0 0
119 0 ▶ return undef unless ref $name eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name eq "SCALAR" 0 0 0
120 0 my $v = $$name;
121 0 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
122 0 ▶ return $v if $v =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A\w+\z/ 0 0 0
123 0 return undef;
124 }
125  
126 0 ▶sub _topo_sort_sources {
Subroutine _topo_sort_sources: not called CC=17 SCAR=57.2
127 0 my ($schema) = @_;
128  
129 0 my %deps;
130 my %by_table;
131 0 my @sources = sort $schema->sources;
132  
133 0 for my $name (@sources) {
134 0 my $s = $schema->source($name);
135 0 my $t = _resolve_table_name($s->name);
136 0 ▶ next unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 0 0
137 0 ▶ $by_table{$t} //= $name;
Condition0%0/3
$by_table{$t} //= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$by_table{$t} //= $name
$by_table{$t} $name
Truth table0%0/3
A //= B
A= $by_table{$t}B= $name
ABresult
1X1
011
000
138 }
139  
140 0 for my $name (@sources) {
141 0 my $s = $schema->source($name);
142 0 ▶ next unless defined _resolve_table_name($s->name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined _resolve_table_name($s->name) 0 0 0
143 0 ▶ $deps{$name} ||= {};
Condition0%0/2
$deps{$name} ||= {}
l!l
00
MC/DC0%0/1
$deps{$name} ||= {}
$deps{$name}
Truth table0%0/2
A
A= $deps{$name}
Aresult
11
00
144 0 for my $rel ($s->relationships) {
145 0 my $info = $s->relationship_info($rel);
146 next unless $info && $info->{attrs}
147 0 ▶ && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 0 0 0
Condition0%0/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
000
$info and $info->{"attrs"}
!ll&&!rl&&r
000
MC/DC0%0/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table0%0/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
148 0 my $foreign = $info->{class};
149 0 my $fs = eval { $schema->source($foreign) }
150 0 ▶ // eval { $schema->source($foreign =~ s/.*:://r) };
Condition0%0/3
eval { // eval {
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
eval { do { $schema->source($foreign) } } eval { do { $schema->source($foreign =~ s/.*:://r) } }
Truth table0%0/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
151 0 ▶ next unless $fs;
Branch: 1 branch
BranchTrueFalseTotal
unless $fs 0 0 0
152 0 my $ft = _resolve_table_name($fs->name);
153 0 ▶ next unless defined $ft;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ft 0 0 0
154 0 my $owner = $by_table{$ft};
155 0 ▶ next unless $owner;
Branch: 1 branch
BranchTrueFalseTotal
unless $owner 0 0 0
156 0 ▶ next if $owner eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $owner eq $name 0 0 0
157 0 $deps{$name}{$owner} = 1;
158 }
159 }
160  
161 0 my @out;
162 my %visited;
163 0 my $visit;
164 0 ▶ $visit = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
165 0 my ($n) = @_;
166 0 ▶ return if $visited{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$n}++ 0 0 0
167 0 ▶ for my $d (sort keys %{ $deps{$n} || {} }) {
Condition0%0/2
$deps{$n} || {}
l!l
00
MC/DC0%0/1
$deps{$n} || {}
$deps{$n}
Truth table0%0/2
A
A= $deps{$n}
Aresult
11
00
168 0 $visit->($d);
169 }
170 0 push @out, $n;
171 0 };
172 0 $visit->($_) for @sources;
173 0 return @out;
174 }
175  
176 7 ▶sub _mssql_column_type {
Subroutine _mssql_column_type: called CC=9 SCAR=27.1
177 7 my ($info) = @_;
178 7 ▶ my $type = $info->{data_type} // 'nvarchar';
Condition50%1/2
$info->{"data_type"} // "nvarchar"
l!l
70
MC/DC0%0/1
$info->{"data_type"} // "nvarchar"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
179  
180 # Pre-parameterized types pass through (e.g. varchar(255), numeric(10,2)).
181 7 ▶ return $type if $type =~ /\(.+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)$/ 0 7 7
182  
183 # Portable base types: delegate to the adapter (single source of truth,
184 # shared shape with DBIO::MySQL / DBIO::PostgreSQL). Introspected live
185 # types arrive as dialect names (int, nvarchar, datetime, ...) which are
186 # not base-type names, so they fall through to the alias map below.
187 7 ▶ if (DBIO::Schema::Type::is_base_type(lc $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DBIO::Schema::Type::is_base_type(lc $type)) 0 7 7
188 0 my $canon = eval { DBIO::Schema::Type::canonical_column('_', $info) };
189 0 ▶ return $ADAPTER->to_native($canon) if $canon;
Branch: 1 branch
BranchTrueFalseTotal
if $canon 0 0 0
190 }
191  
192 # Dialect / alias names only (the eight base-type names are handled
193 # above). Maps legacy and cross-engine spellings to MSSQL natives.
194 7 my %type_map = (
195 # integers
196 tinyint => 'tinyint',
197 smallint => 'smallint',
198 int => 'int',
199 bigint => 'bigint',
200 serial => 'int',
201 bigserial => 'bigint',
202  
203 # floats / decimals
204 real => 'real',
205 float => 'float',
206 'double precision'=> 'float',
207 decimal => 'numeric',
208  
209 # strings
210 string => 'nvarchar',
211 varchar => 'nvarchar',
212 ntext => 'ntext',
213  
214 # booleans
215 bool => 'bit',
216  
217 # blobs
218 bytea => 'varbinary',
219 tinyblob => 'varbinary',
220 mediumblob => 'varbinary',
221 longblob => 'varbinary',
222 binary => 'binary',
223 varbinary => 'varbinary',
224  
225 # temporal
226 date => 'date',
227 time => 'time',
228 datetime => 'datetime',
229 timestamptz => 'datetimeoffset',
230 'timestamp with time zone' => 'datetimeoffset',
231 smalldatetime => 'smalldatetime',
232 );
233  
234 7 ▶ my $mapped = $type_map{ lc $type } // $type;
Condition33%1/3
$type_map{lc $type} // $type
l!l&&r!l&&!r
700
MC/DC0%0/2
$type_map{lc $type} // $type
$type_map{lc $type} $type
Truth table33%1/3
A // B
A= $type_map{lc $type}B= $type
ABresult
1X1
011
000
235  
236 # Append length to types that take one (char/varchar/binary families).
237 # MSSQL reports nvarchar(max)/varbinary(max) as size -1, so only emit a
238 # length for positive sizes.
239 7 ▶ if ($mapped =~ /^(?:n?char|n?varchar|binary|varbinary)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped =~ /^(?:n?char|n?varchar|binary|varbinary)$/i) 3 4 7
240 3 my $size = $info->{size};
241 3 ▶ return "$mapped($size)" if defined $size && $size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and $size > 0 3 0 3
Condition33%1/3
defined $size and $size > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $size and $size > 0
defined $size $size > 0
Truth table33%1/3
A and B
A= defined $sizeB= $size > 0
ABresult
0X0
100
111
242 }
243  
244 4 return $mapped;
245 }
246  
247 3 1;
248  
249 __END__
250  
251 =pod
252  
253 =encoding UTF-8
254  
255 =head1 NAME
256  
257 DBIO::MSSQL::DDL - Generate MSSQL DDL from DBIO Result classes
258  
259 =head1 VERSION
260  
261 version 0.900001
262  
263 =head1 DESCRIPTION
264  
265 C<DBIO::MSSQL::DDL> generates a MSSQL DDL script from a L<DBIO::Schema>
266 class hierarchy. It is the desired-state side of the test-deploy-and-
267 compare strategy used by L<DBIO::MSSQL::Deploy>.
268  
269 my $ddl = DBIO::MSSQL::DDL->install_ddl($schema_class_or_instance);
270  
271 The output is plain SQL, suitable for executing one statement at a time
272 against a fresh MSSQL database. Emits C<CREATE TABLE> (inline columns,
273 primary key, unique, foreign keys) and C<CREATE INDEX>.
274  
275 =head1 METHODS
276  
277 =head2 install_ddl
278  
279 my $ddl = DBIO::MSSQL::DDL->install_ddl($schema);
280  
281 Returns the full installation DDL as a single string.
282  
283 =head1 AUTHOR
284  
285 DBIO & DBIx::Class Authors
286  
287 =head1 COPYRIGHT AND LICENSE
288  
289 Copyright (C) 2026 DBIO Authors
290 Portions Copyright (C) 2005-2025 DBIx::Class Authors
291 Based on DBIx::Class, heavily modified.
292  
293 This is free software; you can redistribute it and/or modify it under
294 the same terms as the Perl 5 programming language system itself.
295  
296 =cut
↑ Summary MSSQL/Introspect/Columns.pm »