day_of_month diff does not make any sense
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / MySQL.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLMaker::MySQL;
3
4 use base qw( DBIx::Class::SQLMaker );
5
6 #
7 # MySQL does not understand the standard INSERT INTO $table DEFAULT VALUES
8 # Adjust SQL here instead
9 #
10 sub insert {
11   my $self = shift;
12
13   if (! $_[1] or (ref $_[1] eq 'HASH' and !keys %{$_[1]} ) ) {
14     my $table = $self->_quote($_[0]);
15     return "INSERT INTO ${table} () VALUES ()"
16   }
17
18   return $self->next::method (@_);
19 }
20
21 # Allow STRAIGHT_JOIN's
22 sub _generate_join_clause {
23     my ($self, $join_type) = @_;
24
25     if( $join_type && $join_type =~ /^STRAIGHT\z/i ) {
26         return ' STRAIGHT_JOIN '
27     }
28
29     return $self->next::method($join_type);
30 }
31
32 # LOCK IN SHARE MODE
33 my $for_syntax = {
34    update => 'FOR UPDATE',
35    shared => 'LOCK IN SHARE MODE'
36 };
37
38 sub _lock_select {
39    my ($self, $type) = @_;
40
41    my $sql = $for_syntax->{$type}
42     || $self->throw_exception("Unknown SELECT .. FOR type '$type' requested");
43
44    return " $sql";
45 }
46
47 {
48   my %part_map = (
49      month        => 'MONTH',
50      day_of_month => 'DAY',
51      year         => 'YEAR',
52   );
53
54   my %diff_part_map = %part_map;
55   $diff_part_map{day} = delete $diff_part_map{day_of_month};
56
57   sub _datetime_sql { "EXTRACT($part_map{$_[1]} FROM $_[2])" }
58   sub _datetime_diff_sql { "TIMESTAMPDIFF($diff_part_map{$_[1]}, $_[2], $_[3])" }
59 }
60
61 1;