clean up fields handling
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / Clauses.pm
1 package SQL::Abstract::Clauses;
2
3 use strict;
4 use warnings;
5 use mro 'c3';
6 use base 'SQL::Abstract';
7
8 BEGIN { *puke = \&SQL::Abstract::puke }
9
10 sub new {
11   shift->next::method(@_)->register_defaults
12 }
13
14 sub register_defaults {
15   my ($self) = @_;
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'
33   ];
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);
38   };
39   $self->{expand_clause}{'insert.values'} = '_expand_insert_clause_values';
40   $self->{expand_clause}{'insert.returning'} = sub {
41     shift->_expand_maybe_list_expr(@_, -ident);
42   };
43   $self->{render_clause}{'insert.fields'} = sub {
44     return $_[0]->render_aqt($_[1]);
45   };
46   return $self;
47 }
48
49 sub _expand_select_clause_select {
50   my ($self, $select) = @_;
51   +(select => $self->_expand_maybe_list_expr($select, -ident));
52 }
53
54 sub _expand_select_clause_from {
55   my ($self, $from) = @_;
56   +(from => $self->_expand_maybe_list_expr($from, -ident));
57 }
58
59 sub _expand_select_clause_where {
60   my ($self, $where) = @_;
61   +(where => $self->expand_expr($where));
62 }
63
64 sub _expand_select_clause_order_by {
65   my ($self, $order_by) = @_;
66   +(order_by => $self->_expand_order_by($order_by));
67 }
68
69 sub _expand_update_clause_update {
70   my ($self, $target) = @_;
71   +(update => $self->expand_expr($target, -ident));
72 }
73
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(@_));
77 }
78
79 sub _expand_update_clause_where {
80   +(where => shift->expand_expr(@_));
81 }
82
83 sub _expand_update_clause_returning {
84   +(returning => shift->_expand_maybe_list_expr(@_, -ident));
85 }
86
87 sub _expand_delete_clause_delete_from {
88   +(delete_from => shift->_expand_maybe_list_expr(@_, -ident));
89 }
90
91 sub _expand_delete_clause_where { +(where => shift->expand_expr(@_)); }
92
93 sub _expand_delete_clause_returning {
94   +(returning => shift->_expand_maybe_list_expr(@_, -ident));
95 }
96
97 sub _expand_statement {
98   my ($self, $type, $args) = @_;
99   my $ec = $self->{expand_clause};
100   return +{ "-${type}" => +{
101     map {
102       my $val = $args->{$_};
103       if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
104         if ((my (@exp) = $self->$exp($val)) == 1) {
105           ($_ => $exp[0])
106         } else {
107           @exp
108         }
109       } else {
110         ($_ => $val)
111       }
112     } sort keys %$args
113   } };
114 }
115
116 sub _render_statement {
117   my ($self, $type, $args) = @_;
118   my @parts;
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);
125       } else {
126         my ($clause_sql, @bind) = $self->render_aqt($clause_expr);
127         my $sql = join ' ',
128           $self->_sqlcase(join ' ', split '_', $clause),
129           $clause_sql;
130         ($sql, @bind);
131       }
132     };
133     next unless defined($sql) and length($sql);
134     push @parts, \@part;
135   }
136   return $self->_join_parts(' ', @parts);
137 }
138
139 sub select {
140   my ($self, @args) = @_;
141   my %clauses;
142   @clauses{qw(from select where order_by)} = @args;
143
144   # This oddity is to literalify since historically SQLA doesn't quote
145   # a single identifier argument, so we convert it into a literal
146
147   $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
148     unless ref($clauses{select});
149
150   my ($sql, @bind) = $self->render_expr({ -select => \%clauses });
151   return wantarray ? ($sql, @bind) : $sql;
152 }
153
154 sub update {
155   my ($self, $table, $set, $where, $options) = @_;
156   my %clauses;
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;
163 }
164
165 sub delete {
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;
170 }
171
172 sub insert {
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;
177 }
178
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) : ()));
184 }
185
186 1;