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