1 package SQL::Translator::Producer::TTSchema;
3 # -------------------------------------------------------------------
4 # $Id: TTSchema.pm,v 1.9 2004-11-25 23:10:58 grommit Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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.
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.
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
21 # -------------------------------------------------------------------
27 SQL::Translator::Producer::TTSchema -
28 Produces output using the Template Toolkit from a SQL schema
33 my $translator = SQL::Translator->new(
35 filename => 'foo_schema.sql',
38 ttfile => 'foo_template.tt', # Template file to use
40 # Extra template variables
46 print $translator->translate;
50 Produces schema output using a given Template Tookit template.
52 It needs one additional producer_arg of C<ttfile> which is the file
53 name of the template to use. This template will be passed a variable
54 called C<schema>, which is the C<SQL::Translator::Producer::Schema> object
55 created by the parser. You can then use it to walk the schema via the
56 methods documented in that module.
58 Here's a brief example of what the template could look like:
60 database: [% schema.database %]
62 [% FOREACH table = schema.get_tables %]
65 [% FOREACH field = table.get_fields %]
66 [% field.name %] [% field.data_type %]([% field.size %])
70 See F<t/data/template/basic.tt> for a more complete example.
72 The template will also get the set of extra variables given as a hashref via the
73 C<ttvars> producer arg.
75 You can set any of the options used to initiallize the Template object by
76 adding them to your producer_args. See Template Toolkit docs for details of
79 $translator = SQL::Translator->new(
82 ttfile => 'foo_template.tt',
84 INCLUDE_PATH => '/foo/templates/tt',
89 You can use this producer to create any type of text output you like,
90 even using it to create your own versions of what the other producers
91 make. For example, you could create a template that translates the
92 schema into MySQL's syntax, your own HTML documentation, your own
93 Class::DBI classes (or some other code) -- the opportunities are
102 The template file to generate the output with.
106 A hash ref of extra variables you want to add to the template.
112 # -------------------------------------------------------------------
116 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
117 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
118 $DEBUG = 0 unless defined $DEBUG;
123 use base qw(Exporter);
124 @EXPORT_OK = qw(produce);
126 use SQL::Translator::Utils 'debug';
129 my $translator = shift;
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!";
134 if ( exists $args->{ttargs} ) {
135 warn "Use of 'ttargs' producer arg is deprecated."
136 ." Please use 'tt_vars' instead.\n";
137 $args->{tt_vars} = delete $args->{ttargs};
139 my $ttvars = delete $args->{'tt_vars'} || {};
140 # Any args left here get given to the Template object.
142 debug "Processing template $file\n";
144 my $tt = Template->new(
146 ABSOLUTE => 1, # Set so we can use from the command line sensibly
147 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
148 %$args, # Allow any TT opts to be passed in the producer_args
149 ) || die "Failed to initialize Template object: ".Template->error;
153 { schema => $scma , %$ttvars },
155 ) or die "Error processing template '$file': ".$tt->error;
162 # -------------------------------------------------------------------
168 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
172 B<More template vars?> e.g. [% tables %] as a shortcut for
173 [% schema.get_tables %].