1 package SQL::Abstract::Clauses;
6 use base 'SQL::Abstract';
9 shift->next::method(@_)->register_defaults
12 sub register_defaults {
14 $self->{clauses_of}{select} = [ qw(select from where order_by) ];
15 $self->{expand}{select} = sub { shift->_expand_statement(@_) };
16 $self->{render}{select} = sub { shift->_render_statement(select => @_) };
17 $self->{expand_clause}{'select.select'} = sub {
18 $_[0]->_expand_maybe_list_expr($_[1], -ident)
20 $self->{expand_clause}{'select.from'} = sub {
21 $_[0]->_expand_maybe_list_expr($_[1], -ident)
23 $self->{expand_clause}{'select.where'} = 'expand_expr';
24 $self->{expand_clause}{'select.order_by'} = '_expand_order_by';
28 sub _expand_statement {
29 my ($self, $type, $args) = @_;
30 my $ec = $self->{expand_clause};
31 return +{ "-${type}" => +{
33 my $val = $args->{$_};
34 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
43 sub _render_statement {
44 my ($self, $type, $args) = @_;
46 foreach my $clause (@{$self->{clauses_of}{$type}}) {
47 next unless my $clause_expr = $args->{$clause};
48 local $self->{convert_where} = $self->{convert} if $clause eq 'where';
49 my ($sql, @bind) = $self->render_aqt($clause_expr);
50 next unless defined($sql) and length($sql);
52 $self->_sqlcase(join ' ', split '_', $clause).' '.$sql,
56 return $self->_join_parts(' ', @parts);
60 my ($self, @args) = @_;
62 @clauses{qw(from select where order_by)} = @args;
63 # This oddity is to literalify since historically SQLA doesn't quote
64 # a single identifier argument, and the .'' is to copy $clauses{select}
65 # before taking a reference to it to avoid making a reference loop
66 $clauses{select}= \(($clauses{select}||'*').'')
67 unless ref($clauses{select});
68 my ($sql, @bind) = $self->render_expr({ -select => \%clauses });
69 return wantarray ? ($sql, @bind) : $sql;