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.$_"} = "_expand_select_clause_$_"
20 for @{$self->{clauses_of}{select}};
21 $self->{clauses_of}{update} = [ qw(update set where returning) ];
22 $self->{expand}{update} = sub { shift->_expand_statement(@_) };
23 $self->{render}{update} = sub { shift->_render_statement(update => @_) };
24 $self->{expand_clause}{"update.$_"} = "_expand_update_clause_$_"
25 for @{$self->{clauses_of}{update}};
26 $self->{clauses_of}{delete} = [ qw(delete_from where returning) ];
27 $self->{expand}{delete} = sub { shift->_expand_statement(@_) };
28 $self->{render}{delete} = sub { shift->_render_statement(delete => @_) };
29 $self->{expand_clause}{"delete.$_"} = "_expand_delete_clause_$_"
30 for @{$self->{clauses_of}{delete}};
31 $self->{clauses_of}{insert} = [
32 'insert_into', 'fields', 'values', 'returning'
34 $self->{expand}{insert} = sub { shift->_expand_statement(@_) };
35 $self->{render}{insert} = sub { shift->_render_statement(insert => @_) };
36 $self->{expand_clause}{'insert.insert_into'} = sub {
37 shift->expand_expr(@_, -ident);
39 $self->{expand_clause}{'insert.values'} = '_expand_insert_clause_values';
40 $self->{expand_clause}{'insert.returning'} = sub {
41 shift->_expand_maybe_list_expr(@_, -ident);
43 $self->{render_clause}{'insert.fields'} = sub {
44 return $_[0]->render_aqt({ -row => [ $_[1] ] });
49 sub _expand_select_clause_select {
50 my ($self, $select) = @_;
51 +(select => $self->_expand_maybe_list_expr($select, -ident));
54 sub _expand_select_clause_from {
55 my ($self, $from) = @_;
56 +(from => $self->_expand_maybe_list_expr($from, -ident));
59 sub _expand_select_clause_where {
60 my ($self, $where) = @_;
61 +(where => $self->expand_expr($where));
64 sub _expand_select_clause_order_by {
65 my ($self, $order_by) = @_;
66 +(order_by => $self->_expand_order_by($order_by));
69 sub _expand_update_clause_update {
70 my ($self, $target) = @_;
71 +(update => $self->expand_expr($target, -ident));
74 sub _expand_update_clause_set {
75 return $_[1] if ref($_[1]) eq 'HASH' and ($_[1]->{-op}||[''])->[0] eq ',';
76 +(set => shift->_expand_update_set_values(@_));
79 sub _expand_update_clause_where {
80 +(where => shift->expand_expr(@_));
83 sub _expand_update_clause_returning {
84 +(returning => shift->_expand_maybe_list_expr(@_, -ident));
87 sub _expand_delete_clause_delete_from {
88 +(delete_from => shift->_expand_maybe_list_expr(@_, -ident));
91 sub _expand_delete_clause_where { +(where => shift->expand_expr(@_)); }
93 sub _expand_delete_clause_returning {
94 +(returning => shift->_expand_maybe_list_expr(@_, -ident));
97 sub _expand_statement {
98 my ($self, $type, $args) = @_;
99 my $ec = $self->{expand_clause};
100 return +{ "-${type}" => +{
102 my $val = $args->{$_};
103 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
104 if ((my (@exp) = $self->$exp($val)) == 1) {
116 sub _render_statement {
117 my ($self, $type, $args) = @_;
119 foreach my $clause (@{$self->{clauses_of}{$type}}) {
120 next unless my $clause_expr = $args->{$clause};
121 local $self->{convert_where} = $self->{convert} if $clause eq 'where';
122 my ($sql) = my @part = do {
123 if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
124 $self->$rdr($clause_expr);
126 my ($clause_sql, @bind) = $self->render_aqt($clause_expr);
128 $self->_sqlcase(join ' ', split '_', $clause),
133 next unless defined($sql) and length($sql);
136 return $self->_join_parts(' ', @parts);
140 my ($self, @args) = @_;
142 @clauses{qw(from select where order_by)} = @args;
144 # This oddity is to literalify since historically SQLA doesn't quote
145 # a single identifier argument, so we convert it into a literal
147 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
148 unless ref($clauses{select});
150 my ($sql, @bind) = $self->render_expr({ -select => \%clauses });
151 return wantarray ? ($sql, @bind) : $sql;
155 my ($self, $table, $set, $where, $options) = @_;
157 @clauses{qw(update set where)} = ($table, $set, $where);
158 puke "Unsupported data type specified to \$sql->update"
159 unless ref($clauses{set}) eq 'HASH';
160 @clauses{keys %$options} = values %$options;
161 my ($sql, @bind) = $self->render_expr({ -update => \%clauses });
162 return wantarray ? ($sql, @bind) : $sql;
166 my ($self, $table, $where, $options) = @_;
167 my %clauses = (delete_from => $table, where => $where, %{$options||{}});
168 my ($sql, @bind) = $self->render_expr({ -delete => \%clauses });
169 return wantarray ? ($sql, @bind) : $sql;
173 my ($self, $table, $data, $options) = @_;
174 my %clauses = (insert_into => $table, values => $data, %{$options||{}});
175 my ($sql, @bind) = $self->render_expr({ -insert => \%clauses });
176 return wantarray ? ($sql, @bind) : $sql;
179 sub _expand_insert_clause_values {
180 my ($self, $data) = @_;
181 return $data if ref($data) eq 'HASH' and $data->{-row};
182 my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
183 return (values => $v_aqt, ($f_aqt ? (fields => $f_aqt) : ()));