File Coverage

blib/lib/Jifty/DBI/Handle/SQLite.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1              
2             package Jifty::DBI::Handle::SQLite;
3 2     2   3536 use Jifty::DBI::Handle;
  0            
  0            
4             @ISA = qw(Jifty::DBI::Handle);
5              
6             use vars qw($VERSION @ISA $DBIHandle $DEBUG);
7             use strict;
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             my $self = shift;
32             return '' unless $self->dbh;
33             return $self->dbh->{sqlite_version} || '';
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             my $self = shift;
48             my $table = shift;
49             my %args = ( id => undef, @_ );
50              
51             # We really don't want an empty id
52              
53             my $sth = $self->SUPER::insert( $table, %args );
54             return unless $sth;
55              
56             # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
57             $self->{'id'} = $args{'id'} || $self->dbh->func('last_insert_rowid');
58              
59             warn "$self no row id returned on row creation" unless ( $self->{'id'} );
60             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             my $self = shift;
72             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             my $self = shift;
84             my $statementref = shift;
85              
86             # Wrapper select query in a subselect as Oracle doesn't allow
87             # DISTINCT against CLOB/BLOB column types.
88             $$statementref
89             = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
90              
91             }
92              
93             =head2 _build_joins
94              
95             Adjusts syntax of join queries for SQLite.
96              
97             =cut
98              
99             #SQLite can't handle
100             # SELECT DISTINCT main.* FROM (Groups main LEFT JOIN Principals Principals_2 ON ( main.id = Principals_2.id)) , GroupMembers GroupMembers_1 WHERE ((GroupMembers_1.MemberId = '70')) AND ((Principals_2.Disabled = '0')) AND ((main.Domain = 'UserDefined')) AND ((main.id = GroupMembers_1.GroupId))
101             # ORDER BY main.Name ASC
102             # It needs
103             # SELECT DISTINCT main.* FROM Groups main LEFT JOIN Principals Principals_2 ON ( main.id = Principals_2.id) , GroupMembers GroupMembers_1 WHERE ((GroupMembers_1.MemberId = '70')) AND ((Principals_2.Disabled = '0')) AND ((main.Domain = 'UserDefined')) AND ((main.id = GroupMembers_1.GroupId)) ORDER BY main.Name ASC
104              
105             sub _build_joins {
106             my $self = shift;
107             my $sb = shift;
108             my %seen_aliases;
109              
110             $seen_aliases{'main'} = 1;
111              
112             # We don't want to get tripped up on a dependency on a simple alias.
113             foreach my $alias ( @{ $sb->{'aliases'} } ) {
114             if ( $alias =~ /^(.*?)\s+(.*?)$/ ) {
115             $seen_aliases{$2} = 1;
116             }
117             }
118              
119             my $join_clause = $sb->table . " main ";
120              
121             my @keys = ( keys %{ $sb->{'leftjoins'} } );
122             my %seen;
123              
124             while ( my $join = shift @keys ) {
125             if ( !$sb->{'leftjoins'}{$join}{'depends_on'}
126             || $seen_aliases{ $sb->{'leftjoins'}{$join}{'depends_on'} } )
127             {
128              
129             #$join_clause = "(" . $join_clause;
130             $join_clause
131             .= $sb->{'leftjoins'}{$join}{'alias_string'} . " ON (";
132             $join_clause .= join( ') AND( ',
133             values %{ $sb->{'leftjoins'}{$join}{'criteria'} } );
134             $join_clause .= ") ";
135              
136             $seen_aliases{$join} = 1;
137             } else {
138             push( @keys, $join );
139             die "Unsatisfied dependency chain in Joins @keys"
140             if $seen{"@keys"}++;
141             }
142              
143             }
144             return ( join( ", ", ( $join_clause, @{ $sb->{'aliases'} } ) ) );
145              
146             }
147              
148             1;
149              
150             __END__
151              
152             =head1 AUTHOR
153              
154             Jesse Vincent, jesse@fsck.com
155              
156             =head1 SEE ALSO
157              
158             perl(1), Jifty::DBI
159              
160             =cut