SearchBuilder/Handle/SQLite.pm

filter: Statementstmt 84.2% 59 / 70 Branchbran 45.4% 10 / 22 Conditioncond 55.5% 10 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 92.8% 13 / 14 Podpod 88.8% 8 / 9 totaltotal 72.7% 104 / 143 CC 21 SCAR 21.5
CC
21
Cov
72%
ConvertTimezoneFunction
25.9
Fields
6.0
Insert
4.3
CRAP 8.6
« 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 162 ▶sub _last_insert_rowid {
Subroutine _last_insert_rowid: called CC=2 SCAR=10.9
47 162 my $self = shift;
48 162 my $table = shift;
49  
50 162 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 162 ▶sub Insert {
Subroutine Insert: called CC=4 SCAR=14.6
Pod documented
66 162 my $self = shift;
67 162 my $table = shift;
68  
69 162 my %args = ( id => undef, @_);
70 # We really don't want an empty id
71  
72 162 my $sth = $self->SUPER::Insert($table, %args);
73 162 ▶ return unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 162 162
74  
75 # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
76 162 ▶ $self->{'id'}= $args{'id'} || $self->_last_insert_rowid($table);
Condition33%1/3
$args{'id'} || $self->_last_insert_rowid($table)
l!l&&r!l&&!r
01620
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 162 ▶ warn "$self no row id returned on row creation" unless ($self->{'id'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 0 162 162
79 162 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 111 ▶sub DistinctQuery {
Subroutine DistinctQuery: called CC=2 SCAR=6.9
Pod documented
100 111 my $self = shift;
101 111 my $statementref = shift;
102 111 my $sb = shift;
103  
104 111 ▶ return $self->SUPER::DistinctQuery( $statementref, $sb, @_ )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_DistinctNeedsGroupBy($sb) 73 38 111
105 unless $self->_DistinctNeedsGroupBy($sb);
106  
107 38 local $sb->{'group_by'} = [{FIELD => 'id'}];
108 38 local $sb->{'order_by'} = $self->_DistinctGroupByOrder($sb);
109 38 $$statementref = "SELECT main.* FROM $$statementref";
110 38 $$statementref .= $sb->_GroupClause;
111 38 $$statementref .= $sb->_OrderClause;
112 }
113  
114 =head2 DistinctCount STATEMENTREF
115  
116 takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result count
117  
118  
119 =cut
120  
121 41 ▶sub DistinctCount {
Subroutine DistinctCount: called CC=1 SCAR=0.0
Pod documented
122 41 my $self = shift;
123 41 my $statementref = shift;
124 41 my $sb = shift;
125  
126 41 $$statementref = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
127 }
128  
129 2 ▶sub Fields {
Subroutine Fields: called CC=6 SCAR=18.0
Pod undocumented
130 2 my $self = shift;
131 2 my $table = shift;
132  
133 2 my $cache = \%DBIx::SearchBuilder::Handle::FIELDS_IN_TABLE;
134 2 ▶ unless ( $cache->{lc $table} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$cache{lc $table}) 2 0 2
135 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
136 or return ();
137  
138 2 foreach my $e ( @$info ) {
139 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
140 }
141 }
142  
143 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
144 }
145  
146 =head2 SimpleDateTimeFunctions
147  
148 Returns hash reference with specific date time functions of this
149 database for L<DBIx::SearchBuilder::Handle/DateTimeFunction>.
150  
151 =cut
152  
153 20 ▶sub SimpleDateTimeFunctions {
Subroutine SimpleDateTimeFunctions: called CC=2 SCAR=6.9
Pod documented
154 20 my $self = shift;
155 return $self->{'_simple_date_time_functions'} ||= {
156 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
157 datetime => 'datetime(?)',
158 time => 'time(?)',
159  
160 hourly => "strftime('%Y-%m-%d %H', ?)",
161 hour => "strftime('%H', ?)",
162  
163 date => 'date(?)',
164 daily => 'date(?)',
165  
166 day => "strftime('%d', ?)",
167 dayofmonth => "strftime('%d', ?)",
168  
169 monthly => "strftime('%Y-%m', ?)",
170 month => "strftime('%m', ?)",
171  
172 annually => "strftime('%Y', ?)",
173 year => "strftime('%Y', ?)",
174  
175 dayofweek => "strftime('%w', ?)",
176 dayofyear => "strftime('%j', ?)",
177 weekofyear => "strftime('%W', ?)",
178 };
179 }
180  
181 2 ▶sub ConvertTimezoneFunction {
Subroutine ConvertTimezoneFunction: called CC=8 SCAR=32.5
Pod documented
182 2 my $self = shift;
183 2 my %args = (
184 From => 'UTC',
185 To => undef,
186 Field => '',
187 @_
188 );
189 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
190 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
191  
192 0 my $res;
193 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
194 0 $res = "datetime($args{'Field'}, 'utc')";
195 }
196 ▶ 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
197 0 $res = "datetime($args{'Field'}, 'localtime')";
198 }
199 else {
200 0 warn "SQLite only supports TZ convesion from UTC or to UTC";
201 0 $res = $args{'Field'};
202 }
203 0 return $res;
204 }
205  
206 2 ▶sub _DateTimeIntervalFunction {
Subroutine _DateTimeIntervalFunction: called CC=1 SCAR=0.0
207 2 my $self = shift;
208 2 my %args = ( From => undef, To => undef, @_ );
209  
210 2 return "strftime('%s',$args{'To'}) - strftime('%s',$args{'From'})";
211 }
212  
213 24 1;
« SearchBuilder.pm ↑ Summary SearchBuilder/Handle/Sybase.pm »