SQL::Tr...::Table(3)
okay so bestie, welcome to SQL::Tr...::Table(3) from the User Contributed Perl Documentation π
β¨ This is the SQL::Translator::Producer::TT::Table page, and itβs giving βTemplate Toolkit + SQL schema, one table at a time.β π¦
πΈ NAME πΈ
SQL::Translator::Producer::TT::Table β produces output using the Template Toolkit from a SQL schema, per table. π
β¨ SYNOPSIS β¨
Two modes, bestie: straight to STDOUT, or one file per table. The code below is exact, no cap π:
# 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
#
π DESCRIPTION π
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. (Yes, βTookitβ is literally in the original β we preserve, we donβt fix ππ)
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 SQL::Translator::Schema::Table 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 SQL::Translator::Schema for the table and translator, the SQL::Translator 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 initialize 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). Itβs also particularly good for code generation where you want to produce a class file per table. π
π οΈ OPTIONS π οΈ
Every option, exactly as listed β no skipping, periodt π₯
tt_tableβ File name of the template to run for each table.mk_filesβ Set to true to output a file for each table in the schema (as well as returning the whole lot back to theTranslalorand hence STDOUT). The file will be named after the table, with the optionalmk_files_extadded and placed in the directorymk_files_base.mk_files_extβ Extension (without the dot) to add to the filename when usingmk_files.mk_files_base = DIRβ Dir to build the table files into when usingmk_files. Defaults to the current directory.mk_file_dirβ Set true and if the file needs to written to a directory that doesnβt exist, it will be created first.on_exists [Default:replace]β What to do if we are running withmk_filesand a file already exists where we want to write our output. One ofskip,die,replace,insert. The default is die. (The heading says[Default:replace]while the text says the default isdieβ literally both are in the original, bestie ππ)replaceβ Over-write the existing file with the new one, clobbering anything already there.skipβ Leave the original 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 between 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_comment_startβ The comment to look for in the file whenon_existsisinsert. Default isSQLF INSERT START. Must appear on it own line, with only whitespace either side, to be recognised.insert_comment_endβ The end comment to look for in the file whenon_existsisinsert. Default isSQLF INSERT END. Must appear on it own line, with only whitespace either side, to be recognised.
π AUTHOR π
Mark Addison <grommit@users.sourceforge.net>. π
π§ TODO π§
- Some tests for the various on exists options (they have been tested implicitly through use in a project but need some proper tests).
- More docs on code generation strategies.
- Better hooks for filename generation.
- Integrate with
TT::BaseandTTSchema.
π SEE ALSO π
SQL::Translator.
okay thatβs the page, bestie π«Ά β this was SQL::Tr...::Table(3) from the perl v5.42.0 docs, dated 2024-12-12. Bye and slay β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-SQL-Translator 1.660.0-bp160.1.11 Β· (Artistic-1.0 OR GPL-1.0-or-later) AND GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms