SQL::Translator::Producer::TT::Table - Produces output using the Template Toolkit from a SQL schema, per table. |
SQL::Translator::Producer::TT::Table - Produces output using the Template Toolkit from a SQL schema, per table.
# Normal STDOUT version # my $translator = SQL::Translator->new( from => 'MySQL', filename => 'foo_schema.sql', to => 'TT::Table', producer_args => { tt_table => 'foo_table.tt', }, ); print $translator->translate;
# To generate a file per table # my $translator = SQL::Translator->new( from => 'MySQL', filename => 'foo_schema.sql', to => 'TT::Table', producer_args => { tt_table => 'foo_table.tt.html', mk_files => 1, mk_files_base => "./doc/tables", mk_file_ext => ".html", on_exists => "replace", }, ); # # ./doc/tables/ now contains the templated tables as $tablename.html #
Produces schema output using a given Template Tookit template, processing that template for each table in the schema. Optionally allows you to write the result for each table to a separate file.
It needs one additional producer_arg of tt_table
which is the file
name of the template to use. This template will be passed a template
var of table
, which is the current
the SQL::Translator::Producer::Table manpage table we are producing, which you
can then use to walk the schema via the methods documented in that
module. You also get schema as a shortcut to the
the SQL::Translator::Producer::Schema manpage for the table and translator
,
the the SQL::Translator manpage object for this parse in case you want to get
access to any of the options etc set here.
Here's a brief example of what the template could look like:
[% table.name %] ================ [% FOREACH field = table.get_fields %] [% field.name %] [% field.data_type %]([% field.size %]) [% END -%]
See t/data/template/table.tt for a more complete example.
You can also set any of the options used to initiallize the Template object by adding them to your producer_args. See Template Toolkit docs for details of the options.
$translator = SQL::Translator->new( to => 'TT', producer_args => { ttfile => 'foo_template.tt', INCLUDE_PATH => '/foo/templates/tt', INTERPOLATE => 1, }, );
If you set mk_files
and its additional options the producer will
write a separate file for each table in the schema. This is useful for
producing things like HTML documentation where every table gets its
own page (you could also use TTSchema producer to add an index page).
Its also particulary good for code generation where you want to
produce a class file per table.
mk_files_ext
added and placed in the directory mk_files_base
.
replace - Over-write the existing file with the new one, clobbering anything already there.
skip - Leave the origional file as it was and don't write the new version anywhere.
die - Die with an existing file error.
insert - Insert the generated output into the file bewteen a set of special comments (defined by the following options.) Any code between the comments will be overwritten (ie the results from a previous produce) but the rest of the file is left alone (your custom code). This is particularly useful for code generation as it allows you to generate schema derived code and then add your own custom code to the file. Then when the schema changes you just re-produce to insert the new code.
insert
. Default
is SQLF INSERT START
. Must appear on it own line, with only
whitespace either side, to be recognised.
insert
.
Default is SQLF INSERT END
. Must appear on it own line, with only
whitespace either side, to be recognised.
Mark Addison <grommit@users.sourceforge.net>.
- Some tests for the various on exists options (they have been tested implicitley through use in a project but need some proper tests).
- More docs on code generation strategies.
- Better hooks for filename generation.
- Integrate with the TT::Base manpage and TTSchema.
SQL::Translator.
SQL::Translator::Producer::TT::Table - Produces output using the Template Toolkit from a SQL schema, per table. |