File Coverage

blib/lib/JsonSQL/Query/Query.pm
Criterion Covered Total %
statement 25 26 96.1
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 36 38 94.7


line stmt bran cond sub pod time code
1             # ABSTRACT: JSON query base class. Provides the quote_identifier method for escaping table and column identifiers.
2              
3              
4 2     2   14 use strict;
  2         4  
  2         50  
5 2     2   9 use warnings;
  2         2  
  2         37  
6 2     2   24 use 5.014;
  2         6  
7              
8             package JsonSQL::Query::Query;
9              
10             our $VERSION = '0.4'; # VERSION
11              
12 2     2   500 use JsonSQL::Validator;
  2         5  
  2         323  
13              
14              
15              
16             sub new {
17 11     11 1 32 my ( $class, $query_rulesets, $json_schema ) = @_;
18            
19             ## DB specific values can be used in the future, but for now we are setting this to a common default.
20 11         33 my $self = {
21             _quoteChar => "'"
22             };
23            
24             # Get a JsonSQL::Validator object with the provided $json_schema and $query_rulesets.
25 11         54 my $validator = JsonSQL::Validator->new($json_schema, $query_rulesets);
26 11 50       21 if ( eval { $validator->is_error } ) {
  11         207  
27 0         0 return $validator;
28             }
29            
30             # Save a reference to the JsonSQL::Validator for future processing of whitelisting rule sets.
31 11         368 $self->{_validator} = $validator;
32            
33 11         32 bless $self, $class;
34 11         35 return $self;
35             }
36              
37              
38             sub quote_identifier {
39 45     45 1 82 my ( $self, $identifier ) = @_;
40            
41             ## This was taken and modified from Perl's DBI class.
42 45         64 my $quote = $self->{_quoteChar};
43 45 100       82 if ( defined $identifier ) {
44 44         124 $identifier =~ s/$quote/$quote$quote/g;
45 44         84 $identifier = qq($quote$identifier$quote);
46             }
47            
48 45         115 return $identifier;
49             }
50              
51              
52             1;
53              
54             __END__
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             JsonSQL::Query::Query - JSON query base class. Provides the quote_identifier method for escaping table and column identifiers.
63              
64             =head1 VERSION
65              
66             version 0.4
67              
68             =head1 SYNOPSIS
69              
70             This is a base module used to construct JsonSQL::Query modules. It is not meant to be instantiated directly.
71             Instead have a look at,
72              
73             =over
74              
75             =item * L<JsonSQL::Query::Select>
76              
77             =item * L<JsonSQL::Query::Insert>
78              
79             =back
80              
81             You can also create your own subclass...
82              
83             =head1 METHODS
84              
85             =head2 Constructor new($query_rulesets, $json_schema) -> JsonSQL::Query::Query
86              
87             Creates a JsonSQL::Validator object using the supplied $query_rulesets and $json_schema and stores a reference to use for future
88             validation and whitelist checking purposes. See L<JsonSQL::Validator> for more information.
89              
90             $query_rulesets => The whitelist rule sets to be associated with this JsonSQL::Query object.
91             $json_schema => The name of the JSON schema to use for validation of the query.
92              
93             =head2 ObjectMethod quote_identifier($identifier) -> quoted $identifier
94              
95             Since table and column identifiers cannot be parameterized by most databases they have to be quoted. This method
96             is used during SQL query construction to quote non-parameterized identifiers.
97              
98             $identifier => The identifier string to quote.
99              
100             Ex:
101             Column1 => 'Column1'
102             Co'lumn1 => 'Co''lumn1'
103              
104             =head1 AUTHOR
105              
106             Chris Hoefler <bhoefler@draper.com>
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2017 by Chris Hoefler.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut