1 package SQL::Translator::Producer::TTSchema;
3 # -------------------------------------------------------------------
4 # $Id: TTSchema.pm,v 1.4 2004-02-09 23:02:17 kycl4rk 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',
41 print $translator->translate;
45 Produces schema output using a given Template Tookit template.
47 It needs one additional producer_arg of C<ttfile> which is the file
48 name of the template to use. This template will be passed a single
49 argument called C<schema>, which is the
50 C<SQL::Translator::Producer::Schema> object, which you can then use to
51 walk the schema via the methods documented in that module.
53 Here's a brief example of what the template could look like:
55 database: [% schema.database %]
57 [% FOREACH table = schema.get_tables %]
60 [% FOREACH field = table.get_fields %]
61 [% field.name %] [% field.data_type %]([% field.size %])
65 See F<t/data/template/basic.tt> for a more complete example.
67 You can also set any of the options used to initiallize the Template object by
68 adding them to your producer_args. See Template Toolkit docs for details of
71 $translator = SQL::Translator->new(
74 ttfile => 'foo_template.tt',
75 INCLUDE_PATH => '/foo/templates/tt',
80 You can use this producer to create any type of text output you like,
81 even using it to create your own versions of what the other producers
82 make. For example, you could create a template that translates the
83 schema into MySQL's syntax, your own HTML documentation, your own
84 Class::DBI classes (or some other code) -- the opportunities are
89 # -------------------------------------------------------------------
93 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
94 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
95 $DEBUG = 0 unless defined $DEBUG;
100 use base qw(Exporter);
101 @EXPORT_OK = qw(produce);
103 use SQL::Translator::Utils 'debug';
106 my $translator = shift;
107 local $DEBUG = $translator->debug;
108 my $scma = $translator->schema;
109 my $args = $translator->producer_args;
110 my $file = delete $args->{'ttfile'} or die "No template file!";
112 debug "Processing template $file\n";
114 my $tt = Template->new(
116 ABSOLUTE => 1, # Set so we can use from the command line sensibly
117 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
118 %$args, # Allow any TT opts to be passed in the producer_args
119 ) || die "Failed to initialize Template object: ".Template->error;
121 $tt->process( $file, { schema => $scma }, \$out )
122 or die "Error processing template '$file': ".$tt->error;
129 # -------------------------------------------------------------------
135 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
139 B<More template vars?> e.g. [% tables %] as a shortcut for
140 [% schema.get_tables %].