SearchBuilder/Handle/Pg.pm

filter: Statementstmt 10.7% 13 / 121 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 23.5% 4 / 17 Podpod 100% 11 / 11 totaltotal 11.7% 28 / 238 CC 35 SCAR 37.4
CC
35
Cov
7%
DistinctQuery
110.0
Insert
42.0
ApplyLimits
42.0
CRAP 42.2
« SearchBuilder/Handle/mysql.pm ↑ Summary SearchBuilder/Handle.pm »
1 package DBIx::SearchBuilder::Handle::Pg;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw(DBIx::SearchBuilder::Handle);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Want qw(howmany);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 DBIx::SearchBuilder::Handle::Pg - A Postgres specific Handle object
13  
14 =head1 SYNOPSIS
15  
16  
17 =head1 DESCRIPTION
18  
19 This module provides a subclass of DBIx::SearchBuilder::Handle that
20 compensates for some of the idiosyncrasies of Postgres.
21  
22 =head1 METHODS
23  
24 =cut
25  
26  
27 =head2 Connect
28  
29 Connect takes a hashref and passes it off to SUPER::Connect;
30 Forces the timezone to GMT
31 it returns a database handle.
32  
33 =cut
34  
35 0 ▶sub Connect {
Subroutine Connect: not called CC=1 SCAR=6.9
Pod documented
36 0 my $self = shift;
37  
38 0 my $rv = $self->SUPER::Connect(@_);
39 0 $self->SimpleQuery("SET TIME ZONE 'GMT'");
40 0 $self->SimpleQuery("SET DATESTYLE TO 'ISO'");
41 0 $self->AutoCommit(1);
42 0 return ($rv);
43 }
44  
45 =head2 BuildDSN
46  
47 Extend L<DBIx::SearchBuilder::Handle/BuildDNS> to force
48 C<client_encoding> to be UTF-8, so that character strings can be
49 safely passed to, and retrieved from, the database. See
50 L<DBD::Pg/Encoding>.
51  
52 =cut
53  
54 0 ▶sub BuildDSN {
Subroutine BuildDSN: not called CC=1 SCAR=6.9
Pod documented
55 0 my $self = shift;
56 0 $self->SUPER::BuildDSN(@_);
57 0 $self->{'dsn'} .= ';client_encoding=UTF8';
58 0 return $self->{'dsn'};
59 }
60  
61 =head2 Insert
62  
63 Takes a table name as the first argument and assumes
64 that the rest of the arguments are an array of key-value
65 pairs to be inserted.
66  
67 In case of insert failure, returns a L<Class::ReturnValue>
68 object preloaded with error info.
69  
70 =cut
71  
72  
73 0 ▶sub Insert {
Subroutine Insert: not called CC=6 SCAR=37.4
Pod documented
74 0 my $self = shift;
75 0 my $table = shift;
76 0 my %args = (@_);
77  
78 0 my $sth = $self->SUPER::Insert( $table, %args );
79 0 ▶ return $sth unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
80  
81 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
82 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
83 0 return ( $self->{'id'} );
84 }
85  
86 0 my $sequence_name = $self->IdSequenceName($table);
87 0 ▶ unless ($sequence_name) { return ($sequence_name) } # Class::ReturnValue
Branch: 1 branch
BranchTrueFalseTotal
unless ($sequence_name) 0 0 0
88 0 my $seqsth = $self->dbh->prepare(
89 qq{SELECT CURRVAL('} . $sequence_name . qq{')} );
90 0 $seqsth->execute;
91 0 $self->{'id'} = $seqsth->fetchrow_array();
92  
93 0 return ( $self->{'id'} );
94 }
95  
96 =head2 InsertQueryString
97  
98 Postgres sepcific overriding method for
99 L<DBIx::SearchBuilder::Handle/InsertQueryString>.
100  
101 =cut
102  
103 0 ▶sub InsertQueryString {
Subroutine InsertQueryString: not called CC=1 SCAR=6.9
Pod documented
104 0 my $self = shift;
105 0 my ($query_string, @bind) = $self->SUPER::InsertQueryString( @_ );
106 0 $query_string =~ s/\(\s*\)\s+VALUES\s+\(\s*\)\s*$/DEFAULT VALUES/;
107 0 return ($query_string, @bind);
108 }
109  
110 =head2 IdSequenceName TABLE
111  
112 Takes a TABLE name and returns the name of the sequence of the primary key for that table.
113  
114 =cut
115  
116 0 ▶sub IdSequenceName {
Subroutine IdSequenceName: not called CC=5 SCAR=34.0
Pod documented
117 0 my $self = shift;
118 0 my $table = shift;
119  
120 0 ▶ return $self->{'_sequences'}{$table} if (exists $self->{'_sequences'}{$table});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_sequences"}{$table} 0 0 0
121 # Let's get the id of that row we just inserted
122 0 my $seq;
123 0 my $colinfosth = $self->dbh->column_info( undef, undef, lc($table), '%' );
124 0 ▶ while ( my $foo = $colinfosth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $foo = $colinfosth->fetchrow_hashref) 0 0 0
125  
126 # Regexp from DBIx::Class's Pg handle. Thanks to Marcus Ramberg
127 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
128 && $foo->{'COLUMN_DEF'}
129 =~ m!^nextval\(+'"?([^"']+)"?'(::(?:text|regclass)\))+!i )
130  
131 {
132 0 return $self->{'_sequences'}{$table} = $1;
133 }
134  
135 }
136  
137 0 my $ret = Class::ReturnValue->new();
138 0 $ret->as_error(
139 errno => '-1',
140 message => "Found no sequence for $table",
141 do_backtrace => undef
142 );
143 0 return ( $ret->return_value );
144 }
145  
146  
147  
148 =head2 BinarySafeBLOBs
149  
150 Return undef, as no current version of postgres supports binary-safe blobs
151  
152 =cut
153  
154 0 ▶sub BinarySafeBLOBs {
Subroutine BinarySafeBLOBs: not called CC=1 SCAR=6.9
Pod documented
155 0 my $self = shift;
156 0 return(undef);
157 }
158  
159  
160 =head2 ApplyLimits STATEMENTREF ROWS_PER_PAGE FIRST_ROW
161  
162 takes an SQL SELECT statement and massages it to return ROWS_PER_PAGE starting with FIRST_ROW;
163  
164  
165 =cut
166  
167 0 ▶sub ApplyLimits {
Subroutine ApplyLimits: not called CC=6 SCAR=37.4
Pod documented
168 0 my $self = shift;
169 0 my $statementref = shift;
170 0 my $per_page = shift;
171 0 my $first = shift;
172 0 my $sb = shift;
173  
174 0 my $limit_clause = '';
175  
176 0 ▶ if ( $per_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_page) 0 0 0
177 0 $limit_clause = " LIMIT ";
178  
179 0 ▶ if ( $sb->{_bind_values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sb->{"_bind_values"}) 0 0 0
180 0 ▶ push @{ $sb->{_bind_values} }, $per_page, $first || ();
Condition0%0/3
$first || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$first || ()
$first ()
Truth table0%0/3
A || B
A= $firstB= ()
ABresult
1X1
011
000
181 0 ▶ $first = '?' if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 0 0 0
182 0 $per_page = '?';
183 }
184  
185 0 $limit_clause .= $per_page;
186 0 ▶ if ( $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 0 0 0
187 0 $limit_clause .= " OFFSET $first";
188 }
189 }
190  
191 0 $$statementref .= $limit_clause;
192 }
193  
194  
195 =head2 _MakeClauseCaseInsensitive FIELD OPERATOR VALUE
196  
197 Takes a field, operator and value. performs the magic necessary to make
198 your database treat this clause as case insensitive.
199  
200 Returns a FIELD OPERATOR VALUE triple.
201  
202 =cut
203  
204 0 ▶sub _MakeClauseCaseInsensitive {
Subroutine _MakeClauseCaseInsensitive: not called CC=5 SCAR=34.0
205 0 my $self = shift;
206 0 my $field = shift;
207 0 my $operator = shift;
208 0 my $value = shift;
209  
210 # we don't need to downcase numeric values and dates
211 0 ▶ if ($value =~ /^$DBIx::SearchBuilder::Handle::RE_CASE_INSENSITIVE_CHARS+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^$DBIx::SearchBuilder::Handle::RE_CASE_INSENSITIVE_CHARS+$/o) 0 0 0
212 0 return ( $field, $operator, $value);
213 }
214  
215 0 ▶ if ( $operator =~ /LIKE/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($operator =~ /LIKE/i) { } 0 0 0
elsif ($operator =~ /=/) { } 0 0 0
216 0 $operator =~ s/LIKE/ILIKE/ig;
217 0 return ( $field, $operator, $value );
218 }
219 elsif ( $operator =~ /=/ ) {
220 0 ▶ if (howmany() >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (&howmany() >= 4) { } 0 0 0
221 0 return ( "LOWER($field)", $operator, $value, "LOWER(?)");
222 }
223 # RT 3.0.x and earlier don't know how to cope with a "LOWER" function
224 # on the value. they only expect field, operator, value.
225 #
226 else {
227 0 return ( "LOWER($field)", $operator, lc($value));
228 }
229 }
230 else {
231 0 $self->SUPER::_MakeClauseCaseInsensitive( $field, $operator, $value );
232 }
233 }
234  
235  
236 =head2 DistinctQuery STATEMENTREF
237  
238 takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result set.
239  
240 =cut
241  
242 0 ▶sub DistinctQuery {
Subroutine DistinctQuery: not called CC=10 SCAR=47.0
Pod documented
243 0 my $self = shift;
244 0 my $statementref = shift;
245 0 my $sb = shift;
246 0 my $table = $sb->Table;
247  
248 0 ▶ return $self->SUPER::DistinctQuery( $statementref, $sb, @_ )
Branch: 1 branch
BranchTrueFalseTotal
unless $sb->_OrderClause =~ /(?<!main)\./ 0 0 0
249 if $sb->_OrderClause !~ /(?<!main)\./;
250  
251 # "SELECT main.* FROM ( SELECT id FROM ... ORDER BY ... ) as dist,
252 # X main WHERE (main.id = dist.id);" doesn't work in some cases.
253 # It's hard to show with tests. Pg's optimizer can choose execution
254 # plan not guaranting order
255  
256 0 my $groups;
257 0 ▶ if ($self->DatabaseVersion =~ /^(\d+)\.(\d+)/ and ($1 > 9 or ($1 == 9 and $2 >= 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DatabaseVersion =~ /^(\d+)\.(\d+)/ and $1 > 9 || $1 == 9 && $2 >= 1) { } 0 0 0
Condition0%0/9
$self->DatabaseVersion =~ /^(\d+)\.(\d+)/ and $1 > 9 || $1 == 9 && $2 >= 1
!ll&&!rl&&r
000
$1 > 9 or $1 == 9 and $2 >= 1
l!l&&r!l&&!r
000
$1 == 9 and $2 >= 1
!ll&&!rl&&r
000
MC/DC0%0/4
$self->DatabaseVersion =~ /^(\d+)\.(\d+)/ and $1 > 9 || $1 == 9 && $2 >= 1
$self->DatabaseVersion =~ /^(\d+)\.(\d+)/ $1 > 9 $1 == 9 $2 >= 1
Truth table0%0/5
A and B or C and D
A= $self->DatabaseVersion =~ /^(\d+)\.(\d+)/B= $1 > 9C= $1 == 9D= $2 >= 1
ABCDresult
0XXX0
100X0
10100
11XX1
10111
258 # Pg 9.1 supports "SELECT main.foo ... GROUP BY main.id" if id is the primary key
259 0 $groups = [ {FIELD => "id"} ];
260 } else {
261 # For earlier versions, we have to list out all of the columns
262 0 $groups = [ map {+{FIELD => $_}} $self->Fields($table) ];
263 }
264 0 local $sb->{group_by} = $groups;
265 local $sb->{'order_by'} = [
266 map {
267 ($_->{'ALIAS'}||'') ne "main"
268 0 ▶ ? { %{$_}, FIELD => ((($_->{'ORDER'}||'') =~ /^des/i)?'MAX':'MIN') ."(".$_->{FIELD}.")" }
Branch: 2 branches
BranchTrueFalseTotal
($_->{'ALIAS'} || '') ne 'main' ? : 0 0 0
($_->{'ORDER'} || '') =~ /^des/i ? : 0 0 0
Condition0%0/4
$_->{'ALIAS'} || ''
l!l
00
$_->{'ORDER'} || ''
l!l
00
MC/DC0%0/1
$_->{'ALIAS'} || ''
$_->{'ALIAS'}
MC/DC0%0/1
$_->{'ORDER'} || ''
$_->{'ORDER'}
Truth table0%0/2
A
A= $_->{'ALIAS'}
Aresult
11
00
Truth table0%0/2
A
A= $_->{'ORDER'}
Aresult
11
00
269 : $_
270 }
271 0 @{$sb->{'order_by'}}
272 ];
273 0 my $group = $sb->_GroupClause;
274 0 my $order = $sb->_OrderClause;
275 0 $$statementref = "SELECT main.* FROM $$statementref $group $order";
276 }
277  
278 =head2 SimpleDateTimeFunctions
279  
280 Returns hash reference with specific date time functions of this
281 database for L<DBIx::SearchBuilder::Handle/DateTimeFunction>.
282  
283 =cut
284  
285 0 ▶sub SimpleDateTimeFunctions {
Subroutine SimpleDateTimeFunctions: not called CC=4 SCAR=30.0
Pod documented
286 0 my $self = shift;
287 return $self->{'_simple_date_time_functions'}
288 0 ▶ if $self->{'_simple_date_time_functions'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_simple_date_time_functions"} 0 0 0
289  
290 0 my %res = %{ $self->SUPER::SimpleDateTimeFunctions(@_) };
291 0 s/SUBSTR\s*\(\s*\?/SUBSTR( CAST(? AS text)/ig for values %res;
292  
293 # everything else we should implement through date_trunc that
294 # does SUBSTR(?, 1, X) on a date, but leaves trailing values
295 # when we don't need them
296  
297 0 ▶ return $self->{'_simple_date_time_functions'} ||= {
Condition0%0/2
$self->{"_simple_date_time_functions"} ||= {%res, "datetime", "?", "time", "CAST(? AS time)", "hour", "EXTRACT(HOUR FROM ?)", "date", "CAST(? AS date)", "daily", "CAST(? AS date)", "day", "EXTRACT(DAY FROM ?)", "month", "EXTRACT(MONTH FROM ?)", "annually", "EXTRACT(YEAR FROM ?)", "year", "EXTRACT(YEAR FROM ?)", "dayofweek", "EXTRACT(DOW FROM ?)", "dayofyear", "EXTRACT(DOY FROM ?)", "weekofyear", "EXTRACT(WEEK FROM ?)"}
l!l
00
MC/DC0%0/1
$self->{"_simple_date_time_functions"} ||= {%res, "datetime", "?", "time", "CAST(? AS time)", "hour", "EXTRACT(HOUR FROM ?)", "date", "CAST(? AS date)", "daily", "CAST(? AS date)", "day", "EXTRACT(DAY FROM ?)", "month", "EXTRACT(MONTH FROM ?)", "annually", "EXTRACT(YEAR FROM ?)", "year", "EXTRACT(YEAR FROM ?)", "dayofweek", "EXTRACT(DOW FROM ?)", "dayofyear", "EXTRACT(DOY FROM ?)", "weekofyear", "EXTRACT(WEEK FROM ?)"}
$self->{"_simple_date_time_functions"}
Truth table0%0/2
A
A= $self->{"_simple_date_time_functions"}
Aresult
11
00
298 %res,
299 datetime => '?',
300 time => 'CAST(? AS time)',
301  
302 hour => 'EXTRACT(HOUR FROM ?)',
303  
304 date => 'CAST(? AS date)',
305 daily => 'CAST(? AS date)',
306  
307 day => 'EXTRACT(DAY FROM ?)',
308  
309 month => 'EXTRACT(MONTH FROM ?)',
310  
311 annually => 'EXTRACT(YEAR FROM ?)',
312 year => 'EXTRACT(YEAR FROM ?)',
313  
314 dayofweek => "EXTRACT(DOW FROM ?)", # 0-6, 0 - Sunday
315 dayofyear => "EXTRACT(DOY FROM ?)", # 1-366
316 # 1-53, 1st week January 4, week starts on Monay
317 weekofyear => "EXTRACT(WEEK FROM ?)",
318 };
319 }
320  
321 =head2 ConvertTimezoneFunction
322  
323 Custom implementation of L<DBIx::SearchBuilder::Handle/ConvertTimezoneFunction>.
324  
325 In Pg time and timestamp data types may be "with time zone" or "without time zone".
326 So if Field argument is timestamp "with time zone" then From argument is not
327 required and is useless. Otherwise From argument identifies time zone of the Field
328 argument that is "without time zone".
329  
330 For consistency with other DBs use timestamp columns without time zones and provide
331 From argument.
332  
333 =cut
334  
335 0 ▶sub ConvertTimezoneFunction {
Subroutine ConvertTimezoneFunction: not called CC=5 SCAR=34.0
Pod documented
336 0 my $self = shift;
337 0 my %args = (
338 From => 'UTC',
339 To => undef,
340 Field => '',
341 @_
342 );
343 0 ▶ return $args{'Field'} unless $args{From} && $args{'To'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"From"} and $args{"To"} 0 0 0
Condition0%0/3
$args{"From"} and $args{"To"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"From"} and $args{"To"}
$args{"From"} $args{"To"}
Truth table0%0/3
A and B
A= $args{"From"}B= $args{"To"}
ABresult
0X0
100
111
344 0 ▶ return $args{'Field'} if lc $args{From} eq lc $args{'To'};
Branch: 1 branch
BranchTrueFalseTotal
if lc $args{"From"} eq lc $args{"To"} 0 0 0
345  
346 0 my $dbh = $self->dbh;
347 0 my $res = $args{'Field'};
348 0 $res = "TIMEZONE($_, $res)" foreach map $dbh->quote( $_ ), grep $_, @args{'From', 'To'};
349 0 return $res;
350 }
351  
352 0 ▶sub _DateTimeIntervalFunction {
Subroutine _DateTimeIntervalFunction: not called CC=1 SCAR=6.9
353 0 my $self = shift;
354 0 my %args = ( From => undef, To => undef, @_ );
355  
356 0 return "(EXTRACT(EPOCH FROM $args{'To'}) - EXTRACT(EPOCH FROM $args{'From'}))";
357 }
358  
359 0 ▶sub HasSupportForNullsOrder {
Subroutine HasSupportForNullsOrder: not called CC=1 SCAR=6.9
Pod documented
360 0 return 1;
361 }
362  
363 1 1;
« SearchBuilder/Handle/mysql.pm ↑ Summary SearchBuilder/Handle.pm »