File Coverage

blib/lib/JsonSQL/Param/Condition.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: JsonSQL::Param::Condition object. This is a base class used to derive subclasses for parsing condition statements.
2              
3              
4              
5 1     1   8 use strict;
  1         2  
  1         34  
6 1     1   6 use warnings;
  1         2  
  1         27  
7 1     1   17 use 5.014;
  1         4  
8              
9             package JsonSQL::Param::Condition;
10              
11             our $VERSION = '0.4'; # VERSION
12              
13 1     1   7 use JsonSQL::Error;
  1         2  
  1         200  
14              
15              
16              
17             sub new {
18 7     7 1 18 my ( $class, $conditionhashref ) = @_;
19            
20             ## 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.
21 7         11 my ( $op ) = keys %{ $conditionhashref };
  7         18  
22 7         25 my $self = {
23             _op => $op
24             };
25            
26 7         15 bless $self, $class;
27 7         20 return $self;
28             }
29              
30              
31             sub get_cond {
32 2     2 1 6 my ( $self, $queryObj ) = @_;
33            
34 2         8 my $condObj = $self->get_sql_obj($queryObj);
35 2         102 my $sql = $condObj->as_sql;
36 2         212 my @binds = $condObj->bind;
37              
38             ## SMELL: Another QueryMaker workaround. Doesn't quote right.
39 2         27 $sql =~ s/`//g;
40            
41 2         38 return ($sql, \@binds);
42             }
43              
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =encoding UTF-8
52              
53             =head1 NAME
54              
55             JsonSQL::Param::Condition - JsonSQL::Param::Condition object. This is a base class used to derive subclasses for parsing condition statements.
56              
57             =head1 VERSION
58              
59             version 0.4
60              
61             =head1 SYNOPSIS
62              
63             This module constructs a Perl object representing the VALUES parameter of an SQL INSERT statement and has methods for
64             generating the appropriate SQL string and bind values for use with the L<DBI> module.
65              
66             =head1 DESCRIPTION
67              
68             =head3 Object properties:
69              
70             =over
71              
72             =item _op => The operator used to construct the condition
73              
74             (ex: 'and', 'eq', or 'in').
75              
76             =back
77              
78             =head3 Generated parameters:
79              
80             =over
81              
82             =item $sql => SQL string of the condition
83              
84             =item $binds => Arrayref of bind values to use with the query.
85              
86             =back
87              
88             =head1 METHODS
89              
90             =head2 Constructor new($conditionhashref)
91              
92             Instantiates and returns a new JsonSQL::Param::Condition object.
93              
94             $conditionhashref => A hashref of the condition statement keyed by the operator.
95              
96             Returns a JsonSQL::Error object on failure.
97              
98             =head2 ObjectMethod get_cond -> ( $sql, $binds )
99              
100             Generates the SQL statement represented by the object. Returns:
101              
102             $sql => An SQL string of conditional parameters to use with a conditional clause (ex: WHERE or ON).
103             $binds => An arrayref of parameterized values to pass with the query.
104              
105             =head1 AUTHOR
106              
107             Chris Hoefler <bhoefler@draper.com>
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2017 by Chris Hoefler.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut