1 package SQL::Translator::Producer::TTSchema;
3 # -------------------------------------------------------------------
4 # $Id: TTSchema.pm,v 1.7 2004-11-16 09:15:36 boconnor 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',
39 ttfile => 'foo_template.tt',
42 print $translator->translate;
46 Produces schema output using a given Template Tookit template.
48 It needs one additional producer_arg of C<ttfile> which is the file
49 name of the template to use. This template will be passed a single
50 argument called C<schema>, which is the
51 C<SQL::Translator::Producer::Schema> object, which you can then use to
52 walk the schema via the methods documented in that module.
54 Here's a brief example of what the template could look like:
56 database: [% schema.database %]
58 [% FOREACH table = schema.get_tables %]
61 [% FOREACH field = table.get_fields %]
62 [% field.name %] [% field.data_type %]([% field.size %])
66 See F<t/data/template/basic.tt> for a more complete example.
68 You can also set any of the options used to initiallize the Template object by
69 adding them to your producer_args. See Template Toolkit docs for details of
72 $translator = SQL::Translator->new(
75 ttfile => 'foo_template.tt',
77 INCLUDE_PATH => '/foo/templates/tt',
82 You can use this producer to create any type of text output you like,
83 even using it to create your own versions of what the other producers
84 make. For example, you could create a template that translates the
85 schema into MySQL's syntax, your own HTML documentation, your own
86 Class::DBI classes (or some other code) -- the opportunities are
91 # -------------------------------------------------------------------
95 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
96 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
97 $DEBUG = 0 unless defined $DEBUG;
102 use base qw(Exporter);
103 @EXPORT_OK = qw(produce);
105 use SQL::Translator::Utils 'debug';
108 my $translator = shift;
109 local $DEBUG = $translator->debug;
110 my $scma = $translator->schema;
111 my $args = $translator->producer_args;
112 my $file = delete $args->{'ttfile'} or die "No template file!";
114 debug "Processing template $file\n";
116 my $tt = Template->new(
118 ABSOLUTE => 1, # Set so we can use from the command line sensibly
119 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
120 %$args, # Allow any TT opts to be passed in the producer_args
121 ) || die "Failed to initialize Template object: ".Template->error;
125 { schema => $scma , %{ $args || {} } },
127 ) or die "Error processing template '$file': ".$tt->error;
134 # -------------------------------------------------------------------
140 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
144 B<More template vars?> e.g. [% tables %] as a shortcut for
145 [% schema.get_tables %].