File Coverage

blib/lib/JsonSQL/Query/Query.pm
Criterion Covered Total %
statement 26 27 96.3
branch 3 4 75.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 38 41 92.6


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