Add datetime adding and now to 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     "EXTRACT($diff_part_map{$_[1]} FROM ($_[2] - $_[3]))"
46   }
47
48   sub _reorder_add_datetime_vars {
49      my ($self, $amount, $date) = @_;
50
51      return ($date, $amount);
52   }
53
54   sub _datetime_add_sql {
55     my ($self, $part, $date, $amount) = @_;
56
57     die $self->_unsupported_date_adding($part, 'PostgreSQL')
58       unless exists $diff_part_map{$part};
59
60     return "($date + $amount || ' $part_map{$part}'))"
61   }
62 }
63
64 =head1 DATE FUNCTION IMPLEMENTATION
65
66 The function used to extract date information is C<EXTRACT>, which supports
67
68  century
69  decade
70  day_of_month
71  day_of_week
72  day_of_year
73  seconds_since_epoch
74  hour
75  iso_day_of_week
76  iso_year
77  microsecond
78  millenium
79  millisecond
80  minute
81  month
82  quarter
83  second
84  timezone
85  timezone_hour
86  timezone_minute
87  week
88  year
89
90 The function used to diff dates is subtraction and C<EXTRACT>, which supports
91
92  century
93  decade
94  day
95  seconds_since_epoch
96  hour
97  iso_day_of_week
98  iso_year
99  microsecond
100  millenium
101  millisecond
102  minute
103  month
104  quarter
105  second
106  timezone
107  timezone_hour
108  timezone_minute
109  week
110  year
111
112 =cut
113
114
115 1;