File Coverage

blib/lib/JsonSQL/Param/Order.pm
Criterion Covered Total %
statement 17 44 38.6
branch 0 14 0.0
condition 0 2 0.0
subroutine 6 10 60.0
pod 2 2 100.0
total 25 72 34.7


line stmt bran cond sub pod time code
1             # ABSTRACT: JsonSQL::Param::Order object. Stores a Perl representation of an SQL ORDER BY clause for use in JsonSQL::Query objects.
2              
3              
4              
5 1     1   6 use strict;
  1         2  
  1         25  
6 1     1   4 use warnings;
  1         2  
  1         19  
7 1     1   12 use 5.014;
  1         3  
8              
9             package JsonSQL::Param::Order;
10              
11             our $VERSION = '0.4'; # VERSION
12              
13 1     1   5 use List::Util qw( any );
  1         2  
  1         41  
14              
15 1     1   6 use JsonSQL::Param::Field;
  1         1  
  1         15  
16 1     1   4 use JsonSQL::Error;
  1         2  
  1         358  
17              
18              
19             ## These are validated by the JSON schema, but an extra check doesn't hurt.
20             my @validDirs = ('ASC','DESC');
21             my @validNullPlacements = ('FIRST','LAST');
22              
23              
24              
25             sub new {
26 0     0 1   my ( $class, $orderhashref, $queryObj, $default_table_rules ) = @_;
27            
28 0           my $orderField = JsonSQL::Param::Field->new($orderhashref->{field}, $queryObj, $default_table_rules);
29 0 0         if ( eval { $orderField->is_error } ) {
  0            
30 0           return JsonSQL::Error->new("invalid_fields", "Error creating field $orderhashref->{field} for ORDER BY.");
31             } else {
32 0           my $self = {
33             _field => $orderField
34             };
35              
36             ## These are optional parameters. Store if they have been provided.
37 0           my $orderDir = $orderhashref->{order};
38 0 0   0     if ( any { $_ eq $orderDir } @validDirs ) {
  0            
39 0           $self->{_orderDir} = $orderDir;
40             }
41            
42 0   0       my $nullsPlacement = $orderhashref->{nulls} || '';
43 0 0   0     if ( any { $_ eq $nullsPlacement } @validNullPlacements ) {
  0            
44 0           $self->{_nullsPlacement} = $nullsPlacement;
45             }
46            
47             ## Return the blessed reference.
48 0           bless $self, $class;
49 0           return $self;
50             }
51             }
52              
53              
54             sub get_orderby {
55 0     0 1   my ( $self, $queryObj ) = @_;
56              
57 0           my $orderByField = $self->{_field}->get_field_param($queryObj);
58              
59             ## Format the ordering modifiers, if any have been provided.
60 0           my $orderByMod;
61 0 0         if (defined $self->{_orderDir}) {
    0          
62 0           $orderByMod = $self->{_orderDir};
63 0 0         if (defined $self->{_nullsPlacement}) {
64 0           $orderByMod .= ' ' . $self->{_nullsPlacement};
65             }
66             } elsif (defined $self->{_nullsPlacement}) {
67 0           $orderByMod = $self->{_nullsPlacement};
68             }
69              
70             ## If an ordering modifier has been specified, return a hash ref. If not, return a scalar ref.
71 0 0         if (defined $orderByMod) {
72 0           return [ $orderByField, $orderByMod ];
73             } else {
74 0           return $orderByField;
75             }
76             }
77              
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             JsonSQL::Param::Order - JsonSQL::Param::Order object. Stores a Perl representation of an SQL ORDER BY clause for use in JsonSQL::Query objects.
90              
91             =head1 VERSION
92              
93             version 0.4
94              
95             =head1 SYNOPSIS
96              
97             This module constructs a Perl object representing the ORDER BY parameter of an SQL SELECT statement and has methods for
98             extracting the parameters to generate the appropriate SQL string.
99              
100             =head1 DESCRIPTION
101              
102             =head3 Object properties:
103              
104             =over
105              
106             =item _field => L<JsonSQL::Param::Field>
107              
108             =item _orderDir => "ASC" or "DESC"
109              
110             =item _nullsPlacement => "FIRST" or "LAST"
111              
112             =back
113              
114             =head3 Generated parameters:
115              
116             =over
117              
118             =item $orderByField => <string>
119              
120             =item $orderByMod => <string>
121              
122             =back
123              
124             =head1 METHODS
125              
126             =head2 Constructor new($orderhashref, $queryObj, $default_table_rules)
127              
128             Instantiates and returns a new JsonSQL::Param::Order object.
129              
130             $orderhashref => A hashref of field/sortorder/nullplacement properties used to construct the object.
131             $queryObj => A reference to the JsonSQL::Query object that will own this object.
132             $default_table_rules => The default whitelist table rules to use to validate access when the table params
133             are not provided to the field object. Usually, these are acquired from the table params
134             of another object (ex: the FROM clause of a SELECT statement).
135              
136             Returns a JsonSQL::Error object on failure.
137              
138             =head2 ObjectMethod get_orderby -> ( $orderByField, $orderByMod )
139              
140             Generates parameters represented by the object for the SQL statement. Returns:
141              
142             $orderByField => The column to sort by.
143             $orderByMod => A string of sort modifiers to place after the column (ex: "ASC NULLS FIRST").
144              
145             =head1 AUTHOR
146              
147             Chris Hoefler <bhoefler@draper.com>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2017 by Chris Hoefler.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut