Sybase/Introspect/Columns.pm

filter: Statementstmt 18.4% 7 / 38 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 2 / 4 Podpod 100% 1 / 1 totaltotal 14.2% 10 / 70 CC 16 SCAR 45.0
CC
16
Cov
11%
fetch
132.0
_mark_identity
42.0
BEGIN
1.0
CRAP 89.8
« Sybase/Storage/ASE/BulkInsert.pm ↑ Summary Sybase/Storage/ASE.pm »
1 package DBIO::Sybase::Introspect::Columns;
2 # ABSTRACT: Introspect Sybase ASE columns
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8  
9 0 ▶sub fetch {
Subroutine fetch: not called CC=11 SCAR=48.8
Pod documented
10 0 my ($class, $dbh, $schema, $tables) = @_;
11 0 ▶ $schema //= 'dbo';
Condition0%0/2
$schema //= "dbo"
l!l
00
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
12 0 my %columns;
13  
14 0 my $col_sth = $dbh->prepare(q{
15 SELECT table_name, column_name, data_type,
16 is_nullable, column_default, ordinal_position
17 FROM INFORMATION_SCHEMA.COLUMNS
18 WHERE table_schema = ?
19 ORDER BY table_name, ordinal_position
20 });
21 0 $col_sth->execute($schema);
22  
23 0 ▶ while (my $row = $col_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $col_sth->fetchrow_hashref) 0 0 0
24 0 ▶ next unless exists $tables->{ $row->{table_name} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"table_name"}} 0 0 0
25 0 ▶ push @{ $columns{ $row->{table_name} } }, {
Branch: 1 branch
BranchTrueFalseTotal
lc($row->{'is_nullable'} // 'YES') eq 'no' ? : 0 0 0
Condition0%0/2
$row->{"is_nullable"} // "YES"
l!l
00
MC/DC0%0/1
$row->{"is_nullable"} // "YES"
$row->{"is_nullable"}
Truth table0%0/2
A
A= $row->{"is_nullable"}
Aresult
11
00
26 column_name => $row->{column_name},
27 data_type => $row->{data_type},
28 not_null => (lc($row->{is_nullable} // 'YES') eq 'no') ? 1 : 0,
29 default_value => $row->{column_default},
30 0 is_pk => 0,
31 pk_position => 0,
32 is_auto_increment => 0,
33 };
34 }
35  
36 0 my $pk_sth = $dbh->prepare(q{
37 SELECT kcu.table_name, kcu.column_name, kcu.ordinal_position
38 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
39 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
40 ON tc.constraint_name = kcu.constraint_name
41 AND tc.table_schema = kcu.table_schema
42 AND tc.table_name = kcu.table_name
43 WHERE tc.constraint_type = 'PRIMARY KEY'
44 AND tc.table_schema = ?
45 });
46 0 $pk_sth->execute($schema);
47  
48 0 ▶ while (my $row = $pk_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $pk_sth->fetchrow_hashref) 0 0 0
49 0 ▶ my $list = $columns{ $row->{table_name} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $columns{$row->{"table_name"}} or next 0 0 0
50 0 for my $col (@$list) {
51 0 ▶ if ($col->{column_name} eq $row->{column_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"column_name"} eq $row->{"column_name"}) 0 0 0
52 0 $col->{is_pk} = 1;
53 0 ▶ $col->{pk_position} = $row->{ordinal_position} || 1;
Condition0%0/2
$row->{'ordinal_position'} || 1
l!l
00
MC/DC0%0/1
$row->{'ordinal_position'} || 1
$row->{'ordinal_position'}
Truth table0%0/2
A
A= $row->{'ordinal_position'}
Aresult
11
00
54 0 last;
55 }
56 }
57 }
58  
59 0 _mark_identity($dbh, $schema, \%columns);
60  
61 0 return \%columns;
62 }
63  
64 # Flag identity (autoincrement) columns. The identity attribute is carried in
65 # syscolumns.status bit 0x80, which INFORMATION_SCHEMA does not surface. This
66 # is best-effort: any failure (privileges, non-ASE server) leaves
67 # is_auto_increment at its 0 default rather than aborting introspection.
68 0 ▶sub _mark_identity {
Subroutine _mark_identity: not called CC=6 SCAR=37.4
69 0 my ($dbh, $schema, $columns) = @_;
70  
71 0 ▶ my $rows = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $rows = eval { do { my $sth = $dbh->prepare("\n SELECT o.name AS table_name, c.name AS column_name\n FROM syscolumns c\n JOIN sysobjects o ON c.id = o.id\n JOIN sysusers u ON o.uid = u.uid\n WHERE o.type = 'U'\n AND u.name = ?\n AND (c.status & 128) = 128\n "); $sth->execute($schema); $sth->fetchall_arrayref({}) } } or return 0 0 0
72 0 my $sth = $dbh->prepare(q{
73 SELECT o.name AS table_name, c.name AS column_name
74 FROM syscolumns c
75 JOIN sysobjects o ON c.id = o.id
76 JOIN sysusers u ON o.uid = u.uid
77 WHERE o.type = 'U'
78 AND u.name = ?
79 AND (c.status & 128) = 128
80 });
81 0 $sth->execute($schema);
82 0 $sth->fetchall_arrayref({});
83 } or return;
84  
85 0 for my $row (@$rows) {
86 0 ▶ my $list = $columns->{ $row->{table_name} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $$columns{$row->{"table_name"}} or next 0 0 0
87 0 for my $col (@$list) {
88 0 ▶ if ($col->{column_name} eq $row->{column_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"column_name"} eq $row->{"column_name"}) 0 0 0
89 0 $col->{is_auto_increment} = 1;
90 0 last;
91 }
92 }
93 }
94 }
95  
96 1 1;
97  
98 __END__
99  
100 =pod
101  
102 =encoding UTF-8
103  
104 =head1 NAME
105  
106 DBIO::Sybase::Introspect::Columns - Introspect Sybase ASE columns
107  
108 =head1 VERSION
109  
110 version 0.900001
111  
112 =head1 DESCRIPTION
113  
114 Fetches column metadata via C<INFORMATION_SCHEMA.COLUMNS>. Primary key
115 information is joined in via C<INFORMATION_SCHEMA.KEY_COLUMN_USAGE> +
116 C<INFORMATION_SCHEMA.TABLE_CONSTRAINTS>.
117  
118 =head1 METHODS
119  
120 =head2 fetch
121  
122 my $columns = DBIO::Sybase::Introspect::Columns->fetch($dbh, $schema, $tables);
123  
124 Given the tables hashref from L<DBIO::Sybase::Introspect::Tables>,
125 returns a hashref keyed by table name. Each value is an arrayref of
126 column hashrefs in C<ordinal_position> order with keys:
127 C<column_name>, C<data_type>, C<not_null>, C<default_value>,
128 C<is_pk>, C<pk_position>, C<is_auto_increment>.
129  
130 Identity (autoincrement) columns are detected from the C<syscolumns>
131 identity status bit (C<0x80>), which is not exposed through
132 C<INFORMATION_SCHEMA>. The detection query is best-effort: if it fails
133 (insufficient privileges, or a non-ASE server), columns are still returned
134 with C<is_auto_increment> defaulting to C<0>.
135  
136 =head1 AUTHOR
137  
138 DBIO & DBIx::Class Authors
139  
140 =head1 COPYRIGHT AND LICENSE
141  
142 Copyright (C) 2026 DBIO Authors
143 Portions Copyright (C) 2005-2025 DBIx::Class Authors
144 Based on DBIx::Class, heavily modified.
145  
146 This is free software; you can redistribute it and/or modify it under
147 the same terms as the Perl 5 programming language system itself.
148  
149 =cut
« Sybase/Storage/ASE/BulkInsert.pm ↑ Summary Sybase/Storage/ASE.pm »