« Crop/Object/Warehouse/Lang/SQL/Query/Select/Node.pm
↑ Summary
Crop/Object/Warehouse/Lang/SQL/Query/Delete.pm »
| 1 | package Crop::Object::Warehouse::Lang::SQL::Query::Insert; | ||
| 2 | use base qw/ Crop::Object::Warehouse::Lang::SQL::Query /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Lang::SQL::Query::Insert | ||
| 6 | The Insert query. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Crop::Error; | ||
| 13 | |||
| 14 | use Crop::Debug; | ||
| 15 | |||
| 16 | =begin nd | ||
| 17 | Variable: our %Attributes | ||
| 18 | Class members: | ||
| 19 | |||
| 20 | attr - hash of attr=>values; or undef | ||
| 21 | returning - resulting attr to return | ||
| 22 | table - table | ||
| 23 | =cut | ||
| 24 | our %Attributes = ( | ||
| 25 | attr => {default => {}}, | ||
| 26 | returning => undef, | ||
| 27 | table => undef, | ||
| 28 | ); | ||
| 29 | |||
| 30 | =begin nd | ||
| 31 | Method: print_sql ( ) | ||
| 32 | Compose query prepared string based on inner presentation. | ||
| 33 | |||
| 34 | Returns: | ||
| 35 | the array of two element, SQL string, and arrayref of values for placeholders | ||
| 36 | =cut | ||
| 37 | sub print_sql { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | # debug 'INSERT_PRINTSQL_SELF=', $self; | ||
| 41 | |||
| 42 | my $sql = "INSERT INTO $self->{table} "; | ||
| 43 | my @val; | ||
| 44 | |||
| 45 | if (keys %{$self->{attr}}) { | ||
| 46 | my @attr; | ||
| 47 | while (my ($attr, $val) = each %{$self->{attr}}) { | ||
| 48 | push @attr, $attr; | ||
| 49 | push @val, $attr, $val; | ||
| 50 | } | ||
| 51 | |||
| 52 | my @ph = split '', '?' x @attr; | ||
| 53 | { | ||
| 54 | local $" = ', '; | ||
| 55 | $sql .= "(@attr) VALUES (@ph)"; | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | $sql .= "(id) VALUES (default)"; | ||
| 59 | } | ||
| 60 | |||
| 61 | $sql .= " RETURNING $self->{returning}" if defined $self->{returning}; | ||
| 62 | |||
| 63 | ($sql, \@val); | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; |