blib/lib/SQL/Translator/Parser/DBI/Sybase.pm

filter: Statementstmt 21.1% 22 / 104 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 19.3% 29 / 150 CC 26 SCAR 63.5
CC
26
Cov
16%
parse
702.0
BEGIN
1.0
BEGIN
1.0
CRAP 570.6
« blib/lib/SQL/Translator/Parser/DBI/DB2.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/SQLServer.pm »
1 package SQL::Translator::Parser::DBI::Sybase;
2  
3 =head1 NAME
4  
5 SQL::Translator::Parser::DBI::Sybase - parser for DBD::Sybase
6  
7 =head1 SYNOPSIS
8  
9 See SQL::Translator::Parser::DBI.
10  
11 =head1 DESCRIPTION
12  
13 Uses DBI Catalog Methods.
14  
15 =cut
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use SQL::Translator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our ($DEBUG, @EXPORT_OK);
24 1 our $VERSION = '1.66';
25 1 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Parser::DBI::Sybase::DEBUG 1 0 1
26  
27 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub parse {
Subroutine parse: not called CC=26 SCAR=65.5
Pod undocumented
30 0 my ($tr, $dbh) = @_;
31  
32 0 ▶ if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"FetchHashKeyName"} ne "NAME_uc") 0 0 0
33 0 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
34 0 $dbh->{FetchHashKeyName} = 'NAME_uc';
35 }
36  
37 0 ▶ if ($dbh->{ChopBlanks} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"ChopBlanks"} != 1) 0 0 0
38 0 warn "setting dbh attribute {ChopBlanks} to 1";
39 0 $dbh->{ChopBlanks} = 1;
40 }
41  
42 0 my $schema = $tr->schema;
43  
44 0 my ($sth, @tables, $columns);
45 0 my $stuff;
46  
47 ### Columns
48  
49 # it is much quicker to slurp back everything all at once rather
50 # than make repeated calls
51  
52 0 $sth = $dbh->column_info(undef, undef, undef, undef);
53  
54 0 foreach my $c (@{ $sth->fetchall_arrayref({}) }) {
55 $columns->{ $c->{TABLE_CAT} }->{ $c->{TABLE_SCHEM} }
56 0 ->{ $c->{TABLE_NAME} }->{columns}->{ $c->{COLUMN_NAME} } = $c;
57 }
58  
59 ### Tables and views
60  
61 # Get a list of the tables and views.
62 0 $sth = $dbh->table_info();
63 0 @tables = @{ $sth->fetchall_arrayref({}) };
64  
65 0 my $h = $dbh->selectall_arrayref(
66 q{
67 SELECT o.name, colid,colid2,c.text
68 FROM syscomments c
69 JOIN sysobjects o
70 ON c.id = o.id
71 WHERE o.type ='V'
72 ORDER BY o.name,
73 c.colid,
74 c.colid2
75 }
76 );
77  
78 # View text
79 # I had always thought there was something 'hard' about
80 # reconstructing text from syscomments ..
81 # this seems to work fine and is certainly not complicated!
82  
83 0 foreach (@{$h}) {
84 0 $stuff->{view}->{ $_->[0] }->{text} .= $_->[3];
85 }
86  
87 #### objects with indexes.
88 0 ▶ map { $stuff->{indexes}->{ $_->[0] }++ if defined; } @{
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 0 0
89 0 $dbh->selectall_arrayref(
90 "SELECT DISTINCT object_name(id) AS name
91 FROM sysindexes
92 WHERE indid > 0"
93 )
94 };
95  
96 ## slurp objects
97 0 map { $stuff->{ $_->[1] }->{ $_->[0] } = $_; } @{ $dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects") };
98  
99 ### Procedures
100  
101 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
102 map {
103 0 my $n = $_->{PROCEDURE_NAME};
104 0 $n =~ s/;\d+$//; # Ignore versions for now
105 0 $_->{name} = $n;
106 0 $stuff->{procedures}->{$n} = $_;
107 0 } values %{ $dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME') };
108  
109 # And this blasts in the text of 'legit' stored procedures. Do
110 # this rather than calling sp_helptext in a loop.
111  
112 0 $h = $dbh->selectall_arrayref(
113 q{
114 SELECT o.name, colid,colid2,c.text
115 FROM syscomments c
116 JOIN sysobjects o
117 ON c.id = o.id
118 WHERE o.type ='P'
119 ORDER BY o.name,
120 c.colid,
121 c.colid2
122 }
123 );
124  
125 0 foreach (@{$h}) {
126 $stuff->{procedures}->{ $_->[0] }->{text} .= $_->[3]
127 0 ▶ if (defined($stuff->{procedures}->{ $_->[0] }));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stuff->{"procedures"}{$_->[0]} 0 0 0
128 }
129  
130 ### Defaults
131 ### Rules
132 ### Bind Defaults
133 ### Bind Rules
134  
135 ### Triggers
136 # Since the 'target' of the trigger is defined in the text, we will
137 # just create them independently for now rather than associating them
138 # with a table.
139  
140 0 $h = $dbh->selectall_arrayref(
141 q{
142 SELECT o.name, colid,colid2,c.text
143 FROM syscomments c
144 JOIN sysobjects o
145 ON c.id = o.id
146 JOIN sysobjects o1
147 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
148 WHERE o.type ='TR'
149 ORDER BY o.name,
150 c.colid,
151 c.colid2
152 }
153 );
154 0 foreach (@{$h}) {
155 0 $stuff->{triggers}->{ $_->[0] }->{text} .= $_->[3];
156 }
157  
158 ### References
159 ### Keys
160  
161 ### Types
162 # Not sure what to do with these?
163 0 $stuff->{type_info_all} = $dbh->type_info_all;
164  
165 ### Tables
166 # According to the DBI docs, these can be
167  
168 # "TABLE"
169 # "VIEW"
170 # "SYSTEM TABLE"
171 # "GLOBAL TEMPORARY",
172 # "LOCAL TEMPORARY"
173 # "ALIAS"
174 # "SYNONYM"
175  
176 0 foreach my $table_info (@tables) {
177 next
178 0 ▶ unless (defined($table_info->{TABLE_TYPE}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_info->{"TABLE_TYPE"} 0 0 0
179  
180 0 ▶ if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($table_info->{'TABLE_TYPE'} =~ /TABLE/) { } 0 0 0
elsif ($table_info->{'TABLE_TYPE'} eq 'VIEW') { } 0 0 0
181 my $table = $schema->add_table(
182 name => $table_info->{TABLE_NAME},
183 type => $table_info->{TABLE_TYPE},
184 0 ▶ ) || die $schema->error;
Condition0%0/2
$schema->add_table('name', $table_info->{'TABLE_NAME'}, 'type', $table_info->{'TABLE_TYPE'}) || die($schema->error)
l!l
00
MC/DC0%0/1
$schema->add_table('name', $table_info->{'TABLE_NAME'}, 'type', $table_info->{'TABLE_TYPE'}) || die($schema->error)
$schema->add_table('name', $table_info->{'TABLE_NAME'}, 'type', $table_info->{'TABLE_TYPE'})
Truth table0%0/2
A
A= $schema->add_table('name', $table_info->{'TABLE_NAME'}, 'type', $table_info->{'TABLE_TYPE'})
Aresult
11
00
185  
186 # find the associated columns
187  
188 my $cols = $columns->{ $table_info->{TABLE_QUALIFIER} }
189 ->{ $table_info->{TABLE_OWNER} }->{ $table_info->{TABLE_NAME} }
190 0 ->{columns};
191  
192 0 foreach my $c (values %{$cols}) {
193 my $f = $table->add_field(
194 name => $c->{COLUMN_NAME},
195 data_type => $c->{TYPE_NAME},
196 order => $c->{ORDINAL_POSITION},
197 size => $c->{COLUMN_SIZE},
198 0 ▶ ) || die $table->error;
Condition0%0/2
$table->add_field('name', $c->{'COLUMN_NAME'}, 'data_type', $c->{'TYPE_NAME'}, 'order', $c->{'ORDINAL_POSITION'}, 'size', $c->{'COLUMN_SIZE'}) || die($table->error)
l!l
00
MC/DC0%0/1
$table->add_field('name', $c->{'COLUMN_NAME'}, 'data_type', $c->{'TYPE_NAME'}, 'order', $c->{'ORDINAL_POSITION'}, 'size', $c->{'COLUMN_SIZE'}) || die($table->error)
$table->add_field('name', $c->{'COLUMN_NAME'}, 'data_type', $c->{'TYPE_NAME'}, 'order', $c->{'ORDINAL_POSITION'}, 'size', $c->{'COLUMN_SIZE'})
Truth table0%0/2
A
A= $table->add_field('name', $c->{'COLUMN_NAME'}, 'data_type', $c->{'TYPE_NAME'}, 'order', $c->{'ORDINAL_POSITION'}, 'size', $c->{'COLUMN_SIZE'})
Aresult
11
00
199  
200 $f->is_nullable(1)
201 0 ▶ if ($c->{NULLABLE} == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"NULLABLE"} == 1 0 0 0
202 }
203  
204 # add in primary key
205 0 my $h = $dbh->selectall_hashref(
206 "sp_pkeys
207 [$table_info->{TABLE_NAME}]", 'COLUMN_NAME'
208 );
209 0 ▶ if (scalar keys %{$h} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$h;} > 1) 0 0 0
210 0 my @c = map { $_->{COLUMN_NAME} }
211 0 sort { $a->{KEY_SEQ} <=> $b->{KEY_SEQ} } values %{$h};
212  
213 0 ▶ $table->primary_key(@c)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @c 0 0 0
214 if (scalar @c);
215 }
216  
217 # add in any indexes ... how do we tell if the index has
218 # already been created as part of a primary key or other
219 # constraint?
220  
221 0 ▶ if (defined($stuff->{indexes}->{ $table_info->{TABLE_NAME} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stuff->{"indexes"}{$table_info->{"TABLE_NAME"}}) 0 0 0
222 0 my $h = $dbh->selectall_hashref(
223 "sp_helpindex
224 [$table_info->{TABLE_NAME}]", 'INDEX_NAME'
225 );
226 0 foreach (values %{$h}) {
227 0 my $fields = $_->{'INDEX_KEYS'};
228 0 $fields =~ s/\s*//g;
229 my $i = $table->add_index(
230 name => $_->{INDEX_NAME},
231 0 fields => $fields,
232 );
233 0 ▶ if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"INDEX_DESCRIPTION"} =~ /unique/i) 0 0 0
234 0 $i->type('unique');
235  
236 # we could make this a primary key if there
237 # isn't already one defined and if there
238 # aren't any nullable columns in thisindex.
239  
240 0 ▶ if (!defined($table->primary_key())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table->primary_key) 0 0 0
241 $table->primary_key($fields)
242 0 ▶ unless grep { $table->get_field($_)->is_nullable() }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$table->get_field($_)->is_nullable;} split(/,\s*/, $fields, 0) 0 0 0
243 split(/,\s*/, $fields);
244 }
245 }
246 }
247 }
248 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
249 0 my $view = $schema->add_view(name => $table_info->{TABLE_NAME},);
250  
251 my $cols = $columns->{ $table_info->{TABLE_QUALIFIER} }
252 ->{ $table_info->{TABLE_OWNER} }->{ $table_info->{TABLE_NAME} }
253 0 ->{columns};
254  
255 $view->fields(
256 0 map { $_->{COLUMN_NAME} }
257 0 sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} } values %{$cols}
258 );
259  
260 $view->sql($stuff->{view}->{ $table_info->{TABLE_NAME} }->{text})
261 0 ▶ if (defined($stuff->{view}->{ $table_info->{TABLE_NAME} }->{text}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stuff->{"view"}{$table_info->{"TABLE_NAME"}}{"text"} 0 0 0
262 }
263 }
264  
265 0 foreach my $p (values %{ $stuff->{procedures} }) {
266 my $proc = $schema->add_procedure(
267 name => $p->{name},
268 owner => $p->{PROCEDURE_OWNER},
269 comments => $p->{REMARKS},
270 sql => $p->{text},
271 0 );
272  
273 }
274  
275 ### Permissions
276 ### Groups
277 ### Users
278 ### Aliases
279 ### Logins
280 0 return 1;
281 }
282  
283 1 1;
284  
285 =pod
286  
287 =head1 AUTHOR
288  
289 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
290  
291 =head1 SEE ALSO
292  
293 DBI, DBD::Sybase, SQL::Translator::Schema.
294  
295 =cut
« blib/lib/SQL/Translator/Parser/DBI/DB2.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/SQLServer.pm »