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