| 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 | ▶ | 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::Join; | ||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = '0.4'; # VERSION | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use JsonSQL::Param::Table; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use JsonSQL::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | sub new { | ||||||||||
| 19 | 1 | ▶ | my ( $class, $joinhashref, $queryObj ) = @_; | ||||||||
|
Subroutine
new: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | my $self = {}; | |||||||||
| 22 | 1 | my @join_errors; | |||||||||
| 23 | |||||||||||
| 24 | # The join FROM table is just a JsonSQL::Param::Table, so we can inherit the error handling. | ||||||||||
| 25 | 1 | my $joinFrom = JsonSQL::Param::Table->new($joinhashref->{from}, $queryObj); | |||||||||
| 26 | 1 | ▶ | if ( eval { $joinFrom->is_error } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | push(@join_errors, "Error creating join FROM table $joinhashref->{from}->{table}: $joinFrom->{message}"); | |||||||||
| 28 | } else { | ||||||||||
| 29 | 1 | $self->{_joinFrom} = $joinFrom; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | # Same with the join TO table. | ||||||||||
| 33 | 1 | my $joinTo = JsonSQL::Param::Table->new($joinhashref->{to}, $queryObj); | |||||||||
| 34 | 1 | ▶ | if ( eval { $joinTo->is_error } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | push(@join_errors, "Error creating join TO table $joinhashref->{to}->{table}: $joinTo->{message}"); | |||||||||
| 36 | } else { | ||||||||||
| 37 | 1 | $self->{_joinTo} = $joinTo; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | ## SMELL: more fixes to work around QueryMaker bugs | ||||||||||
| 41 | 1 | for ( $joinhashref->{jointype} ) { | |||||||||
| 42 | 1 | when("outerleft") { $self->{_joinType} = "LEFT"; } | |||||||||
| 43 | 1 | when("outerright") { $self->{_joinType} = "RIGHT"; } | |||||||||
| 44 | 1 | when("outerfull") { $self->{_joinType} = "FULL"; } | |||||||||
| 45 | 1 | when("inner") { $self->{_joinType} = "INNER"; } | |||||||||
| 46 | 0 | when("cross") { $self->{_joinType} = "CROSS"; } | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | if (defined $joinhashref->{on}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 1 | my $joinOn = JsonSQL::Param::ConditionDispatcher->parse($joinhashref->{on}, $queryObj); | |||||||||
| 51 | 1 | ▶ | if ( eval { $joinOn->is_error } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | push(@join_errors, "Error creating join ON condition: $joinOn->{message}"); | |||||||||
| 53 | } else { | ||||||||||
| 54 | 1 | $self->{_joinCondition} = $joinOn; | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | ▶ | if ( @join_errors ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | my $err = "Error(s) constructing JOIN object: \n\t"; | |||||||||
| 60 | 0 | $err .= join("\n\t", @join_errors); | |||||||||
| 61 | 0 | return JsonSQL::Error->new("invalid_joins", $err); | |||||||||
| 62 | } else { | ||||||||||
| 63 | 1 | bless $self, $class; | |||||||||
| 64 | 1 | return $self; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | sub get_join { | ||||||||||
| 70 | 1 | ▶ | my ( $self, $queryObj ) = @_; | ||||||||
|
Subroutine
get_join: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 71 | |||||||||||
| 72 | 1 | my $joinFrom = $self->{_joinFrom}->get_table_param($queryObj); | |||||||||
| 73 | my $joinParams = { | ||||||||||
| 74 | type => $self->{_joinType}, | ||||||||||
| 75 | 1 | table => $self->{_joinTo}->get_table_param($queryObj) | |||||||||
| 76 | }; | ||||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | if (defined $self->{_joinCondition}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | my ($sql, $bind) = $self->{_joinCondition}->get_cond($queryObj); | |||||||||
| 85 | 1 | my $condition = $sql =~ s/\?/$bind->[0]/r; | |||||||||
| 86 | 1 | $condition =~ s/`//g; | |||||||||
| 87 | 1 | $joinParams->{condition} = $condition; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | ## Return join as a hash ref. | ||||||||||
| 91 | 1 | return { $joinFrom => $joinParams }; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 1 | 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.4 | ||||||||||
| 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 | ||||||||||