SQLEngine/Driver/Trait/NoComplexJoins.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 128 CC 14 SCAR 49.9
CC
14
Cov
0%
sql_join
210.0
import
2.0
dbms_join_on_unsupported
2.0
CRAP 147.6
1 =head1 NAME
2  
3 DBIx::SQLEngine::Driver::Trait::NoComplexJoins - For databases without complex joins
4  
5 =head1 SYNOPSIS
6  
7 # Classes can import this behavior if they don't have joins using "on"
8 use DBIx::SQLEngine::Driver::Trait::NoComplexJoins ':all';
9
10 # Implements a workaround for missing "inner join on ..." capability
11 $rows = $sqldb->fetch_select_rows( tables => [
12 'foo', inner_join=>[ 'foo.id = bar.id' ], 'bar'
13 ] );
14
15 # Attempts to use an "outer join" produce an exception
16 $rows = $sqldb->fetch_select_rows( tables => [
17 'foo', outer_join=>[ 'foo.id = bar.id' ], 'bar'
18 ] );
19  
20 =head1 DESCRIPTION
21  
22 This package supports SQL database servers which do natively provide a SQL
23 select with inner and outer joins.
24  
25 This package causes inner joins to be replaced with cross joins and a where clause. Outer joins, including left and right joins, are not supported and will cause an exception.
26  
27 =head2 About Driver Traits
28  
29 You do not need to use this package directly; it is used internally by those driver subclasses which need it.
30  
31 For more information about Driver Traits, see L<DBIx::SQLEngine::Driver/"About Driver Traits">.
32  
33 =cut
34  
35 ########################################################################
36  
37 package DBIx::SQLEngine::Driver::Trait::NoComplexJoins;
38  
39 use Exporter;
40 sub import { goto &Exporter::import }
41 @EXPORT_OK = qw(
42 sql_join
43 dbms_join_on_unsupported dbms_outer_join_unsupported
44 );
45 %EXPORT_TAGS = ( all => \@EXPORT_OK );
46  
47 use strict;
48 use Carp;
49  
50 ########################################################################
51  
52 =head1 REFERENCE
53  
54 =cut
55  
56 ########################################################################
57  
58 =head2 Database Capability Information
59  
60 =over 4
61  
62 =item dbms_join_on_unsupported
63  
64 $sqldb->dbms_join_on_unsupported() : 1
65  
66 Capability Limitation: This driver does not support the "join ... on ..." syntax.
67  
68 =item dbms_outer_join_unsupported
69  
70 $sqldb->dbms_outer_join_unsupported() : 1
71  
72 Capability Limitation: This driver does not support any type of outer joins.
73  
74 =back
75  
76 =cut
77  
78 sub dbms_join_on_unsupported { 1 }
79 sub dbms_outer_join_unsupported { 1 }
80  
81 ########################################################################
82  
83 =head2 Select to Retrieve Data
84  
85 =over 4
86  
87 =item sql_join()
88  
89 $sqldb->sql_join( $table1, $table2, ... ) : $sql, @params
90 $sqldb->sql_join( $table1, \%criteria, $table2 ) : $sql, @params
91 $sqldb->sql_join( $table1, $join_type=>\%criteria, $table2 ) : $sql, @params
92  
93 Processes one or more table names to create the "from" clause of a select statement. Table names may appear in succession for normal "cross joins", or you may specify a join criteria between them.
94  
95 Inner joins are replaced with normal "comma" cross joins and a where clause. Use of a left, right or full outer join causes an exception to be thrown.
96  
97 Note that using join criteria will cause the return from this method to be a bit different than that of the superclass; instead of just being a "from" expression with table names, the returned SQL statement will also include a "where" expression. Conveniently, the sql_where method allows post-processing of a statement that already includes a where clause, so this value can still be combined with additional criteria supplied as a separate "where" argument to one of the select methods.
98  
99 =back
100  
101 =cut
102  
103 sub sql_join {
104 my ($self, @exprs) = @_;
105 my $sql = '';
106 my @params;
107 my @where_sql;
108 my @where_params;
109 while ( scalar @exprs ) {
110 my $expr = shift @exprs;
111  
112 my ( $table, $join, $criteria );
113 if ( ! ref $expr and $expr =~ /^[\w\s]+join$/i and ref($exprs[0]) ) {
114 $join = $expr;
115 $criteria = shift @exprs;
116 $table = shift @exprs;
117  
118 } elsif ( ref($expr) eq 'HASH' ) {
119 $join = 'inner join';
120 $criteria = $expr;
121 $table = shift @exprs;
122  
123 } else {
124 $join = ',';
125 $criteria = undef;
126 $table = $expr;
127 }
128  
129 ( $table ) or croak("No table name provided to join to");
130 ( $join ) or croak("No join type provided for link to $table");
131  
132 ( $join !~ /outer|right|left/i )
133 or confess("This database does not support outer joins");
134
135 my ( $expr_sql, @expr_params );
136 if ( ! ref $table ) {
137 $expr_sql = $table
138 } elsif ( ref($table) eq 'ARRAY' ) {
139 my ( $sub_sql, @sub_params ) = $self->sql_join( @$table );
140 # No need for parentheses because everything's going to be cross joined.
141 $expr_sql = $sub_sql;
142 push @expr_params, @sub_params
143 } elsif ( ref($table) eq 'HASH' ) {
144 my %seen_tables;
145 my @tables = grep { ! $seen_tables{$_} ++ } map { ( /^([^\.]+)\./ )[0] } %$table;
146 if ( @tables == 2 ) {
147 my ( $sub_sql, @sub_params ) = $self->sql_join(
148 $tables[0],
149 inner_join => { map { $_ => \($table->{$_}) } keys %$table },
150 $tables[1],
151 );
152 $expr_sql = $sub_sql;
153 push @expr_params, @sub_params
154 } else {
155 confess("sql_join on hash with more than two tables not yet supported")
156 }
157 } elsif ( UNIVERSAL::can($table, 'name') ) {
158 $expr_sql = $table->name
159 } else {
160 Carp::confess("Unsupported expression in sql_join: '$table'");
161 }
162
163 if ( $expr_sql =~ s/ where (.*)$// ) {
164 push @where_sql, $1;
165 push @where_params, @expr_params;
166 }
167 $sql .= ", $expr_sql";
168 push @params, @expr_params;
169
170 if ( $criteria ) {
171 my ($crit_sql, @crit_params) =
172 DBIx::SQLEngine::Criteria->auto_where( $criteria );
173 if ( $crit_sql ) {
174 push @where_sql, $crit_sql if ( $crit_sql );
175 push @where_params, @crit_params;
176 }
177 }
178  
179 }
180 $sql =~ s/^, // or carp("Suspect table join: '$sql'");
181 if ( scalar @where_sql ) {
182 $sql .= " where " . ( ( scalar(@where_sql) == 1 ) ? $where_sql[0]
183 : join( 'and', map "( $_ )", @where_sql ) );
184 push @params, @where_params;
185 }
186 ( $sql, @params );
187 }
188  
189 ########################################################################
190  
191 =head1 SEE ALSO
192  
193 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
194  
195 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
196 this distribution, including installation and license information.
197  
198 =cut
199  
200 ########################################################################
201  
202 1;
203