DBI/Handle/Pg.pm

filter: Statementstmt 33.6% 34 / 101 Branchbran 30.5% 11 / 36 Conditioncond 42.3% 11 / 26 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 38.4% 5 / 13 Podpod 100% 8 / 8 totaltotal 37.5% 75 / 200 CC 28 SCAR 29.1
CC
28
Cov
34%
insert
42.0
id_sequence_name
30.0
apply_limits
20.0
CRAP 18.4
« DBI/Collection.pm ↑ Summary DBI/Handle/mysql.pm »
1 package Jifty::DBI::Handle::Pg;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use vars qw($VERSION @ISA $DBIHandle $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw(Jifty::DBI::Handle);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Jifty::DBI::Handle::Pg - A Postgres specific Handle object
12  
13 =head1 SYNOPSIS
14  
15  
16 =head1 DESCRIPTION
17  
18 This module provides a subclass of L<Jifty::DBI::Handle> that
19 compensates for some of the idiosyncrasies of Postgres.
20  
21 =head1 METHODS
22  
23 =cut
24  
25 =head2 connect
26  
27 connect takes a hashref and passes it off to SUPER::connect; Forces
28 the timezone to GMT, returns a database handle.
29  
30 =cut
31  
32 sub connect {
33 0 ▶ my $self = shift;
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
34  
35 0 $self->SUPER::connect(@_);
36 0 $self->simple_query("SET TIME ZONE 'GMT'");
37 0 $self->simple_query("SET DATESTYLE TO 'ISO'");
38 0 $self->auto_commit(1);
39 0 return ($DBIHandle);
40 }
41  
42 =head2 insert
43  
44 Takes a table name as the first argument and assumes that the rest of
45 the arguments are an array of key-value pairs to be inserted.
46  
47 In case of insert failure, returns a L<Class::ReturnValue> object
48 preloaded with error info
49  
50 =cut
51  
52 sub insert {
53 0 ▶ my $self = shift;
Subroutine insert: not called CC=6 SCAR=37.4
Pod documented
54 0 my $table = shift;
55 0 my %args = (@_);
56 0 my $sth = $self->SUPER::insert( $table, %args );
57  
58 0 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
59 0 return ($sth);
60 }
61  
62 0 ▶ if ( $args{'id'} || $args{'Id'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"id"} or $args{"Id"}) 0 0 0
Condition0%0/3
$args{"id"} or $args{"Id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"id"} or $args{"Id"}
$args{"id"} $args{"Id"}
Truth table0%0/3
A or B
A= $args{"id"}B= $args{"Id"}
ABresult
1X1
011
000
63 0 ▶ $self->{'id'} = $args{'id'} || $args{'Id'};
Condition0%0/3
$args{'id'} || $args{'Id'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $args{'Id'}
$args{'id'} $args{'Id'}
Truth table0%0/3
A || B
A= $args{'id'}B= $args{'Id'}
ABresult
1X1
011
000
64 0 return ( $self->{'id'} );
65 }
66  
67 0 my $sequence_name = $self->id_sequence_name($table);
68 0 ▶ unless ($sequence_name) { return ($sequence_name) } # Class::ReturnValue
Branch: 1 branch
BranchTrueFalseTotal
unless ($sequence_name) 0 0 0
69 0 my $seqsth = $self->dbh->prepare(
70 qq{SELECT CURRVAL('} . $sequence_name . qq{')} );
71 0 $seqsth->execute;
72 0 $self->{'id'} = $seqsth->fetchrow_array();
73  
74 0 return ( $self->{'id'} );
75 }
76  
77 =head2 id_sequence_name TABLE
78  
79 Takes a TABLE name and returns the name of the sequence of the primary key for that table.
80  
81 =cut
82  
83 sub id_sequence_name {
84 0 ▶ my $self = shift;
Subroutine id_sequence_name: not called CC=5 SCAR=34.0
Pod documented
85 0 my $table = shift;
86  
87 return $self->{'_sequences'}{$table}
88 0 ▶ if ( exists $self->{'_sequences'}{$table} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_sequences"}{$table} 0 0 0
89  
90 #Lets get the id of that row we just inserted
91 0 my $seq;
92 0 my $colinfosth = $self->dbh->column_info( undef, undef, lc($table), '%' );
93 0 ▶ while ( my $foo = $colinfosth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $foo = $colinfosth->fetchrow_hashref) 0 0 0
94  
95 # Regexp from DBIx::Class's Pg handle. Thanks to Marcus Ramberg
96 0 ▶ if ( defined $foo->{'COLUMN_DEF'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foo->{"COLUMN_DEF"} and $foo->{"COLUMN_DEF"} =~ /^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+/i) 0 0 0
Condition0%0/3
defined $foo->{"COLUMN_DEF"} and $foo->{"COLUMN_DEF"} =~ /^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $foo->{"COLUMN_DEF"} and $foo->{"COLUMN_DEF"} =~ /^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+/i
defined $foo->{"COLUMN_DEF"} $foo->{"COLUMN_DEF"} =~ /^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+/i
Truth table0%0/3
A and B
A= defined $foo->{"COLUMN_DEF"}B= $foo->{"COLUMN_DEF"} =~ /^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+/i
ABresult
0X0
100
111
97 && $foo->{'COLUMN_DEF'}
98 =~ m!^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+!i )
99 {
100 0 return $self->{'_sequences'}{$table} = $1;
101 }
102  
103 }
104 0 my $ret = Class::ReturnValue->new();
105 0 $ret->as_error(
106 errno => '-1',
107 message => "Found no sequence for $table",
108 do_backtrace => undef
109 );
110 0 return ( $ret->return_value );
111  
112 }
113  
114 =head2 blob_params column_NAME column_type
115  
116 Returns a hash ref for the bind_param call to identify BLOB types used
117 by the current database for a particular column type. The current
118 Postgres implementation only supports BYTEA types.
119  
120 =cut
121  
122 sub blob_params {
123 0 ▶ my $self = shift;
Subroutine blob_params: not called CC=2 SCAR=17.9
Pod documented
124 0 my $name = shift;
125 0 my $type = shift;
126  
127 # Don't assign to key 'value' as it is defined later.
128 0 ▶ return ( { pg_type => DBD::Pg::PG_BYTEA() } )
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(?:blob|bytea)$/ 0 0 0
129 if $type =~ /^(?:blob|bytea)$/;
130 0 return ( {} );
131 }
132  
133 =head2 apply_limits STATEMENTREF ROWS_PER_PAGE FIRST_ROW
134  
135 takes an SQL SELECT statement and massages it to return ROWS_PER_PAGE
136 starting with FIRST_ROW;
137  
138 =cut
139  
140 sub apply_limits {
141 0 ▶ my $self = shift;
Subroutine apply_limits: not called CC=4 SCAR=30.0
Pod documented
142 0 my $statementref = shift;
143 0 my $per_page = shift;
144 0 my $first = shift;
145  
146 0 my $limit_clause = '';
147  
148 0 ▶ if ($per_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_page) 0 0 0
149 0 $limit_clause = " LIMIT ";
150 0 $limit_clause .= $per_page;
151 0 ▶ if ( $first && $first != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first and $first != 0) 0 0 0
Condition0%0/3
$first and $first != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$first and $first != 0
$first $first != 0
Truth table0%0/3
A and B
A= $firstB= $first != 0
ABresult
0X0
100
111
152 0 $limit_clause .= " OFFSET $first";
153 }
154 }
155  
156 0 $$statementref .= $limit_clause;
157  
158 }
159  
160 =head2 _make_clause_case_insensitive column operator VALUE
161  
162 Takes a column, operator and value. performs the magic necessary to make
163 your database treat this clause as case insensitive.
164  
165 Returns a column operator value triple.
166  
167 =cut
168  
169 sub _make_clause_case_insensitive {
170 0 ▶ my $self = shift;
Subroutine _make_clause_case_insensitive: not called CC=4 SCAR=30.0
171 0 my $column = shift;
172 0 my $operator = shift;
173 0 my $value = shift;
174  
175 0 ▶ if ( $self->_case_insensitivity_valid( $column, $operator, $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_case_insensitivity_valid($column, $operator, $value)) 0 0 0
176 0 $column = "LOWER($column)";
177 0 ▶ if ( $operator =~ /^(IN|=)$/i and ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator =~ /^(IN|=)$/i and ref $value eq 'ARRAY') { } 0 0 0
Condition0%0/3
$operator =~ /^(IN|=)$/i and ref $value eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$operator =~ /^(IN|=)$/i and ref $value eq "ARRAY"
$operator =~ /^(IN|=)$/i ref $value eq "ARRAY"
Truth table0%0/3
A and B
A= $operator =~ /^(IN|=)$/iB= ref $value eq "ARRAY"
ABresult
0X0
100
111
178 0 $value = [ map {"LOWER($_)"} @$value ];
179 } else {
180 0 $value = "LOWER($value)";
181 }
182 }
183 0 return ( $column, $operator, $value );
184 }
185  
186 =head2 distinct_query STATEMENTREF
187  
188 takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result set.
189  
190 =cut
191  
192 sub distinct_query {
193 1 ▶ my $self = shift;
Subroutine distinct_query: called CC=12 SCAR=25.3
Pod documented
194 1 my $statementref = shift;
195 1 my $collection = shift;
196 1 my $table = $collection->table;
197  
198 1 ▶ if (grep {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_->{'alias'} and $_->{'alias'} ne 'main' or defined $_->{'function'};} @{$collection->order_by;}) { } 1 0 1
199 ( defined $_->{'alias'} and $_->{'alias'} ne 'main' )
200 7 ▶ || defined $_->{'function'}
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{"alias"} and $_->{"alias"} ne "main" or defined $_->{"function"} 4 3 7
Condition100%3/3
defined $_->{"alias"} and $_->{"alias"} ne "main"
!ll&&!rl&&r
223
MC/DC100%2/2
defined $_->{"alias"} and $_->{"alias"} ne "main"
defined $_->{"alias"} $_->{"alias"} ne "main"
Truth table100%3/3
A and B
A= defined $_->{"alias"}B= $_->{"alias"} ne "main"
ABresult
0X0
100
111
201 1 } @{ $collection->order_by }
202 )
203 {
204  
205 # If we are ordering by something not in 'main', we need to GROUP
206 # BY and adjust the ORDER_BY accordingly
207 local $collection->{group_by}
208 1 ▶ = [ @{ $collection->{group_by} || [] }, { column => 'id' } ];
Branch: 1 branch
BranchTrueFalseTotal
$collection->{"group_by"} or [] 1 0 1
209 local $collection->{order_by} = [
210 map {
211 7 ▶ my $alias = $_->{alias} || '';
Condition100%2/2
$_->{'alias'} || ''
l!l
43
MC/DC100%1/1
$_->{'alias'} || ''
$_->{'alias'}
Truth table100%2/2
A
A= $_->{'alias'}
Aresult
11
00
212 7 my $column = $_->{column};
213 7 my $order = $_->{order};
214 7 ▶ if ($column =~ /\W/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column =~ /\W/) 0 7 7
215 0 warn "Possible SQL injection in column '$column' in order_by\n";
216 0 next;
217 }
218 7 ▶ $alias .= '.' if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 4 3 7
219  
220 ( ( !$alias or $alias eq 'main.' ) and $column eq 'id' )
221 ? $_
222 7 ▶ : { %{$_}, column => undef, function => ($order =~ /^des/i ? 'MAX':'MIN'). "($alias$column)" }
Branch: 2 branches
BranchTrueFalseTotal
(!$alias || $alias eq 'main.') && $column eq 'id' ? : 2 5 7
$order =~ /^des/i ? : 3 2 5
Condition100%6/6
!$alias || $alias eq "main." and $column eq "id"
!ll&&!rl&&r
232
not $alias or $alias eq "main."
l!l&&r!l&&!r
322
MC/DC100%3/3
!$alias || $alias eq "main." and $column eq "id"
not $alias $alias eq "main." $column eq "id"
Truth table100%5/5
not(A) or B and C
A= not $aliasB= $alias eq "main."C= $column eq "id"
ABCresult
00X0
1X00
0100
1X11
0111
223 1 } @{ $collection->{order_by} }
224 ];
225 1 my $group = $collection->_group_clause;
226 1 my $order = $collection->_order_clause;
227 1 $$statementref
228 = "SELECT "
229 . $collection->query_columns
230 . " FROM ( SELECT main.id FROM $$statementref $group $order ) distinctquery, $table main WHERE (main.id = distinctquery.id)";
231 } else {
232 0 $$statementref
233 = "SELECT DISTINCT "
234 . $collection->query_columns
235 . " FROM $$statementref";
236 0 $$statementref .= $collection->_group_clause;
237 0 $$statementref .= $collection->_order_clause;
238 }
239 }
240  
241 =head2 canonical_true
242  
243 The canonical true value in Postgres is 't'.
244  
245 =cut
246  
247 0 ▶sub canonical_true { 't' }
Subroutine canonical_true: not called CC=1 SCAR=6.9
Pod documented
248  
249 =head2 canonical_false
250  
251 The canonical false value in Postgres is 'f'.
252  
253 =cut
254  
255 0 ▶sub canonical_false { 'f' }
Subroutine canonical_false: not called CC=1 SCAR=6.9
Pod documented
256  
257 2 1;
258  
259 __END__
260  
261 =head1 SEE ALSO
262  
263 L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<DBD::Pg>
264  
265 =cut
266  
« DBI/Collection.pm ↑ Summary DBI/Handle/mysql.pm »