MyParseX/Item.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 138 CC 10 SCAR 26.1
CC
10
Cov
0%
renameTable
42.0
getItems
12.0
renameTablex
12.0
CRAP 13.5
1 package DBIx::MyParseX::Item;
2 our $VERSION = '0.06';
3
4 use 5.008008;
5 use base 'DBIx::MyParse::Item';
6 use DBIx::MyParse;
7 use DBIx::MyParse::Item;
8 use DBIx::MyParseX;
9  
10  
11 1;
12  
13  
14 # ---------------------------------------------------------------------
15 # package DBIx::MyParse::Item
16 # WE set the package to 'DBIx::MyParse::Item' since this package
17 # provides ONLY extension methods and no methods in its own namespace
18 #
19 # Items:
20 # FUNC_ITEM, SUM_FUNC_ITEM (c) Yes. getFuncArgs
21 # getType
22 # getFuncType
23 # getFuncName, hasArguments, getFuncArgs
24 # is a collection on args
25 #
26 # FIELD_ITEM, getFieldName
27 #
28 # REF_ITEM
29 # TABLE_ITEM getTableName
30 #
31 #
32 # JOIN_ITEM*(C)
33 # getJoinItems: SUBSELECT_ITEM,JOIN_ITEM,TABLE_ITEM
34 # getJoinFields
35 #
36 # SUBSELECT_ITEM*
37 # getSubselectQuery
38 #
39 # 'STRING_ITEM', 'INT_ITEM', 'DECIMAL_ITEM', 'REAL_ITEM' and 'VARBIN_ITEM'
40 # getValue()
41 #
42 # $query->each( ITEM_TYPE, function, args );
43 # FIELD_ITEM, rename, old_name, new_name
44 # There is a function that will walk the tree
45 # $query->action( CLAUSE | ITEM level, function call, args )
46 # action( CLAUSE, function(
47 package DBIx::MyParse::Item;
48  
49 use strict;
50 use warnings;
51 use Carp;
52  
53 use List::MoreUtils qw( any );
54 use Perl6::Say;
55 use YAML;
56 use self;
57 use Data::Dumper;
58  
59 # HASHREF to contain the methods for getting the children based on
60 # ITEM_TYPE => sub { .. }
61 # These should be the only items that should have children.
62 # These Items can have subitems.
63 my $getChildrenFor = {
64 FUNC_ITEM => sub { getArguments( @_ ) } ,
65 SUM_FUNC_ITEM => sub { getArguments( @_ ) } ,
66 JOIN_ITEM => sub { getJoinItems( @_ ) } ,
67 SUBSELECT_ITEM => sub { getSubselectQuery( @_ ) } ,
68 };
69  
70  
71 # ---------------------------------------------------------------------
72 # _string_to _method
73 # converts a method_name to a closure
74 # ---------------------------------------------------------------------
75 sub _string_to_method {
76  
77 my ( $item, $method_name, $args ) = @_;
78  
79 $method_name = __PACKAGE__ ."::$method_name" ;
80 my $eval_string = 'sub { $_[0]->' . $method_name . '( @_[ 1 .. $#_ ] ) }';
81 my $method = eval( $eval_string );
82  
83 return $method;
84  
85 }
86  
87  
88  
89 # ---------------------------------------------------------------------
90 # getItems
91 # with DBIx::MyParseX::getItems returns a collapsed list of all
92 # DBIx::MyParse::Items from the query tree
93 # ---------------------------------------------------------------------
94 sub getItems {
95  
96 my $type = self->getItemType();
97 my @items; # Array of items-refs to return
98
99 # COLLECTION?
100 if ( my $getChildren = $getChildrenFor->{ $type } ) {
101  
102 foreach my $subitem ( @{ self->$getChildren } ) {
103  
104 if (
105 ref( $subitem ) eq 'DBIx::MyParse::Item' or
106 ref( $subitem ) eq 'DBIx::MyParse::Query'
107 ) {
108 push( @items, $subitem->getItems )
109 }
110  
111 }
112  
113 }
114  
115 # JUST A PLAIN ITEM
116 else {
117 push @items, self;
118 };
119  
120 return @items;
121  
122 } # END SUB getItems
123  
124  
125  
126 # ----------------------------------------------------------------------
127 # SUB: renameTable
128 # USAGE: $table_item->rename( $new_name )
129 #
130 # No return value, exist solely for it's side-effects. Case switches
131 # based on the type of item.
132 #
133 # TODO:
134 # x Generalize to any DBIx::MyParse::Item?
135 # - Handle subquery objects
136 #
137 # ----------------------------------------------------------------------
138
139  
140 sub renameTable {
141  
142 # my $item = shift;
143  
144 # say "@_";
145 my ( $old_table_name, $new_table_name ) = args;
146  
147 # TRAP non DBIx::MyParse::Items
148 Carp( "Cannot renameTable for non-DBIx::MyParse::Item" )
149 if ( ref self ne 'DBIx::MyParse::Item' );
150  
151 # CASE-SWITCH on DBIx::MyParse::Item::Type
152  
153 my $type = self->getItemType;
154  
155 # ----------------------------------------------------------
156 # CASE: JOIN_ITEM
157 # JOIN_ITEMs contains more than one table therefore, we
158 # recurse on each subitem.
159 # ----------------------------------------------------------
160 if ( $type eq 'JOIN_ITEM' ) {
161  
162 foreach my $join_item ( @{ self->getJoinItems } ) {
163
164 $join_item->renameTable( $old_table_name, $new_table_name );
165  
166 }
167  
168 } # END CASE: JOIN_ITEM
169  
170  
171 # ----------------------------------------------------------
172 # CASE: FUNC_ITEM, COND_ITEM, COND_AND_FUNC
173 # similar to JOIN_ITEM. Dispatch on getArguments
174 # ----------------------------------------------------------
175 if (
176 any { $type eq $_ }
177 qw( FUNC_ITEM COND_ITEM COND_AND_FUNC )
178 ) {
179
180 foreach my $arg ( @{ self->getArguments } ) {
181  
182 $arg->renameTable( $old_table_name, $new_table_name );
183  
184 }
185
186 } # END CASE: FUNC_ITEM
187
188
189 # ----------------------------------------------------------
190 # CASE: TABLE_ITEM, FIELD_ITEM
191 # match on regular expression match
192 # ----------------------------------------------------------
193 if (
194 any { $type eq $_ } qw( TABLE_ITEM FIELD_ITEM )
195 ) {
196
197 # TEST for match on old table name.
198 # TableName must exist and match for it to be changed otherwise ...
199 # there is nothing to change
200 if (
201 self->getTableName &&
202 self->getTableName =~ m/$old_table_name/
203 ) {
204  
205 self->setTableName( $new_table_name );
206  
207 }
208  
209 } # END CASE: TABLE_ITEM
210  
211 } # END SUB: renameTable
212  
213  
214 # The problem is that the item can be a collection of items or a single item
215 # map { } self->getItems
216 # item->doMethod( 'method_name', args );
217 # item->$_[1]( @[2,.] );
218 sub renameTablex {
219  
220 my $type = self->getItemType;
221 my ( $old_table_name, $new_table_name ) = args;
222  
223
224 if ( any { $type eq $_ } qw( TABLE_ITEM FIELD_ITEM ) ) {
225  
226 if (
227 self->getTableName &&
228 self->getTableName =~ m/$old_table_name/
229 ) {
230 self->setTableName( $new_table_name );
231 }
232
233 }
234  
235 } # renameTable
236  
237 1;
238 __END__