JsonSQL/Param/Conditions/TestCondition.pm

filter: Statementstmt 90.5% 48 / 53 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 88.1% 67 / 76 CC 7 SCAR 10.6
CC
7
Cov
86%
new
5.5
get_sql_obj
3.0
BEGIN
1.0
CRAP 2.9
« JsonSQL/Param/Insert.pm ↑ Summary JsonSQL/Param/Joins.pm »
1 # ABSTRACT: JsonSQL::Param::Conditions::TestCondition object. Subclass for parsing test conditions (ex: 'eq', 'gt', etc).
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::Conditions::TestCondition;
10  
11 1 our $VERSION = '0.4'; # VERSION
12  
13 1 ▶use base qw( JsonSQL::Param::Condition );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use SQL::QueryMaker qw( sql_eq sql_ne sql_gt sql_ge sql_lt sql_le );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Data::Dumper;
17  
18 1 ▶use JsonSQL::Param::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use JsonSQL::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 ## Define the SQL::QueryMaker methods used for building up SQL condition statements.
23 1 my %dispatch = (
24 'eq' => \&sql_eq,
25 'ne' => \&sql_ne,
26 'gt' => \&sql_gt,
27 'ge' => \&sql_ge,
28 'lt' => \&sql_lt,
29 'le' => \&sql_le
30 );
31  
32  
33  
34 sub new {
35 5 ▶ my ( $class, $conditionhashref, $queryObj, $default_table_rules ) = @_;
Subroutine new: called CC=5 SCAR=17.0
Pod documented
36
37 5 my $self = $class->SUPER::new($conditionhashref);
38 5 my @condition_errors;
39
40 5 my $field = $conditionhashref->{$self->{_op}}->{field};
41 5 my $conditionField = JsonSQL::Param::Field->new($field, $queryObj, $default_table_rules);
42 5 ▶ if ( eval { $conditionField->is_error } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $conditionField->is_error } }) { } 0 5 5
43 0 push(@condition_errors, "Error using field $field in test condition: $conditionField->{message}");
44 } else {
45 5 $self->{_field} = $conditionField;
46 }
47
48 5 my $value = $conditionhashref->{$self->{_op}}->{value};
49 5 ▶ if ( ref ($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 1 4 5
50 1 my $conditionValue = JsonSQL::Param::Field->new($value, $queryObj, $default_table_rules);
51 1 ▶ if ( eval { $conditionValue->is_error } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $conditionValue->is_error } }) { } 0 1 1
52 0 push(@condition_errors, "Error using field $value in test condition: $conditionValue->{message}");
53 } else {
54 1 $self->{_value} = $conditionValue;
55 }
56 } else {
57 4 $self->{_value} = $value;
58 }
59  
60 5 ▶ if ( @condition_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@condition_errors) { } 0 5 5
61 0 my $err = "Error(s) constructing JsonSQL Condition object: \n\t";
62 0 $err .= join("\n\t", @condition_errors);
63 0 return JsonSQL::Error->new("jsonsql_testcondition", $err);
64 } else {
65 5 return $self;
66 }
67 }
68  
69  
70 sub get_sql_obj {
71 5 ▶ my ( $self, $queryObj ) = @_;
Subroutine get_sql_obj: called CC=3 SCAR=11.0
Pod documented
72
73 5 ▶ if (exists $dispatch{$self->{_op}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dispatch{$self->{"_op"}}) 5 0 5
74 5 my $field = $self->{_field}->get_field_param($queryObj);
75 5 my $value;
76
77 5 ▶ if (ref $self->{_value} eq 'JsonSQL::Param::Field') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_value'} eq 'JsonSQL::Param::Field') { } 1 4 5
78 1 $value = $self->{_value}->get_field_param($queryObj);
79 } else {
80 4 $value = $self->{_value};
81 }
82
83 5 return $dispatch{$self->{_op}}->($field => $value);
84 }
85 }
86  
87  
88 1 1;
89  
90 __END__
91  
92 =pod
93  
94 =encoding UTF-8
95  
96 =head1 NAME
97  
98 JsonSQL::Param::Conditions::TestCondition - JsonSQL::Param::Conditions::TestCondition object. Subclass for parsing test conditions (ex: 'eq', 'gt', etc).
99  
100 =head1 VERSION
101  
102 version 0.4
103  
104 =head1 SYNOPSIS
105  
106 This module constructs a Perl object representing the VALUES parameter of an SQL INSERT statement and has methods for
107 generating the appropriate SQL string and bind values for use with the L<DBI> module.
108  
109 =head1 DESCRIPTION
110  
111 =head3 Object properties:
112  
113 =over
114  
115 =item _field => L<JsonSQL::Param::Field>
116  
117 =item _value => <scalar> or L<JsonSQL::Param::Field>
118  
119 =back
120  
121 =head3 Generated parameters:
122  
123 =over
124  
125 =item $testparameter => L<SQL::QueryMaker> object.
126  
127 =back
128  
129 =head1 METHODS
130  
131 =head2 Constructor new($conditionhashref, $queryObj, $default_table_rules)
132  
133 Instantiates and returns a new JsonSQL::Param::Conditions::TestCondition object.
134  
135 $conditionhashref => A hashref of the condition statement keyed by the operator.
136 $queryObj => A reference to the JsonSQL::Query object that will own this object.
137 $default_table_rules => The default whitelist table rules to use to validate access when the table params
138 are not provided to the field object. Usually, these are acquired from the table params
139 of another object (ex: the FROM clause of a SELECT statement).
140  
141 Returns a JsonSQL::Error object on failure.
142  
143 =head2 ObjectMethod get_sql_obj -> $testparameter
144  
145 Generates parameters represented by the object for the SQL statement. Returns:
146  
147 $testparameter => The test condition to append to the WHERE clause. Constructed by calling the
148 L<SQL::QueryMaker> function defined by the dispatcher with the $field and $value
149 parameters.
150  
151 =head1 AUTHOR
152  
153 Chris Hoefler <bhoefler@draper.com>
154  
155 =head1 COPYRIGHT AND LICENSE
156  
157 This software is copyright (c) 2017 by Chris Hoefler.
158  
159 This is free software; you can redistribute it and/or modify it under
160 the same terms as the Perl 5 programming language system itself.
161  
162 =cut
« JsonSQL/Param/Insert.pm ↑ Summary JsonSQL/Param/Joins.pm »