File Coverage

blib/lib/DBIx/MyParseX/Query.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package DBIx::MyParseX::Query;
2             our $VERSION = '0.06';
3 1     1   1368 use base 'DBIx::MyParse::Query';
  1         2  
  1         719  
4             1;
5              
6             # ---------------------------------------------------------------------
7             # package DBIx::MyParse::Query
8             # WE set the package to 'DBIx::MyParse::Query' since this package
9             # provides ONLY extension methods and no methods in its own namespace
10             #
11             package DBIx::MyParse::Query;
12              
13             use 5.008008;
14             use strict;
15             use warnings;
16             use DBIx::MyParse;
17             use DBIx::MyParse::Query;
18             use DBIx::MyParseX;
19             use Perl6::Say;
20             # use DBIx::MyParseX::Item;
21             use List::MoreUtils qw(any);
22             use self;
23            
24             # ---------------------------------------------------------------------
25             # CLAUSES :
26             # getChildrenFor: methods for returning the children of a clause
27             # ---------------------------------------------------------------------
28             my $getChildrenFor = {
29             SELECT => sub { getSelectItems( @_ ) } ,
30             WHERE => sub { getWhere( @_ ) } ,
31             HAVING => sub { getHaving( @_ ) } ,
32             ORDER => sub { getOrder( @_ ) } ,
33             LIMIT => sub { getLimit( @_ ) } ,
34             GROUP => sub { getGroup( @_ ) } ,
35              
36             TABLES => sub { getTables( @_ ) } ,
37             FROM => sub { getTables( @_ ) } ,
38              
39             TEST => sub { print "testing" ; }
40             };
41              
42              
43             my @clauses = qw( SELECT FROM WHERE GROUP HAVING ORDER LIMIT ) ;
44              
45             sub getFrom { getTables(@_) }; # Alias for getTables;
46              
47              
48             # ---------------------------------------------------------------------
49             # Test Methods :
50             # Indicates if the Query has one of the following clauses
51             # ---------------------------------------------------------------------
52             sub hasSelect { return 1 if ( self->getSelectItems ); return 0 };
53             sub hasWhere { return 1 if ( self->getWhere ); return 0 };
54             sub hasHaving { return 1 if ( self->getHaving ); return 0 };
55             sub hasOrder { return 1 if ( self->getOrder ); return 0 };
56             sub hasLimit { return 1 if ( self->getLimit ); return 0 };
57              
58             sub hasTables { return 1 if ( self->getTables ); return 0};
59             sub hasTable { return 1 if ( self->getTables ); return 0};
60             sub hasFrom { return 1 if ( self->getTables ); return 0};
61             sub hasGroup { return 1 if ( self->getGroup ); return 0};
62              
63              
64             # Queries have different clauses.
65             # Clauses have different items.
66             # CLAUSES
67             # SELECT getSelectItems ARRAY[ITEMS]
68             # TABLE getTables ARRAY[ITEMS] getType
69             # WHERE, HAVING getWhere, getHaving ITEM[TREE]
70             # GROUP getGroup ARRAY[ITEM]
71             # ORDER getOrder ARRAY[ITEM]
72             # LIMIT getLimit ARRAY[ITEM,ITEM]
73             #
74              
75             # ---------------------------------------------------------------------
76             # SUB ROUTINE getItems
77             # Return an array of refs to the items from the query
78             #
79             # This routine flattens out the parse tree.
80             #
81             # ---------------------------------------------------------------------
82             sub getItems {
83              
84             my ( $q ) = @_;
85             my @items; # array to contain the query items;
86              
87             foreach my $clause ( @clauses ) {
88              
89             # CLAUSE
90             my $method = $getChildrenFor->{ $clause };
91             foreach my $child ( $q->$method ) { # Iterate children
92              
93             # ITEM or QUERY
94             if (
95             ref $child eq 'DBIx::MyParse::Item'
96             or ref $child eq 'DBIx::MyParse::Query'
97             ) {
98             # push @items , $child->getItems( @_[ 1..$#_ ] ) ;
99             push @items , $child->getItems( args ) ;
100             }
101              
102             # ARRAY REF
103             elsif ( ref $child eq 'ARRAY' ) {
104            
105             foreach my $element ( @$child ) {
106            
107             if (
108             ref $element eq 'DBIx::MyParse::Item' or
109             ref $element eq 'DBIx::MyParse::Query'
110             ) {
111             push @items, $element->getItems( args );
112             } else {
113             carp( "Non-DBIx::Parse object encountered in Parsed Query" );
114             }
115             }
116             } # END ARRAYREF
117              
118             # ARRAY?
119             # else {
120             #
121             # use YAML;
122             # # print Dump $child;
123             #
124             # }
125              
126             } # Iterate children of clause
127              
128             } # Iterate clause
129              
130             return @items;
131              
132             } # END SUB: getItems
133            
134            
135             # --------------------------------------------------------------------
136             # SUB: renameTable
137             # package: DBIx::MyParse::Query
138             # Usage:
139             # $q->renameTable( old_name, new_name )
140             #
141             # Given a query, will rename all the tables with the new name
142             # has no return value, exists for the side-effects.
143             #
144             # --------------------------------------------------------------------
145             sub renameTable {
146              
147             carp( "A non DBIx:;MyParse::Query Object was passed to renameTable()" )
148             if ( ref self ne 'DBIx::MyParse::Query' );
149            
150             map { $_->renameTable( args ) } self->getItems;
151              
152             return 1;
153            
154             } # END sub: renameTable
155              
156              
157              
158              
159             1;
160              
161              
162             __END__