| 1 | # ABSTRACT: JsonSQL::Param::Insert object. Stores a Perl representation of an INSERT statement used by the JsonSQL Insert query object. | ||||||||||
| 2 | |||||||||||
| 3 | |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | package JsonSQL::Param::Insert; | ||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = '0.4'; # VERSION | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use JsonSQL::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use JsonSQL::Param::Table; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use JsonSQL::Param::InsertValues; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | sub new { | ||||||||||
| 20 | 7 | ▶ | my ( $class, $inserthashref, $queryObj ) = @_; | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 21 | |||||||||||
| 22 | 7 | my $self = {}; | |||||||||
| 23 | 7 | my @insert_errors; | |||||||||
| 24 | |||||||||||
| 25 | # Construct _insertTable property. | ||||||||||
| 26 | 7 | my $inserttable = JsonSQL::Param::Table->new($inserthashref->{table}, $queryObj); | |||||||||
| 27 | 7 | ▶ | if ( eval { $inserttable->is_error } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 2 | push(@insert_errors, "Error creating table $inserthashref->{table}->{table} for INSERT: $inserttable->{message}"); | |||||||||
| 29 | } else { | ||||||||||
| 30 | 5 | $self->{_insertTable} = $inserttable; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # Construct _insertValues property. | ||||||||||
| 34 | 7 | my $insertvalues = JsonSQL::Param::InsertValues->new($inserthashref->{values}, $queryObj, $self->{_insertTable}->{_tableRules}); | |||||||||
| 35 | 7 | ▶ | if ( eval { $insertvalues->is_error } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | push(@insert_errors, "Error parsing VALUES parameters for INSERT: $insertvalues->{message}"); | |||||||||
| 37 | } else { | ||||||||||
| 38 | 6 | $self->{_insertValues} = $insertvalues; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | ## SMELL: this is a little bit of a hack and should be redone properly at some point. | ||||||||||
| 42 | ## Requires support for CTEs. | ||||||||||
| 43 | 7 | ▶ | if ( defined $inserthashref->{returning} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 1 | $self->{_insertReturning} = $inserthashref->{returning}; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 7 | ▶ | if ( @insert_errors ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 3 | my $err = "Could not construct INSERT statement: \n\t"; | |||||||||
| 49 | 3 | $err .= join("\n\t", @insert_errors); | |||||||||
| 50 | 3 | return JsonSQL::Error->new("invalid_inserts", $err); | |||||||||
| 51 | } else { | ||||||||||
| 52 | 4 | bless $self, $class; | |||||||||
| 53 | 4 | return $self; | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | sub get_returning_param_string { | ||||||||||
| 59 | 4 | ▶ | my ( $self, $queryObj ) = @_; | ||||||||
|
Subroutine
get_returning_param_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | if (defined $self->{_insertReturning}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 1 | my @returningParam; | |||||||||
| 63 | |||||||||||
| 64 | 1 | for my $returnvalue (@{ $self->{_insertReturning} }) { | |||||||||
| 65 | 2 | my $column = $queryObj->quote_identifier($returnvalue->{column}); | |||||||||
| 66 | 2 | my $alias = $queryObj->quote_identifier($returnvalue->{as}); | |||||||||
| 67 | |||||||||||
| 68 | 2 | ▶ | if ( defined $alias ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | push(@returningParam, "$column AS $alias"); | |||||||||
| 70 | } else { | ||||||||||
| 71 | 1 | push(@returningParam, "$column"); | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | return join(",", @returningParam); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | sub get_insert_stmt { | ||||||||||
| 81 | 4 | ▶ | my ( $self, $queryObj ) = @_; | ||||||||
|
Subroutine
get_insert_stmt: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | |||||||||||
| 83 | 4 | my $table = $self->{_insertTable}->get_table_param($queryObj); | |||||||||
| 84 | 4 | my ($columns, $placeholders, $values) = $self->{_insertValues}->get_insert_param_strings($queryObj); | |||||||||
| 85 | 4 | my $returning = $self->get_returning_param_string($queryObj); | |||||||||
| 86 | |||||||||||
| 87 | 4 | my $insertSql = "INSERT INTO $table ($columns) VALUES ($placeholders)"; | |||||||||
| 88 | |||||||||||
| 89 | 4 | ▶ | if ( $returning ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | my $insertWrapper = "WITH insert_q AS (\n"; | |||||||||
| 91 | 1 | $insertWrapper .= $insertSql . "\n"; | |||||||||
| 92 | 1 | $insertWrapper .= "RETURNING " . $returning . "\n)\n"; | |||||||||
| 93 | 1 | $insertWrapper .= "SELECT * FROM insert_q"; | |||||||||
| 94 | |||||||||||
| 95 | 1 | return ($insertWrapper, $values); | |||||||||
| 96 | } else { | ||||||||||
| 97 | 3 | return ($insertSql, $values); | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | 1 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||
| 105 | |||||||||||
| 106 | =pod | ||||||||||
| 107 | |||||||||||
| 108 | =encoding UTF-8 | ||||||||||
| 109 | |||||||||||
| 110 | =head1 NAME | ||||||||||
| 111 | |||||||||||
| 112 | JsonSQL::Param::Insert - JsonSQL::Param::Insert object. Stores a Perl representation of an INSERT statement used by the JsonSQL Insert query object. | ||||||||||
| 113 | |||||||||||
| 114 | =head1 VERSION | ||||||||||
| 115 | |||||||||||
| 116 | version 0.4 | ||||||||||
| 117 | |||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||
| 119 | |||||||||||
| 120 | This module constructs a Perl object representing an SQL INSERT statement and has methods for generating the appropriate SQL statement | ||||||||||
| 121 | and bind values for use with the L<DBI> module. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 DESCRIPTION | ||||||||||
| 124 | |||||||||||
| 125 | =head3 Object properties: | ||||||||||
| 126 | |||||||||||
| 127 | =over | ||||||||||
| 128 | |||||||||||
| 129 | =item _insertTable => L<JsonSQL::Param::Table> | ||||||||||
| 130 | |||||||||||
| 131 | =item _insertValues => L<JsonSQL::Param::InsertValues> | ||||||||||
| 132 | |||||||||||
| 133 | =item _insertReturning => <string> | ||||||||||
| 134 | |||||||||||
| 135 | ( Note: not currently whitelist validated due to the way this is implemented. Will change in future. ) | ||||||||||
| 136 | |||||||||||
| 137 | =back | ||||||||||
| 138 | |||||||||||
| 139 | =head3 | ||||||||||
| 140 | |||||||||||
| 141 | Structure of INSERT statement: | ||||||||||
| 142 | |||||||||||
| 143 | INSERT INTO <table> ( <columns> ) VALUES ( <parameterized values> ) | ||||||||||
| 144 | |||||||||||
| 145 | =head3 RETURNING clause | ||||||||||
| 146 | |||||||||||
| 147 | When using the RETURNING clause, the INSERT statement is wrapped in a WITH CTE, so your database has to support this. | ||||||||||
| 148 | |||||||||||
| 149 | WITH insert_q AS ( | ||||||||||
| 150 | INSERT INTO <table> ( <columns> ) VALUES ( <parameterized values> ) | ||||||||||
| 151 | RETURNING <return columns> | ||||||||||
| 152 | ) | ||||||||||
| 153 | SELECT * FROM insert_q | ||||||||||
| 154 | |||||||||||
| 155 | =head1 METHODS | ||||||||||
| 156 | |||||||||||
| 157 | =head2 Constructor new($inserthashref, $queryObj) | ||||||||||
| 158 | |||||||||||
| 159 | Instantiates and returns a new JsonSQL::Param::Insert object. | ||||||||||
| 160 | |||||||||||
| 161 | $inserthashref => A hashref with the properties needed to construct the object. | ||||||||||
| 162 | $queryObj => A reference to the JsonSQL::Query object that will own this object. | ||||||||||
| 163 | |||||||||||
| 164 | Returns a JsonSQL::Error object on failure. | ||||||||||
| 165 | |||||||||||
| 166 | =head2 ObjectMethod get_returning_param_string -> $returningParam | ||||||||||
| 167 | |||||||||||
| 168 | Generates the RETURNING clause from the _insertReturning property. | ||||||||||
| 169 | |||||||||||
| 170 | =head2 ObjectMethod get_insert_stmt -> ( $sql, $binds ) | ||||||||||
| 171 | |||||||||||
| 172 | Generates the SQL statement represented by the object. Returns: | ||||||||||
| 173 | |||||||||||
| 174 | $sql => An SQL INSERT string. | ||||||||||
| 175 | $binds => An arrayref of parameterized values to pass with the query. | ||||||||||
| 176 | |||||||||||
| 177 | =head1 AUTHOR | ||||||||||
| 178 | |||||||||||
| 179 | Chris Hoefler <bhoefler@draper.com> | ||||||||||
| 180 | |||||||||||
| 181 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 182 | |||||||||||
| 183 | This software is copyright (c) 2017 by Chris Hoefler. | ||||||||||
| 184 | |||||||||||
| 185 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 186 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 187 | |||||||||||
| 188 | =cut | ||||||||||