Template/Command.pm untested

filter: Statementstmt 0.0% 0 / 264 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 53 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 457 CC 36 SCAR 30.7
CC
36
Cov
0%
new_from
132.0
new
12.0
_iterate_childs
12.0
CRAP 21.5
1 package SQL::Template::Command;
2  
3 =head1 NAME
4  
5 SQL::Template::Command - Commands supported by SQL::Template
6  
7 =cut
8  
9 use strict;
10  
11  
12 sub new_from {
13 my $p = shift;
14 my $type = $p->{XMLTYPE};
15 my $params = $p->{PARAMS};
16 my $parent = $p->{PARENT};
17 my $previous = $p->{PREVIOUS};
18 my $container = $p->{CONTAINER};
19
20 my $command;
21
22 $command = SQL::Template::Command::Sql->new($params, $container, $parent, $previous) if( $type eq 'st:sql' );
23 $command = SQL::Template::Command::Select->new($params, $container, $parent, $previous) if( $type eq 'st:select' );
24 $command = SQL::Template::Command::Do->new($params, $container, $parent, $previous) if( $type eq 'st:do' );
25 $command = SQL::Template::Command::List->new($params, $container, $parent, $previous) if( $type eq 'st:list' );
26 $command = SQL::Template::Command::If->new($params, $container, $parent, $previous) if( $type eq 'st:if' );
27 $command = SQL::Template::Command::Else->new($params, $container, $parent, $previous) if( $type eq 'st:else' );
28 $command = SQL::Template::Command::Param->new($params, $container, $parent, $previous) if( $type eq 'st:param' );
29 $command = SQL::Template::Command::Fragment->new($params, $container, $parent, $previous) if( $type eq 'st:fragment' );
30 $command = SQL::Template::Command::Include->new($params, $container, $parent, $previous) if( $type eq 'st:include' );
31
32 if( $command ) {
33 ##print "new command: ", ref($command), " parent=", ref($parent), "\n";
34 return $command;
35 }
36
37 die "Unknown command: $type";
38 }
39  
40 sub new {
41 my ($class, $params, $container, $parent, $previous) = @_;
42 my $self = bless {
43 _PARENT => $parent,
44 _LEVEL => 0,
45 _COMMANDS => [],
46 _DYNAMIC => 0,
47 _SQL => '',
48 _CONTAINER => $container,
49 _PREVIOUS => $previous
50 }, $class;
51 if( 'HASH' eq ref($params) ) {
52 foreach my $key( keys %$params ) {
53 $self->{uc($key)} = $params->{$key};
54 }
55 }
56 $parent->add_command($self) if( $parent );
57 #$self->previous($previous);
58 return $self;
59 }
60  
61 sub name {
62 return $_[0]->{NAME};
63 }
64  
65 sub container {
66 return $_[0]->{_CONTAINER};
67 }
68  
69 sub dynamic {
70 if( @_ == 2 ) {
71 $_[0]->{_DYNAMIC} = $_[1];
72 }
73 return $_[0]->{_DYNAMIC};
74 }
75  
76 sub level {
77 if( @_ == 2 ) {
78 $_[0]->{_LEVEL} = $_[1];
79 }
80 return $_[0]->{_LEVEL};
81 }
82  
83 sub previous {
84 return $_[0]->{_PREVIOUS};
85 }
86  
87 sub parent {
88 my $self = shift;
89 my $parent = shift;
90 if( eval { $parent->isa("SQL::Template::Command") } ) {
91 $self->{_PARENT} = $parent;
92 $self->level( 1 + $parent->level() );
93 }
94 return $self->{_PARENT};
95 }
96  
97 sub add_command {
98 my $self = shift;
99 my $command = shift;
100 die "Invalid command: $command" if( !$command or eval {!$command->isa('SQL::Template::Command')} );
101 $command->parent($self);
102 $command->container( $self->container );
103 ##print "add command: ", ref($command), " level: ", $command->level,
104 ## " parent=", ref($self), " container=", ref($self->container), "\n";
105 push @{ $self->{_COMMANDS} }, $command;
106 }
107  
108 sub bindings {
109 my $self = shift;
110 my $params = shift;
111 my $bindings = shift || {};
112
113 $self->_iterate_childs(
114 sub {
115 my $command = shift;
116 $command->bindings($params, $bindings);
117 return 1;
118 });
119
120 return $bindings;
121 }
122  
123 sub sql {
124 my ($self, $params) = @_;
125 my $sql = $self->{_SQL};
126
127 if( $self->dynamic or ($self->{_SQL} eq '') ) {
128 $sql = '';
129 $self->_iterate_childs(
130 sub {
131 my $command = shift;
132 $sql .= $command->sql($params, $sql);
133 return 1;
134 });
135 $self->{_SQL} = $sql;
136 }
137 return $sql;
138 }
139  
140 sub get_commands {
141 return @{ $_[0]->{_COMMANDS} };
142 }
143  
144 sub find_command {
145 my $self = shift;
146 my $name = shift;
147 my $find = undef;
148 $self->_iterate_childs(
149 sub {
150 my $command = shift;
151 if( $name eq $command->name ) {
152 $find = $command;
153 return 0;
154 }
155 return 1;
156 });
157 return $find;
158 }
159  
160  
161  
162 sub dump {
163 my $self = shift;
164 my $padding = ' ' x $self->level();
165 my $str;
166 map {$str.= "$_: $self->{$_}; ";} grep /^[^_]/, keys(%$self);
167 print $padding, ref($self), " $str\n";
168  
169 foreach my $command($self->get_commands) {
170 $command->dump;
171 }
172 }
173  
174 sub _iterate_childs {
175 my $self = shift;
176 my $visit_sub = shift;
177 if( $visit_sub ) {
178 foreach my $command($self->get_commands) {
179 my $continue = &$visit_sub($command);
180 last if !$continue;
181 }
182 }
183
184 }
185  
186 sub _iterate_parent_chain {
187 my $self = shift;
188 my $visit_sub = shift;
189 if( $visit_sub ) {
190 while( my $next_parent = $self->parent ) {
191 my $continue = &$visit_sub($next_parent);
192 last if !$continue;
193 }
194 }
195 }
196  
197 #******************************************************************************
198  
199 package SQL::Template::Command::Composite;
200 use base 'SQL::Template::Command';
201  
202 sub new {
203 my ($class, $params, $container, $parent, $previous) = @_;
204 return $class->SUPER::new($params, $container, $parent, $previous);
205 }
206  
207  
208 #******************************************************************************
209  
210 package SQL::Template::Command::Leaf;
211 use base 'SQL::Template::Command';
212  
213 sub new {
214 my ($class, $params, $container, $parent, $previous) = @_;
215 return $class->SUPER::new($params, $container, $parent, $previous);
216 }
217  
218 #******************************************************************************
219  
220 package SQL::Template::Command::Sql;
221 use base 'SQL::Template::Command::Composite';
222  
223 #******************************************************************************
224  
225 package SQL::Template::Command::Sentence;
226 use base 'SQL::Template::Command::Composite';
227  
228 #******************************************************************************
229  
230 package SQL::Template::Command::Select;
231 use base 'SQL::Template::Command::Sentence';
232  
233 sub new {
234 my ($class, $params, $container, $parent, $previous) = @_;
235 die "name parameter is mandatory" if( !exists $params->{name} );
236 return $class->SUPER::new($params, $container, $parent, $previous);
237 }
238  
239 #******************************************************************************
240  
241 package SQL::Template::Command::Do;
242 use base 'SQL::Template::Command::Sentence';
243  
244  
245 sub new {
246 my ($class, $params, $container, $parent, $previous) = @_;
247 die "name parameter is mandatory" if( !exists $params->{name} );
248 return $class->SUPER::new($params, $container, $parent, $previous);
249 }
250  
251 #******************************************************************************
252  
253 package SQL::Template::Command::Fragment;
254 use base 'SQL::Template::Command::Composite';
255  
256  
257 sub new {
258 my ($class, $params, $container, $parent, $previous) = @_;
259 die "name parameter is mandatory" if( !exists $params->{name} );
260 return $class->SUPER::new($params, $container, $parent, $previous);
261 }
262  
263 sub clone {
264 my $self = shift;
265 my $fragment = {};
266 foreach my $key( keys %$self ) {
267 $fragment->{$key} = $self->{$key};
268 }
269 bless $fragment, __PACKAGE__;
270 return $fragment;
271 }
272  
273  
274 #******************************************************************************
275  
276 package SQL::Template::Command::Text;
277 use base 'SQL::Template::Command::Leaf';
278  
279 sub new {
280 my ($class, $params, $container, $parent, $previous) = @_;
281 my $self = $class->SUPER::new($params, $container, $parent, $previous);
282 #Sustitution of params
283 #$self->{TEXT} =~ s!\${\w+}\b!#$1#!g;
284 return $self;
285 }
286  
287 sub bindings {
288 my $self = shift;
289 my $params = shift;
290 my $bindings = shift || {};
291 my @matches = $self->sql =~ /\$\{\s*(\w+)\s*\}/g;
292 map { $bindings->{'${' . $_ . '}'} = $params->{$_}; } @matches;
293 return $bindings;
294 }
295  
296 sub sql {
297 my ($self, $params, $sql) = @_;
298 return " " . $self->{TEXT};
299 }
300  
301 #******************************************************************************
302  
303 package SQL::Template::Command::List;
304 use base 'SQL::Template::Command::Leaf';
305  
306 sub new {
307 my ($class, $params, $container, $parent, $previous) = @_;
308 return $class->SUPER::new($params, $container, $parent, $previous);
309 }
310  
311 sub _get_array_param {
312 my ($self, $params) = @_;
313 my $pname = $self->name;
314 $pname =~ s!\$\{(\w+)\}!$1!;
315 my $array = $params->{ $pname };
316 die "parameter $pname must be an array reference with 1 or more elements" if( "ARRAY" ne ref($array) or @$array<1);
317 return @$array
318 }
319  
320 sub bindings {
321 my $self = shift;
322 my $params = shift;
323 my $bindings = shift || {};
324 my @array = $self->_get_array_param($params);
325 my $i = 0;
326 map {$i++; $bindings->{ $self->name . "_$i"} = $_;} @array;
327 return $bindings;
328 }
329  
330 sub sql {
331 my ($self, $params) = @_;
332
333 my @array = $self->_get_array_param($params);
334 my $i = 0;
335 my $sql = " (" . join(", ", map {$i++; $self->name . "_$i"} @array) . ") ";
336 }
337  
338 #******************************************************************************
339  
340 package SQL::Template::Command::If;
341 use base 'SQL::Template::Command::Composite';
342  
343 sub new {
344 my ($class, $params, $container, $parent, $previous) = @_;
345 my $self = $class->SUPER::new($params, $container, $parent, $previous);
346 $self->_iterate_parent_chain(
347 sub {
348 #Set the parent chain to dynamic for performance purposes
349 my $parent = shift;
350 $parent->dynamic(1);
351 return $parent->isa('SQL::Template::Command::Sentence') ? 0 : 1
352 } );
353 return $self
354 }
355  
356 sub _test {
357 my $self = shift;
358 my $params = shift;
359 my $test = $self->{TEST};
360 no warnings 'uninitialized';
361 $test =~ s!\$\{\s*(\w+)\s*\}!if( defined($1) && exists($params->{$1}) ) {$params->{$1}} else {undef}!ge;
362 my $rc = eval "$test;";
363 return $rc;
364 }
365  
366 sub sql {
367 my ($self, $params) = @_;
368 return "" if( ! $self->_test($params) );
369 my $sql;
370 foreach my $command($self->get_commands) {
371 $sql .= $command->sql($params);
372 }
373 $sql = " ($sql) ";
374
375 if( exists $self->{PREPEND} ) {
376 $sql = " " . $self->{PREPEND} . $sql;
377 }
378
379 return $sql;
380 }
381  
382 #******************************************************************************
383  
384 package SQL::Template::Command::Else;
385 use base 'SQL::Template::Command::Composite';
386  
387 sub new {
388 my ($class, $params, $container, $parent, $previous) = @_;
389 my $self = $class->SUPER::new($params, $container, $parent, $previous);
390 unless( eval { $self->previous->isa('SQL::Template::Command::If'); } ) {
391 die "sm:else command must be after a sm:if command";
392 }
393 return $self;
394 }
395  
396 sub sql {
397 my ($self, $params) = @_;
398 return "" if( $self->previous->_test($params) );
399 my $sql;
400 foreach my $command($self->get_commands) {
401 $sql .= $command->sql($params);
402 }
403 if( exists $self->previous->{PREPEND} ) {
404 $sql = " " . $self->previous->{PREPEND} . " ($sql) ";
405 }
406 else {
407 $sql = " ($sql) ";
408 }
409 return $sql;
410 }
411  
412 #******************************************************************************
413  
414 package SQL::Template::Command::Param;
415 use base 'SQL::Template::Command';
416  
417 sub new {
418 my ($class, $params, $container, $parent, $previous) = @_;
419 my $self = $class->SUPER::new($params, $container, $parent, $previous);
420 $self->{NAME} =~ s!(:\w+)\b!#$1#!g;
421 return $self;
422 }
423  
424 sub bindings {
425 my $self = shift;
426 my $params = shift;
427 my $bindings = shift || {};
428 $self->name =~ /^#:(\w+)#/;
429 my $key = $1;
430 if( (! exists($bindings->{"#:$1#"})) or (ref($bindings->{"#:$1#"}) ne "ARRAY") ) {
431 $bindings->{"#:$1#"} = [$params->{$1}, $self->{TYPE} ];
432 }
433 return $bindings;
434 }
435  
436 sub sql {
437 my ($self, $params, $sql) = @_;
438 return $self->name;
439 }
440  
441 #******************************************************************************
442  
443 package SQL::Template::Command::Include;
444 use base 'SQL::Template::Command';
445  
446 sub new {
447 my ($class, $params, $container, $parent, $previous) = @_;
448 die "name parameter is mandatory" if( !exists $params->{name} );
449 my $fragment = $parent->container->find_command($params->{name});
450 die "fragment '" . $params->{name} . "' not found" if( !$fragment );
451
452 my $include = $fragment->clone();
453 $parent->add_command($include);
454 return $include;
455 }
456  
457  
458  
459 #******************************************************************************
460 #******************************************************************************
461  
462 1;