take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TTSchema.pm
CommitLineData
2b2601b5 1package SQL::Translator::Producer::TTSchema;
2
44659089 3# -------------------------------------------------------------------
4# Copyright (C) 2002-2009 SQLFairy Authors
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; version 2.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18# 02111-1307 USA
19# -------------------------------------------------------------------
20
2b2601b5 21=pod
22
23=head1 NAME
24
046d668a 25SQL::Translator::Producer::TTSchema -
fcc6f51d 26 Produces output using the Template Toolkit from a SQL schema
27
28=head1 SYNOPSIS
29
30 use SQL::Translator;
31 my $translator = SQL::Translator->new(
32 from => 'MySQL',
33 filename => 'foo_schema.sql',
34 to => 'TTSchema',
35 producer_args => {
046d668a 36 ttfile => 'foo_template.tt', # Template file to use
37
38 # Extra template variables
39 ttargs => {
40 author => "Mr Foo",
41 },
9d6bd3c7 42
43 # Template config options
44 ttargs => {
45 INCLUDE_PATH => '/foo/templates',
46 },
fcc6f51d 47 },
48 );
49 print $translator->translate;
50
51=head1 DESCRIPTION
52
53Produces schema output using a given Template Tookit template.
54
91c59bcf 55It needs one additional producer_arg of C<ttfile> which is the file
046d668a 56name of the template to use. This template will be passed a variable
57called C<schema>, which is the C<SQL::Translator::Producer::Schema> object
58created by the parser. You can then use it to walk the schema via the
59methods documented in that module.
91c59bcf 60
61Here's a brief example of what the template could look like:
62
63 database: [% schema.database %]
64 tables:
65 [% FOREACH table = schema.get_tables %]
66 [% table.name %]
67 ================
68 [% FOREACH field = table.get_fields %]
69 [% field.name %] [% field.data_type %]([% field.size %])
70 [% END -%]
71 [% END %]
fcc6f51d 72
73See F<t/data/template/basic.tt> for a more complete example.
74
046d668a 75The template will also get the set of extra variables given as a hashref via the
9d6bd3c7 76C<tt_vars> producer arg.
046d668a 77
78You can set any of the options used to initiallize the Template object by
9d6bd3c7 79adding a tt_conf producer_arg. See Template Toolkit docs for details of
fcc6f51d 80the options.
9d6bd3c7 81(Note that the old style of passing this config directly in the producer args
82has been deprecated).
83
fcc6f51d 84
85 $translator = SQL::Translator->new(
86 to => 'TT',
87 producer_args => {
88 ttfile => 'foo_template.tt',
10f36920 89 ttargs => {},
9d6bd3c7 90 tt_conf = {
91 INCLUDE_PATH => '/foo/templates/tt',
92 INTERPOLATE => 1,
93 }
fcc6f51d 94 },
95 );
2b2601b5 96
91c59bcf 97You can use this producer to create any type of text output you like,
98even using it to create your own versions of what the other producers
99make. For example, you could create a template that translates the
100schema into MySQL's syntax, your own HTML documentation, your own
101Class::DBI classes (or some other code) -- the opportunities are
102limitless!
103
046d668a 104=head2 Producer Args
105
106=over 4
107
108=item ttfile
109
110The template file to generate the output with.
111
9d6bd3c7 112=item tt_vars
046d668a 113
114A hash ref of extra variables you want to add to the template.
115
9d6bd3c7 116=item tt_conf
117
118A hash ref of configuration options to pass to the L<Template> object's
119constructor.
120
046d668a 121=back
122
2b2601b5 123=cut
124
2b2601b5 125use strict;
2b2601b5 126
da06ac74 127use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
11ad2df9 128$VERSION = '1.59';
2b2601b5 129$DEBUG = 0 unless defined $DEBUG;
130
fcc6f51d 131use Template;
2b2601b5 132use Data::Dumper;
133use Exporter;
134use base qw(Exporter);
135@EXPORT_OK = qw(produce);
136
91c59bcf 137use SQL::Translator::Utils 'debug';
2b2601b5 138
139sub produce {
140 my $translator = shift;
fcc6f51d 141 local $DEBUG = $translator->debug;
142 my $scma = $translator->schema;
143 my $args = $translator->producer_args;
144 my $file = delete $args->{'ttfile'} or die "No template file!";
9d6bd3c7 145
146 my $tt_vars = delete $args->{'tt_vars'} || {};
329fc3f0 147 if ( exists $args->{ttargs} ) {
148 warn "Use of 'ttargs' producer arg is deprecated."
149 ." Please use 'tt_vars' instead.\n";
9d6bd3c7 150 %$tt_vars = { %{$args->{ttargs}}, %$tt_vars };
151 }
152
153 my %tt_conf = exists $args->{tt_conf} ? %{$args->{tt_conf}} : ();
154 # sqlt passes the producer args for _all_ producers in, so we use this
155 # grep hack to test for the old usage.
bced3fa7 156 debug(Dumper(\%tt_conf));
9d6bd3c7 157 if ( grep /^[A-Z_]+$/, keys %$args ) {
158 warn "Template config directly in the producer args is deprecated."
159 ." Please use 'tt_conf' instead.\n";
160 %tt_conf = ( %tt_conf, %$args );
329fc3f0 161 }
046d668a 162
2b2601b5 163 debug "Processing template $file\n";
164 my $out;
fcc6f51d 165 my $tt = Template->new(
166 DEBUG => $DEBUG,
91c59bcf 167 ABSOLUTE => 1, # Set so we can use from the command line sensibly
fcc6f51d 168 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
9d6bd3c7 169 %tt_conf,
bced3fa7 170 );
171 debug("Template ERROR: " . Template->error. "\n") if(!$tt);
172 $tt || die "Failed to initialize Template object: ".Template->error;
fcc6f51d 173
bced3fa7 174 my $ttproc = $tt->process(
046d668a 175 $file,
9d6bd3c7 176 { schema => $scma , %$tt_vars },
046d668a 177 \$out
bced3fa7 178 );
179 debug("ERROR: ". $tt->error. "\n") if(!$ttproc);
180 $ttproc or die "Error processing template '$file': ".$tt->error;
2b2601b5 181
182 return $out;
183};
184
1851;
186
2b2601b5 187=pod
188
fcc6f51d 189=head1 AUTHOR
2b2601b5 190
fcc6f51d 191Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
2b2601b5 192
193=head1 TODO
194
195B<More template vars?> e.g. [% tables %] as a shortcut for
196[% schema.get_tables %].
197
fcc6f51d 198=head1 SEE ALSO
199
200SQL::Translator.
201
2b2601b5 202=cut