1 package SQL::Translator::Producer::TTSchema;
7 SQL::Translator::Producer::TTSchema -
8 Produces output using the Template Toolkit from a SQL schema
13 my $translator = SQL::Translator->new(
15 filename => 'foo_schema.sql',
18 ttfile => 'foo_template.tt', # Template file to use
20 # Extra template variables
25 # Template config options
27 INCLUDE_PATH => '/foo/templates',
31 print $translator->translate;
35 Produces schema output using a given Template Tookit template.
37 It needs one additional producer_arg of C<ttfile> which is the file
38 name of the template to use. This template will be passed a variable
39 called C<schema>, which is the C<SQL::Translator::Producer::Schema> object
40 created by the parser. You can then use it to walk the schema via the
41 methods documented in that module.
43 Here's a brief example of what the template could look like:
45 database: [% schema.database %]
47 [% FOREACH table = schema.get_tables %]
50 [% FOREACH field = table.get_fields %]
51 [% field.name %] [% field.data_type %]([% field.size %])
55 See F<t/data/template/basic.tt> for a more complete example.
57 The template will also get the set of extra variables given as a hashref via the
58 C<tt_vars> producer arg.
60 You can set any of the options used to initiallize the Template object by
61 adding a tt_conf producer_arg. See Template Toolkit docs for details of
63 (Note that the old style of passing this config directly in the producer args
67 $translator = SQL::Translator->new(
70 ttfile => 'foo_template.tt',
73 INCLUDE_PATH => '/foo/templates/tt',
79 You can use this producer to create any type of text output you like,
80 even using it to create your own versions of what the other producers
81 make. For example, you could create a template that translates the
82 schema into MySQL's syntax, your own HTML documentation, your own
83 Class::DBI classes (or some other code) -- the opportunities are
92 The template file to generate the output with.
96 A hash ref of extra variables you want to add to the template.
100 A hash ref of configuration options to pass to the L<Template> object's
109 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
111 $DEBUG = 0 unless defined $DEBUG;
116 use base qw(Exporter);
117 @EXPORT_OK = qw(produce);
119 use SQL::Translator::Utils 'debug';
122 my $translator = shift;
123 local $DEBUG = $translator->debug;
124 my $scma = $translator->schema;
125 my $args = $translator->producer_args;
126 my $file = delete $args->{'ttfile'} or die "No template file!";
128 my $tt_vars = delete $args->{'tt_vars'} || {};
129 if ( exists $args->{ttargs} ) {
130 warn "Use of 'ttargs' producer arg is deprecated."
131 ." Please use 'tt_vars' instead.\n";
132 %$tt_vars = { %{$args->{ttargs}}, %$tt_vars };
135 my %tt_conf = exists $args->{tt_conf} ? %{$args->{tt_conf}} : ();
136 # sqlt passes the producer args for _all_ producers in, so we use this
137 # grep hack to test for the old usage.
138 debug(Dumper(\%tt_conf));
139 if ( grep /^[A-Z_]+$/, keys %$args ) {
140 warn "Template config directly in the producer args is deprecated."
141 ." Please use 'tt_conf' instead.\n";
142 %tt_conf = ( %tt_conf, %$args );
145 debug "Processing template $file\n";
147 my $tt = Template->new(
149 ABSOLUTE => 1, # Set so we can use from the command line sensibly
150 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
153 debug("Template ERROR: " . Template->error. "\n") if(!$tt);
154 $tt || die "Failed to initialize Template object: ".Template->error;
156 my $ttproc = $tt->process(
158 { schema => $scma , %$tt_vars },
161 debug("ERROR: ". $tt->error. "\n") if(!$ttproc);
162 $ttproc or die "Error processing template '$file': ".$tt->error;
173 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
177 B<More template vars?> e.g. [% tables %] as a shortcut for
178 [% schema.get_tables %].