~ubuntu-branches/ubuntu/intrepid/libdbix-searchbuilder-perl/intrepid

« back to all changes in this revision

Viewing changes to SearchBuilder/Handle/SQLite.pm

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Quinney
  • Date: 2005-04-18 08:49:48 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050418084948-56icosrjnlekiqd2
Tags: 1.26-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
package DBIx::SearchBuilder::Handle::SQLite;
 
3
use DBIx::SearchBuilder::Handle;
 
4
@ISA = qw(DBIx::SearchBuilder::Handle);
 
5
 
 
6
use vars qw($VERSION @ISA $DBIHandle $DEBUG);
 
7
use strict;
 
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
=cut
 
24
 
 
25
# {{{ sub Insert
 
26
 
 
27
=head2 Insert
 
28
 
 
29
Takes a table name as the first argument and assumes that the rest of the arguments
 
30
are an array of key-value pairs to be inserted.
 
31
 
 
32
If the insert succeeds, returns the id of the insert, otherwise, returns
 
33
a Class::ReturnValue object with the error reported.
 
34
 
 
35
=cut
 
36
 
 
37
sub Insert  {
 
38
    my $self = shift;
 
39
    my $table = shift;
 
40
    my %args = ( id => undef, @_);
 
41
    # We really don't want an empty id
 
42
    
 
43
    my $sth = $self->SUPER::Insert($table, %args);
 
44
    return unless $sth;
 
45
 
 
46
    # If we have set an id, then we want to use that, otherwise, we want to lookup the last _new_ rowid
 
47
    $self->{'id'}= $args{'id'} || $self->dbh->func('last_insert_rowid');
 
48
 
 
49
    warn "$self no row id returned on row creation" unless ($self->{'id'});
 
50
    return( $self->{'id'}); #Add Succeded. return the id
 
51
  }
 
52
 
 
53
# }}}
 
54
 
 
55
 
 
56
=head2 CaseSensitive 
 
57
 
 
58
Returns undef, since SQLite's searches are not case sensitive by default 
 
59
 
 
60
=cut
 
61
 
 
62
sub CaseSensitive {
 
63
    my $self = shift;
 
64
    return(1);
 
65
}
 
66
 
 
67
sub BinarySafeBLOBs { 
 
68
    return undef;
 
69
}
 
70
 
 
71
# }}}
 
72
 
 
73
=head2 DistinctCount STATEMENTREF
 
74
 
 
75
takes an incomplete SQL SELECT statement and massages it to return a DISTINCT result count
 
76
 
 
77
 
 
78
=cut
 
79
 
 
80
sub DistinctCount {
 
81
    my $self = shift;
 
82
    my $statementref = shift;
 
83
 
 
84
    # Wrapper select query in a subselect as Oracle doesn't allow
 
85
    # DISTINCT against CLOB/BLOB column types.
 
86
    $$statementref = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
 
87
 
 
88
}
 
89
 
 
90
# }}}
 
91
 
 
92
 
 
93
=head2 _BuildJoins
 
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 _BuildJoins {
 
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->{'left_joins'} } );
 
122
    my %seen;
 
123
    
 
124
    while ( my $join = shift @keys ) {
 
125
        if ( ! $sb->{'left_joins'}{$join}{'depends_on'} || $seen_aliases{ $sb->{'left_joins'}{$join}{'depends_on'} } ) {
 
126
           #$join_clause = "(" . $join_clause;
 
127
            $join_clause .=
 
128
              $sb->{'left_joins'}{$join}{'alias_string'} . " ON (";
 
129
            $join_clause .=
 
130
              join ( ') AND( ',
 
131
                values %{ $sb->{'left_joins'}{$join}{'criteria'} } );
 
132
            $join_clause .= ") ";
 
133
            
 
134
            $seen_aliases{$join} = 1;
 
135
        }   
 
136
        else {
 
137
            push ( @keys, $join );
 
138
            die "Unsatisfied dependency chain in Joins @keys"
 
139
              if $seen{"@keys"}++;
 
140
        }     
 
141
        
 
142
    }
 
143
    return ( join ( ", ", ( $join_clause, @{ $sb->{'aliases'} } ) ) );
 
144
    
 
145
}
 
146
 
 
147
1;
 
148
 
 
149
__END__
 
150
 
 
151
=head1 AUTHOR
 
152
 
 
153
Jesse Vincent, jesse@fsck.com
 
154
 
 
155
=head1 SEE ALSO
 
156
 
 
157
perl(1), DBIx::SearchBuilder
 
158
 
 
159
=cut