SearchBuilder/Handle/SQLite.pm

filter: Statementstmt 84.9% 62 / 73 Branchbran 50.0% 13 / 26 Conditioncond 54.5% 12 / 22 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 92.8% 13 / 14 Podpod 88.8% 8 / 9 totaltotal 71.7% 112 / 156 CC 25 SCAR 21.5
CC
25
Cov
72%
ConvertTimezoneFunction
25.9
DistinctQuery
6.1
Fields
6.0
CRAP 8.5
« SearchBuilder.pm ↑ Summary SearchBuilder/Handle/Sybase.pm »
1  
2 package DBIx::SearchBuilder::Handle::SQLite;
3  
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use base qw(DBIx::SearchBuilder::Handle);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::SearchBuilder::Handle::SQLite -- A SQLite specific Handle object
12  
13 =head1 SYNOPSIS
14  
15  
16 =head1 DESCRIPTION
17  
18 This module provides a subclass of DBIx::SearchBuilder::Handle that
19 compensates for some of the idiosyncrasies of SQLite.
20  
21 =head1 METHODS
22  
23 =head2 DatabaseVersion
24  
25 Returns the version of the SQLite library which is used, e.g., "2.8.0".
26 SQLite can only return short variant.
27  
28 =cut
29  
30 24 ▶sub DatabaseVersion {
Subroutine DatabaseVersion: called CC=3 SCAR=11.7
Pod documented
31 24 my $self = shift;
32 24 ▶ return '' unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 24 24
33 24 ▶ return $self->dbh->{sqlite_version} || '';
Condition50%1/2
$self->dbh->{'sqlite_version'} || ''
l!l
240
MC/DC0%0/1
$self->dbh->{'sqlite_version'} || ''
$self->dbh->{'sqlite_version'}
Truth table50%1/2
A
A= $self->dbh->{'sqlite_version'}
Aresult
11
00
34 }
35  
36 =head2 Insert
37  
38 Takes a table name as the first argument and assumes that the rest of the arguments
39 are an array of key-value pairs to be inserted.
40  
41 If the insert succeeds, returns the id of the insert, otherwise, returns
42 a Class::ReturnValue object with the error reported.
43  
44 =cut
45  
46 161 ▶sub _last_insert_rowid {
Subroutine _last_insert_rowid: called CC=2 SCAR=10.9
47 161 my $self = shift;
48 161 my $table = shift;
49  
50 161 return $self->dbh->func('last_insert_rowid');
51  
52 # XXX: this is workaround nesty sqlite problem that
53 # last_insert_rowid in transaction is inaccurrate with multiple
54 # inserts.
55  
56 0 ▶ return $self->dbh->func('last_insert_rowid')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TransactionDepth 0 0 0
57 unless $self->TransactionDepth;
58  
59 # XXX: is the name of the column always id ?
60  
61 0 my $ret = $self->FetchResult("select max(id) from $table");
62 0 return $ret;
63 }
64  
65 161 ▶sub Insert {
Subroutine Insert: called CC=4 SCAR=14.6
Pod documented
66 161 my $self = shift;
67 161 my $table = shift;
68  
69 161 my %args = ( id => undef, @_);
70 # We really don't want an empty id
71  
72 161 my $sth = $self->SUPER::Insert($table, %args);
73 161 ▶ return unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 161 161
74  
75 # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
76 161 ▶ $self->{'id'}= $args{'id'} || $self->_last_insert_rowid($table);
Condition33%1/3
$args{'id'} || $self->_last_insert_rowid($table)
l!l&&r!l&&!r
01610
MC/DC0%0/2
$args{'id'} || $self->_last_insert_rowid($table)
$args{'id'} $self->_last_insert_rowid($table)
Truth table33%1/3
A || B
A= $args{'id'}B= $self->_last_insert_rowid($table)
ABresult
1X1
011
000
77  
78 161 ▶ warn "$self no row id returned on row creation" unless ($self->{'id'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 0 161 161
79 161 return( $self->{'id'}); #Add Succeded. return the id
80 }
81  
82  
83  
84 =head2 CaseSensitive
85  
86 Returns undef, since SQLite's searches are not case sensitive by default
87  
88 =cut
89  
90 116 ▶sub CaseSensitive {
Subroutine CaseSensitive: called CC=1 SCAR=0.0
Pod documented
91 116 my $self = shift;
92 116 return(1);
93 }
94  
95 0 ▶sub BinarySafeBLOBs {
Subroutine BinarySafeBLOBs: not called CC=1 SCAR=6.9
Pod documented
96 0 return undef;
97 }
98  
99 96 ▶sub DistinctQuery {
Subroutine DistinctQuery: called CC=6 SCAR=18.1
Pod documented
100 96 my $self = shift;
101 96 my $statementref = shift;
102 96 my $sb = shift;
103  
104 96 ▶ return $self->SUPER::DistinctQuery( $statementref, $sb, @_ )
Branch: 1 branch
BranchTrueFalseTotal
unless $sb->_OrderClause =~ /(?<!main)\./ 64 32 96
105 if $sb->_OrderClause !~ /(?<!main)\./;
106  
107 32 local $sb->{'group_by'} = [{FIELD => 'id'}];
108 local $sb->{'order_by'} = [
109 map {
110 ($_->{'ALIAS'}||'') ne "main"
111 32 ▶ ? { %{$_}, FIELD => ((($_->{'ORDER'}||'') =~ /^des/i)?'MAX':'MIN') ."(".$_->{FIELD}.")" }
Branch: 2 branches
BranchTrueFalseTotal
($_->{'ALIAS'} || '') ne 'main' ? : 32 0 32
($_->{'ORDER'} || '') =~ /^des/i ? : 16 16 32
Condition50%2/4
$_->{'ALIAS'} || ''
l!l
320
$_->{'ORDER'} || ''
l!l
320
MC/DC0%0/1
$_->{'ALIAS'} || ''
$_->{'ALIAS'}
MC/DC0%0/1
$_->{'ORDER'} || ''
$_->{'ORDER'}
Truth table50%1/2
A
A= $_->{'ALIAS'}
Aresult
11
00
Truth table50%1/2
A
A= $_->{'ORDER'}
Aresult
11
00
112 : $_
113 }
114 32 @{$sb->{'order_by'}}
115 ];
116 32 $$statementref = "SELECT main.* FROM $$statementref";
117 32 $$statementref .= $sb->_GroupClause;
118 32 $$statementref .= $sb->_OrderClause;
119 }
120  
121 =head2 DistinctCount STATEMENTREF
122  
123 takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result count
124  
125  
126 =cut
127  
128 41 ▶sub DistinctCount {
Subroutine DistinctCount: called CC=1 SCAR=0.0
Pod documented
129 41 my $self = shift;
130 41 my $statementref = shift;
131 41 my $sb = shift;
132  
133 41 $$statementref = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
134 }
135  
136 2 ▶sub Fields {
Subroutine Fields: called CC=6 SCAR=18.0
Pod undocumented
137 2 my $self = shift;
138 2 my $table = shift;
139  
140 2 my $cache = \%DBIx::SearchBuilder::Handle::FIELDS_IN_TABLE;
141 2 ▶ unless ( $cache->{lc $table} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$cache{lc $table}) 2 0 2
142 2 ▶ my $info = $self->dbh->selectall_arrayref("PRAGMA table_info('$table')")
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->dbh->selectall_arrayref("PRAGMA table_info('${table}')") or return () 2 0 2
143 or return ();
144  
145 2 foreach my $e ( @$info ) {
146 5 ▶ push @{ $cache->{ lc $table } ||= [] }, lc $e->[1];
Condition100%2/2
$$cache{lc $table} ||= []
l!l
41
MC/DC100%1/1
$$cache{lc $table} ||= []
$$cache{lc $table}
Truth table100%2/2
A
A= $$cache{lc $table}
Aresult
11
00
147 }
148 }
149  
150 2 ▶ return @{ $cache->{ lc $table } || [] };
Condition100%2/2
$$cache{lc $table} || []
l!l
11
MC/DC100%1/1
$$cache{lc $table} || []
$$cache{lc $table}
Truth table100%2/2
A
A= $$cache{lc $table}
Aresult
11
00
151 }
152  
153 =head2 SimpleDateTimeFunctions
154  
155 Returns hash reference with specific date time functions of this
156 database for L<DBIx::SearchBuilder::Handle/DateTimeFunction>.
157  
158 =cut
159  
160 20 ▶sub SimpleDateTimeFunctions {
Subroutine SimpleDateTimeFunctions: called CC=2 SCAR=6.9
Pod documented
161 20 my $self = shift;
162 return $self->{'_simple_date_time_functions'} ||= {
163 20 ▶ %{ $self->SUPER::SimpleDateTimeFunctions(@_) },
Condition100%2/2
$self->{"_simple_date_time_functions"} ||= {%{$self->SUPER::SimpleDateTimeFunctions(@_);}, "datetime", "datetime(?)", "time", "time(?)", "hourly", "strftime('%Y-%m-%d %H', ?)", "hour", "strftime('%H', ?)", "date", "date(?)", "daily", "date(?)", "day", "strftime('%d', ?)", "dayofmonth", "strftime('%d', ?)", "monthly", "strftime('%Y-%m', ?)", "month", "strftime('%m', ?)", "annually", "strftime('%Y', ?)", "year", "strftime('%Y', ?)", "dayofweek", "strftime('%w', ?)", "dayofyear", "strftime('%j', ?)", "weekofyear", "strftime('%W', ?)"}
l!l
191
MC/DC100%1/1
$self->{"_simple_date_time_functions"} ||= {%{$self->SUPER::SimpleDateTimeFunctions(@_);}, "datetime", "datetime(?)", "time", "time(?)", "hourly", "strftime('%Y-%m-%d %H', ?)", "hour", "strftime('%H', ?)", "date", "date(?)", "daily", "date(?)", "day", "strftime('%d', ?)", "dayofmonth", "strftime('%d', ?)", "monthly", "strftime('%Y-%m', ?)", "month", "strftime('%m', ?)", "annually", "strftime('%Y', ?)", "year", "strftime('%Y', ?)", "dayofweek", "strftime('%w', ?)", "dayofyear", "strftime('%j', ?)", "weekofyear", "strftime('%W', ?)"}
$self->{"_simple_date_time_functions"}
Truth table100%2/2
A
A= $self->{"_simple_date_time_functions"}
Aresult
11
00
164 datetime => 'datetime(?)',
165 time => 'time(?)',
166  
167 hourly => "strftime('%Y-%m-%d %H', ?)",
168 hour => "strftime('%H', ?)",
169  
170 date => 'date(?)',
171 daily => 'date(?)',
172  
173 day => "strftime('%d', ?)",
174 dayofmonth => "strftime('%d', ?)",
175  
176 monthly => "strftime('%Y-%m', ?)",
177 month => "strftime('%m', ?)",
178  
179 annually => "strftime('%Y', ?)",
180 year => "strftime('%Y', ?)",
181  
182 dayofweek => "strftime('%w', ?)",
183 dayofyear => "strftime('%j', ?)",
184 weekofyear => "strftime('%W', ?)",
185 };
186 }
187  
188 2 ▶sub ConvertTimezoneFunction {
Subroutine ConvertTimezoneFunction: called CC=8 SCAR=32.5
Pod documented
189 2 my $self = shift;
190 2 my %args = (
191 From => 'UTC',
192 To => undef,
193 Field => '',
194 @_
195 );
196 2 ▶ return $args{'Field'} unless $args{From} && $args{'To'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"From"} and $args{"To"} 1 1 2
Condition66%2/3
$args{"From"} and $args{"To"}
!ll&&!rl&&r
011
MC/DC50%1/2
$args{"From"} and $args{"To"}
$args{"From"} $args{"To"}
Truth table66%2/3
A and B
A= $args{"From"}B= $args{"To"}
ABresult
0X0
100
111
197 1 ▶ return $args{'Field'} if lc $args{From} eq lc $args{'To'};
Branch: 1 branch
BranchTrueFalseTotal
if lc $args{"From"} eq lc $args{"To"} 1 0 1
198  
199 0 my $res;
200 0 ▶ if ( lc($args{'To'}||'') eq 'utc' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc($args{'To'} || '') eq 'utc') { } 0 0 0
elsif (lc($args{'From'} || '') eq 'utc') { } 0 0 0
Condition0%0/2
$args{'To'} || ''
l!l
00
MC/DC0%0/1
$args{'To'} || ''
$args{'To'}
Truth table0%0/2
A
A= $args{'To'}
Aresult
11
00
201 0 $res = "datetime($args{'Field'}, 'utc')";
202 }
203 ▶ elsif ( lc($args{'From'}||'') eq 'utc' ) {
Condition0%0/2
$args{'From'} || ''
l!l
00
MC/DC0%0/1
$args{'From'} || ''
$args{'From'}
Truth table0%0/2
A
A= $args{'From'}
Aresult
11
00
204 0 $res = "datetime($args{'Field'}, 'localtime')";
205 }
206 else {
207 0 warn "SQLite only supports TZ convesion from UTC or to UTC";
208 0 $res = $args{'Field'};
209 }
210 0 return $res;
211 }
212  
213 2 ▶sub _DateTimeIntervalFunction {
Subroutine _DateTimeIntervalFunction: called CC=1 SCAR=0.0
214 2 my $self = shift;
215 2 my %args = ( From => undef, To => undef, @_ );
216  
217 2 return "strftime('%s',$args{'To'}) - strftime('%s',$args{'From'})";
218 }
219  
220 24 1;
« SearchBuilder.pm ↑ Summary SearchBuilder/Handle/Sybase.pm »