blib/lib/DB/Introspector/Postgres/Introspector.pm

filter: Statementstmt 39.3% 70 / 178 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.4% 23 / 43 Podpod 0.0% 0 / 3 totaltotal 38.1% 93 / 244 CC 10 SCAR 16.7
CC
10
Cov
36%
_lookup_fk_triggers
30.0
_parse_foreign_key_data
12.0
get_primary_key_column_ids
6.0
CRAP 5.3
« blib/lib/DB/Introspector/Base/Table.pm ↑ Summary blib/lib/DB/Introspector/Base/ForeignKey.pm »
1 package DB::Introspector::Postgres::Introspector;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw( DB::Introspector::CommonRDB::Introspector );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant SINGLE_TABLE_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
9 'SELECT LOWER(relname) AS %s, relowner AS %s FROM pg_class
10 1 WHERE LOWER(relname)=?';
11  
12 1 ▶use constant ALL_TABLES_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
13 q(SELECT LOWER(relname) AS %s, relowner AS %s FROM pg_class
14 1 WHERE relowner>1 AND relkind='r');
15  
16 0 ▶sub get_single_table_lookup_statement {
Subroutine get_single_table_lookup_statement: not called CC=1 SCAR=6.9
Pod undocumented
17 0 my $self = shift;
18 0 my $table_name = lc(shift);
19  
20 0 my $query = sprintf(SINGLE_TABLE_QUERY,
21 &DB::Introspector::CommonRDB::Introspector::TABLE_NAME_COL,
22 &DB::Introspector::CommonRDB::Introspector::OWNER_NAME_COL);
23 0 my $sth = $self->dbh->prepare_cached($query);
24 0 $sth->execute($table_name);
25 0 return $sth;
26 }
27  
28  
29 0 ▶sub _cached_table {
Subroutine _cached_table: not called CC=1 SCAR=6.9
30 0 my $self = shift;
31 0 my $table_name = lc(shift);
32 0 $self->SUPER::_cached_table($table_name);
33 }
34  
35 0 ▶sub get_all_tables_lookup_statement {
Subroutine get_all_tables_lookup_statement: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my $self = shift;
37  
38 0 my $query = sprintf(ALL_TABLES_QUERY,
39 &DB::Introspector::CommonRDB::Introspector::TABLE_NAME_COL,
40 &DB::Introspector::CommonRDB::Introspector::OWNER_NAME_COL);
41 0 my $sth = $self->dbh->prepare_cached($query);
42 0 $sth->execute();
43 0 return $sth;
44 }
45  
46 0 ▶sub get_table_class {
Subroutine get_table_class: not called CC=1 SCAR=6.9
Pod undocumented
47 0 return q(DB::Introspector::Postgres::Table);
48 }
49  
50  
51  
52 package DB::Introspector::Postgres::Table;
53  
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use base qw( DB::Introspector::CommonRDB::Table );
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58  
59 1 ▶use DB::Introspector::Base::BooleanColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use DB::Introspector::Base::IntegerColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use DB::Introspector::Base::CharColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use DB::Introspector::Base::StringColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use DB::Introspector::Base::DateTimeColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use DB::Introspector::Base::CLOBColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use constant COLUMN_CLASS_MAPPING => {
Subroutine BEGIN: called CC=1 SCAR=0.0
67 'bool' => 'DB::Introspector::Base::BooleanColumn',
68 'int2' => 'DB::Introspector::Base::IntegerColumn',
69 'int4' => 'DB::Introspector::Base::IntegerColumn',
70 'int8' => 'DB::Introspector::Base::IntegerColumn',
71 'bpchar' => 'DB::Introspector::Base::CharColumn',
72 'varchar' => 'DB::Introspector::Base::StringColumn',
73 'timestamptz' => 'DB::Introspector::Base::DateTimeColumn',
74 'timestamp' => 'DB::Introspector::Base::DateTimeColumn',
75 'text' => 'DB::Introspector::Base::CLOBColumn',
76 1 };
77  
78  
79  
80 1 ▶use constant COLUMN_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
81 'SELECT LOWER(a.attname) AS NAME, t.typname AS TYPE
82 FROM pg_attribute a, pg_type t, pg_type r
83 WHERE a.attrelid=r.typrelid
84 AND t.oid=a.atttypid
85 AND a.attnum > 0 AND LOWER(r.typname)=?
86 1 ORDER BY a.attnum';
87  
88 1 ▶use constant FOREIGN_KEYS_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
89 'SELECT tr.tgnargs AS LENGTH, tr.tgargs AS ARGUMENTS
90 FROM pg_trigger tr, pg_type t
91 WHERE tr.tgisconstraint
92 AND tr.tgenabled
93 AND tr.tgrelid=t.typrelid
94 AND tr.tgargs like ?
95 1 GROUP BY tr.tgnargs, tr.tgargs';
96  
97 1 ▶use constant PRIMARY_KEYS_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
98 'SELECT indkey AS IDS FROM pg_index i,
99 pg_type c
100 1 WHERE i.indisprimary and i.indrelid=c.typrelid and LOWER(c.typname)=?';
101  
102 1 ▶use constant INDEX_LOOKUP_QUERY =>
Subroutine BEGIN: called CC=1 SCAR=0.0
103 q(select i.relname AS name,
104 ind.indkey AS column_ids,
105 ind.indisunique AS unique
106 FROM pg_class t,
107 pg_class i,
108 pg_index ind
109 WHERE ind.indexrelid=i.oid
110 AND ind.indrelid=t.oid
111 1 AND t.relname=?);
112  
113  
114 1 ▶use constant COLUMN_ARG_DELIM => '\0';
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 0 ▶sub get_primary_key_column_ids {
Subroutine get_primary_key_column_ids: not called CC=2 SCAR=17.9
117 0 my $self = shift;
118  
119 0 ▶ unless( $self->{_primary_key_column_ids} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_primary_key_column_ids"}) 0 0 0
120 0 my $sth = $self->_introspector->dbh->prepare_cached(PRIMARY_KEYS_QUERY);
121 0 $sth->execute(lc($self->name));
122 0 my $fetched_row = $sth->fetchrow_hashref('NAME_lc');
123 0 $sth->finish();
124 0 my @ids = map { $_-1; } split(/\s+/, $fetched_row->{ids});
125 0 $self->{_primary_key_column_ids} = \@ids;
126 }
127 0 return @{$self->{_primary_key_column_ids}};
128 }
129  
130 0 ▶sub get_column_lookup_statement {
Subroutine get_column_lookup_statement: not called CC=1 SCAR=6.9
131 0 my $self = shift;
132  
133 0 my $sth = $self->_introspector->dbh->prepare_cached(COLUMN_QUERY);
134  
135 0 $sth->execute(lc($self->name));
136  
137 0 return $sth;
138 }
139  
140 0 ▶sub get_indexes_lookup_statement {
Subroutine get_indexes_lookup_statement: not called CC=1 SCAR=6.9
141 0 my $self = shift;
142  
143 0 my $sth = $self->_introspector->dbh->prepare_cached(INDEX_LOOKUP_QUERY);
144  
145 0 $sth->execute(lc($self->name));
146  
147 0 return $sth;
148 }
149  
150 0 ▶sub get_foreign_keys_lookup_statement {
Subroutine get_foreign_keys_lookup_statement: not called CC=1 SCAR=6.9
151 0 my $self = shift;
152  
153 0 my $sth = $self->_introspector->dbh->prepare_cached(FOREIGN_KEYS_QUERY);
154  
155 0 $sth->execute('%\000'.$self->name.'\000%');
156  
157 0 return $sth;
158 }
159  
160 0 ▶sub _lookup_dependencies {
Subroutine _lookup_dependencies: not called CC=1 SCAR=6.9
161 0 my $self = shift;
162  
163 0 my ($foreign_keys, $dependencies) = $self->_lookup_fk_triggers;
164  
165 0 return $dependencies;
166 }
167  
168 0 ▶sub _lookup_foreign_keys {
Subroutine _lookup_foreign_keys: not called CC=1 SCAR=6.9
169 0 my $self = shift;
170 0 my ($foreign_keys, $dependencies) = $self->_lookup_fk_triggers;
171  
172 0 return $foreign_keys;
173 }
174  
175  
176 0 ▶sub _lookup_fk_triggers {
Subroutine _lookup_fk_triggers: not called CC=5 SCAR=34.0
177 0 my $self = shift;
178  
179 0 my $sth = $self->get_foreign_keys_lookup_statement();
180  
181 0 my @foreign_keys;
182 my @dependencies;
183  
184 0 my %visited_trigger;
185  
186 # I think the only way for us to know if we have a foreign key (as opposed
187 # to another table referencing us) is to parse the arguments of the trigger
188 # implementation and make sure that the 2nd element is equal to our table
189 # name. With the new dictionary tables (like pg_constraints) in Postgres
190 # 7.3, I don't think we will have to do this.
191 0 ▶ while( my $row = $sth->fetchrow_hashref('NAME_uc') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref("NAME_uc")) 0 0 0
192  
193 # Since triggers appear more than once, but with different ids, this
194 # hack acts as a way to make these triggers unique.
195 0 ▶ if( $visited_trigger{$row->{ARGUMENTS}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visited_trigger{$row->{'ARGUMENTS'}}) { } 0 0 0
196 0 next;
197 } else {
198 0 $visited_trigger{$row->{ARGUMENTS}} = 1;
199 }
200
201 0 my @column_args = split(COLUMN_ARG_DELIM, $row->{'ARGUMENTS'});
202 0 ▶ if( lc($column_args[1]) eq lc($self->name) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $column_args[1] eq lc $self->name) { } 0 0 0
elsif (lc $column_args[2] eq lc $self->name) { } 0 0 0
203 # dealing with foreign keys referencing other tables
204 0 my $foreign_key = $self->get_foreign_key_class()
205 ->new($self,0,%$row);
206 0 push(@foreign_keys, $foreign_key);
207 } elsif( lc($column_args[2]) eq lc($self->name) ) {
208 # dealing with dependencies referencing us
209 0 my $t = $self->_introspector->find_table($column_args[1]);
210 0 my $foreign_key = $self->get_foreign_key_class()->new($t,1,%$row);
211 0 push(@dependencies, $foreign_key);
212 }
213  
214 }
215  
216 0 $sth->finish();
217  
218 0 return (\@foreign_keys, \@dependencies);
219 }
220  
221 0 ▶sub get_column_instance {
Subroutine get_column_instance: not called CC=2 SCAR=17.9
222 0 my $self = shift;
223 0 my $name = shift;
224 0 my $type_name = lc(shift);
225  
226 0 ▶ my $class = COLUMN_CLASS_MAPPING()->{$type_name} || return;
Condition0%0/2
{bool => 'DB::Introspector::Base::BooleanColumn', bpchar => 'DB::Introspector::Base::CharColumn', int2 => 'DB::Introspector::Base::IntegerColumn', int4 => 'DB::Introspector::Base::IntegerColumn', int8 => 'DB::Introspector::Base::IntegerColumn', text => 'DB::Introspector::Base::CLOBColumn', timestamp => 'DB::Introspector::Base::DateTimeColumn', timestamptz => 'DB::Introspector::Base::DateTimeColumn', varchar => 'DB::Introspector::Base::StringColumn'}->{$type_name} || (return)
l!l
00
MC/DC0%0/1
{bool => 'DB::Introspector::Base::BooleanColumn', bpchar => 'DB::Introspector::Base::CharColumn', int2 => 'DB::Introspector::Base::IntegerColumn', int4 => 'DB::Introspector::Base::IntegerColumn', int8 => 'DB::Introspector::Base::IntegerColumn', text => 'DB::Introspector::Base::CLOBColumn', timestamp => 'DB::Introspector::Base::DateTimeColumn', timestamptz => 'DB::Introspector::Base::DateTimeColumn', varchar => 'DB::Introspector::Base::StringColumn'}->{$type_name} || (return)
{bool => 'DB::Introspector::Base::BooleanColumn', bpchar => 'DB::Introspector::Base::CharColumn', int2 => 'DB::Introspector::Base::IntegerColumn', int4 => 'DB::Introspector::Base::IntegerColumn', int8 => 'DB::Introspector::Base::IntegerColumn', text => 'DB::Introspector::Base::CLOBColumn', timestamp => 'DB::Introspector::Base::DateTimeColumn', timestamptz => 'DB::Introspector::Base::DateTimeColumn', varchar => 'DB::Introspector::Base::StringColumn'}->{$type_name}
Truth table0%0/2
A
A= {bool => 'DB::Introspector::Base::BooleanColumn', bpchar => 'DB::Introspector::Base::CharColumn', int2 => 'DB::Introspector::Base::IntegerColumn', int4 => 'DB::Introspector::Base::IntegerColumn', int8 => 'DB::Introspector::Base::IntegerColumn', text => 'DB::Introspector::Base::CLOBColumn', timestamp => 'DB::Introspector::Base::DateTimeColumn', timestamptz => 'DB::Introspector::Base::DateTimeColumn', varchar => 'DB::Introspector::Base::StringColumn'}->{$type_name}
Aresult
11
00
227 0 return $class->new($name);
228 }
229  
230 0 ▶sub get_foreign_key_class { return 'DB::Introspector::Postgres::ForeignKey'; }
Subroutine get_foreign_key_class: not called CC=1 SCAR=6.9
231  
232 0 ▶sub get_index_class { return 'DB::Introspector::Postgres::Index'; }
Subroutine get_index_class: not called CC=1 SCAR=6.9
233  
234  
235  
236 package DB::Introspector::Postgres::ForeignKey;
237  
238 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 1 ▶use base qw( DB::Introspector::CommonRDB::ForeignKey );
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
243 0 my $class = shift;
244 0 my $self = $class->SUPER::new(shift(), shift());
245  
246 0 my %args = @_;
247  
248 0 $self->_parse_foreign_key_data($args{LENGTH}, $args{ARGUMENTS});
249 0 return $self;
250 }
251  
252  
253 # Parsing a string like the following:
254 #<unnamed>\000news_articles\000users\000UNSPECIFIED\000created_by\000user_id\000
255 0 ▶sub _parse_foreign_key_data {
Subroutine _parse_foreign_key_data: not called CC=3 SCAR=24.8
256 0 my $self = shift;
257  
258 # subtracting 4 from our arg length to get the number of column name args
259 # we have
260 0 my $length = shift() - 4;
261 0 my $arg_string = shift;
262  
263 # parse the trigger arguments
264 0 my @data = split(DB::Introspector::Postgres::Table::COLUMN_ARG_DELIM,
265 $arg_string);
266  
267 # the very first string is useless to us
268 0 shift(@data);
269  
270 # the first two arguments are table names.
271 0 my $local_table_name = shift(@data);
272 0 my $foreign_table_name = shift(@data);
273  
274 # if our local table name doesn't match our local table's name then
275 # we have a problem.
276 0 ▶ unless( lc($local_table_name) eq lc($self->local_table->name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (lc $local_table_name eq lc $self->local_table->name) 0 0 0
277 0 die("INVALID FOREIGN KEY: local table $local_table_name "
278 ."doesn't match local table ".$self->local_table->name);
279 }
280  
281 # setting the foreign table name
282 0 $self->set_foreign_table_name($foreign_table_name);
283  
284 # the fourth argument is useless to us so we will toss it.
285 0 shift(@data);
286  
287 # since each argument is grouped in pairs, we will walk through and pull
288 # out two at a time, the first for the local table and the second for the
289 # remote table.
290 0 my (@local_column_names, @foreign_column_names);
291 0 ▶ while( @data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
292 0 push(@local_column_names, shift(@data));
293 0 push(@foreign_column_names, shift(@data));
294 }
295 0 $self->set_local_column_names(\@local_column_names);
296 0 $self->set_foreign_column_names(\@foreign_column_names);
297 }
298  
299  
300 package DB::Introspector::Postgres::Index;
301  
302 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
303  
304 1 ▶use base qw( DB::Introspector::CommonRDB::Index );
Subroutine BEGIN: called CC=1 SCAR=0.0
305  
306 1 ▶use constant COLUMN_ARG_DELIM => ' ';
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
309 0 my $class = shift;
310 0 my $table = shift;
311 0 my %row = @_;
312 0 my $self = $class->SUPER::new($table, $row{UNIQUE});
313 0 ▶ $self->{_name} = $row{NAME} || "";
Condition0%0/2
$row{'NAME'} || ''
l!l
00
MC/DC0%0/1
$row{'NAME'} || ''
$row{'NAME'}
Truth table0%0/2
A
A= $row{'NAME'}
Aresult
11
00
314 0 $self->set_column_names([$self->_resolve_column_names($row{COLUMN_IDS})]);
315 0 $self;
316 }
317  
318 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
319 0 my $self = shift;
320 0 return $self->{_name};
321 }
322  
323 0 ▶sub _resolve_column_names {
Subroutine _resolve_column_names: not called CC=1 SCAR=6.9
324 0 my $self = shift;
325 0 my $column_ids = shift;
326 0 my @ids = split(COLUMN_ARG_DELIM, $column_ids);
327 0 my @columns = $self->table->columns;
328 0 return map { $columns[$_-1]->name; } @ids;
329 }
330  
331 0 ▶sub column_names {
Subroutine column_names: not called CC=1 SCAR=6.9
332 0 my $self = shift;
333 0 return @{$self->{column_names}};
334 }
335  
336 1 1;
« blib/lib/DB/Introspector/Base/Table.pm ↑ Summary blib/lib/DB/Introspector/Base/ForeignKey.pm »