JsonSQL/Param/Order.pm

filter: Statementstmt 43.7% 21 / 48 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 60.0% 6 / 10 Podpod 100% 2 / 2 totaltotal 37.6% 29 / 77 CC 9 SCAR 28.5
CC
9
Cov
33%
new
30.0
get_orderby
30.0
__ANON__
2.0
CRAP 17.2
« JsonSQL/Param/Conditions/RangeCondition.pm ↑ Summary JsonSQL/Param/Field.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package JsonSQL::Param::Order;
10  
11 1 our $VERSION = '0.4'; # VERSION
12  
13 1 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use JsonSQL::Param::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use JsonSQL::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 ## These are validated by the JSON schema, but an extra check doesn't hurt.
20 1 my @validDirs = ('ASC','DESC');
21 1 my @validNullPlacements = ('FIRST','LAST');
22  
23  
24  
25 sub new {
26 0 ▶ my ( $class, $orderhashref, $queryObj, $default_table_rules ) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
27
28 0 my $orderField = JsonSQL::Param::Field->new($orderhashref->{field}, $queryObj, $default_table_rules);
29 0 ▶ if ( eval { $orderField->is_error } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $orderField->is_error } }) { } 0 0 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 ▶ if ( any { $_ eq $orderDir } @validDirs ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $orderDir; } , @validDirs)) 0 0 0
39 0 $self->{_orderDir} = $orderDir;
40 }
41
42 0 ▶ my $nullsPlacement = $orderhashref->{nulls} || '';
Condition0%0/2
$orderhashref->{'nulls'} || ''
l!l
00
MC/DC0%0/1
$orderhashref->{'nulls'} || ''
$orderhashref->{'nulls'}
Truth table0%0/2
A
A= $orderhashref->{'nulls'}
Aresult
11
00
43 0 ▶ if ( any { $_ eq $nullsPlacement } @validNullPlacements ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $nullsPlacement; } , @validNullPlacements)) 0 0 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 ▶ my ( $self, $queryObj ) = @_;
Subroutine get_orderby: not called CC=5 SCAR=34.0
Pod documented
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 ▶ if (defined $self->{_orderDir}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_orderDir'}) { } 0 0 0
elsif (defined $self->{'_nullsPlacement'}) { } 0 0 0
62 0 $orderByMod = $self->{_orderDir};
63 0 ▶ if (defined $self->{_nullsPlacement}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_nullsPlacement"}) 0 0 0
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 ▶ if (defined $orderByMod) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orderByMod) { } 0 0 0
72 0 return [ $orderByField, $orderByMod ];
73 } else {
74 0 return $orderByField;
75 }
76 }
77  
78  
79 1 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
« JsonSQL/Param/Conditions/RangeCondition.pm ↑ Summary JsonSQL/Param/Field.pm »