line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: JsonSQL::Param::Join object. Stores a Perl representation of an SQL join expression for use in JsonSQL::Query objects. |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
7
|
1
|
|
|
1
|
|
11
|
use 5.014; |
|
1
|
|
|
|
|
2
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package JsonSQL::Param::Join; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.41'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
17
|
use JsonSQL::Param::Table; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
14
|
1
|
|
|
1
|
|
3
|
use JsonSQL::Error; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
126
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
1
|
|
|
1
|
1
|
4
|
my ( $class, $joinhashref, $queryObj ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
4
|
my $self = {}; |
22
|
1
|
|
|
|
|
2
|
my @join_errors; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# The join FROM table is just a JsonSQL::Param::Table, so we can inherit the error handling. |
25
|
1
|
|
|
|
|
8
|
my $joinFrom = JsonSQL::Param::Table->new($joinhashref->{from}, $queryObj); |
26
|
1
|
50
|
|
|
|
2
|
if ( eval { $joinFrom->is_error } ) { |
|
1
|
|
|
|
|
9
|
|
27
|
0
|
|
|
|
|
0
|
push(@join_errors, "Error creating join FROM table $joinhashref->{from}->{table}: $joinFrom->{message}"); |
28
|
|
|
|
|
|
|
} else { |
29
|
1
|
|
|
|
|
21
|
$self->{_joinFrom} = $joinFrom; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Same with the join TO table. |
33
|
1
|
|
|
|
|
4
|
my $joinTo = JsonSQL::Param::Table->new($joinhashref->{to}, $queryObj); |
34
|
1
|
50
|
|
|
|
2
|
if ( eval { $joinTo->is_error } ) { |
|
1
|
|
|
|
|
8
|
|
35
|
0
|
|
|
|
|
0
|
push(@join_errors, "Error creating join TO table $joinhashref->{to}->{table}: $joinTo->{message}"); |
36
|
|
|
|
|
|
|
} else { |
37
|
1
|
|
|
|
|
24
|
$self->{_joinTo} = $joinTo; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
## SMELL: more fixes to work around QueryMaker bugs |
41
|
1
|
|
|
|
|
3
|
for ( $joinhashref->{jointype} ) { |
42
|
1
|
|
|
|
|
4
|
when("outerleft") { $self->{_joinType} = "LEFT"; } |
|
0
|
|
|
|
|
0
|
|
43
|
1
|
|
|
|
|
2
|
when("outerright") { $self->{_joinType} = "RIGHT"; } |
|
0
|
|
|
|
|
0
|
|
44
|
1
|
|
|
|
|
1
|
when("outerfull") { $self->{_joinType} = "FULL"; } |
|
0
|
|
|
|
|
0
|
|
45
|
1
|
|
|
|
|
2
|
when("inner") { $self->{_joinType} = "INNER"; } |
|
1
|
|
|
|
|
4
|
|
46
|
0
|
|
|
|
|
0
|
when("cross") { $self->{_joinType} = "CROSS"; } |
|
0
|
|
|
|
|
0
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
1
|
50
|
|
|
|
4
|
if (defined $joinhashref->{on}) { |
50
|
1
|
|
|
|
|
6
|
my $joinOn = JsonSQL::Param::ConditionDispatcher->parse($joinhashref->{on}, $queryObj); |
51
|
1
|
50
|
|
|
|
2
|
if ( eval { $joinOn->is_error } ) { |
|
1
|
|
|
|
|
7
|
|
52
|
0
|
|
|
|
|
0
|
push(@join_errors, "Error creating join ON condition: $joinOn->{message}"); |
53
|
|
|
|
|
|
|
} else { |
54
|
1
|
|
|
|
|
21
|
$self->{_joinCondition} = $joinOn; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
1
|
50
|
|
|
|
3
|
if ( @join_errors ) { |
59
|
0
|
|
|
|
|
0
|
my $err = "Error(s) constructing JOIN object: \n\t"; |
60
|
0
|
|
|
|
|
0
|
$err .= join("\n\t", @join_errors); |
61
|
0
|
|
|
|
|
0
|
return JsonSQL::Error->new("invalid_joins", $err); |
62
|
|
|
|
|
|
|
} else { |
63
|
1
|
|
|
|
|
2
|
bless $self, $class; |
64
|
1
|
|
|
|
|
3
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub get_join { |
70
|
1
|
|
|
1
|
1
|
2
|
my ( $self, $queryObj ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
7
|
my $joinFrom = $self->{_joinFrom}->get_table_param($queryObj); |
73
|
|
|
|
|
|
|
my $joinParams = { |
74
|
|
|
|
|
|
|
type => $self->{_joinType}, |
75
|
1
|
|
|
|
|
4
|
table => $self->{_joinTo}->get_table_param($queryObj) |
76
|
|
|
|
|
|
|
}; |
77
|
|
|
|
|
|
|
|
78
|
1
|
50
|
|
|
|
3
|
if (defined $self->{_joinCondition}) { |
79
|
|
|
|
|
|
|
## SMELL: SQL::Maker doesn't support SQL::QueryMaker objects for JOIN ON |
80
|
|
|
|
|
|
|
## So, we build up the expression manually |
81
|
|
|
|
|
|
|
## Note: the JOIN condition is not parameterized, but this should be ok because the schema forces it to be based on |
82
|
|
|
|
|
|
|
## field identifiers, which are always quoted. It would be nice to do this in a cleaner way, but first have to |
83
|
|
|
|
|
|
|
## remove SQL::Maker dependency. |
84
|
1
|
|
|
|
|
6
|
my ($sql, $bind) = $self->{_joinCondition}->get_cond($queryObj); |
85
|
1
|
|
|
|
|
6
|
my $condition = $sql =~ s/\?/$bind->[0]/r; |
86
|
1
|
|
|
|
|
3
|
$condition =~ s/`//g; |
87
|
1
|
|
|
|
|
3
|
$joinParams->{condition} = $condition; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
## Return join as a hash ref. |
91
|
1
|
|
|
|
|
4
|
return { $joinFrom => $joinParams }; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=pod |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=encoding UTF-8 |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 NAME |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
JsonSQL::Param::Join - JsonSQL::Param::Join object. Stores a Perl representation of an SQL join expression for use in JsonSQL::Query objects. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 VERSION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
version 0.41 |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SYNOPSIS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This module constructs a Perl object representing a JOIN parameter of an SQL SELECT statement and has methods for |
114
|
|
|
|
|
|
|
extracting the parameters to generate the appropriate SQL string. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 DESCRIPTION |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head3 Object properties: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item _joinFrom => L<JsonSQL::Param::Table> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item _joinTo => L<JsonSQL::Param::Table> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item _joinType => "inner" || "outerleft" || "outerright" || "outerfull" || "cross" |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item _joinCondition => L<JsonSQL::Param::Condition> |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=back |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head3 Generated parameters: |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=over |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item { $joinFrom => $joinParams } |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=back |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 METHODS |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 Constructor new($joinhashref, $queryObj) |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Instantiates and returns a new JsonSQL::Param::Join object. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
$joinhashref => A hashref of fromtable/totable/jointype/joincondition properties used to construct the object. |
147
|
|
|
|
|
|
|
$queryObj => A reference to the JsonSQL::Query object that will own this object. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Returns a JsonSQL::Error object on failure. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 ObjectMethod get_join -> { $joinFrom => $joinParams } |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Generates parameters represented by the object for the SQL statement. Returns: |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
$joinFrom => The table to JOIN from. |
156
|
|
|
|
|
|
|
$joinParams => The parameters defining the join. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHOR |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Chris Hoefler <bhoefler@draper.com> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Chris Hoefler. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
167
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |