SQLEngine/Driver/Trait/NoUnions.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 167 CC 9 SCAR 20.4
CC
9
Cov
0%
fetch_select
12.0
fetch_select_rows
12.0
visit_select
12.0
CRAP 7.7
1 =head1 NAME
2  
3 DBIx::SQLEngine::Driver::Trait::NoUnions - For databases without select unions
4  
5 =head1 SYNOPSIS
6  
7 # Classes can import this behavior if they don't have native unions
8 use DBIx::SQLEngine::Driver::Trait::NoUnions ':all';
9
10 # Implements a workaround for unavailable sql_union capability
11 $rows = $sqldb->fetch_select_rows( union => [
12 { table => 'foo', columns => '*' },
13 { table => 'bar', columns => '*' },
14 ] );
15  
16 =head1 DESCRIPTION
17  
18 This package supports SQL database servers which do natively provide a SQL
19 select with unions. Instead, queries with unions are executed separately and
20 their results combined.
21  
22 =head2 About Driver Traits
23  
24 You do not need to use this package directly; it is used internally by those driver subclasses which need it.
25  
26 For more information about Driver Traits, see L<DBIx::SQLEngine::Driver/"About Driver Traits">.
27  
28 =cut
29  
30 ########################################################################
31  
32 package DBIx::SQLEngine::Driver::Trait::NoUnions;
33  
34 use Exporter;
35 sub import { goto &Exporter::import }
36 @EXPORT_OK = qw(
37 sql_union dbms_union_unsupported
38 fetch_select fetch_select_rows
39 visit_select visit_select_rows
40 );
41 %EXPORT_TAGS = ( all => \@EXPORT_OK );
42  
43 use strict;
44 use Carp;
45  
46 ########################################################################
47  
48 =head1 REFERENCE
49  
50 The following methods are provided:
51  
52 =cut
53  
54 ########################################################################
55  
56 =head2 Database Capability Information
57  
58 =over 4
59  
60 =item dbms_union_unsupported()
61  
62 $sqldb->dbms_union_unsupported() : 1
63  
64 Capability Limitation: This driver does not support native select unions.
65  
66 =back
67  
68 =cut
69  
70 sub dbms_union_unsupported { 1 }
71  
72 ########################################################################
73  
74 =head2 Select to Retrieve Data
75  
76 =over 4
77  
78 =item fetch_select()
79  
80 $sqldb->fetch_select( %sql_clauses ) : $row_hashes
81 $sqldb->fetch_select( %sql_clauses ) : ($row_hashes,$column_hashes)
82  
83 Unless passed a "union" argument pair, simply calls the superclass method.
84 Runs each of the provided queries separately and concatenates their results.
85 Munges the keys used to turn rows into hashes, so that all results use the
86 column names produced by the first of the queries.
87  
88 =item fetch_select_rows()
89  
90 $sqldb->fetch_select_rows( %sql_clauses ) : $row_arrays
91 $sqldb->fetch_select_rows( %sql_clauses ) : ($row_arrays,$column_hashes)
92  
93 Unless passed a "union" argument pair, simply calls the superclass method.
94 Runs each of the provided queries separately and concatenates their results.
95  
96 =item visit_select()
97  
98 $sqldb->visit_select( $code_ref, %sql_clauses ) : @results
99 $sqldb->visit_select( %sql_clauses, $code_ref ) : @results
100  
101 Unless passed a "union" argument pair, simply calls the superclass method.
102 Runs each of the provided queries separately and concatenates their results.
103  
104 To Do: This method doesn't yet munge the column names retrived by the later queries to match the first.
105  
106 =item visit_select_rows()
107  
108 $sqldb->visit_select_rows( $code_ref, %sql_clauses ) : @results
109 $sqldb->visit_select_rows( %sql_clauses, $code_ref ) : @results
110  
111 Unless passed a "union" argument pair, simply calls the superclass method.
112 Runs each of the provided queries separately and concatenates their results.
113  
114 =item fetchsub_select()
115  
116 $sqldb->fetchsub_select( %sql_clauses ) : $coderef
117  
118 Unless passed a "union" argument pair, simply calls the superclass method.
119 Runs each of the provided queries separately and concatenates their results.
120  
121 To Do: This method doesn't yet munge the column names retrived by the later queries to match the first.
122  
123 =item fetchsub_select_rows()
124  
125 $sqldb->fetchsub_select_rows( %sql_clauses ) : $coderef
126  
127 Unless passed a "union" argument pair, simply calls the superclass method.
128 Runs each of the provided queries separately and concatenates their results.
129  
130 =item sql_union()
131  
132 Calls Carp::confess().
133  
134 =back
135  
136 =cut
137  
138 sub fetch_select {
139 my ( $self, %clauses ) = @_;
140 my $union = delete $clauses{'union'}
141 or return $self->NEXT('fetch_select', %clauses);
142  
143 my ( $union_rows, $union_columns );
144 foreach my $query ( @$union ) {
145 my ( $rows, $columns ) = $self->fetch_select_rows(
146 ( ref($query) eq 'ARRAY' ) ? @$query : %$query );
147  
148 # use Data::Dumper;
149 # warn "No union hashes: " . Dumper( $rows );
150 # warn "No union cols: " . Dumper( $columns );
151  
152 push @$union_rows, @$rows;
153 $union_columns ||= $columns;
154 }
155
156 my @colnames = map { $_->{name} } @$union_columns;
157
158 my $union_hashes = [
159 map { my %hash; @hash{ @colnames } = @$_; \%hash } @$union_rows
160 ];
161
162 wantarray ? ( $union_hashes, $union_columns ) : $union_hashes;
163 }
164  
165 sub fetch_select_rows {
166 my ( $self, %clauses ) = @_;
167 my $union = delete $clauses{'union'}
168 or return $self->NEXT('fetch_select_rows', %clauses );
169  
170 my ( $union_rows, $union_columns );
171 foreach my $query ( @$union ) {
172 my ( $rows, $columns ) = $self->fetch_select_rows(
173 ( ref($query) eq 'ARRAY' ) ? @$query : %$query );
174  
175 use Data::Dumper;
176 # warn "No union rows: " . Dumper( $rows );
177 # warn "No union cols: " . Dumper( $columns );
178  
179 push @$union_rows, @$rows;
180 $union_columns ||= $columns;
181 }
182 wantarray ? ( $union_rows, $union_columns ) : $union_rows;
183 }
184  
185 sub visit_select {
186 my $self = shift;
187 my $code = ( ref($_[0]) ? shift : pop );
188 my %clauses = @_;
189  
190 my $union = delete $clauses{'union'}
191 or return $self->NEXT('visit_select', $code, %clauses );
192  
193 my @results;
194 foreach my $query ( @$union ) {
195  
196 # INCOMPLETE -- this should mangle the column names to match first query
197  
198 push @results, $self->visit_select( $code,
199 ( ref($query) eq 'ARRAY' ) ? @$query : %$query );
200 }
201 @results;
202 }
203  
204 sub visit_select_rows {
205 my $self = shift;
206 my $code = ( ref($_[0]) ? shift : pop );
207 my %clauses = @_;
208  
209 my $union = delete $clauses{'union'}
210 or return $self->NEXT('visit_select_rows', $code, %clauses );
211  
212 my @results;
213 foreach my $query ( @$union ) {
214 push @results, $self->visit_select_rows( $code,
215 ( ref($query) eq 'ARRAY' ) ? @$query : %$query );
216 }
217 @results;
218 }
219  
220 sub fetchsub_select {
221 my $self = shift;
222 my %clauses = @_;
223  
224 my $union = delete $clauses{'union'}
225 or return $self->NEXT('fetchsub_select', %clauses );
226 my @queries = @$union;
227 my $subsub;
228
229 # INCOMPLETE -- this should mangle the column names to match first query
230
231 sub {
232 UNIONSUB: {
233 $subsub ||= $self->fetchsub_select( shift @queries or return );
234 &$subsub( @_ ) or ( undef($subsub), redo UNIONSUB )
235 }
236 }
237 }
238  
239 sub fetchsub_select_rows {
240 my $self = shift;
241 my %clauses = @_;
242  
243 my $union = delete $clauses{'union'}
244 or return $self->NEXT('fetchsub_select_rows', %clauses );
245 my @queries = @$union;
246 my $subsub;
247
248 sub {
249 UNIONSUB: {
250 $subsub ||= $self->fetchsub_select_rows( shift @queries or return );
251 &$subsub( @_ ) or ( undef($subsub), redo UNIONSUB )
252 }
253 }
254 }
255  
256 sub sql_union { confess("Union unsupported on this platform") }
257  
258 ########################################################################
259  
260 =head1 SEE ALSO
261  
262 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
263  
264 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
265 this distribution, including installation and license information.
266  
267 =cut
268  
269 ########################################################################
270  
271 1;
272