File Coverage

blib/lib/JsonSQL/Param/Conditions/LogicCondition.pm
Criterion Covered Total %
statement 35 39 89.7
branch 3 6 50.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 48 55 87.2


line stmt bran cond sub pod time code
1             # ABSTRACT: JsonSQL::Param::Conditions::LogicCondition object. Subclass for parsing logic conditions (ex: 'and', 'or', etc).
2              
3              
4              
5 1     1   899 use strict;
  1         3  
  1         46  
6 1     1   10 use warnings;
  1         2  
  1         48  
7 1     1   31 use 5.014;
  1         6  
8              
9             package JsonSQL::Param::Conditions::LogicCondition;
10              
11             our $VERSION = '0.41'; # VERSION
12              
13 1     1   9 use base qw( JsonSQL::Param::Condition );
  1         3  
  1         415  
14              
15 1     1   273 use SQL::QueryMaker qw( sql_and sql_or );
  1         2247  
  1         58  
16             #use Data::Dumper;
17              
18 1     1   6 use JsonSQL::Error;
  1         2  
  1         248  
19              
20              
21             ## Define the SQL::QueryMaker methods used for building up SQL condition statements.
22             my %dispatch = (
23             'and' => \&sql_and,
24             'or' => \&sql_or,
25             );
26              
27              
28              
29             sub new {
30 2     2 1 4 my ( $class, $conditionhashref, $queryObj, $default_table_rules ) = @_;
31            
32 2         10 my $self = $class->SUPER::new($conditionhashref);
33 2         2 my @condition_errors;
34              
35 2         3 for my $arghash (@{ $conditionhashref->{$self->{_op}} }) {
  2         9  
36 5         19 my $argCondition = JsonSQL::Param::ConditionDispatcher->parse($arghash, $queryObj, $default_table_rules);
37 5 50       5 if ( eval { $argCondition->is_error } ) {
  5         44  
38 0         0 push(@condition_errors, "Error parsing logic condition argument: $argCondition->{message}");
39             } else {
40 5         91 push(@{ $self->{_args} }, $argCondition);
  5         15  
41             }
42             }
43            
44 2 50       5 if ( @condition_errors ) {
45 0         0 my $err = "Error(s) constructing JsonSQL Condition object: \n\t";
46 0         0 $err .= join("\n\t", @condition_errors);
47 0         0 return JsonSQL::Error->new("jsonsql_logiccondition", $err);
48             } else {
49 2         6 return $self;
50             }
51             }
52              
53              
54             sub get_sql_obj {
55 2     2 1 3 my ( $self, $queryObj ) = @_;
56              
57 2         3 my @cond_args = map { $_->get_sql_obj($queryObj) } @{ $self->{_args} };
  5         60  
  2         4  
58              
59 2 50       63 if (exists $dispatch{$self->{_op}}) {
60 2         7 return $dispatch{$self->{_op}}->(\@cond_args);
61             }
62             }
63              
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             JsonSQL::Param::Conditions::LogicCondition - JsonSQL::Param::Conditions::LogicCondition object. Subclass for parsing logic conditions (ex: 'and', 'or', etc).
76              
77             =head1 VERSION
78              
79             version 0.41
80              
81             =head1 SYNOPSIS
82              
83             This module constructs a Perl object representing the VALUES parameter of an SQL INSERT statement and has methods for
84             generating the appropriate SQL string and bind values for use with the L<DBI> module.
85              
86             =head1 DESCRIPTION
87              
88             =head3 Object properties:
89              
90             =over
91              
92             =item _args => Array of L<JsonSQL::Param::Condition> objects to use as parameters.
93              
94             =back
95              
96             =head3 Generated parameters:
97              
98             =over
99              
100             =item $logicparameter => L<SQL::QueryMaker> object.
101              
102             =back
103              
104             =head1 METHODS
105              
106             =head2 Constructor new($conditionhashref, $queryObj, $default_table_rules)
107              
108             Instantiates and returns a new JsonSQL::Param::Conditions::LogicCondition object.
109              
110             $conditionhashref => A hashref of the condition statement keyed by the operator.
111             $queryObj => A reference to the JsonSQL::Query object that will own this object.
112             $default_table_rules => The default whitelist table rules to use to validate access when the table params
113             are not provided to the field object. Usually, these are acquired from the table params
114             of another object (ex: the FROM clause of a SELECT statement).
115              
116             Returns a JsonSQL::Error object on failure.
117              
118             =head2 ObjectMethod get_sql_obj -> $logicparameter
119              
120             Generates parameters represented by the object for the SQL statement. Returns:
121              
122             $logicparameter => The logic condition to append to the WHERE clause. Constructed by calling the
123             L<SQL::QueryMaker> function defined by the dispatcher with the \@cond_args parameter.
124              
125             =head1 AUTHOR
126              
127             Chris Hoefler <bhoefler@draper.com>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2017 by Chris Hoefler.
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut