Sybase/DDL.pm

filter: Statementstmt 95.0% 210 / 221 Branchbran 82.7% 134 / 162 Conditioncond 68.5% 96 / 140 MC/DCmcdc 55.1% 43 / 78 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 80.9% 502 / 620 CC 140 SCAR 36.0
CC
140
Cov
84%
install_ddl
63.6
sybase_column_type
42.0
_table_foreign_keys
40.6
CRAP 36.7
« Sybase/Storage/ASE/BulkInsert.pm ↑ Summary Sybase/Introspect/Views.pm »
1 package DBIO::Sybase::DDL;
2 # ABSTRACT: Generate Sybase ASE DDL from DBIO Result classes
3  
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 our $VERSION = '0.900007';
8  
9 40 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use DBIO::Sybase::Quote qw(quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our @EXPORT_OK = qw(sybase_column_type sybase_default_clause);
15  
16 # ASE datatypes that take no length or precision.
17 40 our $NO_LENGTH_TYPE = qr/\A(?:
18 (?:unsigned\s+)?(?:tiny|small|big)?int(?:eger)? | u(?:tiny|small|big)?int
19 | real | money | smallmoney | bit | image | text | unitext | sysname | longsysname
20 | date | time | bigtime | bigdatetime | datetime | smalldatetime
21 )\z/x;
22  
23  
24  
25 81 ▶sub install_ddl {
Subroutine install_ddl: called CC=40 SCAR=41.5
Pod documented
26 81 my ($class, $schema) = @_;
27 81 my @stmts;
28 my @view_stmts;
29 81 my %seen_table;
30 81 my %seen_view;
31 81 my @fk_sources;
32  
33 81 for my $source_name (sort $schema->sources) {
34 101 my $source = $schema->source($source_name);
35 101 my $result_class = $source->result_class;
36 101 my $table_name = $source->name;
37  
38 # Views: emit CREATE VIEW after all tables; skip virtual views.
39 101 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 0 101 101
40 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
41 0 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 0 0
42 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
43 0 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 0 0 0
44 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
45 0 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', quote_ident($vname), $def;
46 0 next;
47 }
48  
49 # A scalar-ref name is either a literal table name (\'cd', emitted
50 # unquoted) or an inline subquery source; deref the former, skip the latter.
51 101 ▶ if (ref $table_name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table_name eq "SCALAR") 0 101 101
52 0 $table_name = $$table_name;
53 0 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 0 0
54 }
55 101 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 101 101
56  
57 # Several result sources can map to one physical table; emit it once.
58 101 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 101 101
59 101 push @fk_sources, [ $table_name, $source ];
60  
61 101 my @col_defs;
62 my %is_pk;
63 101 my @pk_cols = $source->primary_columns;
64 101 @is_pk{@pk_cols} = (1) x @pk_cols;
65  
66 101 for my $col_name ($source->columns) {
67 357 my $info = $source->column_info($col_name);
68 357 _check_column_type($table_name, $col_name, $info);
69 354 my $type = sybase_column_type($info->{data_type}, $info->{size});
70 354 my $def = sprintf ' %s %s', quote_ident($col_name), $type;
71  
72 # ASE's column definition is
73 # column_name datatype [default ...] [{identity | null | not null}]
74 # (karr k47): the default comes BEFORE the nullability -- `NOT NULL
75 # DEFAULT 'x'` is Msg 156 on ASE 16.0 SP02 -- and identity, null and
76 # not null are alternatives. So an IDENTITY column is the attribute
77 # alone: never nullable, and its value is the server's, not a default.
78 354 ▶ if ($info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_auto_increment"}) 46 308 354
79 push @col_defs, $def
80 46 . _identity_clause($type, $info->{data_type}, $col_name, $table_name);
81 28 next;
82 }
83  
84 308 $def .= sybase_default_clause($info->{default_value}, $info->{data_type}, $col_name);
85  
86 # ADR 0043: a missing is_nullable means NOT NULL, matching what
87 # DBIO::Schema::Type::canonical_column already claims (not_null => 1).
88 # The old gate -- "defined && !" -- emitted nothing for a missing key, so
89 # the deployed column was nullable while the compiled model said NOT NULL,
90 # and target_from_compiled diffs diverged on exactly that axis.
91 #
92 # A nullable column is spelled NULL, never left bare (karr k47): a bare
93 # column takes the database's "allow nulls by default" option, which is
94 # off in model and so in every fresh database -- the temp DB of
95 # Deploy::diff included -- while the live database may have it on. The
96 # same DDL then deployed two different columns.
97 308 ▶ $def .= $info->{is_nullable} ? ' NULL' : ' NOT NULL';
Branch: 1 branch
BranchTrueFalseTotal
$info->{'is_nullable'} ? : 33 275 308
98  
99 308 push @col_defs, $def;
100 }
101  
102 # Primary key constraint
103 80 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 75 5 80
104 push @col_defs, sprintf ' PRIMARY KEY (%s)',
105 75 join(', ', map { quote_ident($_) } @pk_cols);
106 }
107  
108 80 my $qualified = quote_ident($table_name);
109 78 my $sql = sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs);
110 78 push @stmts, $sql;
111  
112 # Unique indexes (Sybase emits these as separate CREATE INDEX).
113 # %unique_set answers "is this column set already unique" (sorted, as
114 # the unique constraints are compared); %written_cols and %written_name
115 # answer "is there an index over these columns in this order" and "is the
116 # name taken" for the declared indexes below (karr k88).
117 78 my %unique_set = (join("\0", sort @pk_cols) => 1);
118 78 my (%written_cols, %written_name);
119 78 ▶ $written_cols{ join "\0", @pk_cols } = 1 if @pk_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 73 5 78
120 78 for my $col_name ($source->columns) {
121 331 my $info = $source->column_info($col_name);
122 331 ▶ if ($info->{is_unique} || $info->{is_single_unique_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_unique"} or $info->{"is_single_unique_key"}) 4 327 331
Condition66%2/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
l!l&&r!l&&!r
40327
MC/DC50%1/2
$info->{"is_unique"} or $info->{"is_single_unique_key"}
$info->{"is_unique"} $info->{"is_single_unique_key"}
Truth table66%2/3
A or B
A= $info->{"is_unique"}B= $info->{"is_single_unique_key"}
ABresult
1X1
011
000
123 4 $unique_set{$col_name} = 1;
124 4 my $iname = "${table_name}_${col_name}_idx";
125 4 $written_cols{$col_name} = 1;
126 4 $written_name{$iname} = [ $col_name ];
127 4 push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);',
128 quote_ident($iname),
129 $qualified,
130 quote_ident($col_name);
131 }
132 }
133  
134 # The unique constraints of the source (add_unique_constraint), one
135 # CREATE UNIQUE INDEX each, named after the constraint (karr k56) -- ASE
136 # index names are per table. 'primary' is the key itself, and a column
137 # set already unique (the key, a column flag above, an earlier constraint)
138 # is not indexed twice.
139 78 my %uniques = $source->unique_constraints;
140 78 for my $uname (sort keys %uniques) {
141 61 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 53 8 61
142 8 my @cols = @{ $uniques{$uname} };
143 8 ▶ next if !@cols || $unique_set{ join "\0", sort @cols }++;
Branch: 1 branch
BranchTrueFalseTotal
if not @cols or $unique_set{join "\0", sort(@cols)}++ 3 5 8
Condition66%2/3
not @cols or $unique_set{join "\0", sort(@cols)}++
l!l&&r!l&&!r
035
MC/DC50%1/2
not @cols or $unique_set{join "\0", sort(@cols)}++
not @cols $unique_set{join "\0", sort(@cols)}++
Truth table66%2/3
not(A) or B
A= not @colsB= $unique_set{join "\0", sort(@cols)}++
ABresult
1X1
011
000
144 5 $written_cols{ join "\0", @cols } = 1;
145 5 $written_name{$uname} = [ @cols ];
146 push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);',
147 quote_ident($uname), $qualified,
148 5 join(', ', map { quote_ident($_) } @cols);
149 }
150  
151 # The index declarations of the source (Cake idx, indices; ADR 0061 in
152 # core, karr k88). ASE has no partial and no function index, so the
153 # sybase block takes no key at all: the helper refuses an unknown key and
154 # an entry that carries only another driver's block, naming the index.
155 # What ASE cannot do is refused here as well, never written plainer: a
156 # type other than unique, options, more than 31 key columns.
157 78 for my $decl ($source->index_declarations_for('sybase', [])) {
158 14 my $iname = $decl->{name};
159 14 my @cols = @{ $decl->{columns} };
160 Carp::croak sprintf(
161 "DBIO::Sybase: index '%s' on %s has type '%s', which ASE cannot "
162 . 'build -- only a plain or a unique index exists (type => '
163 . "'unique'); nothing plainer is written in its place",
164 $iname, $table_name, $decl->{type},
165 14 ▶ ) if defined $decl->{type} && $decl->{type} !~ /\Aunique\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"type"} and not $decl->{"type"} =~ /\Aunique\z/i 1 13 14
Condition100%3/3
defined $decl->{"type"} and not $decl->{"type"} =~ /\Aunique\z/i
!ll&&!rl&&r
1121
MC/DC100%2/2
defined $decl->{"type"} and not $decl->{"type"} =~ /\Aunique\z/i
defined $decl->{"type"} not $decl->{"type"} =~ /\Aunique\z/i
Truth table100%3/3
A and not(B)
A= defined $decl->{"type"}B= not $decl->{"type"} =~ /\Aunique\z/i
ABresult
0X0
100
111
166 Carp::croak sprintf(
167 "DBIO::Sybase: index '%s' on %s has options, which this DDL has no "
168 . 'rendering for (fillfactor and the like are server settings of '
169 . 'the index); nothing plainer is written in its place',
170 $iname, $table_name,
171 ) if defined $decl->{options}
172 13 ▶ && (ref $decl->{options} ne 'HASH' || %{ $decl->{options} });
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"options"} and ref $decl->{"options"} ne "HASH" || %{$decl->{"options"};} 1 12 13
Condition50%3/6
defined $decl->{"options"} and ref $decl->{"options"} ne "HASH" || %{$decl->{"options"};}
!ll&&!rl&&r
1201
ref $decl->{"options"} ne "HASH" or %{$decl->{"options"};}
l!l&&r!l&&!r
010
MC/DC33%1/3
defined $decl->{"options"} and ref $decl->{"options"} ne "HASH" || %{$decl->{"options"};}
defined $decl->{"options"} ref $decl->{"options"} ne "HASH" %{$decl->{"options"};}
Truth table50%2/4
A and B or C
A= defined $decl->{"options"}B= ref $decl->{"options"} ne "HASH"C= %{$decl->{"options"};}
ABCresult
0XX0
1000
11X1
1011
173 12 ▶ Carp::croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if @cols > 31 1 11 12
174 "DBIO::Sybase: index '%s' on %s has %d columns; ASE takes at most "
175 . '31 key columns in an index',
176 $iname, $table_name, scalar @cols,
177 ) if @cols > 31;
178  
179 # The same name over the same columns is the index already written;
180 # the same name over other columns is a different index that ASE
181 # cannot hold beside it (names are per table).
182 11 ▶ if (my $have = $written_name{$iname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $have = $written_name{$iname}) 2 9 11
183 2 ▶ next if join("\0", @$have) eq join("\0", @cols);
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", @$have) eq join("\0", @cols) 1 1 2
184 1 Carp::croak sprintf(
185 "DBIO::Sybase: index '%s' on %s is declared over (%s), but an "
186 . "index of that name over (%s) is already written; ASE index "
187 . 'names are per table',
188 $iname, $table_name, join(', ', @cols), join(', ', @$have),
189 );
190 }
191 # A second index over the columns of a unique one already written, in
192 # that order, is a duplicate (as dbio-sqlite karr k79) and is not
193 # written. Only unique indexes are remembered: a plain index never
194 # swallows a unique declaration over the same columns.
195 9 ▶ next if $written_cols{ join "\0", @cols };
Branch: 1 branch
BranchTrueFalseTotal
if $written_cols{join "\0", @cols} 3 6 9
196 6 ▶ $written_cols{ join "\0", @cols } = 1 if $decl->{unique};
Branch: 1 branch
BranchTrueFalseTotal
if $decl->{"unique"} 2 4 6
197 6 $written_name{$iname} = [ @cols ];
198 push @stmts, sprintf 'CREATE %sINDEX %s ON %s (%s);',
199 $decl->{unique} ? 'UNIQUE ' : '', quote_ident($iname), $qualified,
200 6 ▶ join(', ', map { quote_ident($_) } @cols);
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 2 4 6
201 }
202 }
203  
204 # Foreign keys, after every CREATE TABLE (karr k54): cd references track and
205 # track references cd in the shared test schema, so no table order would do.
206 51 for my $pair (@fk_sources) {
207 61 my ($table_name, $source) = @$pair;
208 61 push @stmts, map { _fk_statement($table_name, $_) }
209 _table_foreign_keys($schema, $source, \%seen_table);
210 }
211  
212 41 return join "
213  
214 ", @stmts, @view_stmts;
215 }
216  
217 # One ALTER TABLE ... ADD CONSTRAINT per FK. No ON DELETE / ON UPDATE: ASE has
218 # no declarative referential actions (Msg 156 on 16.0, for NO ACTION as well),
219 # a reference always restricts -- which is what Introspect reports (NO ACTION)
220 # for both sides. A declared action other than NO ACTION never gets here:
221 # _table_foreign_keys refuses it (karr k91), so nothing is silently dropped.
222 11 ▶sub _fk_statement {
Subroutine _fk_statement: called CC=1 SCAR=0.0
223 11 my ($table_name, $fk) = @_;
224 return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);',
225 quote_ident($table_name), quote_ident($fk->{constraint_name}),
226 12 join(', ', map { quote_ident($_) } @{ $fk->{from_columns} }),
227 quote_ident($fk->{to_table}),
228 11 join(', ', map { quote_ident($_) } @{ $fk->{to_columns} });
229 }
230  
231 # The FK constraints of one source, from its relationships marked
232 # is_foreign_key_constraint (belongs_to sets it unless told otherwise) -- the
233 # signal DBIO::MySQL::DDL, DBIO::SQLite::DDL and DBIO::MSSQL::DDL read too. A
234 # relationship is left out when it cannot be one ASE constraint:
235 #
236 # - its condition is not plain 'foreign.x' => 'self.y' pairs (a coderef);
237 # - its target table is not part of this deploy (%$deployed): not in the
238 # schema, a view, or an inline subquery source;
239 # - its target columns are neither the target's primary key nor one of its
240 # unique constraints -- ASE refuses a reference without a unique index
241 # behind it, and a belongs_to may legitimately join on any column.
242 #
243 # A relationship that does become a constraint and declares an on_delete or
244 # on_update other than NO ACTION (CASCADE, SET NULL, SET DEFAULT, RESTRICT, any
245 # unknown word) is refused, checked before the de-duplication below so the
246 # second relationship over the same columns is refused too (karr k91). What
247 # does not become a constraint is not looked at.
248 #
249 # Two relationships over the same columns to the same target (cd.artist and
250 # cd.very_long_artist_relationship) are one constraint. Pairs are ordered by
251 # the target's key order, so a composite reference matches its key; the name
252 # fk_<table>_<cols> is DBIO::Sybase::Diff::ForeignKey's generated name.
253 61 ▶sub _table_foreign_keys {
Subroutine _table_foreign_keys: called CC=26 SCAR=37.0
254 61 my ($schema, $source, $deployed) = @_;
255 61 my $table_name = $source->name;
256 61 ▶ $table_name = $$table_name if ref $table_name eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name eq "SCALAR" 0 61 61
257 61 my (@fks, %seen);
258  
259 61 for my $rel (sort $source->relationships) {
260 27 my $info = $source->relationship_info($rel);
261 27 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 2 25 27
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
0225
$info and $info->{"attrs"}
!ll&&!rl&&r
0027
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
262 25 my $cond = $info->{cond};
263 25 ▶ next unless ref $cond eq 'HASH' && %$cond;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" and %$cond 1 24 25
Condition66%2/3
ref $cond eq "HASH" and %$cond
!ll&&!rl&&r
1024
MC/DC50%1/2
ref $cond eq "HASH" and %$cond
ref $cond eq "HASH" %$cond
Truth table66%2/3
A and B
A= ref $cond eq "HASH"B= %$cond
ABresult
0X0
100
111
264  
265 24 my @pairs;
266 24 for my $key (keys %$cond) {
267 25 my $val = $cond->{$key};
268 25 ▶ next if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 25 25
269 25 ▶ my ($to) = $key =~ /\Aforeign\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($to) = $key =~ /\Aforeign\.(\w+)\z/ or next 25 0 25
270 25 ▶ my ($from) = $val =~ /\Aself\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($from) = $val =~ /\Aself\.(\w+)\z/ or next 25 0 25
271 25 push @pairs, [ $from, $to ];
272 }
273 24 ▶ next unless @pairs == keys %$cond;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs == keys %$cond 0 24 24
274  
275 24 my $fs = eval { $schema->source($info->{source}) }
276 0 // eval { $schema->source($info->{class}) }
277 0 ▶ // eval { $schema->source(($info->{source} // '') =~ s/.*:://r) }
Branch: 1 branch
BranchTrueFalseTotal
my $fs = (eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } } or next 24 0 24
Condition25%2/8
(eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
l!l&&r!l&&!r
2400
eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }
l!l&&r!l&&!r
2400
$info->{"source"} // ""
l!l
00
MC/DC0%0/3
(eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
eval { do { $schema->source($info->{"source"}) } } eval { do { $schema->source($info->{"class"}) } } eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
MC/DC0%0/1
$info->{"source"} // ""
$info->{"source"}
Truth table25%1/4
A // B // C
A= eval { do { $schema->source($info->{"source"}) } }B= eval { do { $schema->source($info->{"class"}) } }C= eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $info->{"source"}
Aresult
11
00
278 or next;
279 24 ▶ next if $fs->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $fs->isa("DBIO::ResultSource::View") 0 24 24
280 24 my $to_table = $fs->name;
281 24 ▶ $to_table = $$to_table if ref $to_table eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_table eq "SCALAR" 0 24 24
282 24 ▶ next if ref $to_table || !$deployed->{$to_table};
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_table or not $deployed->{$to_table} 0 24 24
Condition33%1/3
ref $to_table or not $deployed->{$to_table}
l!l&&r!l&&!r
0024
MC/DC0%0/2
ref $to_table or not $deployed->{$to_table}
ref $to_table not $deployed->{$to_table}
Truth table33%1/3
A or not(B)
A= ref $to_tableB= not $deployed->{$to_table}
ABresult
1X1
011
000
283  
284 24 my @key = $fs->primary_columns;
285 24 my %pos;
286 24 @pos{@key} = (0 .. $#key);
287 24 ▶ @pairs = sort { ($pos{ $a->[1] } // 1e9) <=> ($pos{ $b->[1] } // 1e9)
Condition42%3/7
($pos{$DBIO::Sybase::DDL::a->[1]} // 1000000000.0) <=> ($pos{$DBIO::Sybase::DDL::b->[1]} // 1000000000.0) || $DBIO::Sybase::DDL::a->[1] cmp $DBIO::Sybase::DDL::b->[1]
l!l&&r!l&&!r
100
$pos{$DBIO::Sybase::DDL::a->[1]} // "1000000000"
l!l
10
$pos{$DBIO::Sybase::DDL::b->[1]} // "1000000000"
l!l
10
MC/DC0%0/2
($pos{$DBIO::Sybase::DDL::a->[1]} // 1000000000.0) <=> ($pos{$DBIO::Sybase::DDL::b->[1]} // 1000000000.0) || $DBIO::Sybase::DDL::a->[1] cmp $DBIO::Sybase::DDL::b->[1]
$pos{$DBIO::Sybase::DDL::a->[1]} $DBIO::Sybase::DDL::a->[1] cmp $DBIO::Sybase::DDL::b->[1]
MC/DC0%0/1
$pos{$DBIO::Sybase::DDL::b->[1]} // "1000000000"
$pos{$DBIO::Sybase::DDL::b->[1]}
Truth table33%1/3
A || B
A= $pos{$DBIO::Sybase::DDL::a->[1]}B= $DBIO::Sybase::DDL::a->[1] cmp $DBIO::Sybase::DDL::b->[1]
ABresult
1X1
011
000
Truth table50%1/2
A
A= $pos{$DBIO::Sybase::DDL::b->[1]}
Aresult
11
00
288 || $a->[1] cmp $b->[1] } @pairs;
289  
290 24 my $to_set = join "\0", sort map { $_->[1] } @pairs;
291 24 my %uniques = $fs->unique_constraints;
292 24 ▶ next unless grep { join("\0", sort @$_) eq $to_set } values %uniques;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {join("\0", sort(@$_)) eq $to_set;} values %uniques 1 23 24
293  
294 23 my @from = map { $_->[0] } @pairs;
295 23 my @to = map { $_->[1] } @pairs;
296 23 _check_fk_action("relationship '$rel'", $table_name, 'ON DELETE', $info->{attrs}{on_delete}, \@from);
297 14 _check_fk_action("relationship '$rel'", $table_name, 'ON UPDATE', $info->{attrs}{on_update}, \@from);
298 13 ▶ next if $seen{ join "\0", @from, '->', $to_table, @to }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{join "\0", @from, "->", $to_table, @to}++ 1 12 13
299 12 push @fks, {
300 constraint_name => join('_', 'fk', $table_name, @from),
301 from_columns => \@from,
302 to_table => $to_table,
303 to_columns => \@to,
304 };
305 }
306 51 return @fks;
307 }
308  
309 # What ASE cannot hold is refused before the server is asked (karr k97),
310 # naming table and column, instead of passing on Msg 2715 (no ENUM type) or
311 # writing something plainer than the declaration: a size on BIT would be
312 # dropped without a word, and a nullable BIT is Msg 2718 (ASE keeps no NULL
313 # flag for a bit).
314 357 ▶sub _check_column_type {
Subroutine _check_column_type: called CC=9 SCAR=22.3
315 357 my ($table_name, $col_name, $info) = @_;
316 357 ▶ my $declared = lc($info->{data_type} // '');
Condition50%1/2
$info->{"data_type"} // ""
l!l
3570
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
317 357 ▶ DBIO::Exception->throw(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if $declared eq "enum" 1 356 357
318 "DBIO::Sybase: table '%s', column '%s': type 'enum' is refused: ASE has no enum "
319 . "type (Msg 2715). Declare a varchar column",
320 $table_name, $col_name) if $declared eq 'enum';
321  
322 356 ▶ return unless sybase_column_type($declared, $info->{size}) eq 'BIT';
Branch: 1 branch
BranchTrueFalseTotal
unless sybase_column_type($declared, $info->{"size"}) eq "BIT" 350 6 356
323 6 ▶ my $size = ref $info->{size} eq 'ARRAY' ? $info->{size}[0] : $info->{size};
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'size'} eq 'ARRAY' ? : 0 6 6
324 DBIO::Exception->throw(sprintf
325 "DBIO::Sybase: table '%s', column '%s': size '%s' on type '%s' is refused: bit "
326 . "has no length in ASE",
327 $table_name, $col_name, $size, $info->{data_type})
328 6 ▶ if defined $size && length $size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size and length $size 1 5 6
Condition66%2/3
defined $size and length $size
!ll&&!rl&&r
501
MC/DC50%1/2
defined $size and length $size
defined $size length $size
Truth table66%2/3
A and B
A= defined $sizeB= length $size
ABresult
0X0
100
111
329 DBIO::Exception->throw(sprintf
330 "DBIO::Sybase: table '%s', column '%s': NULL in a bit column is refused: ASE "
331 . "cannot hold NULL in a BIT column (Msg 2718). Declare it is_nullable => 0",
332 5 ▶ $table_name, $col_name) if $info->{is_nullable} && !$info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_nullable"} and not $info->{"is_auto_increment"} 1 4 5
Condition66%2/3
$info->{"is_nullable"} and not $info->{"is_auto_increment"}
!ll&&!rl&&r
401
MC/DC50%1/2
$info->{"is_nullable"} and not $info->{"is_auto_increment"}
$info->{"is_nullable"} not $info->{"is_auto_increment"}
Truth table66%2/3
A and not(B)
A= $info->{"is_nullable"}B= not $info->{"is_auto_increment"}
ABresult
0X0
100
111
333 }
334  
335 # Throws unless $value is no action: undef, '' or NO ACTION (case and
336 # whitespace ignored). Shared with DBIO::Sybase::Diff::ForeignKey::Create.
337 # $what names the relationship (DDL) or the constraint (Diff).
338 61 ▶sub _check_fk_action {
Subroutine _check_fk_action: called CC=6 SCAR=18.2
339 61 my ($what, $table_name, $event, $value, $columns) = @_;
340 61 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 33 28 61
341 28 (my $norm = uc $value) =~ s/\s+/ /g;
342 28 $norm =~ s/\A | \z//g;
343 28 ▶ return if $norm eq '' || $norm eq 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if $norm eq "" or $norm eq "NO ACTION" 14 14 28
Condition100%3/3
$norm eq "" or $norm eq "NO ACTION"
l!l&&r!l&&!r
21214
MC/DC100%2/2
$norm eq "" or $norm eq "NO ACTION"
$norm eq "" $norm eq "NO ACTION"
Truth table100%3/3
A or B
A= $norm eq ""B= $norm eq "NO ACTION"
ABresult
1X1
011
000
344 14 ▶ DBIO::Exception->throw(sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$columns && @$columns ? : 14 0 14
Condition33%1/3
$columns and @$columns
!ll&&!rl&&r
0014
MC/DC0%0/2
$columns and @$columns
$columns @$columns
Truth table33%1/3
A and B
A= $columnsB= @$columns
ABresult
0X0
100
111
345 "DBIO::Sybase: %s on table %s%s declares %s %s, but ASE has no referential "
346 . 'actions (Msg 156, not even NO ACTION is accepted as syntax); the '
347 . 'action is refused and the reference always restricts. Drop the action '
348 . 'from the relationship (or set it to NO ACTION) -- nothing is written in '
349 . 'its place',
350 $what, $table_name,
351 ( $columns && @$columns ? ' (column ' . join(', ', @$columns) . ')' : '' ),
352 $event, $value,
353 ));
354 }
355  
356  
357 1035 ▶sub sybase_column_type {
Subroutine sybase_column_type: called CC=42 SCAR=37.4
Pod documented
358 1035 my ($data_type, $size) = @_;
359 1035 ▶ my $type = lc($data_type // 'varchar');
Condition100%2/2
$data_type // "varchar"
l!l
10341
MC/DC100%1/1
$data_type // "varchar"
$data_type
Truth table100%2/2
A
A= $data_type
Aresult
11
00
360  
361 # Integer types. The logical name carries the width and nothing else: ASE has
362 # had a native BIGINT since 15.0, so a column declared `bigint` is deployed
363 # as 64 bits rather than narrowed to INT -- a narrowing that stayed silent
364 # until the first value over 2^31-1. `serial` is the 32-bit auto-increment
365 # variant (DBIO::Cake lists serial/bigserial/smallserial as three widths,
366 # mirroring integer/bigint/smallint), so it maps by width exactly like its
367 # non-serial twin; IDENTITY is a column attribute in ASE, not a type, and
368 # install_ddl appends it separately. These two lines were transposed until
369 # karr k40, which also meant a deployed `serial` (BIGINT) read back from
370 # the catalog as `bigint` and re-rendered as INT, so the diff never
371 # converged against its own deployment.
372 1035 ▶ return 'INT' if $type eq 'integer' || $type eq 'serial';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "integer" or $type eq "serial" 224 811 1035
Condition100%3/3
$type eq "integer" or $type eq "serial"
l!l&&r!l&&!r
2195811
MC/DC100%2/2
$type eq "integer" or $type eq "serial"
$type eq "integer" $type eq "serial"
Truth table100%3/3
A or B
A= $type eq "integer"B= $type eq "serial"
ABresult
1X1
011
000
373 811 ▶ return 'SMALLINT' if $type eq 'smallint' || $type eq 'tinyint';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "smallint" or $type eq "tinyint" 8 803 811
Condition100%3/3
$type eq "smallint" or $type eq "tinyint"
l!l&&r!l&&!r
62803
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
374 803 ▶ return 'BIGINT' if $type eq 'bigint' || $type eq 'bigserial';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bigint" or $type eq "bigserial" 16 787 803
Condition100%3/3
$type eq "bigint" or $type eq "bigserial"
l!l&&r!l&&!r
133787
MC/DC100%2/2
$type eq "bigint" or $type eq "bigserial"
$type eq "bigint" $type eq "bigserial"
Truth table100%3/3
A or B
A= $type eq "bigint"B= $type eq "bigserial"
ABresult
1X1
011
000
375  
376 # Character types
377 #
378 # VARCHAR and CHAR take exactly ONE scalar length in ASE, so an arrayref size
379 # has one member that can mean anything: the width. Read it, rather than let
380 # the reference numify to its memory address and render as
381 # VARCHAR(94182096057624) -- a length the caller never wrote, emitted without
382 # a warning (karr k33; dbio-mssql settled the identical blind spot in its
383 # karr k27). That is the same "first member is the width" reading the numeric
384 # branch below has always had. The most plausible origin of the shape is a
385 # column retyped from numeric/decimal to varchar whose size => [p, s] was
386 # left behind, which argues for keeping the width rather than refusing the
387 # column. An arrayref naming no width ([], [undef]) falls through to the
388 # historical default exactly as an absent size does, so every arrayref shape
389 # renders identically to its scalar counterpart. This driver's own
390 # introspection never produces an arrayref here (Introspect::Columns sets a
391 # scalar size for char types), so the shape is hand-written column info.
392 787 ▶ my $len = ref $size eq 'ARRAY' ? $size->[0] : $size;
Branch: 1 branch
BranchTrueFalseTotal
ref $size eq 'ARRAY' ? : 34 753 787
393 # `character` and `char varying` / `character varying` are the standard
394 # spellings of CHAR and VARCHAR (karr k70). Passed through they lost their
395 # size: `character` size 10 deployed as char(1), without an error.
396 787 ▶ if ($type eq 'varchar' || $type eq 'nvarchar'
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "varchar" or $type eq "nvarchar" or $type eq "char varying" or $type eq "character varying") 548 239 787
Condition100%9/9
$type eq "varchar" or $type eq "nvarchar" or $type eq "char varying" or $type eq "character varying"
l!l&&r!l&&!r
5471239
$type eq "varchar" or $type eq "nvarchar" or $type eq "char varying"
l!l&&r!l&&!r
5434240
$type eq "varchar" or $type eq "nvarchar"
l!l&&r!l&&!r
5403244
MC/DC100%4/4
$type eq "varchar" or $type eq "nvarchar" or $type eq "char varying" or $type eq "character varying"
$type eq "varchar" $type eq "nvarchar" $type eq "char varying" $type eq "character varying"
Truth table100%5/5
A or B or C or D
A= $type eq "varchar"B= $type eq "nvarchar"C= $type eq "char varying"D= $type eq "character varying"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
397 || $type eq 'char varying' || $type eq 'character varying') {
398 548 ▶ return sprintf 'VARCHAR(%d)', defined $len ? $len : 255;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 477 71 548
399 }
400 239 ▶ if ($type eq 'char' || $type eq 'nchar' || $type eq 'character') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "char" or $type eq "nchar" or $type eq "character") 18 221 239
Condition100%6/6
$type eq "char" or $type eq "nchar" or $type eq "character"
l!l&&r!l&&!r
135221
$type eq "char" or $type eq "nchar"
l!l&&r!l&&!r
112226
MC/DC100%3/3
$type eq "char" or $type eq "nchar" or $type eq "character"
$type eq "char" $type eq "nchar" $type eq "character"
Truth table100%4/4
A or B or C
A= $type eq "char"B= $type eq "nchar"C= $type eq "character"
ABCresult
1XX1
01X1
0011
0000
401 18 ▶ return sprintf 'CHAR(%d)', defined $len ? $len : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 13 5 18
402 }
403 # mediumtext is MySQL's; ASE has one text type (Msg 2715 passed through).
404 221 ▶ return 'TEXT' if $type eq 'text' || $type eq 'long' || $type eq 'mediumtext';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" or $type eq "long" or $type eq "mediumtext" 5 216 221
Condition83%5/6
$type eq "text" or $type eq "long" or $type eq "mediumtext"
l!l&&r!l&&!r
32216
$type eq "text" or $type eq "long"
l!l&&r!l&&!r
30218
MC/DC66%2/3
$type eq "text" or $type eq "long" or $type eq "mediumtext"
$type eq "text" $type eq "long" $type eq "mediumtext"
Truth table75%3/4
A or B or C
A= $type eq "text"B= $type eq "long"C= $type eq "mediumtext"
ABCresult
1XX1
01X1
0011
0000
405  
406 # Date/time types
407 216 ▶ 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" 19 197 216
Condition100%6/6
$type eq "date" or $type eq "timestamp" or $type eq "datetime"
l!l&&r!l&&!r
415197
$type eq "date" or $type eq "timestamp"
l!l&&r!l&&!r
13212
MC/DC100%3/3
$type eq "date" or $type eq "timestamp" or $type eq "datetime"
$type eq "date" $type eq "timestamp" $type eq "datetime"
Truth table100%4/4
A or B or C
A= $type eq "date"B= $type eq "timestamp"C= $type eq "datetime"
ABCresult
1XX1
01X1
0011
0000
408 197 ▶ return 'SMALLDATETIME' if $type eq 'smalldatetime';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "smalldatetime" 1 196 197
409  
410 # Binary types
411 196 ▶ return 'IMAGE' if $type eq 'bytea' || $type eq 'blob';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bytea" or $type eq "blob" 4 192 196
Condition100%3/3
$type eq "bytea" or $type eq "blob"
l!l&&r!l&&!r
13192
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
412 192 ▶ return 'TEXT' if $type eq 'clob';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "clob" 1 191 192
413  
414 # Numeric types
415 191 ▶ if ($type eq 'numeric' || $type eq 'decimal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "numeric" or $type eq "decimal") 79 112 191
Condition100%3/3
$type eq "numeric" or $type eq "decimal"
l!l&&r!l&&!r
6910112
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
416 79 ▶ return sprintf 'NUMERIC(%d,%d)', $size->[0], $size->[1] // 0 if ref $size eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $size eq "ARRAY" 19 60 79
Condition50%1/2
$size->[1] // 0
l!l
190
MC/DC0%0/1
$size->[1] // 0
$size->[1]
Truth table50%1/2
A
A= $size->[1]
Aresult
11
00
417 60 ▶ return sprintf 'NUMERIC(%d,0)', $size if defined $size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 3 57 60
418 57 return 'NUMERIC(18,6)';
419 }
420 112 ▶ return 'FLOAT' if $type eq 'float' || $type eq 'real';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "float" or $type eq "real" 11 101 112
Condition100%3/3
$type eq "float" or $type eq "real"
l!l&&r!l&&!r
65101
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
421 101 ▶ return 'DOUBLE PRECISION' if $type eq 'double precision' || $type eq 'double';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "double precision" or $type eq "double" 6 95 101
Condition100%3/3
$type eq "double precision" or $type eq "double"
l!l&&r!l&&!r
5195
MC/DC100%2/2
$type eq "double precision" or $type eq "double"
$type eq "double precision" $type eq "double"
Truth table100%3/3
A or B
A= $type eq "double precision"B= $type eq "double"
ABresult
1X1
011
000
422  
423 # Boolean — Sybase has bit
424 95 ▶ return 'BIT' if $type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" 16 79 95
425  
426 # GUID. ASE has no such type -- passed through, `UNIQUEIDENTIFIER` is Msg
427 # 2715 "Can't find type" -- so the column holds the string form (karr k52):
428 # 36 characters hyphenated, which also takes newid()'s 32. The width is the
429 # type's own; a declared size does not move it.
430 79 ▶ return 'VARCHAR(36)' if $type eq 'uniqueidentifier';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "uniqueidentifier" 15 64 79
431  
432 # Pass through unknown types as-is (uppercase), with the size they were
433 # given (karr k70): binary(n), varbinary(n), unichar(n), univarchar(n) and a
434 # user-defined type lose their length otherwise and deploy as a length of 1.
435 # The ASE types that take no length are left bare, so a hand-written
436 # `int, size => 11` does not become INT(11).
437 64 ▶ return uc($type) if $type =~ $NO_LENGTH_TYPE;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /$DBIO::Sybase::DDL::NO_LENGTH_TYPE/ 50 14 64
438 14 ▶ my @args = grep { defined } (ref $size eq 'ARRAY' ? @$size : $size);
Branch: 1 branch
BranchTrueFalseTotal
ref $size eq 'ARRAY' ? : 0 14 14
439 14 ▶ return uc($type) unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 9 5 14
440 5 return sprintf '%s(%s)', uc($type), join ',', @args;
441 }
442  
443  
444 525 ▶sub sybase_default_clause {
Subroutine sybase_default_clause: called CC=8 SCAR=20.8
Pod documented
445 525 my ($dv, $data_type, $column_name) = @_;
446 525 ▶ return '' unless defined $dv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dv 373 152 525
447 152 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq "SCALAR") 21 131 152
448 # CURRENT_TIMESTAMP is Msg 128 in an ASE DEFAULT; getdate() is its function.
449 21 ▶ return ' DEFAULT getdate()' if $$dv =~ /\A\s*current_timestamp\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $$dv =~ /\A\s*current_timestamp\s*\z/i 4 17 21
450 17 return " DEFAULT $$dv";
451 }
452 131 ▶ return '' if $dv eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $dv eq "null" 6 125 131
453 125 ▶ if (_is_numeric_type($data_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_numeric_type($data_type)) 66 59 125
454 66 ▶ return " DEFAULT $dv" if _is_numeric_literal($dv);
Branch: 1 branch
BranchTrueFalseTotal
if _is_numeric_literal($dv) 55 11 66
455 11 ▶ Carp::croak sprintf(
Condition100%2/2
$column_name // "(unnamed)"
l!l
101
MC/DC100%1/1
$column_name // "(unnamed)"
$column_name
Truth table100%2/2
A
A= $column_name
Aresult
11
00
456 'DBIO::Sybase: default_value %s of column %s is not a number, but its '
457 . 'type %s takes a numeric default -- quoted, ASE stores it as a VARCHAR '
458 . 'constant and every INSERT leaving the column out fails with Msg 257; '
459 . 'write a number, or a SCALAR ref for an SQL expression',
460 "'$dv'", $column_name // '(unnamed)', sybase_column_type($data_type),
461 );
462 }
463 59 (my $lit = $dv) =~ s/'/''/g;
464 59 return " DEFAULT '$lit'";
465 }
466  
467 # True when a column of this logical/ASE type takes a numeric default. Read off
468 # what sybase_column_type renders, so a type mapped to a numeric SQL type here
469 # is numeric there too; the bare ASE names (MONEY, UINT, ...) pass through that
470 # mapping uppercased and are listed alongside.
471 155 ▶sub _is_numeric_type {
Subroutine _is_numeric_type: called CC=3 SCAR=11.0
472 155 my ($data_type) = @_;
473 155 ▶ return 0 unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 10 145 155
474 145 (my $sql = sybase_column_type($data_type)) =~ s/\(.*//s;
475 145 ▶ return $sql =~ /\A(?:
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /\A(?: (?:UNSIGNED\s+)?(?:TINY|SMALL|BIG)?INT(?:EGER)? | U(?:TINY|SMALL|BIG)?INT | NUMERIC | DECIMAL | FLOAT | REAL | DOUBLE\ PRECISION | BIT | (?:SMALL)?MONEY )\z/x ? : 77 68 145
476 (?:UNSIGNED\s+)?(?:TINY|SMALL|BIG)?INT(?:EGER)?
477 | U(?:TINY|SMALL|BIG)?INT
478 | NUMERIC | DECIMAL | FLOAT | REAL | DOUBLE\ PRECISION
479 | BIT | (?:SMALL)?MONEY
480 )\z/x ? 1 : 0;
481 }
482  
483 # The IDENTITY attribute of an is_auto_increment column, with its leading
484 # space -- or a refusal (karr k52). ASE takes the attribute on the integer
485 # types and NUMERIC(p,0) only (Msg 2764 "Identity field must be a numeric with
486 # a scale of 0", live: INT/SMALLINT/TINYINT/BIGINT/unsigned and NUMERIC(p,0)
487 # accepted; NUMERIC with a scale, DECIMAL, FLOAT, REAL, DOUBLE PRECISION, BIT,
488 # MONEY, SMALLMONEY and every character, date/time and binary type refused). So
489 # on a type KNOWN not to take it there is nothing to render: with it the server
490 # refuses the CREATE TABLE, without it the column deploys and the value the
491 # model expects from the server never comes.
492 #
493 # $sql_type is the column's rendered type (sybase_column_type), so a logical
494 # name is judged by what it deploys as (numeric/decimal without a size is
495 # NUMERIC(18,6), so refused). Three cases, deliberately:
496 #
497 # - an integer type or NUMERIC with scale 0: IDENTITY.
498 # - known not to take it (the lists below): croak.
499 # - anything else is a name this module does not know -- a user-defined
500 # datatype, which may be numeric -- and passes with its IDENTITY.
501 #
502 # Called by DBIO::Sybase::Diff::Table::Create too: both emitters of the column
503 # grammar carry the one rule.
504 63 ▶sub _identity_clause {
Subroutine _identity_clause: called CC=10 SCAR=23.3
505 63 my ($sql_type, $data_type, $column_name, $table_name) = @_;
506 63 (my $base = $sql_type) =~ s/\(.*//s;
507 63 ▶ my $refused = $base =~ /\A(?:
Branch: 1 branch
BranchTrueFalseTotal
$base =~ /\A(?: (?:UNI)?(?:VAR)?CHAR | (?:UNI)?TEXT | (?:SMALL|BIG)?DATETIME | (?:BIG)?TIME | DATE | (?:VAR)?BINARY | IMAGE | FLOAT | REAL | DOUBLE\ PRECISION | BIT | (?:SMALL)?MONEY | DECIMAL )\z/x ? : 23 40 63
508 (?:UNI)?(?:VAR)?CHAR | (?:UNI)?TEXT
509 | (?:SMALL|BIG)?DATETIME | (?:BIG)?TIME | DATE
510 | (?:VAR)?BINARY | IMAGE
511 | FLOAT | REAL | DOUBLE\ PRECISION | BIT | (?:SMALL)?MONEY | DECIMAL
512 )\z/x ? 1 : 0;
513 # NUMERIC takes it with a scale of 0 only.
514 63 ▶ $refused = 1 if !$refused && $base eq 'NUMERIC'
Branch: 1 branch
BranchTrueFalseTotal
if not $refused and $base eq "NUMERIC" and $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/ and $1 != 0 7 56 63
Condition88%8/9
not $refused and $base eq "NUMERIC" and $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/ and $1 != 0
!ll&&!rl&&r
5247
not $refused and $base eq "NUMERIC" and $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/
!ll&&!rl&&r
52011
not $refused and $base eq "NUMERIC"
!ll&&!rl&&r
232911
MC/DC75%3/4
not $refused and $base eq "NUMERIC" and $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/ and $1 != 0
not $refused $base eq "NUMERIC" $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/ $1 != 0
Truth table80%4/5
not(A) and B and C and D
A= not $refusedB= $base eq "NUMERIC"C= $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/D= $1 != 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
515 && $sql_type =~ /\(\s*\d+\s*,\s*(\d+)\s*\)/ && $1 != 0;
516 63 ▶ Carp::croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $refused 30 33 63
Condition50%3/6
$column_name // "(unnamed)"
l!l
300
$table_name // "(unnamed)"
l!l
300
$data_type // "(none)"
l!l
300
MC/DC0%0/1
$column_name // "(unnamed)"
$column_name
MC/DC0%0/1
$table_name // "(unnamed)"
$table_name
MC/DC0%0/1
$data_type // "(none)"
$data_type
Truth table50%1/2
A
A= $column_name
Aresult
11
00
Truth table50%1/2
A
A= $table_name
Aresult
11
00
Truth table50%1/2
A
A= $data_type
Aresult
11
00
517 'DBIO::Sybase: column %s of table %s is is_auto_increment, but its '
518 . 'data_type %s is %s on ASE, and ASE takes IDENTITY on an integer '
519 . 'or NUMERIC(p,0) column only (Msg 2764); if the value is generated '
520 . 'some other way (a GUID, a function), declare the column with '
521 . 'auto_nextval instead of is_auto_increment',
522 $column_name // '(unnamed)', $table_name // '(unnamed)',
523 $data_type // '(none)', $sql_type,
524 ) if $refused;
525 33 return ' IDENTITY';
526 }
527  
528 # A numeric literal as ASE parses one in a DEFAULT: optional sign, digits with
529 # an optional fraction (or a fraction alone), optional exponent. Nothing else --
530 # no whitespace, no currency sign, no hex.
531 77 ▶sub _is_numeric_literal {
Subroutine _is_numeric_literal: called CC=4 SCAR=14.9
532 77 my ($v) = @_;
533 77 ▶ return defined $v && !ref $v
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) && $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/ ? : 64 13 77
Condition50%3/6
defined $v and not ref $v and $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/
!ll&&!rl&&r
01364
defined $v and not ref $v
!ll&&!rl&&r
0077
MC/DC33%1/3
defined $v and not ref $v and $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/
defined $v not ref $v $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/
Truth table50%2/4
A and not(B) and C
A= defined $vB= not ref $vC= $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/
ABCresult
0XX0
10X0
1100
1111
534 && $v =~ /\A[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/ ? 1 : 0;
535 }
536  
537  
538 40 1;
539  
540 __END__
« Sybase/Storage/ASE/BulkInsert.pm ↑ Summary Sybase/Introspect/Views.pm »