1 package SQL::Translator::Producer::TT::Base;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Producer::TT::Base - TT (Template Toolkit) based Producer base
32 use vars qw[ $VERSION @EXPORT_OK ];
39 use base qw(Exporter);
40 @EXPORT_OK = qw(produce);
42 use SQL::Translator::Utils 'debug';
44 # Hack to convert the produce call into an object. ALL sub-classes need todo
45 # this so that the correct class gets created.
47 return __PACKAGE__->new( translator => shift )->run;
52 my $class = ref $proto || $proto;
55 my $me = bless {}, $class;
56 $me->{translator} = delete $args{translator} || die "Need a translator.";
61 sub translator { shift->{translator}; }
62 sub schema { shift->{translator}->schema(@_); }
64 # Util args access method.
65 # No args - Return hashref (the actual hash in Translator) or hash of args.
66 # 1 arg - Return that named args value.
67 # Args - List of names. Return values of the given arg names in list context
68 # or return as hashref in scalar context. Any names given that don't
69 # exist in the args are returned as undef.
76 ? %{ $me->{translator}->producer_args }
77 : $me->{translator}->producer_args
81 # 1 arg. Return the value whatever the context.
82 return $me->{translator}->producer_args->{$_[0]} if @_ == 1;
84 # More args so return values list or hash ref
85 my %args = %{ $me->{translator}->producer_args };
86 return wantarray ? @args{@_} : { map { ($_=>$args{$_}) } @_ };
89 # Run the produce and return the result.
92 my $scma = $me->schema;
93 my %args = %{$me->args};
94 my $tmpl = $me->tt_schema or die "No template!";
96 debug "Processing template $tmpl\n";
98 my $tt = Template->new(
99 #DEBUG => $me->translator->debug,
100 ABSOLUTE => 1, # Set so we can use from the command line sensibly
101 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
102 $me->tt_config, # Hook for sub-classes to add config
103 %args, # Allow any TT opts to be passed in the producer_args
104 ) || die "Failed to initialize Template object: ".Template->error;
106 $tt->process( $tmpl, {
107 $me->tt_default_vars,
108 $me->tt_vars, # Sub-class hook for adding vars
110 or die "Error processing template '$tmpl': ".$tt->error;
117 #-----------------------------------------------------------------------------
119 sub tt_config { () };
125 my $file = $me->args("ttfile");
126 return $file if $file;
129 my $ref = *{"$class\:\:DATA"}{IO};
130 if ( $ref->opened ) {
131 local $/ = undef; # Slurp mode
138 sub tt_default_vars {
141 translator => $me->translator,
142 schema => $me->pre_process_schema($me->translator->schema),
146 sub pre_process_schema { $_[1] }
156 # Create a producer using a template in the __DATA__ section.
157 package SQL::Translator::Producer::Foo;
159 use base qw/SQL::Translator::Producer::TT::Base/;
161 # Convert produce call into a method call on our new class
162 sub produce { return __PACKAGE__->new( translator => shift )->run; };
164 # Configure the Template object.
165 sub tt_config { ( INTERPOLATE => 1 ); }
167 # Extra vars to add to the template
168 sub tt_vars { ( foo => "bar" ); }
170 # Put template in DATA section (or use file with ttfile producer arg)
174 Database: [% schema.database %]
180 A base class producer designed to be sub-classed to create new TT based
181 producers cheaply - by simply giving the template to use and sprinkling in some
182 extra template variables and config.
184 You can find an introduction to this module in L<SQL::Translator::Manual>.
186 The 1st thing the module does is convert the produce sub routine call we get
187 from SQL::Translator into a method call on an object, which we can then
188 sub-class. This is done with the following code which needs to appear in B<all>
191 # Convert produce call into an object method call
192 sub produce { return __PACKAGE__->new( translator => shift )->run; };
194 See L<PRODUCER OBJECT> below for details.
196 The upshot of this is we can make new template producers by sub classing this
197 base class, adding the above snippet and a template.
198 The module also provides a number of hooks into the templating process,
199 see L<SUB CLASS HOOKS> for details.
201 See the L<SYNOPSIS> above for an example of creating a simple producer using
202 a single template stored in the producers DATA section.
204 =head1 SUB CLASS HOOKS
206 Sub-classes can override these methods to control the templating by giving
207 the template source, adding variables and giving config to the Tempate object.
211 sub tt_config { ( INTERPOLATE => 1 ); }
213 Return hash of Template config to add to that given to the L<Template> C<new>
218 sub tt_schema { "foo.tt"; }
219 sub tt_schema { local $/ = undef; \<DATA>; }
221 The template to use, return a file name or a scalar ref of TT
222 source, or an L<IO::Handle>. See L<Template> for details, as the return from
223 this is passed on to it's C<produce> method.
225 The default implimentation uses the producer arg C<ttfile> as a filename to read
226 the template from. If the arg isn't there it will look for a C<__DATA__> section
227 in the class, reading it as template source if found. Returns undef if both
228 these fail, causing the produce call to fail with a 'no template!' error.
232 sub tt_vars { ( foo => "bar" ); }
234 Return hash of template vars to use in the template. Nothing added here
235 by default, but see L<tt_default_vars> for the variables you get for free.
237 =head2 tt_default_vars
239 Return a hash-ref of the default vars given to the template.
240 You wouldn't normally over-ride this, just inherit the default implimentation,
241 to get the C<translator> & C<schema> variables, then over-ride L<tt_vars> to add
244 The current default variables are:
250 The schema to template.
254 The L<SQL::Translator> object.
258 =head2 pre_process_schema
260 WARNING: This method is Experimental so may change!
262 Called with the L<SQL::Translator::Schema> object and should return one (it
263 doesn't have to be the same one) that will become the C<schema> varibale used
266 Gets called from tt_default_vars.
268 =head1 PRODUCER OBJECT
270 The rest of the methods in the class set up a sub-classable producer object.
271 You normally just inherit them.
275 my $tt_producer = TT::Base->new( translator => $translator );
277 Construct a new TT Producer object. Takes a single, named arg of the
278 L<SQL::Translator> object running the translation. Dies if this is not given.
282 Return the L<SQL::Translator> object.
286 Return the L<SQL::Translator::Schema> we are translating. This is equivilent
287 to C<< $tt_producer->translator->schema >>.
291 Called to actually produce the output, calling the sub class hooks. Returns the
296 Util wrapper method around C<< TT::Base->translator->producer_args >> for
297 (mostley) readonly access to the producer args. How it works depends on the
298 number of arguments you give it and the context.
300 No args - Return hashref (the actual hash in Translator) or hash of args.
301 1 arg - Return value of the arg with the passed name.
302 2+ args - List of names. In list context returns values of the given arg
303 names, returns as a hashref in scalar context. Any names given
304 that don't exist in the args are returned as undef.
306 This is still a bit messy but is a handy way to access the producer args when
307 you use your own to drive the templating.
317 - Add support for a sqlf template repository, set as an INCLUDE_PATH,
318 so that sub-classes can easily include file based templates using relative
321 - Pass in template vars from the producer args and command line.
323 - Merge in TT::Table.
325 - Hooks to pre-process the schema and post-process the output.
329 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.