First cut. Need to add control of how many rows are sent at once.
[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 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
6
7 #
8 # MySQL does not understand the standard INSERT INTO $table DEFAULT VALUES
9 # Adjust SQL here instead
10 #
11 sub insert {
12   my $self = shift;
13
14   my $table = $_[0];
15   $table = $self->_quote($table);
16
17   if (! $_[1] or (ref $_[1] eq 'HASH' and !keys %{$_[1]} ) ) {
18     return "INSERT INTO ${table} () VALUES ()"
19   }
20
21   return $self->SUPER::insert (@_);
22 }
23
24 sub insert_bulk {
25   my ($self, $table, $data, $cols) = @_;
26
27   my $sql = sprintf(
28     'INSERT INTO %s ( ', $self->_quote($table),
29   );
30   $sql .= join( ', ', map { $self->_quote($_) } @$cols );
31   $sql .= ' ) VALUES ';
32
33   my @bind;
34   my @sql;
35   foreach my $datum ( @$data ) {
36     push @sql, '('
37       . join( ', ', ('?') x @$datum )
38       . ')';
39     push @bind, map { [ dummy => $_ ] } @$datum;
40   }
41
42   return (
43     $sql . join(',', @sql),
44     @bind
45   );
46 }
47
48 # Allow STRAIGHT_JOIN's
49 sub _generate_join_clause {
50     my ($self, $join_type) = @_;
51
52     if( $join_type && $join_type =~ /^STRAIGHT\z/i ) {
53         return ' STRAIGHT_JOIN '
54     }
55
56     return $self->SUPER::_generate_join_clause( $join_type );
57 }
58
59 # LOCK IN SHARE MODE
60 my $for_syntax = {
61    update => 'FOR UPDATE',
62    shared => 'LOCK IN SHARE MODE'
63 };
64
65 sub _lock_select {
66    my ($self, $type) = @_;
67
68    my $sql = $for_syntax->{$type} || croak "Unknown SELECT .. FOR type '$type' requested";
69
70    return " $sql";
71 }
72
73 1;