Fixed up POD, some other cosmetics changes, removed "use warnings" to make
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TTSchema.pm
CommitLineData
2b2601b5 1package SQL::Translator::Producer::TTSchema;
2
fcc6f51d 3# -------------------------------------------------------------------
4# $Id: TTSchema.pm,v 1.2 2003-08-20 21:26:02 kycl4rk Exp $
5# -------------------------------------------------------------------
6# Copyright (C) 2003 Mark Addison <grommit@users.sourceforge.net>,
7# Ken Y. Clark <kclark@cpan.org>.
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
2b2601b5 24=pod
25
26=head1 NAME
27
fcc6f51d 28SQL::Translator::Producer::TTSchema -
29 Produces output using the Template Toolkit from a SQL schema
30
31=head1 SYNOPSIS
32
33 use SQL::Translator;
34 my $translator = SQL::Translator->new(
35 from => 'MySQL',
36 filename => 'foo_schema.sql',
37 to => 'TTSchema',
38 producer_args => {
39 ttfile => 'foo_template.tt',
40 },
41 );
42 print $translator->translate;
43
44=head1 DESCRIPTION
45
46Produces schema output using a given Template Tookit template.
47
48It needs one additional producer_arg of C<ttfile> that is the file
49name of the template to use. This template has one var added to it
50called C<schema>, which is the SQL::Translator::Producer::Schema
51object so you can then template via its methods.
52
53 database: [% schema.database %]
54 tables:
55 [% FOREACH table = schema.get_tables %]
56 [% table.name %]
57 ================
58 [% FOREACH field = table.get_fields %]
59 [% field.name %] [% field.data_type %]([% field.size %])
60 [% END -%]
61 [% END %]
62
63See F<t/data/template/basic.tt> for a more complete example.
64
65You can also set any of the options used to initiallize the Template object by
66adding them to your producer_args. See Template Toolkit docs for details of
67the options.
68
69 $translator = SQL::Translator->new(
70 to => 'TT',
71 producer_args => {
72 ttfile => 'foo_template.tt',
73 INCLUDE_PATH => '/foo/templates/tt',
74 INTERPOLATE => 1,
75 },
76 );
2b2601b5 77
78=cut
79
fcc6f51d 80# -------------------------------------------------------------------
2b2601b5 81
82use strict;
2b2601b5 83
84use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
fcc6f51d 85$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
2b2601b5 86$DEBUG = 0 unless defined $DEBUG;
87
fcc6f51d 88use Template;
2b2601b5 89use Data::Dumper;
90use Exporter;
91use base qw(Exporter);
92@EXPORT_OK = qw(produce);
93
94use base qw/SQL::Translator::Producer/; # Doesn't do anything at the mo!
2b2601b5 95
96sub debug {
97 warn @_,"\n" if $DEBUG;
98}
99
100sub produce {
101 my $translator = shift;
fcc6f51d 102 local $DEBUG = $translator->debug;
103 my $scma = $translator->schema;
104 my $args = $translator->producer_args;
105 my $file = delete $args->{'ttfile'} or die "No template file!";
2b2601b5 106
107 debug "Processing template $file\n";
108 my $out;
fcc6f51d 109 my $tt = Template->new(
110 DEBUG => $DEBUG,
2b2601b5 111 ABSOLUTE => 1, # Set so we can use from the command line sensible.
fcc6f51d 112 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
2b2601b5 113 %$args, # Allow any TT opts to be passed in the producer_args
2b2601b5 114 ) || die "Failed to initialize Template object: ".Template->error;
fcc6f51d 115
116 $tt->process( $file, { schema => $scma }, \$out )
117 or die "Error processing template '$file': ".$tt->error;
2b2601b5 118
119 return $out;
120};
121
1221;
123
fcc6f51d 124# -------------------------------------------------------------------
2b2601b5 125
126=pod
127
fcc6f51d 128=head1 AUTHOR
2b2601b5 129
fcc6f51d 130Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
2b2601b5 131
132=head1 TODO
133
134B<More template vars?> e.g. [% tables %] as a shortcut for
135[% schema.get_tables %].
136
fcc6f51d 137=head1 SEE ALSO
138
139SQL::Translator.
140
2b2601b5 141=cut