root/branches/release-40/lib/MT/ObjectDriver/SQL/SQLite.pm @ 2625

Revision 2625, 1.8 kB (checked in by fumiakiy, 18 months ago)

Removed hard-coded SQL statement and pass select list to SQL class. This is mainly for enterprise database drivers to generate SQL statements correctly. BugId:79817

  • 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    my @select = @{$stmt->select};
33    $stmt->select([$col]);
34    my $class = ref $stmt;
35    my $main_stmt = $class->new;
36    $main_stmt->select(\@select);
37    $main_stmt->from_stmt($stmt);
38    $main_stmt->as_sql(@_);
39}
40
41sub field_decorator {
42    my $stmt = shift;
43    my ($class) = @_;
44    return sub {
45        my($term) = @_;
46        my $field_prefix = $class->datasource;
47        my $new_term = q();
48        while ($term =~ /extract\((\w+)\s+from\s+([\w_]+)\)(\s*desc|asc)?/ig) {
49            my $extract = "strftime('";
50            if ('year' eq lc($1)) {
51                $extract .= '%Y';
52            } elsif ('month' eq lc($1)) {
53                $extract .= '%m';
54            } elsif ('day' eq lc($1)) {
55                $extract .= '%d';
56            }
57            $extract .= "', $2)";
58            $extract .= $3 if defined $3;
59            $new_term .= ', ' if $new_term;
60            $new_term .= $extract;
61        }
62        $new_term = $term unless $new_term;
63        for my $col (@{ $class->column_names }) {
64            $new_term =~ s/\b$col\b/${field_prefix}_$col/g;
65        }
66        return $new_term;
67    };
68}
69
701;
Note: See TracBrowser for help on using the browser.