-dt_$foo works for Pg
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / Pg.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLMaker::Pg;
3
4 use base qw( DBIx::Class::SQLMaker );
5 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
6
7 sub _datetime_now_sql { 'NOW()' }
8
9 {
10   my %part_map = (
11      century             => 'CENTURY',
12      decade              => 'DECADE',
13      day_of_month        => 'DAY',
14      day_of_week         => 'DOW',
15      day_of_year         => 'DOY',
16      seconds_since_epoch => 'EPOCH',
17      hour                => 'HOUR',
18      iso_day_of_week     => 'ISODOW',
19      iso_year            => 'ISOYEAR',
20      microsecond         => 'MICROSECONDS',
21      millenium           => 'MILLENIUM',
22      millisecond         => 'MILLISECONDS',
23      minute              => 'MINUTE',
24      month               => 'MONTH',
25      quarter             => 'QUARTER',
26      second              => 'SECOND',
27      timezone            => 'TIMEZONE',
28      timezone_hour       => 'TIMEZONE_HOUR',
29      timezone_minute     => 'TIMEZONE_MINUTE',
30      week                => 'WEEK',
31      year                => 'YEAR',
32   );
33
34   my %diff_part_map = %part_map;
35   $diff_part_map{day} = delete $diff_part_map{day_of_month};
36
37   sub _datetime_sql {
38     die $_[0]->_unsupported_date_extraction($_[1], 'PostgreSQL')
39        unless exists $part_map{$_[1]};
40     "EXTRACT($part_map{$_[1]} FROM $_[2])"
41   }
42   sub _datetime_diff_sql {
43     die $_[0]->_unsupported_date_diff($_[1], 'PostgreSQL')
44        unless exists $diff_part_map{$_[1]};
45     my $field_to_extract;
46     if ( $diff_part_map{$_[1]} eq 'SECOND' ) {
47        $field_to_extract = "EPOCH" ;
48     } else {
49         $field_to_extract = $diff_part_map{$_[1]};
50     }
51     ## adjusting this HERE as second will be needed elsewhere
52     "EXTRACT($field_to_extract FROM ($_[2]::timestamp with time zone - $_[3]::timestamp with time zone))"
53   }
54
55   sub _reorder_add_datetime_vars {
56      my ($self, $amount, $date) = @_;
57
58      return ($date, $amount);
59   }
60
61   sub _datetime_add_sql {
62     my ($self, $part, $date, $amount) = @_;
63
64     die $self->_unsupported_date_adding($part, 'PostgreSQL')
65       unless exists $diff_part_map{$part};
66
67     return "($date + $amount || ' $part_map{$part}'))"
68   }
69 }
70
71 =head1 DATE FUNCTION IMPLEMENTATION
72
73 The function used to extract date information is C<EXTRACT>, which supports
74
75  century
76  decade
77  day_of_month
78  day_of_week
79  day_of_year
80  seconds_since_epoch
81  hour
82  iso_day_of_week
83  iso_year
84  microsecond
85  millenium
86  millisecond
87  minute
88  month
89  quarter
90  second
91  timezone
92  timezone_hour
93  timezone_minute
94  week
95  year
96
97 The function used to diff dates is subtraction and C<EXTRACT>, which supports
98
99  century
100  decade
101  day
102  seconds_since_epoch
103  hour
104  iso_day_of_week
105  iso_year
106  microsecond
107  millenium
108  millisecond
109  minute
110  month
111  quarter
112  second
113  timezone
114  timezone_hour
115  timezone_minute
116  week
117  year
118
119 =cut
120
121
122 1;