1 package SQL::Abstract::Clauses;
6 use base 'SQL::Abstract';
8 BEGIN { *puke = \&SQL::Abstract::puke }
11 shift->next::method(@_)->register_defaults
14 sub register_defaults {
16 $self->{clauses_of}{select} = [ qw(select from where order_by) ];
17 $self->{expand}{select} = sub { shift->_expand_statement(@_) };
18 $self->{render}{select} = sub { shift->_render_statement(select => @_) };
19 $self->{expand_clause}{'select.select'} = sub {
20 $_[0]->_expand_maybe_list_expr($_[1], -ident)
22 $self->{expand_clause}{'select.from'} = sub {
23 $_[0]->_expand_maybe_list_expr($_[1], -ident)
25 $self->{expand_clause}{'select.where'} = 'expand_expr';
26 $self->{expand_clause}{'select.order_by'} = '_expand_order_by';
27 $self->{clauses_of}{update} = [ qw(update set where returning) ];
28 $self->{expand}{update} = sub { shift->_expand_statement(@_) };
29 $self->{render}{update} = sub { shift->_render_statement(update => @_) };
30 $self->{expand_clause}{'update.update'} = sub {
31 $_[0]->expand_expr($_[1], -ident)
33 $self->{expand_clause}{'update.set'} = '_expand_update_set_values';
34 $self->{expand_clause}{'update.where'} = 'expand_expr';
35 $self->{expand_clause}{'update.returning'} = sub {
36 shift->_expand_maybe_list_expr(@_, -ident);
41 sub _expand_statement {
42 my ($self, $type, $args) = @_;
43 my $ec = $self->{expand_clause};
44 return +{ "-${type}" => +{
46 my $val = $args->{$_};
47 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
56 sub _render_statement {
57 my ($self, $type, $args) = @_;
59 foreach my $clause (@{$self->{clauses_of}{$type}}) {
60 next unless my $clause_expr = $args->{$clause};
61 local $self->{convert_where} = $self->{convert} if $clause eq 'where';
62 my ($sql, @bind) = $self->render_aqt($clause_expr);
63 next unless defined($sql) and length($sql);
65 $self->_sqlcase(join ' ', split '_', $clause).' '.$sql,
69 return $self->_join_parts(' ', @parts);
73 my ($self, @args) = @_;
75 @clauses{qw(from select where order_by)} = @args;
77 # This oddity is to literalify since historically SQLA doesn't quote
78 # a single identifier argument, so we convert it into a literal
80 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
81 unless ref($clauses{select});
83 my ($sql, @bind) = $self->render_expr({ -select => \%clauses });
84 return wantarray ? ($sql, @bind) : $sql;
88 my ($self, $table, $set, $where, $options) = @_;
90 @clauses{qw(update set where)} = ($table, $set, $where);
91 puke "Unsupported data type specified to \$sql->update"
92 unless ref($clauses{set}) eq 'HASH';
93 @clauses{keys %$options} = values %$options;
94 my ($sql, @bind) = $self->render_expr({ -update => \%clauses });
95 return wantarray ? ($sql, @bind) : $sql;