1 package SQL::Translator::Producer::TT::Base;
7 SQL::Translator::Producer::TT::Base - TT (Template Toolkit) based Producer base
14 use vars qw[ $VERSION @EXPORT_OK ];
21 use base qw(Exporter);
22 @EXPORT_OK = qw(produce);
24 use SQL::Translator::Utils 'debug';
26 # Hack to convert the produce call into an object. ALL sub-classes need todo
27 # this so that the correct class gets created.
29 return __PACKAGE__->new( translator => shift )->run;
34 my $class = ref $proto || $proto;
37 my $me = bless {}, $class;
38 $me->{translator} = delete $args{translator} || die "Need a translator.";
43 sub translator { shift->{translator}; }
44 sub schema { shift->{translator}->schema(@_); }
46 # Util args access method.
47 # No args - Return hashref (the actual hash in Translator) or hash of args.
48 # 1 arg - Return that named args value.
49 # Args - List of names. Return values of the given arg names in list context
50 # or return as hashref in scalar context. Any names given that don't
51 # exist in the args are returned as undef.
58 ? %{ $me->{translator}->producer_args }
59 : $me->{translator}->producer_args
63 # 1 arg. Return the value whatever the context.
64 return $me->{translator}->producer_args->{$_[0]} if @_ == 1;
66 # More args so return values list or hash ref
67 my %args = %{ $me->{translator}->producer_args };
68 return wantarray ? @args{@_} : { map { ($_=>$args{$_}) } @_ };
71 # Run the produce and return the result.
74 my $scma = $me->schema;
75 my %args = %{$me->args};
76 my $tmpl = $me->tt_schema or die "No template!";
78 debug "Processing template $tmpl\n";
80 my $tt = Template->new(
81 #DEBUG => $me->translator->debug,
82 ABSOLUTE => 1, # Set so we can use from the command line sensibly
83 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
84 $me->tt_config, # Hook for sub-classes to add config
85 %args, # Allow any TT opts to be passed in the producer_args
86 ) || die "Failed to initialize Template object: ".Template->error;
88 $tt->process( $tmpl, {
90 $me->tt_vars, # Sub-class hook for adding vars
92 or die "Error processing template '$tmpl': ".$tt->error;
99 #-----------------------------------------------------------------------------
101 sub tt_config { () };
107 my $file = $me->args("ttfile");
108 return $file if $file;
111 my $ref = *{"$class\:\:DATA"}{IO};
112 if ( $ref->opened ) {
113 local $/ = undef; # Slurp mode
120 sub tt_default_vars {
123 translator => $me->translator,
124 schema => $me->pre_process_schema($me->translator->schema),
128 sub pre_process_schema { $_[1] }
134 # -------------------------------------------------------------------
140 # Create a producer using a template in the __DATA__ section.
141 package SQL::Translator::Producer::Foo;
143 use base qw/SQL::Translator::Producer::TT::Base/;
145 # Convert produce call into a method call on our new class
146 sub produce { return __PACKAGE__->new( translator => shift )->run; };
148 # Configure the Template object.
149 sub tt_config { ( INTERPOLATE => 1 ); }
151 # Extra vars to add to the template
152 sub tt_vars { ( foo => "bar" ); }
154 # Put template in DATA section (or use file with ttfile producer arg)
158 Database: [% schema.database %]
164 A base class producer designed to be sub-classed to create new TT based
165 producers cheaply - by simply giving the template to use and sprinkling in some
166 extra template variables and config.
168 You can find an introduction to this module in L<SQL::Translator::Manual>.
170 The 1st thing the module does is convert the produce sub routine call we get
171 from SQL::Translator into a method call on an object, which we can then
172 sub-class. This is done with the following code which needs to appear in B<all>
175 # Convert produce call into an object method call
176 sub produce { return __PACKAGE__->new( translator => shift )->run; };
178 See L<PRODUCER OBJECT> below for details.
180 The upshot of this is we can make new template producers by sub classing this
181 base class, adding the above snippet and a template.
182 The module also provides a number of hooks into the templating process,
183 see L<SUB CLASS HOOKS> for details.
185 See the L<SYNOPSIS> above for an example of creating a simple producer using
186 a single template stored in the producers DATA section.
188 =head1 SUB CLASS HOOKS
190 Sub-classes can override these methods to control the templating by giving
191 the template source, adding variables and giving config to the Tempate object.
195 sub tt_config { ( INTERPOLATE => 1 ); }
197 Return hash of Template config to add to that given to the L<Template> C<new>
202 sub tt_schema { "foo.tt"; }
203 sub tt_schema { local $/ = undef; \<DATA>; }
205 The template to use, return a file name or a scalar ref of TT
206 source, or an L<IO::Handle>. See L<Template> for details, as the return from
207 this is passed on to it's C<produce> method.
209 The default implimentation uses the producer arg C<ttfile> as a filename to read
210 the template from. If the arg isn't there it will look for a C<__DATA__> section
211 in the class, reading it as template source if found. Returns undef if both
212 these fail, causing the produce call to fail with a 'no template!' error.
216 sub tt_vars { ( foo => "bar" ); }
218 Return hash of template vars to use in the template. Nothing added here
219 by default, but see L<tt_default_vars> for the variables you get for free.
221 =head2 tt_default_vars
223 Return a hash-ref of the default vars given to the template.
224 You wouldn't normally over-ride this, just inherit the default implimentation,
225 to get the C<translator> & C<schema> variables, then over-ride L<tt_vars> to add
228 The current default variables are:
234 The schema to template.
238 The L<SQL::Translator> object.
242 =head2 pre_process_schema
244 WARNING: This method is Experimental so may change!
246 Called with the L<SQL::Translator::Schema> object and should return one (it
247 doesn't have to be the same one) that will become the C<schema> varibale used
250 Gets called from tt_default_vars.
252 =head1 PRODUCER OBJECT
254 The rest of the methods in the class set up a sub-classable producer object.
255 You normally just inherit them.
259 my $tt_producer = TT::Base->new( translator => $translator );
261 Construct a new TT Producer object. Takes a single, named arg of the
262 L<SQL::Translator> object running the translation. Dies if this is not given.
266 Return the L<SQL::Translator> object.
270 Return the L<SQL::Translator::Schema> we are translating. This is equivilent
271 to C<< $tt_producer->translator->schema >>.
275 Called to actually produce the output, calling the sub class hooks. Returns the
280 Util wrapper method around C<< TT::Base->translator->producer_args >> for
281 (mostley) readonly access to the producer args. How it works depends on the
282 number of arguments you give it and the context.
284 No args - Return hashref (the actual hash in Translator) or hash of args.
285 1 arg - Return value of the arg with the passed name.
286 2+ args - List of names. In list context returns values of the given arg
287 names, returns as a hashref in scalar context. Any names given
288 that don't exist in the args are returned as undef.
290 This is still a bit messy but is a handy way to access the producer args when
291 you use your own to drive the templating.
301 - Add support for a sqlf template repository, set as an INCLUDE_PATH,
302 so that sub-classes can easily include file based templates using relative
305 - Pass in template vars from the producer args and command line.
307 - Merge in TT::Table.
309 - Hooks to pre-process the schema and post-process the output.
313 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.