use Scalar::Util ();
use Exporter 'import';
-our @EXPORT_OK = qw(is_plain_value is_literal_value);
+our @EXPORT_OK = qw(is_plain_value is_literal_value is_undef_value);
BEGIN {
if ($] < 5.009_005) {
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',
},
expand_op => {
- 'between' => '_expand_between',
- 'not_between' => '_expand_between',
- 'in' => '_expand_in',
- 'not_in' => '_expand_in',
- 'nest' => '_expand_nest',
+ (map +($_ => __PACKAGE__->make_binop_expander('_expand_between')),
+ qw(between not_between)),
+ (map +($_ => __PACKAGE__->make_binop_expander('_expand_in')),
+ qw(in not_in)),
(map +($_ => '_expand_op_andor'), ('and', 'or')),
(map +($_ => '_expand_op_is'), ('is', 'is_not')),
- 'ident' => '_expand_ident',
- 'value' => '_expand_value',
+ (map +($_ => __PACKAGE__->make_unop_expander("_expand_${_}")),
+ qw(ident value nest)),
},
render => {
- (map +($_, "_render_$_"), qw(op func bind ident literal row)),
+ (map +($_, "_render_$_"),
+ qw(op func bind ident literal row values keyword)),
},
render_op => {
(map +($_ => '_render_op_between'), 'between', 'not_between'),
(map +($_ => '_render_op_andor'), qw(and or)),
',' => '_render_op_multop',
},
+ clauses_of => {
+ delete => [ qw(target where returning) ],
+ update => [ qw(target set where returning) ],
+ insert => [ qw(target fields from returning) ],
+ select => [ qw(select from where order_by) ],
+ },
+ expand_clause => {
+ 'delete.from' => '_expand_delete_clause_target',
+ 'update.update' => '_expand_update_clause_target',
+ 'insert.into' => '_expand_insert_clause_target',
+ 'insert.values' => '_expand_insert_clause_from',
+ },
+ render_clause => {
+ 'delete.target' => '_render_delete_clause_target',
+ 'update.target' => '_render_update_clause_target',
+ 'insert.target' => '_render_insert_clause_target',
+ 'insert.fields' => '_render_insert_clause_fields',
+ 'insert.from' => '_render_insert_clause_from',
+ },
);
+foreach my $stmt (keys %{$Defaults{clauses_of}}) {
+ $Defaults{expand}{$stmt} = '_expand_statement';
+ $Defaults{render}{$stmt} = '_render_statement';
+ foreach my $clause (@{$Defaults{clauses_of}{$stmt}}) {
+ $Defaults{expand_clause}{"${stmt}.${clause}"}
+ = "_expand_${stmt}_clause_${clause}";
+ }
+}
+
sub new {
my $self = shift;
my $class = ref($self) || $self;
# 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{expand_unary} = {};
foreach my $name (sort keys %Defaults) {
- $opt{$name} = { %{$Defaults{$name}} };
+ $opt{$name} = { %{$Defaults{$name}}, %{$opt{$name}||{}} };
+ }
+
+ 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 ];
+ };
+ }
}
if ($opt{lazy_join_sql_parts}) {
return bless \%opt, $class;
}
+sub _ext_rw {
+ my ($self, $name, $key, $value) = @_;
+ return $self->{$name}{$key} unless @_ > 3;
+ $self->{$name}{$key} = $value;
+ return $self;
+}
+
+sub make_unop_expander {
+ my (undef, $exp) = @_;
+ sub {
+ my ($self, $name, $body, $k) = @_;
+ return $self->_expand_hashpair_cmp($k, { "-${name}" => $body })
+ if defined($k);
+ return $self->$exp($name, $body);
+ }
+}
+
+sub make_binop_expander {
+ my (undef, $exp) = @_;
+ sub {
+ my ($self, $name, $body, $k) = @_;
+ $k = shift @{$body = [ @$body ]} unless defined $k;
+ $k = ref($k) ? $k : { -ident => $k };
+ return $self->$exp($name, $body, $k);
+ }
+}
+
+BEGIN {
+ foreach my $type (qw(
+ expand op_expand render op_render clause_expand clause_render
+ )) {
+ my $name = join '_', reverse split '_', $type;
+ my $singular = "${type}er";
+
+ eval qq{sub ${singular} { shift->${singular}s(\@_) }; 1 }
+ or die "Method builder failed for ${singular}: $@";
+ eval qq{sub wrap_${singular} {
+ shift->wrap_${singular}s(\@_)
+ }; 1 } or die "Method builder failed for wrap_${singular}: $@";
+
+ eval qq{sub ${singular}s {
+ my (\$self, \@args) = \@_;
+ while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
+ \$self->_ext_rw('${name}', \$this_key, \$this_value);
+ }
+ return \$self;
+ }; 1 } or die "Method builder failed for ${singular}s: $@";
+ eval qq{sub wrap_${singular}s {
+ my (\$self, \@args) = \@_;
+ while (my (\$this_key, \$this_builder) = splice(\@args, 0, 2)) {
+ my \$orig = \$self->_ext_rw('${name}', \$this_key);
+ \$self->_ext_rw(
+ '${name}', \$this_key,
+ \$this_builder->(\$orig, '${name}', \$this_key),
+ );
+ }
+ return \$self;
+ }; 1 } or die "Method builder failed for wrap_${singular}s: $@";
+ eval qq{sub ${singular}_list { sort keys %{\$_[0]->{\$name}} }; 1; }
+ or die "Method builder failed for ${singular}_list: $@";
+ }
+ foreach my $singular (qw(unop_expander binop_expander)) {
+ eval qq{sub ${singular} { shift->${singular}s(\@_) }; 1 }
+ or die "Method builder failed for ${singular}: $@";
+ eval qq{sub ${singular}s {
+ my (\$self, \@args) = \@_;
+ while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
+ \$self->_ext_rw(
+ expand_op => \$this_key,
+ \$self->make_${singular}(\$this_value),
+ );
+ }
+ return \$self;
+ }; 1 } or die "Method builder failed for ${singular}s: $@";
+ }
+}
+
+#sub register_op { $_[0]->{is_op}{$_[1]} = 1; $_[0] }
+
+sub statement_list { sort keys %{$_[0]->{clauses_of}} }
+
+sub clauses_of {
+ my ($self, $of, @clauses) = @_;
+ unless (@clauses) {
+ return @{$self->{clauses_of}{$of}||[]};
+ }
+ if (ref($clauses[0]) eq 'CODE') {
+ @clauses = $self->${\($clauses[0])}(@{$self->{clauses_of}{$of}||[]});
+ }
+ $self->{clauses_of}{$of} = \@clauses;
+ return $self;
+}
+
+sub clone {
+ my ($self) = @_;
+ bless(
+ {
+ (map +($_ => (
+ ref($self->{$_}) eq 'HASH'
+ ? { %{$self->{$_}} }
+ : $self->{$_}
+ )), keys %$self),
+ },
+ ref($self)
+ );
+}
+
sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
#======================================================================
sub insert {
- my $self = shift;
- my $table = $self->_table(shift);
- my $data = shift || return;
- my $options = shift;
+ my ($self, $table, $data, $options) = @_;
- my $fields;
+ my $stmt = do {
+ if (ref($table) eq 'HASH') {
+ $table;
+ } else {
+ my %clauses = (target => $table, values => $data, %{$options||{}});
+ \%clauses;
+ }
+ };
+ my @rendered = $self->render_statement({ -insert => $stmt });
+ return wantarray ? @rendered : $rendered[0];
+}
- my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
+sub _expand_insert_clause_target {
+ +(target => $_[0]->expand_maybe_list_expr($_[2], -ident));
+}
- my @parts = ([ $self->_sqlcase('insert into').' '.$table ]);
- push @parts, $self->render_aqt($f_aqt) if $f_aqt;
- push @parts, [ $self->_sqlcase('values') ], $self->render_aqt($v_aqt);
+sub _expand_insert_clause_fields {
+ return +{ -row => [
+ $_[0]->expand_maybe_list_expr($_[2], -ident)
+ ] } if ref($_[2]) eq 'ARRAY';
+ return $_[2]; # should maybe still expand somewhat?
+}
- if ($options->{returning}) {
- push @parts, [ $self->_insert_returning($options) ];
+sub _expand_insert_clause_from {
+ my ($self, undef, $data) = @_;
+ if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) {
+ return $self->expand_expr($data);
}
+ my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
+ return (
+ from => { -values => [ $v_aqt ] },
+ ($f_aqt ? (fields => $f_aqt) : ()),
+ );
+}
- my ($sql, @bind) = @{ $self->join_query_parts(' ', @parts) };
- return wantarray ? ($sql, @bind) : $sql;
+sub _expand_insert_clause_returning {
+ +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
}
sub _expand_insert_values {
}
}
+sub _render_insert_clause_fields {
+ return $_[0]->render_aqt($_[2]);
+}
+
+sub _render_insert_clause_target {
+ my ($self, undef, $from) = @_;
+ $self->join_query_parts(' ', { -keyword => 'insert into' }, $from);
+}
+
+sub _render_insert_clause_from {
+ return $_[0]->render_aqt($_[2], 1);
+}
+
# So that subclasses can override INSERT ... RETURNING separately from
# UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
sub _insert_returning { shift->_returning(@_) }
+sub _redispatch_returning {
+ my ($self, $type, undef, $returning) = @_;
+ [ $self->${\"_${type}_returning"}({ returning => $returning }) ];
+}
+
sub _returning {
my ($self, $options) = @_;
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);
}
# UPDATE methods
#======================================================================
-
sub update {
- my $self = shift;
- my $table = $self->_table(shift);
- my $data = shift || return;
- my $where = shift;
- my $options = shift;
-
- # first build the 'SET' part of the sql statement
- puke "Unsupported data type specified to \$sql->update"
- unless ref $data eq 'HASH';
-
- my ($sql, @all_bind) = $self->_update_set_values($data);
- $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
- . $sql;
-
- if ($where) {
- my($where_sql, @where_bind) = $self->where($where);
- $sql .= $where_sql;
- push @all_bind, @where_bind;
- }
+ my ($self, $table, $set, $where, $options) = @_;
- if ($options->{returning}) {
- my ($returning_sql, @returning_bind) = $self->_update_returning($options);
- $sql .= $returning_sql;
- push @all_bind, @returning_bind;
- }
+ my $stmt = do {
+ if (ref($table) eq 'HASH') {
+ $table
+ } else {
+ my %clauses;
+ @clauses{qw(target set where)} = ($table, $set, $where);
+ puke "Unsupported data type specified to \$sql->update"
+ unless ref($clauses{set}) eq 'HASH';
+ @clauses{keys %$options} = values %$options;
+ \%clauses;
+ }
+ };
+ my @rendered = $self->render_statement({ -update => $stmt });
+ return wantarray ? @rendered : $rendered[0];
+}
- return wantarray ? ($sql, @all_bind) : $sql;
+sub _render_update_clause_target {
+ my ($self, undef, $target) = @_;
+ $self->join_query_parts(' ', { -keyword => 'update' }, $target);
}
sub _update_set_values {
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));
+}
+
+sub _expand_update_clause_set {
+ return $_[2] if ref($_[2]) eq 'HASH' and ($_[2]->{-op}||[''])->[0] eq ',';
+ +(set => $_[0]->_expand_update_set_values($_[1], $_[2]));
+}
+
+sub _expand_update_clause_where {
+ +(where => $_[0]->expand_expr($_[2]));
+}
+
+sub _expand_update_clause_returning {
+ +(returning => $_[0]->expand_maybe_list_expr($_[2], -ident));
+}
+
# So that subclasses can override UPDATE ... RETURNING separately from
# INSERT and DELETE
sub _update_returning { shift->_returning(@_) }
# SELECT
#======================================================================
-
sub select {
- my $self = shift;
- my $table = $self->_table(shift);
- my $fields = shift || '*';
- my $where = shift;
- my $order = shift;
+ my ($self, @args) = @_;
+ my $stmt = do {
+ if (ref(my $sel = $args[0]) eq 'HASH') {
+ $sel
+ } else {
+ my %clauses;
+ @clauses{qw(from select where order_by)} = @args;
- my ($fields_sql, @bind) = $self->_select_fields($fields);
+ # This oddity is to literalify since historically SQLA doesn't quote
+ # a single identifier argument, so we convert it into a literal
- my ($where_sql, @where_bind) = $self->where($where, $order);
- push @bind, @where_bind;
+ $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
+ unless ref($clauses{select});
+ \%clauses;
+ }
+ };
- my $sql = join(' ', $self->_sqlcase('select'), $fields_sql,
- $self->_sqlcase('from'), $table)
- . $where_sql;
+ my @rendered = $self->render_statement({ -select => $stmt });
+ return wantarray ? @rendered : $rendered[0];
+}
- return wantarray ? ($sql, @bind) : $sql;
+sub _expand_select_clause_select {
+ my ($self, undef, $select) = @_;
+ +(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));
+}
+
+sub _expand_select_clause_where {
+ my ($self, undef, $where) = @_;
+
+ my $sqla = do {
+ if (my $conv = $self->{convert}) {
+ my $_wrap = sub {
+ my $orig = shift;
+ sub {
+ my $self = shift;
+ +{ -func => [
+ $conv,
+ $self->$orig(@_)
+ ] };
+ };
+ };
+ $self->clone
+ ->wrap_expander(bind => $_wrap)
+ ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value))
+ ->wrap_expander(func => sub {
+ my $orig = shift;
+ sub {
+ my ($self, $type, $thing) = @_;
+ if (ref($thing) eq 'ARRAY' and $thing->[0] eq $conv
+ and @$thing == 2 and ref($thing->[1]) eq 'HASH'
+ and (
+ $thing->[1]{-ident}
+ or $thing->[1]{-value}
+ or $thing->[1]{-bind})
+ ) {
+ return { -func => $thing }; # already went through our expander
+ }
+ return $self->$orig($type, $thing);
+ }
+ });
+ } else {
+ $self;
+ }
+ };
+
+ return +(where => $sqla->expand_expr($where));
+}
+
+sub _expand_select_clause_order_by {
+ my ($self, undef, $order_by) = @_;
+ +(order_by => $self->_expand_order_by($order_by));
}
sub _select_fields {
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')
) };
}
# DELETE
#======================================================================
-
sub delete {
- my $self = shift;
- my $table = $self->_table(shift);
- my $where = shift;
- my $options = shift;
-
- my($where_sql, @bind) = $self->where($where);
- my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
-
- if ($options->{returning}) {
- my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
- $sql .= $returning_sql;
- push @bind, @returning_bind;
- }
+ my ($self, $table, $where, $options) = @_;
- return wantarray ? ($sql, @bind) : $sql;
+ my $stmt = do {
+ if (ref($table) eq 'HASH') {
+ $table;
+ } else {
+ my %clauses = (target => $table, where => $where, %{$options||{}});
+ \%clauses;
+ }
+ };
+ my @rendered = $self->render_statement({ -delete => $stmt });
+ return wantarray ? @rendered : $rendered[0];
}
# So that subclasses can override DELETE ... RETURNING separately from
# INSERT and UPDATE
sub _delete_returning { shift->_returning(@_) }
+sub _expand_delete_clause_target {
+ +(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));
+}
+sub _render_delete_clause_target {
+ my ($self, undef, $from) = @_;
+ $self->join_query_parts(' ', { -keyword => 'delete from' }, $from);
+}
#======================================================================
# WHERE: entry point
}
sub render_aqt {
- my ($self, $aqt) = @_;
+ my ($self, $aqt, $top_level) = @_;
my ($k, $v, @rest) = %$aqt;
die "No" if @rest;
die "Not a node type: $k" unless $k =~ s/^-//;
if (my $meth = $self->{render}{$k}) {
+ local our $Render_Top_Level = $top_level;
return $self->$meth($k, $v);
}
die "notreached: $k";
) };
}
+sub render_statement {
+ my ($self, $expr, $default_scalar_to) = @_;
+ @{$self->render_aqt(
+ $self->expand_expr($expr, $default_scalar_to), 1
+ )};
+}
+
+sub _expand_statement {
+ my ($self, $type, $args) = @_;
+ my $ec = $self->{expand_clause};
+ if ($args->{_}) {
+ $args = { %$args };
+ $args->{$type} = delete $args->{_}
+ }
+ my %has_clause = map +($_ => 1), @{$self->{clauses_of}{$type}};
+ return +{ "-${type}" => +{
+ map {
+ my $val = $args->{$_};
+ if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
+ if ((my (@exp) = $self->$exp($_ => $val)) == 1) {
+ ($_ => $exp[0])
+ } else {
+ @exp
+ }
+ } elsif ($has_clause{$_}) {
+ ($_ => $self->expand_expr($val))
+ } else {
+ ($_ => $val)
+ }
+ } sort keys %$args
+ } };
+}
+
+sub _render_statement {
+ my ($self, $type, $args) = @_;
+ my @parts;
+ foreach my $clause (@{$self->{clauses_of}{$type}}) {
+ next unless my $clause_expr = $args->{$clause};
+ my $part = do {
+ if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
+ $self->$rdr($clause, $clause_expr, $args);
+ } else {
+ my $r = $self->render_aqt($clause_expr, 1);
+ next unless defined $r->[0] and length $r->[0];
+ $self->join_query_parts(' ',
+ { -keyword => $clause },
+ $r
+ );
+ }
+ };
+ push @parts, $part;
+ }
+ my $q = $self->join_query_parts(' ', @parts);
+ return $self->join_query_parts('',
+ (our $Render_Top_Level ? $q : ('(', $q, ')'))
+ );
+}
+
sub _normalize_op {
my ($self, $raw) = @_;
my $op = lc $raw;
- return $op if grep $_->{$op}, @{$self}{qw(is_op expand_op render_op)};
+ return $op if grep $_->{$op}, @{$self}{qw(expand_op render_op)};
s/^-(?=.)//, s/\s+/_/g for $op;
$op;
}
my $op = $self->_normalize_op($k);
- { # Old SQLA compat
+ my $wsop = join(' ', split '_', $op);
- my $op = join(' ', split '_', $op);
+ my $is_special = List::Util::first { $wsop =~ $_->{regex} }
+ @{$self->{special_ops}};
+
+ { # Old SQLA compat
# the old special op system requires illegality for top-level use
if (
(our $Expand_Depth) == 1
and (
- List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}
+ $is_special
or (
$self->{disable_old_special_ops}
- and List::Util::first { $op =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
+ and List::Util::first { $wsop =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
)
)
) {
- puke "Illegal use of top-level '-$op'"
+ puke "Illegal use of top-level '-$wsop'"
}
}
- if (my $exp = $self->{expand}{$op}) {
+ if (my $exp = $self->{expand}{$op}||$self->{expand_op}{$op}) {
return $self->$exp($op, $v);
}
+ if ($self->{render}{$op}) {
+ return { "-${op}" => $v };
+ }
+
# Ops prefixed with -not_ get converted
if (my ($rest) = $op =~/^not_(.*)$/) {
}
}
- # 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 = $is_special || $self->{render_op}{$op} ? -op : -func;
- my $type = $self->{unknown_unop_always_func} ? -func : -op;
+ if ($self->{restore_old_unop_handling}) {
- { # Old SQLA compat
+ # Old SQLA compat
if (
ref($v) eq 'HASH'
and keys %$v == 1
and (keys %$v)[0] =~ /^-/
+ and not $self->{render_op}{$op}
+ and not $is_special
) {
- $type = (
- (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}})
- ? -op
- : -func
- )
+ $type = -func;
+ } else {
+ $type = -op;
}
}
- 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 {
}
sub _expand_ident {
- my ($self, undef, $body, $k) = @_;
- return $self->_expand_hashpair_cmp(
- $k, { -ident => $body }
- ) if defined($k);
+ my ($self, undef, $body) = @_;
unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) {
puke "-ident requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
}
}
sub _expand_value {
- return $_[0]->_expand_hashpair_cmp(
- $_[3], { -value => $_[2] },
- ) if defined($_[3]);
+{ -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 ] };
sub _expand_between {
my ($self, $op, $vv, $k) = @_;
- $k = shift @{$vv = [ @$vv ]} unless defined $k;
my @rhs = map $self->_expand_expr($_),
ref($vv) eq 'ARRAY' ? @$vv : $vv;
unless (
}
return +{ -op => [
$op,
- $self->expand_expr(ref($k) ? $k : { -ident => $k }),
- @rhs
+ $self->expand_expr($k),
+ map $self->expand_expr($_, -value), @rhs
] }
}
sub _expand_in {
my ($self, $op, $vv, $k) = @_;
- $k = shift @{$vv = [ @$vv ]} unless defined $k;
if (my $literal = is_literal_value($vv)) {
my ($sql, @bind) = @$literal;
my $opened_sql = $self->_open_outer_paren($sql);
return $self->_expand_expr($v);
}
-sub _expand_bind {
- my ($self, undef, $bind) = @_;
- return { -bind => $bind };
+sub _expand_values {
+ my ($self, undef, $values) = @_;
+ return { -values => [
+ map +(
+ ref($_) eq 'HASH'
+ ? $self->expand_expr($_)
+ : +{ -row => [ map $self->expand_expr($_), @$_ ] }
+ ), ref($values) eq 'ARRAY' ? @$values : $values
+ ] };
}
sub _recurse_where {
- my ($self, $where, $logic) = @_;
+ my ($self, $where) = @_;
# Special case: top level simple string treated as literal
my $where_exp = (ref($where)
- ? $self->_expand_expr($where, $logic)
+ ? $self->_expand_select_clause_where(undef, $where)
: { -literal => [ $where ] });
# dispatch expanded expression
sub _render_ident {
my ($self, undef, $ident) = @_;
- return [ $self->_convert($self->_quote($ident)) ];
+ return [ $self->_quote($ident) ];
}
sub _render_row {
sub _render_bind {
my ($self, undef, $bind) = @_;
- return [ $self->_convert('?'), $self->_bindtype(@$bind) ];
+ return [ '?', $self->_bindtype(@$bind) ];
}
sub _render_literal {
return $literal;
}
+sub _render_keyword {
+ my ($self, undef, $keyword) = @_;
+ return [ $self->_sqlcase(
+ ref($keyword) ? $$keyword : join ' ', split '_', $keyword
+ ) ];
+}
+
sub _render_op {
my ($self, undef, $v) = @_;
my ($op, @args) = @$v;
unless $low->{-literal};
$low;
} else {
- +($low, $self->format_keyword('and'), $high);
+ +($low, { -keyword => 'and' }, $high);
}
};
return $self->join_query_parts(' ',
- '(', $left, $self->format_keyword($op), @rh, ')',
+ '(', $left, { -keyword => $op }, @rh, ')',
);
}
return $self->join_query_parts(' ',
$lhs,
- $self->format_keyword($op),
+ { -keyword => $op },
$self->join_query_parts(' ',
'(',
$self->join_query_parts(', ', @rhs),
return $self->render_aqt($parts[0]) if @parts == 1;
my $join = ($op eq ','
? ', '
- : ' '.$self->format_keyword($op).' '
+ : { -keyword => " ${op} " }
);
return $self->join_query_parts($join, @parts);
}
+sub _render_values {
+ my ($self, undef, $values) = @_;
+ my $inner = $self->join_query_parts(' ',
+ { -keyword => 'values' },
+ $self->join_query_parts(', ',
+ ref($values) eq 'ARRAY' ? @$values : $values
+ ),
+ );
+ return $self->join_query_parts('',
+ (our $Render_Top_Level ? $inner : ('(', $inner, ')'))
+ );
+}
+
sub join_query_parts {
my ($self, $join, @parts) = @_;
+ if (ref($join) eq 'HASH') {
+ $join = $self->render_aqt($join)->[0];
+ }
my @final = map +(
ref($_) eq 'HASH'
? $self->render_aqt($_)
: ((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),
];
}
sub _render_unop_prefix {
my ($self, $op, $v) = @_;
+ my $op_sql = $self->{restore_old_unop_handling}
+ ? $self->_sqlcase($op)
+ : { -keyword => $op };
return $self->join_query_parts(' ',
- $self->_sqlcase($op), $v->[0]
+ ($self->{restore_old_unop_handling}
+ ? $self->_sqlcase($op)
+ : { -keyword => \$op }),
+ $v->[0]
);
}
sub _render_unop_postfix {
my ($self, $op, $v) = @_;
return $self->join_query_parts(' ',
- $v->[0], $self->format_keyword($op),
+ $v->[0], { -keyword => $op },
);
}
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),
#my ($self, $arg) = @_;
if (my $conv = $_[0]->{convert_where}) {
return @{ $_[0]->join_query_parts('',
- $_[0]->format_keyword($conv),
+ $_[0]->_sqlcase($conv),
'(' , $_[1] , ')'
) };
}
}
}
-sub _join_sql_clauses {
- my ($self, $logic, $clauses_aref, $bind_aref) = @_;
-
- if (@$clauses_aref > 1) {
- my $join = " " . $self->_sqlcase($logic) . " ";
- my $sql = '( ' . join($join, @$clauses_aref) . ' )';
- return ($sql, @$bind_aref);
- }
- elsif (@$clauses_aref) {
- return ($clauses_aref->[0], @$bind_aref); # no parentheses
- }
- else {
- return (); # if no SQL, ignore @$bind_aref
- }
-}
-
-
# Fix SQL case, if so requested
sub _sqlcase {
# LDNOTE: if $self->{case} is true, then it contains 'lower', so we
return $_[0]->{case} ? $_[1] : uc($_[1]);
}
-sub format_keyword { $_[0]->_sqlcase(join ' ', split '_', $_[1]) }
-
#======================================================================
# DISPATCHING FROM REFKIND
#======================================================================
On failure returns C<undef>, on success returns an B<array> reference
containing the unpacked version of the supplied literal SQL and bind values.
+=head2 is_undef_value
+
+Tests for undef, whether expanded or not.
+
=head1 WHERE CLAUSES
=head2 Introduction
-=head1 SPECIAL OPERATORS
+=head1 OLD EXTENSION SYSTEM
+
+=head2 SPECIAL OPERATORS
my $sqlmaker = SQL::Abstract->new(special_ops => [
{
]);
-=head1 UNARY OPERATORS
+=head2 UNARY OPERATORS
my $sqlmaker = SQL::Abstract->new(unary_ops => [
{
=back
+=head1 NEW METHODS
+
+See L<SQL::Abstract::Reference> for the C<expr> versus C<aqt> concept and
+an explanation of what the below extensions are extending.
+
+=head2 render_expr
+
+ my ($sql, @bind) = $sqla->render_expr($expr);
+
+=head2 render_statement
+
+Use this if you may be rendering a top level statement so e.g. a SELECT
+query doesn't get wrapped in parens
+
+ my ($sql, @bind) = $sqla->render_statement($expr);
+
+=head2 expand_expr
+
+Expression expansion with optional default for scalars.
+
+ my $aqt = $self->expand_expr($expr);
+ my $aqt = $self->expand_expr($expr, -ident);
+
+=head2 expand_maybe_list_expr
+
+expand_expr but with commas if there's more than one entry.
+
+ my $aqt = $self->expand_maybe_list_expr([ @exprs ], $default?);
+
+=head2 render_aqt
+
+Top level means avoid parens on statement AQT.
+
+ my $res = $self->render_aqt($aqt, $top_level);
+ my ($sql, @bind) = @$res;
+
+=head2 join_query_parts
+
+Similar to join() but will render hashrefs as nodes for both join and parts,
+and treats arrayref as a nested C<[ $join, @parts ]> structure.
+
+ my $part = $self->join_query_parts($join, @parts);
+
+=head1 NEW EXTENSION SYSTEM
+
+=head2 clone
+
+ my $sqla2 = $sqla->clone;
+
+Performs a semi-shallow copy such that extension methods won't leak state
+but excessive depth is avoided.
+
+=head2 expander
+
+=head2 expanders
+
+=head2 op_expander
+
+=head2 op_expanders
+
+=head2 clause_expander
+
+=head2 clause_expanders
+
+ $sqla->expander('name' => sub { ... });
+ $sqla->expanders('name1' => sub { ... }, 'name2' => sub { ... });
+
+=head2 expander_list
+
+=head2 op_expander_list
+
+=head2 clause_expander_list
+
+ my @names = $sqla->expander_list;
+
+=head2 wrap_expander
+
+=head2 wrap_expanders
+
+=head2 wrap_op_expander
+
+=head2 wrap_op_expanders
+
+=head2 wrap_clause_expander
+
+=head2 wrap_clause_expanders
+
+ $sqla->wrap_expander('name' => sub { my ($orig) = @_; sub { ... } });
+ $sqla->wrap_expanders(
+ 'name1' => sub { my ($orig1) = @_; sub { ... } },
+ 'name2' => sub { my ($orig2) = @_; sub { ... } },
+ );
+
+=head2 renderer
+
+=head2 renderers
+
+=head2 op_renderer
+
+=head2 op_renderers
+
+=head2 clause_renderer
+
+=head2 clause_renderers
+
+ $sqla->renderer('name' => sub { ... });
+ $sqla->renderers('name1' => sub { ... }, 'name2' => sub { ... });
+
+=head2 renderer_list
+
+=head2 op_renderer_list
+
+=head2 clause_renderer_list
+
+ my @names = $sqla->renderer_list;
+
+=head2 wrap_renderer
+
+=head2 wrap_renderers
+
+=head2 wrap_op_renderer
+
+=head2 wrap_op_renderers
+
+=head2 wrap_clause_renderer
+
+=head2 wrap_clause_renderers
+
+ $sqla->wrap_renderer('name' => sub { my ($orig) = @_; sub { ... } });
+ $sqla->wrap_renderers(
+ 'name1' => sub { my ($orig1) = @_; sub { ... } },
+ 'name2' => sub { my ($orig2) = @_; sub { ... } },
+ );
+
+=head2 clauses_of
+
+ my @clauses = $sqla->clauses_of('select');
+ $sqla->clauses_of(select => \@new_clauses);
+ $sqla->clauses_of(select => sub {
+ my (undef, @old_clauses) = @_;
+ ...
+ return @new_clauses;
+ });
+
+=head2 statement_list
+
+ my @list = $sqla->statement_list;
+
+=head2 make_unop_expander
+
+ my $exp = $sqla->make_unop_expander(sub { ... });
+
+If the op is found as a binop, assumes it wants a default comparison, so
+the inner expander sub can reliably operate as
+
+ sub { my ($self, $name, $body) = @_; ... }
+
+=head2 make_binop_expander
+
+ my $exp = $sqla->make_binop_expander(sub { ... });
+
+If the op is found as a unop, assumes the value will be an arrayref with the
+LHS as the first entry, and converts that to an ident node if it's a simple
+scalar. So the inner expander sub looks like
+
+ sub {
+ my ($self, $name, $body, $k) = @_;
+ { -blah => [ map $self->expand_expr($_), $k, $body ] }
+ }
+
+=head2 unop_expander
+
+=head2 unop_expanders
+
+=head2 binop_expander
+
+=head2 binop_expanders
+
+The above methods operate exactly like the op_ versions but wrap the coderef
+using the appropriate make_ method first.
=head1 PERFORMANCE