Stable 1.1 branch of the Turnkey producer. Needed for the current version of the...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Turnkey.pm
CommitLineData
c00bb9f8 1package SQL::Translator::Producer::Turnkey;
2
3# -------------------------------------------------------------------
74611019 4# $Id: Turnkey.pm,v 1.1.2.1 2003-10-03 05:55:20 boconnor Exp $
c00bb9f8 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Allen Day <allenday@ucla.edu>,
7# Ying Zhang <zyolive@yahoo.com>
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2.
12#
13# This program is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21# 02111-1307 USA
22# -------------------------------------------------------------------
23
24use strict;
25use vars qw[ $VERSION $DEBUG ];
74611019 26$VERSION = sprintf "%d.%02d", q$Revision: 1.1.2.1 $ =~ /(\d+)\.(\d+)/;
c00bb9f8 27$DEBUG = 1 unless defined $DEBUG;
28
29use SQL::Translator::Schema::Constants;
30use SQL::Translator::Utils qw(header_comment);
31use Data::Dumper;
32use Template;
33
34my %CDBI_auto_pkgs = (
35 MySQL => 'mysql',
36 PostgreSQL => 'Pg',
37 Oracle => 'Oracle',
38);
39
40# -------------------------------------------------------------------
41sub produce {
42 my $t = shift;
43 my $create = undef;
44 local $DEBUG = $t->debug;
45 my $no_comments = $t->no_comments;
46 my $schema = $t->schema;
47 my $args = $t->producer_args;
48 my $db_user = $args->{'db_user'} || '';
49 my $db_pass = $args->{'db_pass'} || '';
50 my $main_pkg_name = $args->{'main_pkg_name'} ||
51 $t->format_package_name('DBI');
52 my $header = header_comment(__PACKAGE__, "# ");
53 my $parser_type = ( split /::/, $t->parser_type )[-1];
54 my $from = $CDBI_auto_pkgs{ $parser_type } || '';
55 my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_',
56 $CDBI_auto_pkgs{ $parser_type }
57 ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
58 );
59 my $sep = '# ' . '-' x 67;
60
61 #
62 # Identify "link tables" (have only PK and FK fields).
63 #
64 my %linkable;
65 my %linktable;
66 foreach my $table ( $schema->get_tables ) {
67 my $is_link = 1;
68 foreach my $field ( $table->get_fields ) {
69 unless ( $field->is_primary_key or $field->is_foreign_key ) {
70 $is_link = 0;
71 last;
72 }
73 }
74
75 next unless $is_link;
76
77 foreach my $left ( $table->get_fields ) {
78 next unless $left->is_foreign_key;
79 my $lfk = $left->foreign_key_reference or next;
80 my $lr_table = $schema->get_table( $lfk->reference_table )
81 or next;
82 my $lr_field_name = ($lfk->reference_fields)[0];
83 my $lr_field = $lr_table->get_field($lr_field_name);
84 next unless $lr_field->is_primary_key;
85
86 foreach my $right ( $table->get_fields ) {
87 next if $left->name eq $right->name;
88
89 my $rfk = $right->foreign_key_reference or next;
90 my $rr_table = $schema->get_table( $rfk->reference_table )
91 or next;
92 my $rr_field_name = ($rfk->reference_fields)[0];
93 my $rr_field = $rr_table->get_field($rr_field_name);
94 next unless $rr_field->is_primary_key;
95
96 $linkable{ $lr_table->name }{ $rr_table->name } = $table;
97 $linkable{ $rr_table->name }{ $lr_table->name } = $table;
98 $linktable{ $table->name } = $table;
99 }
100 }
101 }
102
103 #
104 # Iterate over all tables
105 #
106 my ( %packages, $order );
107 for my $table ( $schema->get_tables ) {
108 my $table_name = $table->name or next;
109
110 my $table_pkg_name = $t->format_package_name($table_name);
111 $packages{ $table_pkg_name } = {
112 order => ++$order,
113 pkg_name => $table_pkg_name,
114 base => $main_pkg_name,
115 table => $table_name,
116 };
117
118 #
119 # Primary key may have a differenct accessor method name
120 #
121 if ( my $constraint = $table->primary_key ) {
122 my $field = ($constraint->fields)[0];
123 $packages{ $table_pkg_name }{'columns_primary'} = $field;
124
125 if ( my $pk_xform = $t->format_pk_name ) {
126 my $pk_name = $pk_xform->( $table_pkg_name, $field );
127
128 $packages{ $table_pkg_name }{'pk_accessor'} =
129 "#\n# Primary key accessor\n#\n".
130 "sub $pk_name {\n shift->$field\n}\n\n";
131
132 }
133 }
134
135 my $is_data = 0;
136 foreach my $field ( $table->get_fields ) {
137 if ( !$field->is_foreign_key and !$field->is_primary_key ) {
138 push @{ $packages{ $table_pkg_name }{'columns_essential'} }, $field->name;
139 $is_data++;
140 } elsif ( !$field->is_primary_key ) {
141 push @{ $packages{ $table_pkg_name }{'columns_others'} }, $field->name;
142 }
143 }
144
145 my %linked;
146 if ( $is_data ) {
147 foreach my $link ( keys %{ $linkable{ $table_name } } ) {
148 my $linkmethodname;
149
150 if ( my $fk_xform = $t->format_fk_name ) {
151 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
152 $linkmethodname = $fk_xform->($linkable{$table_name}{$link}->name,
153 ($schema->get_table($link)->primary_key->fields)[0]).'s';
154 } else {
155 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
156 $linkmethodname = $linkable{$table_name}{$link}->name.'_'.
157 ($schema->get_table($link)->primary_key->fields)[0].'s';
158 }
159
160 my @rk_fields = ();
161 my @lk_fields = ();
162 foreach my $field ($linkable{$table_name}{$link}->get_fields) {
163 next unless $field->is_foreign_key;
164
165 next unless(
166 $field->foreign_key_reference->reference_table eq $table_name
167 ||
168 $field->foreign_key_reference->reference_table eq $link
169 );
170 push @lk_fields, ($field->foreign_key_reference->reference_fields)[0]
171 if $field->foreign_key_reference->reference_table eq $link;
172 push @rk_fields, $field->name
173 if $field->foreign_key_reference->reference_table eq $table_name;
174 }
175
176 #if one possible traversal via link table
177 if (scalar(@rk_fields) == 1 and scalar(@lk_fields) == 1) {
178 foreach my $rk_field (@rk_fields) {
179 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link } },
180 push @{ $packages{ $table_pkg_name }{'has_many'}{$link}{'link_one_one'} },
181 "sub ".$linkmethodname." { my \$self = shift; ".
182 "return map \$_->".
183 ($schema->get_table($link)->primary_key->fields)[0].
184 ", \$self->".$linkable{$table_name}{$link}->name.
185 "_".$rk_field." }\n\n";
186 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link }{'one_one'} },
187 # {link_method_name => $linkmethodname, primary_key_field => ($schema->get_table($link)->primary_key->fields)[0],
188 # table_name => $linkable{$table_name}{$link}->name, rk_field => $rk_field};
189 }
190 #else there is more than one way to traverse it. ack!
191 #let's treat these types of link tables as a many-to-one (easier)
192 #
193 #NOTE: we need to rethink the link method name, as the cardinality
194 #has shifted on us.
195 } elsif (scalar(@rk_fields) == 1) {
196 foreach my $rk_field (@rk_fields) {
197 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
198 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link } },
199 push @{ $packages{ $table_pkg_name }{'has_many'}{ $link }{'link_many_one'} },
200 "sub " . $linkable{$table_name}{$link}->name .
201 "s { my \$self = shift; return \$self->" .
202 $linkable{$table_name}{$link}->name . "_" .
203 $rk_field . "(\@_) }\n\n";
204 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link }{'many_one'} },
205 # {
206 # table_name => $linkable{$table_name}{$link}->name, rk_field => $rk_field
207 # };
208 }
209 } elsif (scalar(@lk_fields) == 1) {
210 #these will be taken care of on the other end...
211 } else {
212 #many many many. need multiple iterations here, data structure revision
213 #to handle N FK sources. This code has not been tested and likely doesn't
214 #work here
215 foreach my $rk_field (@rk_fields) {
216 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
217 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link } },
218 push @{ $packages{ $table_pkg_name }{'has_many'}{ $link }{'link_many_many'} },
219 "sub " . $linkable{$table_name}{$link}->name . "_" . $rk_field .
220 "s { my \$self = shift; return \$self->" .
221 $linkable{$table_name}{$link}->name . "_" .
222 $rk_field . "(\@_) }\n\n";
223 #push @{ $packages{ $table_pkg_name }{'has_many'}{ $link }{'many_many'} },
224 # {
225 # table_name => $linkable{$table_name}{$link}->name, rk_field => $rk_field
226 # };
227 }
228 }
229 }
230 }
231
232
233 #
234 # Use foreign keys to set up "has_a/has_many" relationships.
235 #
236 foreach my $field ( $table->get_fields ) {
237 if ( $field->is_foreign_key ) {
238 my $table_name = $table->name;
239 my $field_name = $field->name;
240 my $fk_method = $t->format_fk_name($table_name, $field_name);
241 my $fk = $field->foreign_key_reference;
242 my $ref_table = $fk->reference_table;
243 my $ref_pkg = $t->format_package_name($ref_table);
244 my $ref_field = ($fk->reference_fields)[0];
245
246 push @{ $packages{ $table_pkg_name }{'has_a'} },
247 "$table_pkg_name->has_a(\n".
248 " $field_name => '$ref_pkg'\n);\n\n".
249 "sub $fk_method {\n".
250 " return shift->$field_name\n}\n\n"
251 ;
252
253
254 #
255 # If this table "has a" to the other, then it follows
256 # that the other table "has many" of this one, right?
257 #
258 # No... there is the possibility of 1-1 cardinality
259
260 #if there weren't M-M relationships via the has_many
261 #being set up here, create nice pluralized method alias
262 #rather for user as alt. to ugly tablename_fieldname name
263 if(! $packages{ $ref_pkg }{ 'has_many' }{ $table_name } ){
264 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
265 #push @{ $packages{ $ref_pkg }{'has_many'}{ $table_name } },
266 # "sub $table_name\s {\n return shift->$table_name\_$field_name\n}\n\n";
267 push @{ $packages{ $ref_pkg }{'has_many'}{ $table_name }{'fk_pluralized'} },
268 { table_name => $table_name, field_name => $field_name };
269
270 #else ugly
271 } else {
272 }
273
274 #push @{ $packages{ $ref_pkg }{'has_many'}{ $table_name } },
275 # "$ref_pkg->has_many(\n '${table_name}_${field_name}', ".
276 # "'$table_pkg_name' => '$field_name'\n);\n\n";
277 push @{ $packages{ $ref_pkg }{'has_many'}{ $table_name }{pluralized} },
278 { ref_pkg => $ref_pkg, table_pkg_name => $table_pkg_name, table_name => $table_name, field_name => $field_name };
279 }
280 }
281 }
282
283 my %metadata;
284 $metadata{"packages"} = \%packages;
285 $metadata{"linkable"} = \%linkable;
286 return(translateForm($t, \%metadata));
287}
288
289###########################################
290# Here documents for the tt2 templates #
291###########################################
292
293my $turnkey_atom_tt2 = <<'EOF';
294[% ###### DOCUMENT START ###### %]
295
296[% FOREACH package = linkable %]
297
298##############################################
299
74611019 300package Turnkey::Atom::[% package.key FILTER ucfirst %];
c00bb9f8 301
302[% pname = package.key FILTER ucfirst%]
74611019 303[% pkey = "Turnkey::Model::${pname}" %]
c00bb9f8 304
74611019 305use base qw(Turnkey::Atom);
c00bb9f8 306use Data::Dumper;
307
308sub can_render {
309 return 1;
310}
311
312sub render {
313 my $self = shift;
314 my $dbobject = shift;
315 # Assumption here that if it's not rendering on it's own dbobject
316 # then it's a list. This will be updated when AtomLists are implemented -boconnor
74611019 317 if(ref($dbobject) eq 'Turnkey::Model::[% package.key FILTER ucfirst %]') {
c00bb9f8 318 return(_render_record($dbobject));
319 }
320 else { return(_render_list($dbobject)); }
321}
322
323sub _render_record {
324 my $dbobject = shift;
325 my @output = ();
326 my $row = {};
327 my $field_hash = {};
328 [% FOREACH field = packages.$pkey.columns_essential %]
329 $field_hash->{[% field %]} = $dbobject->[% field %]();
330 [% END %]
74611019 331 [% FOREACH field = packages.$pkey.columns_others %]
332 $field_hash->{[% field %]} = $dbobject->[% field %]();
333 [% END %]
c00bb9f8 334 $row->{data} = $field_hash;
335 $row->{id} = $dbobject->id();
336 push @output, $row;
337 return(\@output);
338}
339
340sub _render_list {
341 my $dbobject = shift;
342 my @output = ();
343 my @objects = $dbobject->[% package.key %]s;
344 foreach my $object (@objects)
345 {
346 my $row = {};
347 my $field_hash = {};
348 [% FOREACH field = packages.$pkey.columns_essential %]
349 $field_hash->{[% field %]} = $object->[% field %]();
74611019 350 [% END %]
351 [% FOREACH field = packages.$pkey.columns_others %]
352 $field_hash->{[% field %]} = $object->[% field %]();
c00bb9f8 353 [% END %]
74611019 354
c00bb9f8 355 $row->{data} = $field_hash;
356 $row->{id} = $object->id();
357 push @output, $row;
358 }
359 return(\@output);
360}
361
362sub head {
363 return 1;
364}
365
3661;
367
368[% END %]
369EOF
370
371my $turnkey_dbi_tt2 = <<EOF;
372[% ####### MACRO START ###### %]
373
374[% MACRO printPackage(package) BLOCK %]
375# --------------------------------------------
376package [% package.pkg_name %];
377use base '[% package.base %]';
378use Class::DBI::Pager;
379
380[% package.pkg_name %]->set_up_table('[% package.table %]');
381[% package.pkg_name %]->columns(Primary => qw/[% printList(package.columns_primary) %]/);
382[% package.pkg_name %]->columns(Essential => qw/[% printList(package.columns_essential) %]/);
383
384[% printPKAccessors(package.columns_primary, package.table) %]
385[% printHasMany(package.has_many, package.table) %]
386[% printHasA(package.has_a, package.pkg_name) %]
387
388[% END %]
389
390[% MACRO printPKAccessors(array, name) BLOCK %]
391#
392# Primary key accessor
393#
394[% FOREACH item = array %]
395sub [% name %] {
396 shift->[% item %];
397}
398[% END %]
399[% END %]
400
401[% MACRO printHasMany(hash, name) BLOCK %]
402#
403# Has Many
404#
405[% FOREACH group = hash %][% FOREACH item = group.value %][% FOREACH arr = item.value %]
406# Key: [% group.key %]
407# Relationship: [% item.key %]
408 [% IF item.key == 'fk_pluralized' %]
409sub [% arr.table_name -%]s {
410 return shift->[% arr.table_name %]_[% arr.field_name %]
411 };
412 [% ELSIF item.key == 'pluralized' %]
413[% arr.ref_pkg %]->has_many('[% arr.table_name %]_[% arr.field_name %]', '[% arr.table_pkg_name %]' => '[% arr.field_name %]');
414 [% ELSIF item.key == 'link_one_one' %]
415 [% FOREACH line = item.value %]
416[% line %]
417 [% END %]
418 [% ELSIF item.key == 'link_many_one' %]
419 [% FOREACH line = item.value %]
420[% line %]
421 [% END %]
422 [% ELSIF item.key == 'link_many_many' %]
423 [% FOREACH line = item.value %]
424[% line %]
425 [% END %]
426 [% END %]
427
428[% END %][% END %][% END %][% END %]
429
430[% MACRO printHasA(hash, pkg_name) BLOCK %]
431#
432# Has A
433#
434[% #name %]
435[% FOREACH item = hash %][% item %]
436[% END %][% END %]
437
438[% MACRO printList(array) BLOCK %][% FOREACH item = array %][% item %] [% END %][% END %]
439
440
441[% ###### DOCUMENT START ###### %]
442
74611019 443package Turnkey::Model::DBI;
c00bb9f8 444
445# Created by SQL::Translator::Producer::ClassDBI
446# Template used AutoDBI.tt2
447
448use strict;
449use base qw(Class::DBI::Pg);
450
74611019 451Turnkey::Model::DBI->set_db('Main', '[% db_str %]', '[% db_user %]', '[% db_pass %]');
c00bb9f8 452
453[% FOREACH package = packages %]
454 [% printPackage(package.value) %]
455[% END %]
456EOF
457
458my $turnkey_xml_tt2 = <<EOF;
459<?xml version="1.0" encoding="UTF-8"?>
74611019 460<!DOCTYPE Turnkey SYSTEM "Turnkey.dtd">
461<Turnkey>
c00bb9f8 462
463<!-- The basic layout is fixed -->
74611019 464 <container bgcolor="#dedbde" cellpadding="0" cellspacing="0" height="90%" orientation="vertical" type="root" width="100%" xlink:label="RootContainer">
c00bb9f8 465 <container cellpadding="3" cellspacing="0" orientation="horizontal" type="container" height="100%" width="100%" xlink:label="MiddleContainer">
466 <container align="center" cellpadding="2" cellspacing="0" class="leftbar" orientation="vertical" type="minor" width="0%" xlink:label="MidLeftContainer"/>
467 <container cellpadding="0" cellspacing="0" orientation="vertical" width="100%" type="major" xlink:label="MainContainer"/>
468 </container>
469 </container>
470
471<!-- Atom Classes -->
472[% FOREACH package = linkable %]
74611019 473 <atom class="Turnkey::Atom::[% package.key FILTER ucfirst %]" name="[% package.key FILTER ucfirst %]" xlink:label="[% package.key FILTER ucfirst %]Atom"/>
c00bb9f8 474[% END %]
475
476<!-- Atom Bindings -->
477<atomatombindings>
478[% FOREACH focus_atom = linkable %]
479 [% FOREACH link_atom = focus_atom.value %]
480 <atomatombinding xlink:from="#[% focus_atom.key FILTER ucfirst %]Atom" xlink:to="#[% link_atom.key FILTER ucfirst %]Atom" xlink:label="[% focus_atom.key FILTER ucfirst %]Atom2[% link_atom.key FILTER ucfirst %]Atom"/>
481 [% END %]
482[% END %]
483</atomatombindings>
484
485<atomcontainerbindings>
486 [% FOREACH focus_atom = linkable %]
74611019 487 <atomcontainerbindingslayout xlink:label="Turnkey::Model::[% focus_atom.key FILTER ucfirst %]">
c00bb9f8 488 [% FOREACH link_atom = focus_atom.value %]
489 <atomcontainerbinding xlink:from="#MidLeftContainer" xlink:label="MidLeftContainer2[% link_atom.key FILTER ucfirst %]Atom" xlink:to="#[% link_atom.key FILTER ucfirst %]Atom"/>
490 [% END %]
491 <atomcontainerbinding xlink:from="#MainContainer" xlink:label="MainContainer2[% focus_atom.key FILTER ucfirst %]Atom" xlink:to="#[% focus_atom.key FILTER ucfirst %]Atom"/>
492 </atomcontainerbindingslayout>
493 [% END %]
494 </atomcontainerbindings>
495
496 <uribindings>
74611019 497 <uribinding uri="/" class="Turnkey::Util::Frontpage"/>
c00bb9f8 498 </uribindings>
499
500 <classbindings>
501 [% FOREACH focus_atom = linkable %]
74611019 502 <classbinding class="Turnkey::Model::[% focus_atom.key FILTER ucfirst %]" plugin="#[% focus_atom.key FILTER ucfirst %]Atom" rank="0"/>
c00bb9f8 503 [% END %]
504 </classbindings>
505
74611019 506</Turnkey>
c00bb9f8 507EOF
508
509my $turnkey_template_tt2 = <<'EOF';
510[% TAGS [- -] %]
511[% MACRO renderpanel(panel,dbobject) BLOCK %]
512 <!-- begin panel: [% panel.label %] -->
513 <table border="0" width="[% panel.width %]" height="[% panel.height %]" bgcolor="[% panel.bgcolor %]" valign="top" cellpadding="[% panel.cellpadding %]" cellspacing="[% panel.cellspacing %]" align="[% panel.align %]" valign="[% panel.valign %]">
514 <tr>
515 [% FOREACH p = panel.containers %]
516 [% IF p.can_render(panel) %]
517 <td valign="top" class="[% p.class %]" align="[% panel.align %]" height="[% p.height || 1 %]" width="[% p.width %]">
518 [% IF p.type == 'Container' %]
519 [% renderpanel(p,dbobject) %]
520 [% ELSE %]
74611019 521 <table cellpadding="2" cellspacing="0" align="left" height="100%" width="100%">
c00bb9f8 522 [% IF p.name %]
74611019 523 <tr bgcolor="#5aa5e2" height="1">
524 <td><font class="font" color="#FFFFFF">[% p.name %][% IF panel.type == 'major' %]: [% dbobject.name %][% END %]</font></td>
c00bb9f8 525 <td align="right" width="0"><!--<nobr><img src="/images/v.gif"/><img src="/images/^.gif"/>[% IF p.delible == 'yes' %]<img src="/images/x.gif"/>[% END %]</nobr>--></td>
526 </tr>
527 [% END %]
74611019 528 <tr><td colspan="2" class="font" bgcolor="#FFFFFF">
c00bb9f8 529 <!-- begin atom: [% p.label %] -->
74611019 530 <table cellpadding="2" cellspacing="0" align="left" height="100%" width="100%"><!-- [% ref(atom) %] [% ref(dbobject) %] -->
c00bb9f8 531 [% renderatom(p,dbobject) %] <!-- used to be renderplugin(p,panel) -->
532 </table>
533 </table>
534 [% END %]
535 </td>
536 [% IF panel.orientation == 'vertical' %]
537 </tr><tr>
538 [% END %]
539 [% END %]
540 [% END %]
541 </tr>
542 </table>
543 <!-- end panel: [% panel.label %] -->
544[% END %]
545[% MACRO renderatom(atom, dbobject) SWITCH atom.name %]
546 [- FOREACH package = linkable -]
547 [% CASE '[- package.key FILTER ucfirst -]' %]
548 [% render[- package.key FILTER ucfirst -]Atom(atom.render(dbobject)) %]
549 [- END -]
550 [% CASE DEFAULT %]
551 [% renderlist(atom.render(dbobject)) %]
552[% END %]
553[- FOREACH package = linkable -]
554[% MACRO render[- package.key FILTER ucfirst -]Atom(lstArr) BLOCK %]
555 [% FOREACH record = lstArr %]
556 [% fields = record.data %]
557 [- pname = package.key FILTER ucfirst -]
74611019 558 [- pkey = "Turnkey::Model::${pname}" -]
c00bb9f8 559 [- FOREACH field = packages.$pkey.columns_essential -]
560 <tr><td><b>[- field -]</b></td><td>[% fields.[- field -] %]</td></tr>
561 [- END -]
74611019 562 [- FOREACH field = packages.$pkey.columns_others -]
563 <tr><td><b>[- field -]</b></td><td>[% fields.[- field -] %]</td></tr>
564 [- END -]
c00bb9f8 565 [% id = record.id %]
74611019 566 <tr><td><a href="?id=[% id %];class=Turnkey::Model::[- package.key FILTER ucfirst -]">Link</a></td><td></td></tr>
c00bb9f8 567 [% END %]
568[% END %]
569[- END -]
570[% MACRO renderlist(lstArr) BLOCK %]
571 [% FOREACH item = lstArr %]
572 <tr>[% item %]</tr>
573 [% END %]
574[% END %]
575EOF
576
577sub translateForm
578{
579 my $t = shift;
580 my $output = shift;
581 my $args = $t->producer_args;
582 my $tt2 = $args->{'template'};
583 my $tt2Ref;
584 if ($tt2 eq 'atom') { $tt2Ref = \$turnkey_atom_tt2; }
585 if ($tt2 eq 'dbi') { $tt2Ref = \$turnkey_dbi_tt2; }
586 if ($tt2 eq 'xml') { $tt2Ref = \$turnkey_xml_tt2; }
587 if ($tt2 eq 'template') { $tt2Ref = \$turnkey_template_tt2; }
588
589 my $vars = {
590 packages => $output->{packages},
591 linkable => $output->{linkable},
592 linktable => $output->{linktable},
593 db_str => $args->{db_str},
594 db_user => $args->{db_user},
595 db_pass => $args->{db_pass},
596 };
597 my $config = {
598 EVAL_PERL => 1, # evaluate Perl code blocks
599 };
600
601 # create Template object
602 my $template = Template->new($config);
603
604 my $result;
605 # specify input filename, or file handle, text reference, etc.
606 # process input template, substituting variables
607 $template->process($tt2Ref, $vars, \$result) || die $template->error();
608 return($result);
609}
610
6111;
612
613# -------------------------------------------------------------------
614
615=pod
616
617=head1 NAME
618
619SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
620
621=head1 SYNOPSIS
622
623Use this producer as you would any other from SQL::Translator. See
624L<SQL::Translator> for details.
625
626This package utilizes SQL::Translator's formatting methods
627format_package_name(), format_pk_name(), format_fk_name(), and
628format_table_name() as it creates classes, one per table in the schema
629provided. An additional base class is also created for database connectivity
630configuration. See L<Class::DBI> for details on how this works.
631
632=head1 AUTHORS
633
634Allen Day E<lt>allenday@ucla.eduE<gt>
635Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
636Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
637Brian O'Connor E<lt>brian.oconnor@excite.comE<gt>.