JsonSQL/Param/InsertValues.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 75.0% 6 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 89.6% 69 / 77 CC 9 SCAR 16.1
CC
9
Cov
91%
new
8.3
get_insert_param_strings
2.0
BEGIN
1.0
CRAP 5.0
« JsonSQL/Param/Join.pm ↑ Summary JsonSQL/Param/Table.pm »
1 # ABSTRACT: JsonSQL::Param::InsertValues object. Stores a Perl representation of the VALUES parameter of an INSERT statement.
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::InsertValues;
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 sub new {
18 7 ▶ my ( $class, $insertvaluesarrayref, $queryObj, $insert_table_rules ) = @_;
Subroutine new: called CC=8 SCAR=21.1
Pod documented
19
20 7 my $self = [];
21 7 my @insertvalue_errors;
22
23 # Get the validator object from the $queryObj
24 7 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 ▶ my $table_rules = $insert_table_rules || [];
Condition100%2/2
$insert_table_rules || []
l!l
52
MC/DC100%1/1
$insert_table_rules || []
$insert_table_rules
Truth table100%2/2
A
A= $insert_table_rules
Aresult
11
00
28
29 7 for my $insertvalue ( @{ $insertvaluesarrayref } ) {
30 14 my $column = $insertvalue->{column};
31 14 ▶ if ( ( defined $column ) and ( $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $column and $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u) { } 14 0 14
Condition33%1/3
defined $column and $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $column and $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
defined $column $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
Truth table33%1/3
A and B
A= defined $columnB= $column =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u
ABresult
0X0
100
111
32 14 my $allowed_field = $validator->check_field_allowed($table_rules, $column);
33 14 ▶ if ( eval { $allowed_field->is_error } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $allowed_field->is_error } }) 2 12 14
34 2 push(@insertvalue_errors, "Setting value of column $column not allowed by the table rule set.");
35 }
36
37 14 my $value = $insertvalue->{value};
38 14 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 14 0 14
39 14 push(@{ $self }, { column => $column, value => $value });
40 } else {
41 0 push(@insertvalue_errors, "Insert column $column specified without an insert value.");
42 }
43 } else {
44 0 push(@insertvalue_errors, "Invalid column name $column specified.");
45 }
46 }
47
48 7 ▶ if ( @insertvalue_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@insertvalue_errors) { } 1 6 7
49 1 my $err = "Could not parse all value parameters for INSERT: \n\t";
50 1 $err .= join("\n\t", @insertvalue_errors);
51 1 return JsonSQL::Error->new("invalid_insertvalues", $err);
52 } else {
53 6 bless $self, $class;
54 6 return $self;
55 }
56 }
57  
58  
59 sub get_insert_param_strings {
60 4 ▶ my ( $self, $queryObj ) = @_;
Subroutine get_insert_param_strings: called CC=2 SCAR=6.9
Pod documented
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 my @columns;
65 my @values;
66 4 for my $column ( @{ $self } ) {
67 8 push(@columns, $column->{column});
68 8 push(@values, $column->{value});
69 }
70  
71 4 my @placeholders = ("?") x scalar(@columns);
72  
73 4 my $columnString = join(",", map { $queryObj->quote_identifier($_) } @columns);
74  
75 4 my $placeholderString = join(",", @placeholders);
76  
77 4 return ($columnString, $placeholderString, \@values);
78 }
79  
80  
81 1 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.4
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
« JsonSQL/Param/Join.pm ↑ Summary JsonSQL/Param/Table.pm »