File Coverage

blib/lib/JsonSQL/Param/ConditionDispatcher.pm
Criterion Covered Total %
statement 29 30 96.6
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 41 43 95.3


line stmt bran cond sub pod time code
1             # ABSTRACT: A dispatcher module that parses condition statements to create the appropriate JsonSQL Condition objects.
2              
3              
4 1     1   6 use strict;
  1         1  
  1         25  
5 1     1   4 use warnings;
  1         2  
  1         21  
6 1     1   14 use 5.014;
  1         3  
7              
8             package JsonSQL::Param::ConditionDispatcher;
9              
10             our $VERSION = '0.41'; # VERSION
11              
12 1     1   5 use Class::Load qw( try_load_class );
  1         2  
  1         44  
13 1     1   9 use List::Util qw( any );
  1         2  
  1         72  
14              
15 1     1   6 use JsonSQL::Error;
  1         2  
  1         166  
16              
17              
18             ## Define the subclasses that support different condition operators.
19             my %opmap = (
20             'TestCondition' => ['eq','ne','gt','ge','lt','le'],
21             'RangeCondition' => ['bt','nb'],
22             'EnumCondition' => ['in','ni'],
23             'NullCondition' => ['isnull','notnull'],
24             'LogicCondition' => ['and','or']
25             );
26              
27              
28              
29             sub parse {
30 7     7 1 20 my ( $caller, $conditionhashref, $queryObj, $default_table_rules ) = @_;
31            
32             ## The schema restricts to one condition operator per level of the hashref, so just grab the first key at this level to use as the operator.
33 7         11 my ( $op ) = keys %{ $conditionhashref };
  7         17  
34            
35             ## Determine the appropriate JsonSQL Condition subclass for this operator.
36 7         14 my $class = "JsonSQL::Param::Conditions::";
37 7         24 for my $classObj ( keys %opmap ) {
38 35 100   74   86 if ( any { $_ eq $op } @{ $opmap{$classObj} }) {
  74         164  
  35         109  
39 7         29 $class .= $classObj;
40             }
41             }
42              
43             ## Load the class and return a new instance if successful. Otherwise return a JsonSQL::Error object.
44 7         29 my ( $success, $err ) = try_load_class($class);
45 7 50       381 if ( $success ) {
46 7         27 return $class->new($conditionhashref, $queryObj, $default_table_rules);
47             } else {
48 0           return JsonSQL::Error->new("jsonsql_condition", $err);
49             }
50             }
51              
52              
53             1;
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             JsonSQL::Param::ConditionDispatcher - A dispatcher module that parses condition statements to create the appropriate JsonSQL Condition objects.
64              
65             =head1 VERSION
66              
67             version 0.41
68              
69             =head1 SYNOPSIS
70              
71             This is a supporting module used by L<JsonSQL::Query> modules for parsing condition operators as parameters for conditional
72             clauses.
73              
74             To use this:
75              
76             my $condObj = JsonSQL::Param::ConditionDispatcher->parse($conditionhashref, $queryObj, $default_table_rules);
77             if ( eval { $condObj->is_error } ) {
78             return "Could not create condition object: $condObj->{message}";
79             } else {
80             ...
81             }
82              
83             The $conditionhashref must contain a single key which is the operator for the condition statement. The operator gets mapped to the
84             appropriate module to load as defined in the %opmap hash. The conditional modules are subclasses of JsonSQL::Param::Condition and
85             reside in JsonSQL::Param::Conditions. The value of the operator key is a hash of args to apply to the operator. For example, a TestCondition
86             needs to have "field" and "value" properties.
87              
88             The collection of conditional modules is fairly complete, but others can be created if need be.
89              
90             =head1 METHODS
91              
92             =head2 Dispatcher parse($conditionhashref, $queryObj, $default_table_rules) -> JsonSQL::Param::Condition
93              
94             Serves as a dispatcher to load the appropriate JsonSQL::Param::Conditions:: class, and create a new instance which is then returned.
95              
96             =head1 AUTHOR
97              
98             Chris Hoefler <bhoefler@draper.com>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             This software is copyright (c) 2017 by Chris Hoefler.
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             =cut