File Coverage

blib/lib/JsonSQL/Param/Conditions/TestCondition.pm
Criterion Covered Total %
statement 45 50 90.0
branch 8 12 66.6
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 64 73 87.6


line stmt bran cond sub pod time code
1             # ABSTRACT: JsonSQL::Param::Conditions::TestCondition object. Subclass for parsing test conditions (ex: 'eq', 'gt', etc).
2              
3              
4              
5 1     1   677 use strict;
  1         3  
  1         35  
6 1     1   7 use warnings;
  1         2  
  1         66  
7 1     1   20 use 5.014;
  1         4  
8              
9             package JsonSQL::Param::Conditions::TestCondition;
10              
11             our $VERSION = '0.4'; # VERSION
12              
13 1     1   6 use base qw( JsonSQL::Param::Condition );
  1         3  
  1         93  
14              
15 1     1   8 use SQL::QueryMaker qw( sql_eq sql_ne sql_gt sql_ge sql_lt sql_le );
  1         2  
  1         71  
16             #use Data::Dumper;
17              
18 1     1   7 use JsonSQL::Param::Field;
  1         3  
  1         31  
19 1     1   6 use JsonSQL::Error;
  1         3  
  1         486  
20              
21              
22             ## Define the SQL::QueryMaker methods used for building up SQL condition statements.
23             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     5 1 31 my ( $class, $conditionhashref, $queryObj, $default_table_rules ) = @_;
36            
37 5         26 my $self = $class->SUPER::new($conditionhashref);
38 5         11 my @condition_errors;
39            
40 5         19 my $field = $conditionhashref->{$self->{_op}}->{field};
41 5         22 my $conditionField = JsonSQL::Param::Field->new($field, $queryObj, $default_table_rules);
42 5 50       13 if ( eval { $conditionField->is_error } ) {
  5         68  
43 0         0 push(@condition_errors, "Error using field $field in test condition: $conditionField->{message}");
44             } else {
45 5         159 $self->{_field} = $conditionField;
46             }
47            
48 5         21 my $value = $conditionhashref->{$self->{_op}}->{value};
49 5 100       15 if ( ref ($value) eq 'HASH' ) {
50 1         5 my $conditionValue = JsonSQL::Param::Field->new($value, $queryObj, $default_table_rules);
51 1 50       5 if ( eval { $conditionValue->is_error } ) {
  1         25  
52 0         0 push(@condition_errors, "Error using field $value in test condition: $conditionValue->{message}");
53             } else {
54 1         35 $self->{_value} = $conditionValue;
55             }
56             } else {
57 4         12 $self->{_value} = $value;
58             }
59              
60 5 50       15 if ( @condition_errors ) {
61 0         0 my $err = "Error(s) constructing JsonSQL Condition object: \n\t";
62 0         0 $err .= join("\n\t", @condition_errors);
63 0         0 return JsonSQL::Error->new("jsonsql_testcondition", $err);
64             } else {
65 5         24 return $self;
66             }
67             }
68              
69              
70             sub get_sql_obj {
71 5     5 1 12 my ( $self, $queryObj ) = @_;
72            
73 5 50       20 if (exists $dispatch{$self->{_op}}) {
74 5         17 my $field = $self->{_field}->get_field_param($queryObj);
75 5         9 my $value;
76            
77 5 100       14 if (ref $self->{_value} eq 'JsonSQL::Param::Field') {
78 1         4 $value = $self->{_value}->get_field_param($queryObj);
79             } else {
80 4         9 $value = $self->{_value};
81             }
82            
83 5         20 return $dispatch{$self->{_op}}->($field => $value);
84             }
85             }
86              
87              
88             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