SQL::Tr...TT::Base(3)
β¨ SQL::Translator::Producer::TT::Base β¨
Manual name: SQL::Tr...TT::Base(3) β from User Contributed Perl Documentation, bestie π«Ά
πΈ NAME πΈ
SQL::Translator::Producer::TT::Base β a TT (Template Toolkit) based Producer base class. That's the name, and honestly it's giving exactly what it needs to give π
.
π¦ SYNOPSIS π¦
okay so here's the whole setup: you create a producer using a template in the __DATA__ section. The code is literally this:
# Create a producer using a template in the __DATA__ section.
package SQL::Translator::Producer::Foo;
use base qw/SQL::Translator::Producer::TT::Base/;
# Convert produce call into a method call on our new class
sub produce { return __PACKAGE__->new( translator => shift )->run; };
# Configure the Template object.
sub tt_config { ( INTERPOLATE => 1 ); }
# Extra vars to add to the template
sub tt_vars { ( foo => "bar" ); }
# Put template in DATA section (or use file with ttfile producer arg)
__DATA__
Schema
Database: [% schema.database %]
Foo: $foo
...
π DESCRIPTION π
A base class producer designed to be sub-classed to create new TT based producers cheaply β by simply giving the template to use and sprinkling in some extra template variables and config. Cheap, iconic, effective. β¨
You can find an introduction to this module in SQL::Translator::Manual.
The 1st thing the module does is convert the produce sub routine call we get from SQL::Translator into a method call on an object, which we can then sub-class. This is done with the following code, which needs to appear in all sub classes.
# Convert produce call into an object method call
sub produce { return __PACKAGE__->new( translator => shift )->run; };
See "PRODUCER OBJECT" below for details.
The upshot of this is we can make new template producers by sub classing this base class, adding the above snippet and a template. The module also provides a number of hooks into the templating process, see "SUB CLASS HOOKS" for details.
See the "SYNOPSIS" above for an example of creating a simple producer using a single template stored in the producers DATA section.
π SUB CLASS HOOKS π
Sub-classes can override these methods to control the templating by giving the template source, adding variables and giving config to the Tempate object.
π tt_config π
sub tt_config { ( INTERPOLATE => 1 ); }
Return hash of Template config to add to that given to the Template new method.
π tt_schema π
sub tt_schema { "foo.tt"; }
sub tt_schema { local $/ = undef; \<DATA>; }
The template to use, return a file name or a scalar ref of TT source, or an IO::Handle. See Template for details, as the return from this is passed on to its produce method.
The default implementation uses the producer arg ttfile as a filename to read the template from. If the arg isn't there it will look for a __DATA__ section in the class, reading it as template source if found. Returns undef if both these fail, causing the produce call to fail with a 'no template!' error.
π§ tt_vars π§
sub tt_vars { ( foo => "bar" ); }
Return hash of template vars to use in the template. Nothing added here by default, but see tt_default_vars for the variables you get for free.
π tt_default_vars π
Return a hash-ref of the default vars given to the template. You wouldn't normally over-ride this, just inherit the default implementation, to get the translator & schema variables, then over-ride tt_vars to add your own.
The current default variables are:
schemaβ the schema to template.translatorβ theSQL::Translatorobject.
β pre_process_schema β
WARNING: This method is Experimental so may change!
Called with the SQL::Translator::Schema object and should return one (it doesn't have to be the same one) that will become the schema variable used in the template.
Gets called from tt_default_vars.
π PRODUCER OBJECT π
The rest of the methods in the class set up a sub-classable producer object. You normally just inherit them.
πΈ new πΈ
my $tt_producer = TT::Base->new( translator => $translator );
Construct a new TT Producer object. Takes a single, named arg of the SQL::Translator object running the translation. Dies if this is not given.
π¦ translator π¦
Return the SQL::Translator object.
π schema π
Return the SQL::Translator::Schema we are translating. This is equivalent to $tt_producer->translator->schema.
π run π
Called to actually produce the output, calling the sub class hooks. Returns the produced text.
π args π
Util wrapper method around TT::Base->translator->producer_args for (mostly) readonly access to the producer args. How it works depends on the number of arguments you give it and the context.
- No args β Return hashref (the actual hash in Translator) or hash of args.
- 1 arg β Return value of the arg with the passed name.
- 2+ args β List of names. In list context returns values of the given arg names, returns as a hashref in scalar context. Any names given that don't exist in the args are returned as
undef.
This is still a bit messy but is a handy way to access the producer args when you use your own to drive the templating.
π SEE ALSO π
perlSQL::TranslatorTemplate
π₯ TODO π₯
- Add support for a sqlf template repository, set as an
INCLUDE_PATH, so that sub-classes can easily include file based templates using relative paths. - Pass in template vars from the producer args and command line.
- Merge in
TT::Table. - Hooks to pre-process the schema and post-process the output.
π«Ά AUTHOR π«Ά
Mark Addison <grommit@users.sourceforge.net>.
and that's the whole manual, bestie π cute sign-off: perl v5.42.0 Β· 2024-12-12 Β· SQL::Tr...TT::Base(3) β¨π«Ά
π¦ 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