File Coverage

blib/lib/JsonSQL/Param/InsertValues.pm
Criterion Covered Total %
statement 48 50 96.0
branch 6 8 75.0
condition 3 5 60.0
subroutine 7 7 100.0
pod 2 2 100.0
total 66 72 91.6


line stmt bran cond sub pod time code
1             # ABSTRACT: JsonSQL::Param::InsertValues object. Stores a Perl representation of the VALUES parameter of an INSERT statement.
2              
3              
4              
5 1     1   6 use strict;
  1         1  
  1         23  
6 1     1   4 use warnings;
  1         2  
  1         19  
7 1     1   12 use 5.014;
  1         3  
8              
9             package JsonSQL::Param::InsertValues;
10              
11             our $VERSION = '0.41'; # VERSION
12              
13 1     1   4 use JsonSQL::Error;
  1         2  
  1         16  
14 1     1   4 use Data::Dumper;
  1         2  
  1         386  
15              
16              
17             sub new {
18 7     7 1 15 my ( $class, $insertvaluesarrayref, $queryObj, $insert_table_rules ) = @_;
19            
20 7         12 my $self = [];
21 7         12 my @insertvalue_errors;
22            
23             # Get the validator object from the $queryObj
24 7         10 my $validator = $queryObj->{_validator};
25            
26             # If no table rules are defined, use an empty list. This will effectively cause validation to fail.
27 7   100     15 my $table_rules = $insert_table_rules || [];
28            
29 7         9 for my $insertvalue ( @{ $insertvaluesarrayref } ) {
  7         13  
30 14         27 my $column = $insertvalue->{column};
31 14 50 33     65 if ( ( defined $column ) and ( $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) ) {
32 14         30 my $allowed_field = $validator->check_field_allowed($table_rules, $column);
33 14 100       19 if ( eval { $allowed_field->is_error } ) {
  14         112  
34 2         6 push(@insertvalue_errors, "Setting value of column $column not allowed by the table rule set.");
35             }
36            
37 14         242 my $value = $insertvalue->{value};
38 14 50       23 if ( defined $value ) {
39 14         15 push(@{ $self }, { column => $column, value => $value });
  14         45  
40             } else {
41 0         0 push(@insertvalue_errors, "Insert column $column specified without an insert value.");
42             }
43             } else {
44 0         0 push(@insertvalue_errors, "Invalid column name $column specified.");
45             }
46             }
47            
48 7 100       18 if ( @insertvalue_errors ) {
49 1         2 my $err = "Could not parse all value parameters for INSERT: \n\t";
50 1         3 $err .= join("\n\t", @insertvalue_errors);
51 1         3 return JsonSQL::Error->new("invalid_insertvalues", $err);
52             } else {
53 6         8 bless $self, $class;
54 6         15 return $self;
55             }
56             }
57              
58              
59             sub get_insert_param_strings {
60 4     4 1 8 my ( $self, $queryObj ) = @_;
61              
62             ## For this, we just iterate through the stored value hashes and parse them out into two arrays: columns and values.
63             ## A placeholder string of ? is constructed for parameterized value handling.
64 4         9 my @columns;
65             my @values;
66 4         7 for my $column ( @{ $self } ) {
  4         17  
67 8         19 push(@columns, $column->{column});
68 8         19 push(@values, $column->{value});
69             }
70              
71 4         13 my @placeholders = ("?") x scalar(@columns);
72              
73 4         12 my $columnString = join(",", map { $queryObj->quote_identifier($_) } @columns);
  8         23  
74              
75 4         14 my $placeholderString = join(",", @placeholders);
76              
77 4         21 return ($columnString, $placeholderString, \@values);
78             }
79              
80              
81             1;
82              
83             __END__
84              
85             =pod
86              
87             =encoding UTF-8
88              
89             =head1 NAME
90              
91             JsonSQL::Param::InsertValues - JsonSQL::Param::InsertValues object. Stores a Perl representation of the VALUES parameter of an INSERT statement.
92              
93             =head1 VERSION
94              
95             version 0.41
96              
97             =head1 SYNOPSIS
98              
99             This module constructs a Perl object representing the VALUES parameter of an SQL INSERT statement and has methods for
100             generating the appropriate SQL string and bind values for use with the L<DBI> module.
101              
102             =head1 DESCRIPTION
103              
104             =head3 Object properties:
105              
106             =over
107              
108             =item <column_name> => <column value>
109              
110             =back
111              
112             =head3 Generated parameters:
113              
114             =over
115              
116             =item $columns => "column1,column2,..."
117              
118             =item $placeholders => "?,?,..."
119              
120             (One ? for each column)
121              
122             =item $values => [value1,value2,...]
123              
124             =back
125              
126             =head1 METHODS
127              
128             =head2 Constructor new($insertvaluesarrayref, $queryObj, $insert_table_rules)
129              
130             Instantiates and returns a new JsonSQL::Param::InsertValues object.
131              
132             $insertvaluesarrayref => An arrayref of column/value hashes used to construct the object.
133             $queryObj => A reference to the JsonSQL::Query object that will own this object.
134             $insert_table_rules => The whitelist table rules used to validate access to the table columns for INSERT.
135              
136             Returns a JsonSQL::Error object on failure.
137              
138             =head2 ObjectMethod get_insert_param_strings -> ( $columns, $placeholders, $values )
139              
140             Generates parameters represented by the object for the SQL statement. Returns:
141              
142             $columns => A string of columns to use for the INSERT statement.
143             $placeholders => A placeholder string to use for parameterized VALUES.
144             $values => An arrayref of values to match the parameterized $columns and $placeholders strings.
145              
146             =head1 AUTHOR
147              
148             Chris Hoefler <bhoefler@draper.com>
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2017 by Chris Hoefler.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156              
157             =cut