convert delete to extended clauses system
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / Clauses.pm
CommitLineData
8f306bb1 1package SQL::Abstract::Clauses;
2
3use strict;
4use warnings;
5use mro 'c3';
6use base 'SQL::Abstract';
7
d5f4b9d3 8BEGIN { *puke = \&SQL::Abstract::puke }
9
8f306bb1 10sub new {
11 shift->next::method(@_)->register_defaults
12}
13
14sub register_defaults {
15 my ($self) = @_;
16 $self->{clauses_of}{select} = [ qw(select from where order_by) ];
17 $self->{expand}{select} = sub { shift->_expand_statement(@_) };
25b45941 18 $self->{render}{select} = sub { shift->_render_statement(select => @_) };
8cad5d13 19 $self->{expand_clause}{"select.$_"} = "_expand_select_clause_$_"
20 for @{$self->{clauses_of}{select}};
d5f4b9d3 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 => @_) };
4c0c4404 24 $self->{expand_clause}{"update.$_"} = "_expand_update_clause_$_"
25 for @{$self->{clauses_of}{update}};
be12624c 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 => @_) };
bc637c4f 29 $self->{expand_clause}{"delete.$_"} = "_expand_delete_clause_$_"
30 for @{$self->{clauses_of}{delete}};
6a47389f 31 $self->{clauses_of}{insert} = [
32 'insert_into', 'fields', 'values', 'returning'
33 ];
5ed4a77e 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.returning'} = sub {
40 shift->_expand_maybe_list_expr(@_, -ident);
41 };
6a47389f 42 $self->{render_clause}{'insert.fields'} = sub {
43 return $_[0]->render_aqt({ -row => [ $_[1] ] });
44 };
8f306bb1 45 return $self;
46}
47
8cad5d13 48sub _expand_select_clause_select {
49 my ($self, $select) = @_;
50 +(select => $self->_expand_maybe_list_expr($select, -ident));
51}
52
53sub _expand_select_clause_from {
54 my ($self, $from) = @_;
55 +(from => $self->_expand_maybe_list_expr($from, -ident));
56}
57
58sub _expand_select_clause_where {
59 my ($self, $where) = @_;
60 +(where => $self->expand_expr($where));
61}
62
63sub _expand_select_clause_order_by {
64 my ($self, $order_by) = @_;
65 +(order_by => $self->_expand_order_by($order_by));
66}
67
4c0c4404 68sub _expand_update_clause_update {
69 my ($self, $target) = @_;
70 +(update => $self->expand_expr($target, -ident));
71}
72
73sub _expand_update_clause_set {
74 +(set => shift->_expand_update_set_values(@_));
75}
76
77sub _expand_update_clause_where {
78 +(where => shift->expand_expr(@_));
79}
80
81sub _expand_update_clause_returning {
82 +(returning => shift->_expand_maybe_list_expr(@_, -ident));
83}
84
bc637c4f 85sub _expand_delete_clause_delete_from {
86 +(delete_from => shift->_expand_maybe_list_expr(@_, -ident));
87}
88
89sub _expand_delete_clause_where { +(where => shift->expand_expr(@_)); }
90
91sub _expand_delete_clause_returning {
92 +(returning => shift->_expand_maybe_list_expr(@_, -ident));
93}
94
8f306bb1 95sub _expand_statement {
96 my ($self, $type, $args) = @_;
97 my $ec = $self->{expand_clause};
25b45941 98 return +{ "-${type}" => +{
95894429 99 map {
8f306bb1 100 my $val = $args->{$_};
101 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
95894429 102 if ((my (@exp) = $self->$exp($val)) == 1) {
103 ($_ => $exp[0])
104 } else {
105 @exp
106 }
8f306bb1 107 } else {
95894429 108 ($_ => $val)
8f306bb1 109 }
95894429 110 } sort keys %$args
25b45941 111 } };
8f306bb1 112}
113
114sub _render_statement {
115 my ($self, $type, $args) = @_;
116 my @parts;
117 foreach my $clause (@{$self->{clauses_of}{$type}}) {
118 next unless my $clause_expr = $args->{$clause};
b96fbdc0 119 local $self->{convert_where} = $self->{convert} if $clause eq 'where';
6a47389f 120 my ($sql) = my @part = do {
121 if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
122 $self->$rdr($clause_expr);
123 } else {
124 my ($clause_sql, @bind) = $self->render_aqt($clause_expr);
125 my $sql = join ' ',
126 $self->_sqlcase(join ' ', split '_', $clause),
127 $clause_sql;
128 ($sql, @bind);
129 }
130 };
8f306bb1 131 next unless defined($sql) and length($sql);
6a47389f 132 push @parts, \@part;
8f306bb1 133 }
134 return $self->_join_parts(' ', @parts);
135}
136
b96fbdc0 137sub select {
138 my ($self, @args) = @_;
139 my %clauses;
140 @clauses{qw(from select where order_by)} = @args;
0c3c4c23 141
b96fbdc0 142 # This oddity is to literalify since historically SQLA doesn't quote
0c3c4c23 143 # a single identifier argument, so we convert it into a literal
144
91d37de5 145 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
146 unless ref($clauses{select});
147
b96fbdc0 148 my ($sql, @bind) = $self->render_expr({ -select => \%clauses });
149 return wantarray ? ($sql, @bind) : $sql;
150}
151
d5f4b9d3 152sub update {
153 my ($self, $table, $set, $where, $options) = @_;
154 my %clauses;
155 @clauses{qw(update set where)} = ($table, $set, $where);
156 puke "Unsupported data type specified to \$sql->update"
157 unless ref($clauses{set}) eq 'HASH';
158 @clauses{keys %$options} = values %$options;
159 my ($sql, @bind) = $self->render_expr({ -update => \%clauses });
160 return wantarray ? ($sql, @bind) : $sql;
161}
162
be12624c 163sub delete {
164 my ($self, $table, $where, $options) = @_;
165 my %clauses = (delete_from => $table, where => $where, %{$options||{}});
166 my ($sql, @bind) = $self->render_expr({ -delete => \%clauses });
167 return wantarray ? ($sql, @bind) : $sql;
168}
169
5ed4a77e 170sub insert {
171 my ($self, $table, $data, $options) = @_;
172 my %clauses = (insert_into => $table, %{$options||{}});
173 my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
6a47389f 174 $clauses{fields} = $f_aqt if $f_aqt;
5ed4a77e 175 $clauses{values} = $v_aqt;
176 my ($sql, @bind) = $self->render_expr({ -insert => \%clauses });
177 return wantarray ? ($sql, @bind) : $sql;
178}
179
8f306bb1 1801;