XML/Template/Element/DB.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 66 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 231 SCAR 17.9
CC
2
Cov
0%
AUTOLOAD
6.0
select
2.0
update
2.0
CRAP 6.0
1 ###############################################################################
2 # XML::Template::Element::DB
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Element::DB;
11 use base qw(XML::Template::Element XML::Template::Element::Iterator);
12  
13 use strict;
14 use XML::Template::Element::Iterator;
15 use IO::String;
16  
17 use vars qw($AUTOLOAD);
18  
19  
20 =pod
21  
22 =head1 NAME
23  
24 XML::Template::Element::DB - XML::Template module that implements the SQL
25 tagset.
26  
27 =head1 SYNOPSIS
28  
29 This XML::Template module implements the SQL tagset. XML::Template plugin
30 modules that query SQL databases should be derived from this module. The
31 database and table to query are associated with the namespace of the tags
32 and is specified in the XML::Template configuration file (see
33 L<XML::Template::Config>).
34  
35 =head1 CONSTRUCTOR
36  
37 XML::Template::Element::Block inherits its constructor method, C<new>,
38 from L<XML::Template::Element>.
39  
40 =head1 SQL TAGSET METHODS
41  
42 =head2 select
43  
44 This method implements a SELECT SQL query on the database and table
45 associated with the tag's namespace. If this tag is nested in a related
46 namespace, the column will be selected via an intermediate mapping
47 database table, defined in the XML::Template configuration file. For
48 instance, suppose the following XML is parsed:
49  
50 <xml xmlns:group="http://syrme.net/xml-template/group/v1"
51 xmlns:item="http://syrme.net/xml-template/item/v1">
52 <group:select name="group1">
53 <item:select fields="*" name="item1">
54 ...
55 </item:select>
56 </group:select>
57 </xml>
58  
59 The relationship table that maps items to groups should be defined in the
60 XML::Template configuration file. If the relation table is group2item,
61 the following SQL would be generated for the item tag:
62  
63 SELECT * FROM items,group2item
64 WHERE items.itemname=group2item.itemname
65 AND group2item.groupname='group1'
66 AND group2item.itemname='item1'
67  
68 The following attributes are used:
69  
70 =over 4
71  
72 =item name
73  
74 A comma-separated list of the names of the primary keys of the database
75 column to select. The primary keys and their order is specified in the
76 XML::Template configuration file. This attribute is not required.
77  
78 =item fields, field
79  
80 A comma separated list of the database table fields to return. To return
81 all fields, use '*'. For each field returned a variable will be set with
82 the field's name and value. These variables will be available in the
83 content of the select element.
84  
85 =back
86  
87 Remaining attributes will be used to constrain the selection. For
88 instance, the element
89  
90 <block:select name="block1" fields="*"
91 title="Title" description="Desc"/>
92  
93 will result in the following SQL query
94  
95 SELECT * FROM blocks
96 WHERE blockname='block1'
97 AND title='Title'
98 AND description='Desc'
99  
100 The value of a remaining attribute may be a comma-separated list, in which
101 case, each element in the list is combined into an OR clause. So the
102 following element:
103  
104 <block:select name="block1" fields="*" title="Title,Title2"/>
105  
106 would produce the following SQL query:
107  
108 SELECT * FROM blocks
109 WHERE blockname='block1'
110 AND (title='Title' OR title='Title2')
111  
112 In addition, if the value of a remaining attribute contains a '%', the
113 LIKE comparison will be used rather than =. For instance,
114  
115 <block:select fields="blockname,body" title="%Title%"/>
116  
117 would produce the following SQL query:
118  
119 SELECT blockname,body FROM blocks
120 WHERE title LIKE '%Title%'
121  
122 =cut
123  
124 sub select {
125 my $self = shift;
126 my ($code, $attribs) = @_;
127  
128 # Get attribs.
129 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
130 my $fields = $self->get_attrib ($attribs, ['fields', 'field']) || 'undef';
131  
132 # Generate named params assignments for the remaining atttribs.
133 my $attribs_named_params = $self->generate_named_params ($attribs);
134  
135 # Get database info.
136 my $namespace = $self->namespace ();
137 my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace);
138 my $dbname = $source_mapping_info->{source};
139 my $table = $source_mapping_info->{table};
140 my $keys = $source_mapping_info->{keys};
141  
142 my $outcode = qq!
143 do {
144 \$vars->create_context ();
145  
146 my \%attribs = ($attribs_named_params);
147 \$vars->set (\%attribs);
148  
149 my \$tables = '$table';
150 my \$where;
151  
152 if (defined $name) {
153 my \@names = split (/\\s*,\\s*/, $name);
154 my \$i = 0;
155 foreach my \$key (split (',', '$keys')) {
156 \$where .= ' and ' if defined \$where;
157 \$where .= "$table.\$key='\$names[\$i]'";
158 \$i++;
159 }
160 }
161 my \$twhere = \$process->generate_where (\\\%attribs, '$table');
162 if (defined \$where && defined \$twhere) {
163 \$where .= " and \$twhere";
164 } elsif (defined \$twhere) {
165 \$where = \$twhere;
166 }
167  
168 no strict;
169 my \$parent_namespace = \$__parent_namespace;
170 my \@parent_names = \@__parent_names;
171 use strict;
172  
173 if (defined \$parent_namespace) {
174 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
175 if (defined \$parent_namespace_info->{relation} &&
176 defined \$parent_namespace_info->{relation}->{'$namespace'}) {
177 my \$rtable = \$parent_namespace_info->{relation}->{'$namespace'}->{table};
178  
179 \$tables .= ",\$rtable";
180  
181 my \$i = 0;
182 foreach my \$key (split (',', '$keys')) {
183 \$where .= ' and ' if defined \$where;
184 \$where .= "$table.\$key=\$rtable.\$key";
185 \$i++;
186 }
187  
188 \$i = 0;
189 foreach my \$key (split (',', \$parent_namespace_info->{keys})) {
190 \$where .= ' and ' if defined \$where;
191 \$where .= "\$rtable.\$key='\$parent_names[\$i]'";
192 \$i++;
193 }
194 }
195 }
196  
197 my \$result;
198 # if (defined \$where) {
199 my \$db = \$process->get_source ('$dbname');
200 if (defined \$db) {
201 \$result = \$db->select (Table => \$tables,
202 Where => \$where);
203 die XML::Template::Exception->new ('DB', \$db->error ()) if defined \$db->error ();
204 if (defined \$result) {
205 if (defined $fields) {
206 my \$fields = $fields;
207 if (\$fields eq '*') {
208 \$vars->set (\%\$result);
209 } else {
210 foreach my \$field (split (/\\s*,\\s*/, \$fields)) {
211 \$vars->set (\$field => \$result->{\$field});
212 }
213 }
214 }
215 }
216 }
217 # }
218  
219 my \$__parent_namespace = '$namespace';
220 my \@__parent_names;
221 if (defined $name) {
222 \@__parent_names = split (/\\s*,\\s*/, $name);
223 } else {
224 if (defined \$result) {
225 foreach my \$key (split (/\\s*,\\s*/, '$keys')) {
226 push (\@__parent_names, \$result->{\$key});
227 }
228 }
229 }
230  
231 $code
232  
233 \$vars->delete_context ();
234 };
235 !;
236 #print "$outcode";
237  
238 return $outcode;
239 }
240  
241 =pod
242  
243 =head2 update
244  
245 This method implements an UPDATE SQL query on the database and table
246 associated with the tag's namespace. If this tag is nested in a related
247 namespace, the column to be updated will be determined via an intermediate
248 mapping database table, defined in the XML::Template configuration file.
249 See C<select> for more details on related namespaces.
250  
251 The children of this element should be tags with names of the database
252 table columns and their new values. For instance:
253  
254 <xml xmlns:item="http://syrme.net/xml-template/block/v1">
255 <item:update name="item1">
256 <item:title>Title</item:title>
257 <item:description>Description</item:description>
258 </item:update>
259 </xml>
260  
261 These children tags are handled by the AUTOLOAD subroutine.
262  
263 The following attributes are used:
264  
265 =over 4
266  
267 =item name
268  
269 A comma-separated list of the names of the primary keys of the database
270 column to select. The primary keys and their order is specified in the
271 XML::Template configuration file. This attribute is required for
272 updating.
273  
274 =item insert
275  
276 If C<true>, insert a new column in the database table if the one named by
277 the attribute C<name> is not found. The default value is C<false>.
278  
279 =back
280  
281 Remaining attributes will be used to constrain the selection of which
282 column to update. See C<select> for more details.
283  
284 =cut
285  
286 sub update {
287 my $self = shift;
288 my ($code, $attribs) = @_;
289  
290 # Get attribs.
291 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
292 my $insert = $self->get_attrib ($attribs, 'insert') || 'undef';
293  
294 # Generate named params assignments for the remaining atttribs.
295 my $attribs_named_params = $self->generate_named_params ($attribs);
296  
297 # Get database info.
298 my $namespace = $self->namespace ();
299 my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace);
300 my $dbname = $source_mapping_info->{source};
301 my $table = $source_mapping_info->{table};
302 my $keys = $source_mapping_info->{keys};
303  
304 my$outcode = qq!
305 do {
306 \$vars->create_context ();
307  
308 my \%attribs = ($attribs_named_params);
309 \$vars->set (\%attribs);
310  
311 my \$tables = '$table';
312 my (\%values, \$where);
313  
314 if (defined $name) {
315 my \@names = split (/\\s*,\\s*/, $name);
316 my \$i = 0;
317 foreach my \$key (split (',', '$keys')) {
318 \$where .= ' and ' if defined \$where;
319 \$where .= "$table.\$key='\$names[\$i]'";
320 \$i++;
321 }
322 }
323 my \$twhere = \$process->generate_where (\\\%attribs, '$table');
324 if (defined \$where && defined \$twhere) {
325 \$where .= " and \$twhere";
326 } elsif (defined \$twhere) {
327 \$where = \$twhere;
328 }
329 my \$select_where = \$where;
330  
331 no strict;
332 my \$parent_namespace = \$__parent_namespace;
333 my \@parent_names = \@__parent_names;
334 use strict;
335  
336 if (defined \$parent_namespace) {
337 my \$parent_namespace_info = \$process->get_namespace_info (\$parent_namespace);
338 if (defined \$parent_namespace_info->{relatedto} &&
339 defined \$parent_namespace_info->{relatedto}->{'$namespace'}) {
340 \$tables .= ",\$parent_namespace_info->{relatedto}->{'$namespace'}->{table}";
341  
342 my \$i = 0;
343 foreach my \$key (split (',', '$keys')) {
344 \$select_where .= ' and ' if defined \$where;
345 \$select_where .= "$table.\$key=\$parent_namespace_info->{relatedto}->{'$namespace'}->{table}.\$key";
346 \$i++;
347 }
348  
349 \$i = 0;
350 foreach my \$key (split (',', \$parent_namespace_info->{key})) {
351 \$select_where .= ' and ' if defined \$where;
352 \$select_where .= "\$parent_namespace_info->{relatedto}->{'$namespace'}->{table}.\$key='\$parent_names[\$i]'";
353 \$i++;
354 }
355 }
356 }
357  
358 my (\$__parent_namespace, \@__parent_names);
359 if (defined $name) {
360 \$__parent_namespace = '$namespace';
361 \@__parent_names = split (/\\s*,\\s*/, $name);
362 }
363  
364 $code
365  
366 if (defined $name) {
367 my \$db = \$process->get_source ('$dbname');
368 if (defined \$db) {
369 my \$result = \$db->select (Table => \$tables,
370 Where => \$select_where);
371 die XML::Template::Exception->new ('DB', \$db->error ()) if defined \$db->error ();
372 if (defined \$result) {
373 \$db->update (Table => '$table',
374 Values => \\\%values,
375 Where => \$where)
376 || die XML::Template::Exception->new ('DB', \$db->error ());
377 } else {
378 my \$insert = $insert;
379 if (defined \$insert && \$insert =~ /^true\$/i) {
380 if (defined $name) {
381 my \@names = split (/\\s*,\\s*/, $name);
382 my \$i = 0;
383 foreach my \$key (split (',', '$keys')) {
384 \$values{\$key} = \$names[\$i] if \! exists \$values{\$key};
385 \$i++;
386 }
387 }
388 \$db->insert (Table => '$table',
389 Values => \\\%values)
390 || die XML::Template::Exception->new ('DB', \$db->error ());
391  
392 if (defined \$parent_namespace) {
393 my \$parent_namespace_info = \$process->get_namespace_info (\$parent_namespace);
394 if (defined \$parent_namespace_info->{relatedto} &&
395 defined \$parent_namespace_info->{relatedto}->{'$namespace'}) {
396 my \%map_values;
397 foreach my \$key (split (',', '$keys')) {
398 \$map_values{\$key} = \$values{\$key};
399 }
400  
401 my \$i = 0;
402 foreach my \$key (split (',', \$parent_namespace_info->{key})) {
403 \$map_values{\$key} = \$parent_names[\$i];
404 \$i++;
405 }
406  
407 \$db->insert (Table => \$parent_namespace_info->{relatedto}->{'$namespace'}->{'table'},
408 Values => \\\%map_values)
409 || die XML::Template::Exception->new ('DB', \$db->error ());
410 }
411 }
412 }
413 }
414 }
415 }
416 };
417 !;
418  
419 return $outcode;
420 }
421  
422 =pod
423  
424 =head2 insert
425  
426 This method implements an INSERT SQL query on the database and table
427 associated with the tag's namespace. If this tag is nested in a related
428 namespace, the relation table defined in the XML::Template configuration
429 file that maps rows between the two related tables will be updated.
430 See C<select> for more details on related namespaces.
431  
432 The children of this element should be tags with names of the database
433 table columns and their new values. See C<update> for more details.
434  
435 The following attributes are used:
436  
437 =over 4
438  
439 =item name
440  
441 A comma-separated list of the names of the primary keys of the database
442 column to insert. The primary keys and their order is specified in the
443 XML::Template configuration file. This attribute is required for
444 inserting.
445  
446 =back
447  
448 =cut
449  
450 sub insert {
451 my $self = shift;
452 my ($code, $attribs) = @_;
453  
454 # Get attribs.
455 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
456  
457 # Get database info.
458 my $namespace = $self->namespace ();
459 my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace);
460 my $dbname = $source_mapping_info->{source};
461 my $table = $source_mapping_info->{table};
462 my $keys = $source_mapping_info->{keys};
463  
464 my $outcode = qq!
465 do {
466 \$vars->create_context ();
467  
468 my (\%values, \$where);
469  
470 if (defined $name) {
471 my \@names = split (/\\s*,\\s*/, $name);
472 my \$i = 0;
473 foreach my \$key (split (',', '$keys')) {
474 \$values{\$key} = \$names[\$i];
475 \$where .= ' and ' if defined \$where;
476 \$where .= "$table.\$key='\$names[\$i]'";
477 \$i++;
478 }
479 }
480  
481 my (\$parent_namespace, \@parent_names);
482 no strict;
483 \$parent_namespace = \$__parent_namespace;
484 \@parent_names = \@__parent_names;
485 use strict;
486  
487 my (\$__parent_namespace, \@__parent_names);
488 if (defined $name) {
489 \$__parent_namespace = '$namespace';
490 \@__parent_names = split (/\\s*,\\s*/, $name);
491 }
492  
493 $code
494  
495 my \$db = \$process->get_source ('$dbname');
496 if (defined \$db) {
497 if (\! \$db->insert (Table => '$table',
498 Values => \\\%values)) {
499 die XML::Template::Exception->new ('DB', \$db->error ())
500 if \! defined \$parent_namespace;
501 }
502  
503 if (defined \$parent_namespace) {
504 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
505 if (defined \$parent_namespace_info->{relation} &&
506 defined \$parent_namespace_info->{relation}->{'$namespace'}) {
507 my \$rtable = \$parent_namespace_info->{relation}->{'$namespace'}->{table};
508  
509 my \%map_values;
510 foreach my \$key (split (',', '$keys')) {
511 \$map_values{\$key} = \$values{\$key};
512 }
513  
514 my \$i = 0;
515 foreach my \$key (split (',', \$parent_namespace_info->{keys})) {
516 \$map_values{\$key} = \$parent_names[\$i];
517 \$i++;
518 }
519  
520 \$db->insert (Table => \$rtable,
521 Values => \\\%map_values)
522 || die XML::Template::Exception->new ('DB', \$db->error ());
523 }
524 }
525 }
526 };
527 !;
528 #print $outcode;
529  
530 return $outcode;
531 }
532  
533 =pod
534  
535 =head2 delect
536  
537 This method implements a DELETE SQL query on the database and table
538 associated with the tag's namespace. If this tag is nesterd in a related
539 namespace, the column to be updated will be determined via an intermediate
540 maping database table, defined in the XML::Template configuration file.
541 See C<select> for more details on related namespaces.
542  
543 The following attributes are used:
544  
545 =over 4
546  
547 =item name
548  
549 A comma-separated list of the names of the primary keys of the database
550 column to delete. The primary keys and their order is specified in the
551 XML::Template configuration file. This attribute is not required.
552  
553 =back
554  
555 Remaining attributes will be used to constrain the selection of which
556 column to delete. See C<select> for more details.
557  
558 =cut
559  
560 sub delete {
561 my $self = shift;
562 my ($code, $attribs) = @_;
563  
564 # Get attribs.
565 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
566  
567 # Generate named params assignments for the remaining atttribs.
568 my $attribs_named_params = $self->generate_named_params ($attribs);
569  
570 # Get database info.
571 my $namespace = $self->namespace ();
572 my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace);
573 my $dbname = $source_mapping_info->{source};
574 my $table = $source_mapping_info->{table};
575 my $keys = $source_mapping_info->{keys};
576  
577 my $outcode = qq{
578 do {
579 \$vars->create_context ();
580  
581 my \%attribs = ($attribs_named_params);
582  
583 my \@names;
584  
585 no strict;
586 my \$parent_namespace = \$__parent_namespace;
587 my \@parent_names = \@__parent_names;
588 use strict;
589  
590 my \$table;
591 if (defined \$parent_namespace) {
592 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
593 if (defined \$parent_namespace_info->{relation} &&
594 defined \$parent_namespace_info->{relation}->{'$namespace'}) {
595 \$table = \$parent_namespace_info->{relation}->{'$namespace'}->{table};
596 } else {
597 \$table = '$table';
598 }
599 } else {
600 \$table = '$table';
601 }
602  
603 my \$where;
604  
605 if (defined $name) {
606 \@names = split (/\\s*,\\s*/, $name);
607 my \$i = 0;
608 foreach my \$key (split (',', '$keys')) {
609 \$where .= ' and ' if defined \$where;
610 \$where .= "\$table.\$key='\$names[\$i]'";
611 \$i++;
612 }
613 }
614 my \$twhere = \$process->generate_where (\\\%attribs, '$table');
615 if (defined \$where && defined \$twhere) {
616 \$where .= " and \$twhere";
617 } elsif (defined \$twhere) {
618 \$where = \$twhere;
619 }
620  
621 if (defined \$parent_namespace) {
622 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
623 if (defined \$parent_namespace_info->{relation} &&
624 defined \$parent_namespace_info->{relation}->{'$namespace'}) {
625 my \$i = 0;
626 foreach my \$key (split (',', \$parent_namespace_info->{keys})) {
627 \$where .= ' and ' if defined \$where;
628 \$where .= "\$table.\$key='\$parent_names[\$i]'";
629 \$i++;
630 }
631 }
632 }
633  
634 my (\$__parent_namespace, \@__parent_names);
635 if (defined $name) {
636 \$__parent_namespace = '$namespace';
637 \@__parent_names = split (/\\s*,\\s*/, $name);
638 }
639  
640 $code
641  
642 my \$result;
643 if (defined \$where) {
644 my \$db = \$process->get_source ('$dbname');
645 if (defined \$db) {
646 \$db->delete (Table => \$table,
647 Where => \$where)
648 || die XML::Template::Exception->new ('DB', \$db->error ());
649 }
650  
651 if (defined $name) {
652 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
653 if (! defined \$parent_namespace
654 || (defined \$parent_namespace
655 && (! defined \$parent_namespace_info->{relation}
656 || ! defined \$parent_namespace_info->{relation}->{'$namespace'}))) {
657  
658 my \$namespace_info = \$process->get_namespace_info ('$namespace');
659 if (defined \$namespace_info->{relation}) {
660 foreach my \$namespace (keys \%{\$namespace_info->{relatedion}}) {
661 my \$source_mapping_info = \$process->get_source_mapping_info (namespace => \$namespace);
662 my \$rtable = \$source_mapping_info->{relation}->{\$namespace}->{table};
663  
664 my \$where;
665 \@names = split (/\\s*,\\s*/, $name);
666 my \$i = 0;
667 foreach my \$key (split (',', '$keys')) {
668 \$where .= ' and ' if defined \$where;
669 \$where .= "\$rtable.\$key='\$names[\$i]'";
670 \$i++;
671 }
672  
673 if (defined \$db) {
674 \$db->delete (Table => \$rtable,
675 Where => \$where)
676 || die XML::Template::Exception->new ('DB', \$db->error ());
677 }
678 }
679 }
680 }
681 }
682 }
683  
684 \$vars->delete_context ();
685 };
686 };
687  
688 return $outcode;
689 }
690  
691 =pod
692  
693 =head2 alter
694  
695 This method implements an ALTER SQL query on the database and table
696 associated with the tag's namespace. The following attributes are used:
697  
698 =over 4
699  
700 =item values
701  
702 =item action
703  
704 =item columns, column
705  
706 =item new_column
707  
708 =item type
709  
710 =item length
711  
712 =item decimals
713  
714 =item unsigned
715  
716 =item zerofull
717  
718 =item binary
719  
720 =item null
721  
722 =item def_default
723  
724 =item auto_increment
725  
726 =item def_primary_key
727  
728 =item position
729  
730 =item index
731  
732 =item primary_key
733  
734 =item unique
735  
736 =item fulltext
737  
738 =item default
739  
740 =item new_table
741  
742 =cut
743  
744 sub alter {
745 my $self = shift;
746 my ($code, $attribs) = @_;
747  
748 # Get attribs.
749 my $values = $self->get_attrib ($attribs, 'values') || 'undef';
750 my $action = $self->get_attrib ($attribs, 'action') || 'undef';
751 my $columns = $self->get_attrib ($attribs, ['columns', 'column']) || 'undef';
752 my $new_column = $self->get_attrib ($attribs, 'new_column') || 'undef';
753 my $type = $self->get_attrib ($attribs, 'type') || 'undef';
754 my $length = $self->get_attrib ($attribs, 'length') || 'undef';
755 my $decimals = $self->get_attrib ($attribs, 'decimals') || 'undef';
756 my $unsigned = $self->get_attrib ($attribs, 'unsigned') || 'undef';
757 my $zerofill = $self->get_attrib ($attribs, 'zerofull') || 'undef';
758 my $binary = $self->get_attrib ($attribs, 'binary') || 'undef';
759 my $null = $self->get_attrib ($attribs, 'null') || 'undef';
760 my $def_default = $self->get_attrib ($attribs, 'def_default') || 'undef';
761 my $autoincrement = $self->get_attrib ($attribs, 'auto_increment') || 'undef';
762 my $def_primarykey = $self->get_attrib ($attribs, 'def_primary_key') || 'undef';
763 my $position = $self->get_attrib ($attribs, 'position') || 'undef';
764 my $index = $self->get_attrib ($attribs, 'index') || 'undef';
765 my $primarykey = $self->get_attrib ($attribs, 'primary_key') || 'undef';
766 my $unique = $self->get_attrib ($attribs, 'unique') || 'undef';
767 my $fulltext = $self->get_attrib ($attribs, 'fulltext') || 'undef';
768 my $default = $self->get_attrib ($attribs, 'default') || 'undef';
769 my $new_table = $self->get_attrib ($attribs, 'new_table') || 'undef';
770  
771 # Generate named params assignments for the remaining atttribs.
772 my $attribs_named_params = $self->generate_named_params ($attribs);
773  
774 # Get database info.
775 my $namespace_info = $self->get_namespace_info ($self->namespace ());
776 my $dbname = $namespace_info->{sourcename};
777 my $table = $namespace_info->{table};
778  
779 my $outcode = qq!;
780 do {
781 \$vars->create_context ();
782  
783 my \%attribs = ($attribs_named_params);
784 \$vars->set (\%attribs);
785  
786 my \$db = \$process->get_source ('$dbname');
787 if (defined \$db) {
788 my \@values;
789 my \$values = $values;
790 \@values = split (/\\s*,\\s*/, \$values) if defined \$values;
791 \$db->alter (
792 Table => '$table',
793 Action => $action,
794 Column => $columns,
795 Definition => {
796 Column => $new_column,
797 Type => $type,
798 Length => $length,
799 Decimals => $decimals,
800 Values => \\\@values,
801 Unsigned => $unsigned,
802 ZeroFill => $zerofill,
803 Binary => $binary,
804 Null => $null,
805 Default => $def_default,
806 AutoIncrement => $autoincrement,
807 PrimaryKey => $def_primarykey,
808 },
809 Position => $position,
810 Index => $index,
811 PrimaryKey => $primarykey,
812 Unique => $unique,
813 FullText => $fulltext,
814 Default => $default,
815 NewTable => $new_table)
816 || die XML::Template::Exception->new ('DB', \$db->error ());
817 }
818  
819 \$vars->delete_context ();
820 };
821 !;
822  
823 return $outcode;
824 }
825  
826 =pod
827  
828 =head2 foreach
829  
830 XML::Template::Element::DB is a subclass of
831 L<XML::Template::Element::Iterator>, so it inherits the C<foreach> method,
832 which in conjunction with the iterator methods defined in this module,
833 implements iteration through the rows in a database. For example,
834  
835 <item:foreach xmlns:item="http://syrme.net/xml-template/item/v1"
836 fields="*">
837 ${title}: ${description}
838 </item:foreach>
839  
840 iterates through each column in the items table and prints the title and
841 description.
842  
843 The following attributes are used:
844  
845 =over 4
846  
847 =item fields, field
848  
849 A comma separated list of the database table fields to return. To return
850 all fields, use '*'. For each field returned a variable will be set with
851 the field's name and value. These variables will be available in the
852 content of the select element.
853  
854 =item orderby
855  
856 A comma-separated list of the fields used to order the list of rows being
857 iterated through.
858  
859 =item limit
860  
861 Constrains the number of rows being iterated through. If one integer, it
862 specifies the number of rows to iterate through, starting at the
863 beginning. If two integers separated by a comma, the first specifies the
864 offset at which to start iterating, and the second specifies the number of
865 rows to iterate through.
866  
867 =back
868  
869 =cut
870  
871 sub loopinit {
872 my $self = shift;
873 my $attribs = shift;
874  
875 # Get attribs.
876 my $fields = $self->get_attrib ($attribs, ['fields', 'field']) || 'undef';
877 my $query = $self->get_attrib ($attribs, 'query') || 'undef';
878 my $orderby = $self->get_attrib ($attribs, 'orderby') || 'undef';
879 my $limit = $self->get_attrib ($attribs, 'limit') || 'undef';
880 my $match = $self->get_attrib ($attribs, 'match') || 'undef';
881 my $round = $self->get_attrib ($attribs, 'round') || 'undef';
882  
883 # Generate named params assignments for the remaining atttribs.
884 my $attribs_named_params = $self->generate_named_params ($attribs);
885  
886 # Get database info.
887 my $namespace = $self->namespace ();
888 my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace);
889 my $dbname = $source_mapping_info->{source};
890 my $table = $source_mapping_info->{table};
891 my $keys = $source_mapping_info->{keys};
892  
893 my $outcode = qq!
894 my %attribs = ($attribs_named_params);
895  
896 my \$tables = '$table';
897  
898 my \$db = \$process->get_source ('$dbname')
899 || die XML::Template::Exception->new ('DB', \$process->error);
900  
901 my \$sql;
902 my \$query = $query;
903 if (defined \$query && \$query eq 'describe') {
904 \$sql = \$db->_prepare_sql ('describe',
905 {Table => '$table'});
906 } else {
907 my \$where = \$process->generate_where (\\\%attribs, '$table');
908  
909 no strict;
910 my \$parent_namespace = \$__parent_namespace;
911 my \@parent_names = \@__parent_names;
912 use strict;
913  
914 if (defined \$parent_namespace) {
915 my \$parent_namespace_info = \$process->get_source_mapping_info (namespace => \$parent_namespace);
916 if (defined \$parent_namespace_info->{relation} &&
917 defined \$parent_namespace_info->{relation}->{'$namespace'}) {
918 my \$rtable = \$parent_namespace_info->{relation}->{'$namespace'}->{table};
919  
920 \$tables .= ",\$rtable";
921  
922 my \$i = 0;
923 foreach my \$key (split (',', '$keys')) {
924 \$where .= ' and ' if defined \$where;
925 \$where .= "$table.\$key=\$rtable.\$key";
926 \$i++;
927 }
928  
929 \$i = 0;
930 foreach my \$key (split (',', \$parent_namespace_info->{keys})) {
931 \$where .= ' and ' if defined \$where;
932 \$where .= "\$rtable.\$key='\$parent_names[\$i]'";
933 \$i++;
934 }
935 }
936 }
937  
938 \$sql = \$db->_prepare_sql ('select',
939 {Fields => $fields,
940 Table => \$tables,
941 Where => \$where,
942 OrderBy => $orderby,
943 Limit => $limit,
944 Match => $match,
945 Round => $round})
946 || die XML::Template::Exception->new ('DB', \$db->error ());
947 }
948  
949 my \$__sth;
950 if (defined $fields) {
951 \$__sth = \$db->{_dbh}->prepare (\$sql)
952 || die XML::Template::Exception->new ('DB', \$db->{_dbh}->errstr);
953 \$__sth->execute
954 || die XML::Template::Exception->new ('DB', \$db->{_dbh}->errstr);
955 }
956 !;
957  
958 return $outcode;
959 }
960  
961 sub get_first {
962 my $self = shift;
963 my $attribs = shift;
964  
965 my $outcode = qq!
966 \$__value = \$__sth->fetchrow_hashref;
967 if (defined \$__value) {
968 if (defined \$query && \$query eq 'describe') {
969 my \$type = \$__value->{Type};
970 \$type =~ s/\\(([^)]+)\\)\$//;
971 \$__value->{Type} = \$type;
972 if (\$type eq 'enum' || \$type eq 'set') {
973 my \@values = split (/\\s*,\\*/, \$1);
974 \$__value->{Values} = \\\@values;
975 } else {
976 \$__value->{Size} = \$1;
977 }
978 }
979 } else {
980 \$__sth->finish;
981 undef \$__sth;
982 }
983 !;
984  
985 return $outcode;
986 }
987  
988 sub set_loopvar {
989 my $self = shift;
990 my $attribs = shift;
991  
992 # Get database info.
993 my $namespace = $self->namespace ();
994 my $namespace_info = $self->get_namespace_info ($namespace);
995 my $keys = $namespace_info->{keys};
996  
997 my $outcode = qq!
998 while (my (\$name, \$val) = each \%\$__value) {
999 \$vars->set (\$name => \$val);
1000 }
1001  
1002 my (\$__parent_namespace, \@__parent_names);
1003 if (defined \$__value) {
1004 \$__parent_namespace = '$namespace';
1005 foreach my \$key (split (/\\s*,\\s*/, '$keys')) {
1006 push (\@__parent_names, \$__value->{\$key});
1007 }
1008 }
1009  
1010 !;
1011  
1012 return $outcode;
1013 }
1014  
1015 sub get_next {
1016 my $self = shift;
1017 my $attribs = shift;
1018  
1019 my $outcode = qq!
1020 \$__value = \$__sth->fetchrow_hashref;
1021 if (defined \$__value) {
1022 if (defined \$query && \$query eq 'describe') {
1023 my \$type = \$__value->{Type};
1024 \$type =~ s/\\(([^)]+)\\)\$//;
1025 \$__value->{Type} = \$type;
1026 if (\$type eq 'enum' || \$type eq 'set') {
1027 my \$values = \$1;
1028 \$values =~ s/^'//;
1029 \$values =~ s/'\$//;
1030 my \@values = split (/',\\s*'/, \$values);
1031 \$__value->{Values} = \\\@values;
1032 } else {
1033 \$__value->{Size} = \$1;
1034 }
1035 }
1036 } else {
1037 \$__sth->finish;
1038 undef \$__sth;
1039 }
1040 !;
1041  
1042 return $outcode;
1043 }
1044
1045 sub foreach_describe {
1046 my $self = shift;
1047 my ($code, $attribs) = @_;
1048  
1049 my $namespace = $self->namespace ();
1050 $attribs->{"{$namespace}query"} = "'describe'";
1051 return $self->foreach ($code, $attribs);
1052 }
1053  
1054 sub AUTOLOAD {
1055 my $self = shift;
1056 my ($code, $attribs) = @_;
1057  
1058 return if $AUTOLOAD =~ /DESTROY$/;
1059  
1060 $AUTOLOAD =~ /([^:]+)$/;
1061 my $field = $1;
1062  
1063 my $outcode = qq!
1064 my \$value;
1065 my \$io = IO::String->new (\$value);
1066 my \$ofh = select \$io;
1067  
1068 $code
1069  
1070 select \$ofh;
1071 \$value =~ s/&(?\!amp)/&amp\;/g;
1072 \$value =~ s/\\\\/\\\\\\\\/g;
1073  
1074 no strict;
1075 \$values{'$field'} = \$value;
1076 use strict;
1077 !;
1078  
1079 return $outcode;
1080 }
1081  
1082 =pod
1083  
1084 =head1 AUTHOR
1085  
1086 Jonathan Waxman
1087 <jowaxman@bbl.med.upenn.edu>
1088  
1089 =head1 COPYRIGHT
1090  
1091 Copyright (c) 2002-2003 Jonathan A. Waxman
1092 All rights reserved.
1093  
1094 This program is free software; you can redistribute it and/or
1095 modify it under the same terms as Perl itself.
1096  
1097 =cut
1098  
1099  
1100 1;