1 package SQL::Translator::Producer::TTSchema;
7 SQL::Translator::Producer::TTSchema - Produces output using the template toolkit
16 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
17 #$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
19 $DEBUG = 0 unless defined $DEBUG;
23 use base qw(Exporter);
24 @EXPORT_OK = qw(produce);
26 use base qw/SQL::Translator::Producer/; # Doesn't do anything at the mo!
30 warn @_,"\n" if $DEBUG;
34 my $translator = shift;
35 local $DEBUG = $translator->debug;
36 my $scma = $translator->schema;
37 my $args = $translator->producer_args;
38 my $file = delete $args->{ttfile} or die "No template file!";
40 debug "Processing template $file\n";
42 my $tt = Template->new(
44 ABSOLUTE => 1, # Set so we can use from the command line sensible.
45 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
46 %$args, # Allow any TT opts to be passed in the producer_args
48 ) || die "Failed to initialize Template object: ".Template->error;
49 $tt->process($file,{ schema => $scma },\$out)
50 or die "Error processing template '$file': ".$tt->error;
64 $translator = SQL::Translator->new(
66 filename => "foo_schema.sql",
69 ttfile => "foo_template.tt",
72 print $translator->translate;
76 Produces schema output using a given Template Tookit template.
78 It needs one additional producer_arg of C<ttfile> that is the file name of the
79 template to use. This template has one var added to it called C<schema>, which
80 is the SQL::Translator::Producer::Schema object so you can then template via
83 database: [% schema.database %]
85 [% FOREACH table = schema.get_tables %]
88 [% FOREACH field = table.get_fields %]
89 [% field.name %] [% field.datatype %]([% field.size %])
93 See F<t/data/template/basic.tt> for a more complete example.
95 You can also set any of the options used to initiallize the Template object by
96 adding them to your producer_args. See Template Toolkit docs for details of
99 $translator = SQL::Translator->new(
102 ttfile => "foo_template.tt",
103 INCLUDE_PATH => "/foo/templates/tt",
110 B<More template vars?> e.g. [% tables %] as a shortcut for
111 [% schema.get_tables %].