root/branches/release-26/lib/MT/ObjectDriver/SQL/SQLite.pm @ 1174

Revision 1174, 1.7 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

  • Property svn:keywords set to Id Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::ObjectDriver::SQL::SQLite;
8
9use strict;
10use warnings;
11use base qw( MT::ObjectDriver::SQL );
12
13sub new {
14    my $class = shift;
15    my %param = @_;
16    my $cd = delete $param{count_distinct};
17    my $stmt = $class->SUPER::new(%param);
18    if ($cd) {
19        $stmt->{count_distinct} = $cd;
20    }
21    return $stmt;
22}
23
24#--------------------------------------#
25# Instance Methods
26
27sub as_sql {
28    my $stmt = shift;
29    return $stmt->SUPER::as_sql(@_) unless exists $stmt->{count_distinct};
30    my $cd  = delete $stmt->{count_distinct};
31    my ($col) = each %$cd;
32    $stmt->select([$col]);
33    my $class = ref $stmt;
34    my $main_stmt = $class->new;
35    $main_stmt->from_stmt($stmt);
36    $main_stmt->as_sql(@_);
37}
38
39sub field_decorator {
40    my $stmt = shift;
41    my ($class) = @_;
42    return sub {
43        my($term) = @_;
44        my $field_prefix = $class->datasource;
45        my $new_term = q();
46        while ($term =~ /extract\((\w+)\s+from\s+([\w_]+)\)(\s*desc|asc)?/ig) {
47            my $extract = "strftime('";
48            if ('year' eq lc($1)) {
49                $extract .= '%Y';
50            } elsif ('month' eq lc($1)) {
51                $extract .= '%m';
52            } elsif ('day' eq lc($1)) {
53                $extract .= '%d';
54            }
55            $extract .= "', $2)";
56            $extract .= $3 if defined $3;
57            $new_term .= ', ' if $new_term;
58            $new_term .= $extract;
59        }
60        $new_term = $term unless $new_term;
61        for my $col (@{ $class->column_names }) {
62            $new_term =~ s/\b$col\b/${field_prefix}_$col/g;
63        }
64        return $new_term;
65    };
66}
67
681;
Note: See TracBrowser for help on using the browser.