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   432 use strict;
  1         2  
  1         25  
6 1     1   4 use warnings;
  1         2  
  1         40  
7 1     1   17 use 5.014;
  1         4  
8              
9             package JsonSQL::Param::Conditions::TestCondition;
10              
11             our $VERSION = '0.41'; # VERSION
12              
13 1     1   6 use base qw( JsonSQL::Param::Condition );
  1         1  
  1         60  
14              
15 1     1   9 use SQL::QueryMaker qw( sql_eq sql_ne sql_gt sql_ge sql_lt sql_le );
  1         2  
  1         58  
16             #use Data::Dumper;
17              
18 1     1   5 use JsonSQL::Param::Field;
  1         2  
  1         19  
19 1     1   4 use JsonSQL::Error;
  1         1  
  1         341  
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 12 my ( $class, $conditionhashref, $queryObj, $default_table_rules ) = @_;
36            
37 5         17 my $self = $class->SUPER::new($conditionhashref);
38 5         7 my @condition_errors;
39            
40 5         13 my $field = $conditionhashref->{$self->{_op}}->{field};
41 5         15 my $conditionField = JsonSQL::Param::Field->new($field, $queryObj, $default_table_rules);
42 5 50       11 if ( eval { $conditionField->is_error } ) {
  5         44  
43 0         0 push(@condition_errors, "Error using field $field in test condition: $conditionField->{message}");
44             } else {
45 5         101 $self->{_field} = $conditionField;
46             }
47            
48 5         12 my $value = $conditionhashref->{$self->{_op}}->{value};
49 5 100       13 if ( ref ($value) eq 'HASH' ) {
50 1         3 my $conditionValue = JsonSQL::Param::Field->new($value, $queryObj, $default_table_rules);
51 1 50       3 if ( eval { $conditionValue->is_error } ) {
  1         7  
52 0         0 push(@condition_errors, "Error using field $value in test condition: $conditionValue->{message}");
53             } else {
54 1         21 $self->{_value} = $conditionValue;
55             }
56             } else {
57 4         6 $self->{_value} = $value;
58             }
59              
60 5 50       10 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         18 return $self;
66             }
67             }
68              
69              
70             sub get_sql_obj {
71 5     5 1 8 my ( $self, $queryObj ) = @_;
72            
73 5 50       14 if (exists $dispatch{$self->{_op}}) {
74 5         11 my $field = $self->{_field}->get_field_param($queryObj);
75 5         7 my $value;
76            
77 5 100       10 if (ref $self->{_value} eq 'JsonSQL::Param::Field') {
78 1         3 $value = $self->{_value}->get_field_param($queryObj);
79             } else {
80 4         6 $value = $self->{_value};
81             }
82            
83 5         14 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.41
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