Skip to content

Add MariaDB.pm package #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: maint/0.0828xx
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions lib/DBIx/Class/SQLMaker/MariaDB.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package # Hide from PAUSE
DBIx::Class::SQLMaker::MariaDB;

use warnings;
use strict;

use base qw( DBIx::Class::SQLMaker );

#
# MariaDB does not understand the standard INSERT INTO $table DEFAULT VALUES
# Adjust SQL here instead
#
sub insert {
my $self = shift;

if (! $_[1] or (ref $_[1] eq 'HASH' and !keys %{$_[1]} ) ) {
my $table = $self->_quote($_[0]);
return "INSERT INTO ${table} () VALUES ()"
}

return $self->next::method (@_);
}

# Allow STRAIGHT_JOIN's
sub _generate_join_clause {
my ($self, $join_type) = @_;

if( $join_type && $join_type =~ /^STRAIGHT\z/i ) {
return ' STRAIGHT_JOIN '
}

return $self->next::method($join_type);
}

my $force_double_subq;
$force_double_subq = sub {
my ($self, $sql) = @_;

require Text::Balanced;
my $new_sql;
while (1) {

my ($prefix, $parenthesized);

($parenthesized, $sql, $prefix) = do {
# idiotic design - writes to $@ but *DOES NOT* throw exceptions
local $@;
Text::Balanced::extract_bracketed( $sql, '()', qr/[^\(]*/ );
};

# this is how an error is indicated, in addition to crapping in $@
last unless $parenthesized;

if ($parenthesized =~ $self->{_modification_target_referenced_re}) {
# is this a select subquery?
if ( $parenthesized =~ /^ \( \s* SELECT \s+ /xi ) {
$parenthesized = "( SELECT * FROM $parenthesized `_forced_double_subquery` )";
}
# then drill down until we find it (if at all)
else {
$parenthesized =~ s/^ \( (.+) \) $/$1/x;
$parenthesized = join ' ', '(', $self->$force_double_subq( $parenthesized ), ')';
}
}

$new_sql .= $prefix . $parenthesized;
}

return $new_sql . $sql;
};

sub update {
my $self = shift;

# short-circuit unless understood identifier
return $self->next::method(@_) unless $self->{_modification_target_referenced_re};

my ($sql, @bind) = $self->next::method(@_);

$sql = $self->$force_double_subq($sql)
if $sql =~ $self->{_modification_target_referenced_re};

return ($sql, @bind);
}

sub delete {
my $self = shift;

# short-circuit unless understood identifier
return $self->next::method(@_) unless $self->{_modification_target_referenced_re};

my ($sql, @bind) = $self->next::method(@_);

$sql = $self->$force_double_subq($sql)
if $sql =~ $self->{_modification_target_referenced_re};

return ($sql, @bind);
}

# LOCK IN SHARE MODE
my $for_syntax = {
update => 'FOR UPDATE',
shared => 'LOCK IN SHARE MODE'
};

sub _lock_select {
my ($self, $type) = @_;

my $sql = $for_syntax->{$type}
|| $self->throw_exception("Unknown SELECT .. FOR type '$type' requested");

return " $sql";
}

1;