Sybase/DDL.pm

filter: Statementstmt 45.0% 36 / 80 Branchbran 53.1% 34 / 64 Conditioncond 64.0% 32 / 50 MC/DCmcdc 59.3% 19 / 32 Subroutinesub 85.7% 6 / 7 Podpod 33.3% 1 / 3 totaltotal 54.2% 128 / 236 CC 53 SCAR 55.5
CC
53
Cov
53%
install_ddl
600.0
sybase_column_type
27.0
sybase_default_clause
4.0
CRAP 256.8
↑ Summary Sybase/Storage/ASE/BulkInsert.pm »
1 package DBIO::Sybase::DDL;
2 # ABSTRACT: Generate Sybase ASE 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 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT_OK = qw(sybase_column_type sybase_default_clause);
11  
12  
13  
14 0 ▶sub install_ddl {
Subroutine install_ddl: not called CC=24 SCAR=64.0
Pod documented
15 0 my ($class, $schema) = @_;
16 0 my @stmts;
17 my @view_stmts;
18 0 my %seen_table;
19 0 my %seen_view;
20  
21 0 for my $source_name (sort $schema->sources) {
22 0 my $source = $schema->source($source_name);
23 0 my $result_class = $source->result_class;
24 0 my $table_name = $source->name;
25  
26 # Views: emit CREATE VIEW after all tables; skip virtual views.
27 0 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 0 0 0
28 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
29 0 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 0 0
30 0 ▶ next if ref $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname or $seen_view{$vname}++ 0 0 0
Condition0%0/3
ref $vname or $seen_view{$vname}++
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname $seen_view{$vname}++
Truth table0%0/3
A or B
A= ref $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
31 0 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 0 0 0
32 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
33 0 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _quote_ident($vname), $def;
34 0 next;
35 }
36  
37 # A scalar-ref name is either a literal table name (\'cd', emitted
38 # unquoted) or an inline subquery source; deref the former, skip the latter.
39 0 ▶ if (ref $table_name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table_name eq "SCALAR") 0 0 0
40 0 $table_name = $$table_name;
41 0 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 0 0
42 }
43 0 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 0 0
44  
45 # Several result sources can map to one physical table; emit it once.
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 = sybase_column_type($info->{data_type});
56 0 my $def = sprintf ' %s %s', _quote_ident($col_name), $type;
57  
58 0 ▶ if ($info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_auto_increment"}) 0 0 0
59 0 $def .= ' IDENTITY'; # Sybase autoincrement
60 }
61  
62 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
63  
64 $def .= sybase_default_clause($info->{default_value})
65 0 ▶ unless $info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_auto_increment"} 0 0 0
66  
67 0 push @col_defs, $def;
68 }
69  
70 # Primary key constraint
71 0 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 0 0 0
72 push @col_defs, sprintf ' PRIMARY KEY (%s)',
73 0 join(', ', map { _quote_ident($_) } @pk_cols);
74 }
75  
76 0 my $qualified = _quote_ident($table_name);
77 0 my $sql = sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs);
78 0 push @stmts, $sql;
79  
80 # Unique indexes (Sybase emits these as separate CREATE INDEX)
81 0 for my $col_name ($source->columns) {
82 0 my $info = $source->column_info($col_name);
83 0 ▶ if ($info->{is_unique} || $info->{is_single_unique_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_unique"} or $info->{"is_single_unique_key"}) 0 0 0
Condition0%0/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"is_unique"} or $info->{"is_single_unique_key"}
$info->{"is_unique"} $info->{"is_single_unique_key"}
Truth table0%0/3
A or B
A= $info->{"is_unique"}B= $info->{"is_single_unique_key"}
ABresult
1X1
011
000
84 0 push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);',
85 _quote_ident("${table_name}_${col_name}_idx"),
86 $qualified,
87 _quote_ident($col_name);
88 }
89 }
90 }
91  
92 0 return join "
93  
94 ", @stmts, @view_stmts;
95 }
96  
97  
98 29 ▶sub sybase_column_type {
Subroutine sybase_column_type: called CC=27 SCAR=33.0
Pod undocumented
99 29 my ($data_type) = @_;
100 29 ▶ my $type = lc($data_type // 'varchar');
Condition100%2/2
$data_type // "varchar"
l!l
281
MC/DC100%1/1
$data_type // "varchar"
$data_type
Truth table100%2/2
A
A= $data_type
Aresult
11
00
101  
102 # Integer types
103 29 ▶ return 'INT' if $type eq 'integer' || $type eq 'bigint';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "integer" or $type eq "bigint" 5 24 29
Condition100%3/3
$type eq "integer" or $type eq "bigint"
l!l&&r!l&&!r
4124
MC/DC100%2/2
$type eq "integer" or $type eq "bigint"
$type eq "integer" $type eq "bigint"
Truth table100%3/3
A or B
A= $type eq "integer"B= $type eq "bigint"
ABresult
1X1
011
000
104 24 ▶ return 'SMALLINT' if $type eq 'smallint' || $type eq 'tinyint';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "smallint" or $type eq "tinyint" 2 22 24
Condition100%3/3
$type eq "smallint" or $type eq "tinyint"
l!l&&r!l&&!r
1122
MC/DC100%2/2
$type eq "smallint" or $type eq "tinyint"
$type eq "smallint" $type eq "tinyint"
Truth table100%3/3
A or B
A= $type eq "smallint"B= $type eq "tinyint"
ABresult
1X1
011
000
105 22 ▶ return 'BIGINT' if $type eq 'bigserial' || $type eq 'serial';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bigserial" or $type eq "serial" 2 20 22
Condition100%3/3
$type eq "bigserial" or $type eq "serial"
l!l&&r!l&&!r
1120
MC/DC100%2/2
$type eq "bigserial" or $type eq "serial"
$type eq "bigserial" $type eq "serial"
Truth table100%3/3
A or B
A= $type eq "bigserial"B= $type eq "serial"
ABresult
1X1
011
000
106  
107 # Character types
108 20 ▶ return 'VARCHAR(255)' if $type eq 'varchar' || $type eq 'nvarchar';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "varchar" or $type eq "nvarchar" 4 16 20
Condition100%3/3
$type eq "varchar" or $type eq "nvarchar"
l!l&&r!l&&!r
3116
MC/DC100%2/2
$type eq "varchar" or $type eq "nvarchar"
$type eq "varchar" $type eq "nvarchar"
Truth table100%3/3
A or B
A= $type eq "varchar"B= $type eq "nvarchar"
ABresult
1X1
011
000
109 16 ▶ return 'CHAR(1)' if $type eq 'char' || $type eq 'nchar';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "char" or $type eq "nchar" 1 15 16
Condition66%2/3
$type eq "char" or $type eq "nchar"
l!l&&r!l&&!r
1015
MC/DC50%1/2
$type eq "char" or $type eq "nchar"
$type eq "char" $type eq "nchar"
Truth table66%2/3
A or B
A= $type eq "char"B= $type eq "nchar"
ABresult
1X1
011
000
110 15 ▶ return 'TEXT' if $type eq 'text' || $type eq 'long';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" or $type eq "long" 1 14 15
Condition66%2/3
$type eq "text" or $type eq "long"
l!l&&r!l&&!r
1014
MC/DC50%1/2
$type eq "text" or $type eq "long"
$type eq "text" $type eq "long"
Truth table66%2/3
A or B
A= $type eq "text"B= $type eq "long"
ABresult
1X1
011
000
111  
112 # Date/time types
113 14 ▶ return 'DATETIME' if $type eq 'date' || $type eq 'timestamp' || $type eq 'datetime';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "date" or $type eq "timestamp" or $type eq "datetime" 2 12 14
Condition83%5/6
$type eq "date" or $type eq "timestamp" or $type eq "datetime"
l!l&&r!l&&!r
2012
$type eq "date" or $type eq "timestamp"
l!l&&r!l&&!r
1112
MC/DC66%2/3
$type eq "date" or $type eq "timestamp" or $type eq "datetime"
$type eq "date" $type eq "timestamp" $type eq "datetime"
Truth table75%3/4
A or B or C
A= $type eq "date"B= $type eq "timestamp"C= $type eq "datetime"
ABCresult
1XX1
01X1
0011
0000
114 12 ▶ return 'SMALLDATETIME' if $type eq 'smalldatetime';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "smalldatetime" 1 11 12
115  
116 # Binary types
117 11 ▶ return 'IMAGE' if $type eq 'bytea' || $type eq 'blob';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bytea" or $type eq "blob" 2 9 11
Condition100%3/3
$type eq "bytea" or $type eq "blob"
l!l&&r!l&&!r
119
MC/DC100%2/2
$type eq "bytea" or $type eq "blob"
$type eq "bytea" $type eq "blob"
Truth table100%3/3
A or B
A= $type eq "bytea"B= $type eq "blob"
ABresult
1X1
011
000
118 9 ▶ return 'TEXT' if $type eq 'clob';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "clob" 1 8 9
119  
120 # Numeric types
121 8 ▶ return 'NUMERIC(18,6)' if $type eq 'numeric' || $type eq 'decimal';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "numeric" or $type eq "decimal" 2 6 8
Condition100%3/3
$type eq "numeric" or $type eq "decimal"
l!l&&r!l&&!r
116
MC/DC100%2/2
$type eq "numeric" or $type eq "decimal"
$type eq "numeric" $type eq "decimal"
Truth table100%3/3
A or B
A= $type eq "numeric"B= $type eq "decimal"
ABresult
1X1
011
000
122 6 ▶ return 'FLOAT' if $type eq 'float' || $type eq 'real';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "float" or $type eq "real" 2 4 6
Condition100%3/3
$type eq "float" or $type eq "real"
l!l&&r!l&&!r
114
MC/DC100%2/2
$type eq "float" or $type eq "real"
$type eq "float" $type eq "real"
Truth table100%3/3
A or B
A= $type eq "float"B= $type eq "real"
ABresult
1X1
011
000
123 4 ▶ return 'DOUBLE PRECISION' if $type eq 'double precision';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "double precision" 1 3 4
124  
125 # Boolean — Sybase has bit
126 3 ▶ return 'BIT' if $type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" 1 2 3
127  
128 # Pass through unknown types as-is (uppercase)
129 2 return uc($type);
130 }
131  
132  
133 10 ▶sub sybase_default_clause {
Subroutine sybase_default_clause: called CC=4 SCAR=13.9
Pod undocumented
134 10 my ($dv) = @_;
135 10 ▶ return '' unless defined $dv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dv 6 4 10
136 4 ▶ return " DEFAULT $$dv" if ref $dv eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $dv eq "SCALAR" 1 3 4
137 3 ▶ return '' if $dv eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $dv eq "null" 1 2 3
138 2 return " DEFAULT '$dv'";
139 }
140  
141  
142 3 1;
143  
144 __END__
145  
146 =pod
147  
148 =encoding UTF-8
149  
150 =head1 NAME
151  
152 DBIO::Sybase::DDL - Generate Sybase ASE DDL from DBIO Result classes
153  
154 =head1 VERSION
155  
156 version 0.900001
157  
158 =head1 DESCRIPTION
159  
160 C<DBIO::Sybase::DDL> generates Sybase ASE DDL from the DBIO schema class hierarchy.
161 It is the desired-state side of the test-deploy-and-compare strategy used by
162 L<DBIO::Sybase::Deploy>.
163  
164 my $ddl = DBIO::Sybase::DDL->install_ddl($schema);
165 # CREATE TABLE ...; CREATE INDEX ...;
166  
167 B<NOTE:> Sybase ASE does not support CHECK constraints in the same way as
168 other databases. Unique constraints are emitted as unique indexes instead.
169  
170 =head1 METHODS
171  
172 =head2 install_ddl
173  
174 my $ddl = DBIO::Sybase::DDL->install_ddl($schema);
175  
176 Returns the full installation DDL as a single string. C<$schema> may be
177 a connected schema instance or a schema class name.
178  
179 =func sybase_column_type
180  
181 my $sql_type = sybase_column_type($data_type);
182  
183 Maps a logical DBIO C<data_type> to its Sybase ASE column type. Shared by
184 the DDL emitter and L<DBIO::Sybase::Diff::Table> so both sides of the
185 test-deploy-and-compare agree on type rendering. Exportable on request.
186  
187 =func sybase_default_clause
188  
189 my $clause = sybase_default_clause($default_value);
190  
191 Renders the C<DEFAULT> fragment for a column definition (with a leading
192 space), or the empty string when there is no default. A SCALAR ref is
193 emitted verbatim (SQL expression); the literal string C<'null'> yields no
194 clause; anything else is single-quoted. Shared by the DDL emitter and the
195 Diff table/column emitters so all three render defaults identically.
196 Exportable on request.
197  
198 =seealso
199  
200 =over
201  
202 =item * L<DBIO::Sybase> - schema component
203  
204 =item * L<DBIO::Sybase::Deploy> - uses this to generate DDL for deployment
205  
206 =item * L<DBIO::Oracle::DDL> - sibling implementation
207  
208 =back
209  
210 =head1 AUTHOR
211  
212 DBIO & DBIx::Class Authors
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 Copyright (C) 2026 DBIO Authors
217 Portions Copyright (C) 2005-2025 DBIx::Class Authors
218 Based on DBIx::Class, heavily modified.
219  
220 This is free software; you can redistribute it and/or modify it under
221 the same terms as the Perl 5 programming language system itself.
222  
223 =cut
↑ Summary Sybase/Storage/ASE/BulkInsert.pm »