our %Defaults = (
expand => {
- not => '_expand_not',
bool => '_expand_bool',
- and => '_expand_op_andor',
- or => '_expand_op_andor',
nest => '_expand_nest',
- bind => '_expand_bind',
- in => '_expand_in',
- not_in => '_expand_in',
row => '_expand_row',
- between => '_expand_between',
- not_between => '_expand_between',
op => '_expand_op',
- (map +($_ => '_expand_op_is'), ('is', 'is_not')),
- ident => '_expand_ident',
- value => '_expand_value',
func => '_expand_func',
values => '_expand_values',
+ bind => '_expand_noop',
+ literal => '_expand_noop',
},
expand_op => {
'between' => '_expand_between',
# special operators
$opt{special_ops} ||= [];
- if ($class->isa('DBIx::Class::SQLMaker')) {
- $opt{warn_once_on_nest} = 1;
- $opt{disable_old_special_ops} = 1;
- }
-
# unary operators
$opt{unary_ops} ||= [];
$opt{$name} = { %{$Defaults{$name}}, %{$opt{$name}||{}} };
}
- foreach my $type (qw(insert update delete)) {
- my $method = "_${type}_returning";
- if ($class ne __PACKAGE__
- and __PACKAGE__->can($method) ne $class->can($method)) {
- my $clause = "${type}.returning";
- $opt{expand_clause}{$clause} = sub { $_[2] },
- $opt{render_clause}{$clause}
- = sub { [ $_[0]->$method($_[3]) ] };
+ if ($class ne __PACKAGE__) {
+
+ # check for overriden methods
+
+ foreach my $type (qw(insert update delete)) {
+ my $method = "_${type}_returning";
+ if (__PACKAGE__->can($method) ne $class->can($method)) {
+ my $clause = "${type}.returning";
+ $opt{expand_clause}{$clause} = sub { $_[2] },
+ $opt{render_clause}{$clause}
+ = sub { [ $_[0]->$method($_[3]) ] };
+ }
+ }
+ if (__PACKAGE__->can('_table') ne $class->can('_table')) {
+ $opt{expand_clause}{'select.from'} = sub {
+ return +{ -literal => [ $_[0]->_table($_[2]) ] };
+ };
+ }
+ if (__PACKAGE__->can('_order_by') ne $class->can('_order_by')) {
+ $opt{expand_clause}{'select.order_by'} = sub { $_[2] };
+ $opt{render_clause}{'select.order_by'} = sub {
+ [ $_[0]->_order_by($_[2]) ];
+ };
+ }
+ if ($class->isa('DBIx::Class::SQLMaker')) {
+ $opt{warn_once_on_nest} = 1;
+ $opt{disable_old_special_ops} = 1;
+ $opt{render_clause}{'select.where'} = sub {
+ my ($sql, @bind) = $_[0]->where($_[2]);
+ s/\A\s+//, s/\s+\Z// for $sql;
+ return [ $sql, @bind ];
+ };
}
}
}
sub _expand_insert_clause_target {
- +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+ +(target => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
sub _expand_insert_clause_fields {
return +{ -row => [
- $_[0]->_expand_maybe_list_expr($_[2], -ident)
+ $_[0]->expand_maybe_list_expr($_[2], -ident)
] } if ref($_[2]) eq 'ARRAY';
return $_[2]; # should maybe still expand somewhat?
}
}
sub _expand_insert_clause_returning {
- +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+ +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
sub _expand_insert_values {
my $f = $options->{returning};
my ($sql, @bind) = @{ $self->render_aqt(
- $self->_expand_maybe_list_expr($f, -ident)
+ $self->expand_maybe_list_expr($f, -ident)
) };
return ($self->_sqlcase(' returning ').$sql, @bind);
}
sub _expand_update_set_values {
my ($self, undef, $data) = @_;
- $self->_expand_maybe_list_expr( [
+ $self->expand_maybe_list_expr( [
map {
my ($k, $set) = @$_;
$set = { -bind => $_ } unless defined $set;
sub _expand_update_clause_target {
my ($self, undef, $target) = @_;
- +(target => $self->_expand_maybe_list_expr($target, -ident));
+ +(target => $self->expand_maybe_list_expr($target, -ident));
}
sub _expand_update_clause_set {
}
sub _expand_update_clause_returning {
- +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+ +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
# So that subclasses can override UPDATE ... RETURNING separately from
sub _expand_select_clause_select {
my ($self, undef, $select) = @_;
- +(select => $self->_expand_maybe_list_expr($select, -ident));
+ +(select => $self->expand_maybe_list_expr($select, -ident));
}
sub _expand_select_clause_from {
my ($self, undef, $from) = @_;
- +(from => $self->_expand_maybe_list_expr($from, -ident));
+ +(from => $self->expand_maybe_list_expr($from, -ident));
}
sub _expand_select_clause_where {
};
};
$self->clone
- ->wrap_expanders(map +($_ => $_wrap), qw(ident value bind))
- ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value bind))
+ ->wrap_expander(bind => $_wrap)
+ ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value))
->wrap_expander(func => sub {
my $orig = shift;
sub {
my ($self, $fields) = @_;
return $fields unless ref($fields);
return @{ $self->render_aqt(
- $self->_expand_maybe_list_expr($fields, '-ident')
+ $self->expand_maybe_list_expr($fields, '-ident')
) };
}
sub _delete_returning { shift->_returning(@_) }
sub _expand_delete_clause_target {
- +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+ +(target => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
sub _expand_delete_clause_where { +(where => $_[0]->expand_expr($_[2])); }
sub _expand_delete_clause_returning {
- +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
+ +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
sub _render_delete_clause_target {
}
}
- if (my $exp = $self->{expand}{$op}) {
+ if (my $exp = $self->{expand}{$op}||$self->{expand_op}{$op}) {
return $self->$exp($op, $v);
}
}
}
- # an explicit node type is currently assumed to be expanded (this is almost
- # certainly wrong and there should be expansion anyway)
-
- if ($self->{render}{$op}) {
- return { $k => $v };
- }
-
- my $type = $self->{unknown_unop_always_func} ? -func : -op;
+ my $type = (
+ $self->{unknown_unop_always_func} && !$self->{render_op}{$op}
+ ? -func
+ : -op
+ );
{ # Old SQLA compat
and (keys %$v)[0] =~ /^-/
) {
$type = (
- (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
+ (
+ (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
+ or $self->{render_op}{$op}
+ )
? -op
: -func
)
}
}
- return +{ $type => [
- $op,
- ($type eq -func and ref($v) eq 'ARRAY')
- ? map $self->_expand_expr($_), @$v
- : $self->_expand_expr($v)
- ] };
+ if ($type eq -func and ref($v) eq 'ARRAY') {
+ return $self->_expand_expr({ -func => [ $op, @$v ] });
+ }
+
+ return $self->_expand_expr({ $type => [ $op, $v ] });
}
sub _expand_hashpair_cmp {
+{ -bind => [ our $Cur_Col_Meta, $_[2] ] };
}
-sub _expand_not {
- +{ -op => [ 'not', $_[0]->_expand_expr($_[2]) ] };
-}
-
sub _expand_row {
my ($self, undef, $args) = @_;
+{ -row => [ map $self->expand_expr($_), @$args ] };
return $self->_expand_expr($v);
}
-sub _expand_bind {
- my ($self, undef, $bind) = @_;
- return { -bind => $bind };
+sub _expand_noop {
+ my ($self, $type, $v) = @_;
+ return { "-${type}" => $v };
}
sub _expand_values {
: ((ref($_) eq 'ARRAY') ? $_ : [ $_ ])
), @parts;
return [
- $self->{join_sql_parts}->($join, grep defined, map $_->[0], @final),
+ $self->{join_sql_parts}->(
+ $join, grep defined && length, map $_->[0], @final
+ ),
(map @{$_}[1..$#$_], @final),
];
}
return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
- return $self->_expand_maybe_list_expr($arg)
+ return $self->expand_maybe_list_expr($arg)
if ref($arg) eq 'HASH' and ($arg->{-op}||[''])->[0] eq ',';
my $expander = sub {
my $self = shift;
my $from = shift;
$self->render_aqt(
- $self->_expand_maybe_list_expr($from, -ident)
+ $self->expand_maybe_list_expr($from, -ident)
)->[0];
}
# UTILITY FUNCTIONS
#======================================================================
-sub _expand_maybe_list_expr {
+sub expand_maybe_list_expr {
my ($self, $expr, $default) = @_;
return { -op => [
',', map $self->expand_expr($_, $default),