JsonSQL/Param/Conditions/RangeCondition.pm untested

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