DBI/Handle/SQLite.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 62.5% 10 / 16 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 85.7% 84 / 98 CC 13 SCAR 14.1
CC
13
Cov
86%
rename_column
7.1
insert
4.3
database_version
3.2
CRAP 4.1
« DBI/Record/Plugin.pm ↑ Summary DBI/Filter/DateTime.pm »
1  
2 package Jifty::DBI::Handle::SQLite;
3 33 ▶use Jifty::DBI::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 @ISA = qw(Jifty::DBI::Handle);
5  
6 33 ▶use vars qw($VERSION @ISA $DBIHandle $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Jifty::DBI::Handle::SQLite -- A SQLite specific Handle object
12  
13 =head1 SYNOPSIS
14  
15  
16 =head1 DESCRIPTION
17  
18 This module provides a subclass of Jifty::DBI::Handle that
19 compensates for some of the idiosyncrasies of SQLite.
20  
21 =head1 METHODS
22  
23 =head2 database_version
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 sub database_version {
31 31 ▶ my $self = shift;
Subroutine database_version: called CC=3 SCAR=11.7
Pod documented
32 31 ▶ return '' unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 31 31
33 31 ▶ return $self->dbh->{sqlite_version} || '';
Condition50%1/2
$self->dbh->{'sqlite_version'} || ''
l!l
310
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 sub insert {
47 104 ▶ my $self = shift;
Subroutine insert: called CC=4 SCAR=14.6
Pod documented
48 104 my $table = shift;
49 104 my %args = ( id => undef, @_ );
50  
51 # We really don't want an empty id
52  
53 104 my $sth = $self->SUPER::insert( $table, %args );
54 104 ▶ return unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 104 104
55  
56 # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
57 104 ▶ $self->{'id'} = $args{'id'} || $self->dbh->func('last_insert_rowid');
Condition33%1/3
$args{'id'} || $self->dbh->func('last_insert_rowid')
l!l&&r!l&&!r
01040
MC/DC0%0/2
$args{'id'} || $self->dbh->func('last_insert_rowid')
$args{'id'} $self->dbh->func('last_insert_rowid')
Truth table33%1/3
A || B
A= $args{'id'}B= $self->dbh->func('last_insert_rowid')
ABresult
1X1
011
000
58  
59 104 ▶ warn "$self no row id returned on row creation" unless ( $self->{'id'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 0 104 104
60 104 return ( $self->{'id'} ); #Add Succeded. return the id
61 }
62  
63 =head2 case_sensitive
64  
65 Returns 1, since SQLite's searches are case sensitive by default.
66 Note, however, SQLite's C<like> operator is case I<in>sensitive.
67  
68 =cut
69  
70 sub case_sensitive {
71 187 ▶ my $self = shift;
Subroutine case_sensitive: called CC=1 SCAR=0.0
Pod documented
72 187 return (1);
73 }
74  
75 =head2 distinct_count STATEMENTREF
76  
77 takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result count
78  
79  
80 =cut
81  
82 sub distinct_count {
83 11 ▶ my $self = shift;
Subroutine distinct_count: called CC=1 SCAR=0.0
Pod documented
84 11 my $statementref = shift;
85  
86 # Wrapper select query in a subselect as Oracle doesn't allow
87 # DISTINCT against CLOB/BLOB column types.
88 11 $$statementref
89 = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
90  
91 }
92  
93 sub _make_clause_case_insensitive {
94 88 ▶ my $self = shift;
Subroutine _make_clause_case_insensitive: called CC=2 SCAR=6.9
95 88 my $column = shift;
96 88 my $operator = shift;
97 88 my $value = shift;
98  
99 88 ▶ return ($column, $operator, $value)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_case_insensitivity_valid($column, $operator, $value) 8 80 88
100 unless $self->_case_insensitivity_valid( $column, $operator, $value );
101  
102 80 return("$column COLLATE NOCASE", $operator, $value);
103 }
104  
105 =head2 rename_column ( table => $table, column => $old_column, to => $new_column )
106  
107 rename column
108  
109 =cut
110  
111 sub rename_column {
112 1 ▶ my $self = shift;
Subroutine rename_column: called CC=7 SCAR=19.7
Pod documented
113 1 my %args = (
114 table => undef,
115 column => undef,
116 to => undef,
117 @_
118 );
119  
120 1 my $table = $args{'table'};
121  
122 # Convert columns
123 1 my ($schema) = $self->fetch_result(
124 "SELECT sql FROM sqlite_master WHERE tbl_name = ? AND type = ?",
125 $table, 'table',
126 );
127 1 ▶ $schema =~ s/(.*create\s+table\s+)\S+(.*?\(\s*)//i
Branch: 1 branch
BranchTrueFalseTotal
$schema =~ s/(.*create\s+table\s+)\S+(.*?\(\s*)//i or die "Cannot find 'CREATE TABLE' statement in schema for '${table}': $schema" 0 1 1
128 or die "Cannot find 'CREATE TABLE' statement in schema for '$table': $schema";
129  
130 1 my $new_table = join( '_', $table, 'new', $$ );
131 1 my $new_create_clause = "$1$new_table$2";
132  
133 1 my @column_info = ( split /,/, $schema );
134 1 ▶ my @column_names = map { /^\s*(\S+)/ ? $1 : () } @column_info;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*(\S+)/ ? : 2 0 2
135  
136 1 s/^(\s*)\b\Q$args{column}\E\b/$1$args{to}/i for @column_info;
137  
138 1 my $new_schema = $new_create_clause . join( ',', @column_info );
139 my $copy_columns = join(
140 ', ',
141 map {
142 1 ▶ ( lc($_) eq lc( $args{column} ) )
Branch: 1 branch
BranchTrueFalseTotal
lc $_ eq lc $args{'column'} ? : 1 1 2
143 ? "$_ AS $args{to}"
144 : $_
145 } @column_names
146 );
147  
148 # Convert indices
149 1 my $indice_sth = $self->simple_query(
150 "SELECT sql FROM sqlite_master WHERE tbl_name = ? AND type = ?",
151 $table, 'index'
152 );
153 1 my @indice_sql;
154 1 ▶ while ( my ($index) = $indice_sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($index) = $indice_sth->fetchrow_array) 0 1 1
155 0 $index =~ s/^(.*\(.*)\b\Q$args{column}\E\b/$1$args{to}/i;
156 0 push @indice_sql, $index;
157 }
158 1 $indice_sth->finish;
159  
160 # Run the conversion SQLs
161 1 $self->begin_transaction;
162 1 $self->simple_query($new_schema);
163 1 $self->simple_query("INSERT INTO $new_table SELECT $copy_columns FROM $table");
164 1 $self->simple_query("DROP TABLE $table");
165 1 $self->simple_query("ALTER TABLE $new_table RENAME TO $table");
166 1 $self->simple_query($_) for @indice_sql;
167 1 $self->commit;
168 }
169  
170 33 1;
171  
172 __END__
173  
174 =head1 AUTHOR
175  
176 Jesse Vincent, jesse@fsck.com
177  
178 =head1 SEE ALSO
179  
180 perl(1), Jifty::DBI
181  
182 =cut
« DBI/Record/Plugin.pm ↑ Summary DBI/Filter/DateTime.pm »