From: Matt S Trout Date: Thu, 4 Apr 2019 23:45:08 +0000 (+0000) Subject: initial sketch of Clauses.pm X-Git-Tag: v1.90_01~253 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bbe638afe9c153af5e8da3a47d9ef165e76c66d1;p=dbsrgits%2FSQL-Abstract.git initial sketch of Clauses.pm --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm new file mode 100644 index 0000000..be8bfa8 --- /dev/null +++ b/lib/SQL/Abstract/Clauses.pm @@ -0,0 +1,56 @@ +package SQL::Abstract::Clauses; + +use strict; +use warnings; +use mro 'c3'; +use base 'SQL::Abstract'; + +sub new { + shift->next::method(@_)->register_defaults +} + +sub register_defaults { + my ($self) = @_; + $self->{clauses_of}{select} = [ qw(select from where order_by) ]; + $self->{expand}{select} = sub { shift->_expand_statement(@_) }; + $self->{render}{select} = sub { shift->_render_statement(@_) }; + $self->{expand_clause}{'select.select'} = '_expand_maybe_list_expr'; + $self->{expand_clause}{'select.from'} = sub { + $_[0]->_expand_maybe_list_expr($_[1], -ident) + }; + $self->{expand_clause}{'select.where'} = 'expand_expr'; + $self->{expand_clause}{'select.order_by'} = '_expand_order_by'; + return $self; +} + +sub _expand_statement { + my ($self, $type, $args) = @_; + my $ec = $self->{expand_clause}; + return +{ + map +($_ => (do { + my $val = $args->{$_}; + if (defined($val) and my $exp = $ec->{"${type}.$_"}) { + $self->$exp($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 ($sql, @bind) = $self->render_expr($clause_expr); + next unless defined($sql) and length($sql); + push @parts, [ + $self->_sqlcase(join ' ', split '_', $clause).' '.$sql, + @bind + ]; + } + return $self->_join_parts(' ', @parts); +} + +1;