Tweaked tt_schema
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TT / Base.pm
CommitLineData
f5f03b78 1package SQL::Translator::Producer::TT::Base;
2
3# -------------------------------------------------------------------
7f3fc883 4# $Id: Base.pm,v 1.2 2004-05-13 22:52:00 grommit Exp $
f5f03b78 5# -------------------------------------------------------------------
6# Copyright (C) 2002-4 SQLFairy Authors
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Producer::TT::Base - TT based Producer base class.
28
29=head1 SYNOPSIS
30
31 package SQL::Translator::Producer::Foo;
32 use base qw/SQL::Translator::Producer::TT::Base/;
33
34 # Convert produce call into an object of our new class
35 sub produce { return __PACKAGE__->new( translator => shift )->run; };
36
37 # Return file name or template source
38 sub tt_schema { local $/ = undef; return \<DATA>; }
39
40 # Extra vars to add to the template
41 sub tt_vars { ( foo => "bar" ); }
42
43=head1 DESCRIPTION
44
45A base class producer designed to be sub-classed to create new TT base
7f3fc883 46producers cheaply by simply giving the template to use and sprinkling in some
f5f03b78 47extra template variables.
48
49See the synopsis above for an example of creating a simple producer using
50a single template stored in the producers DATA section.
51
7f3fc883 52WARNING: This is currently WORK IN PROGRESS and so subject to change,
f5f03b78 53but it does work ;-)
54
55=cut
56
57# -------------------------------------------------------------------
58
59use strict;
60
61use vars qw[ $VERSION @EXPORT_OK ];
7f3fc883 62$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
f5f03b78 63
64use Template;
65use Data::Dumper;
66use Exporter;
67use base qw(Exporter);
68@EXPORT_OK = qw(produce);
69
70use SQL::Translator::Utils 'debug';
71
72# Hack to convert the produce call into an object. ALL sub-classes need todo
73# this so that the correct class gets created.
74sub produce {
75 return __PACKAGE__->new( translator => shift )->run;
76};
77
78sub new {
79 my $proto = shift;
80 my $class = ref $proto || $proto;
81 my %args = @_;
82
83 my $me = bless {}, $class;
84 $me->{translator} = delete $args{translator} || die "Need a translator.";
85
86 return $me;
87}
88
89sub translator { shift->{translator}; }
90sub schema { shift->{translator}->schema(@_); }
91
7f3fc883 92# Util args access method.
f5f03b78 93# No args - Return hashref (the actual hash in Translator) or hash of args.
94# 1 arg - Return that named args value.
95# Args - List of names. Return values of the given arg names in list context
96# or return as hashref in scalar context. Any names given that don't
7f3fc883 97# exist in the args are returned as undef.
f5f03b78 98sub args {
99 my $me = shift;
100
101 # No args
102 unless (@_) {
7f3fc883 103 return wantarray
f5f03b78 104 ? %{ $me->{translator}->producer_args }
105 : $me->{translator}->producer_args
106 ;
107 }
108
109 # 1 arg. Return the value whatever the context.
110 return $me->{translator}->producer_args->{$_[0]} if @_ == 1;
111
112 # More args so return values list or hash ref
113 my %args = %{ $me->{translator}->producer_args };
114 return wantarray ? @args{@_} : { map { ($_=>$args{$_}) } @_ };
115}
116
117# Run the produce and return the result.
118sub run {
119 my $me = shift;
120 my $scma = $me->schema;
121 my %args = %{$me->args};
122 my $tmpl = $me->tt_schema or die "No template!";
123
124 debug "Processing template $tmpl\n";
125 my $out;
7f3fc883 126 my $tt = Template->new(
f5f03b78 127 #DEBUG => $me->translator->debug,
7f3fc883 128 ABSOLUTE => 1, # Set so we can use from the command line sensibly
129 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
130 $me->tt_config, # Hook for sub-classes to add config
131 %args, # Allow any TT opts to be passed in the producer_args
f5f03b78 132 ) || die "Failed to initialize Template object: ".Template->error;
133
7f3fc883 134 $tt->process( $tmpl, {
135 $me->tt_default_vars,
136 $me->tt_vars, # Sub-class hook for adding vars
137 }, \$out )
f5f03b78 138 or die "Error processing template '$tmpl': ".$tt->error;
139
140 return $out;
141}
142
7f3fc883 143# Should returns a template file name to use, or a scalar ref of tt source, or
144# an io handle. See L<Template>
f5f03b78 145sub tt_schema { shift->args("ttfile") };
146
7f3fc883 147# Returns hash-ref of the default vars given to the template.
148# You wouldn't normally over-ride this but its here just in case.
f5f03b78 149sub tt_default_vars {
150 my $me = shift;
151 return (
152 translator => $me->translator,
153 schema => $me->translator->schema,
154 );
155}
156
7f3fc883 157# Return hash of template vars to add to the default set. Override this!
158sub tt_vars { () };
159
160# Return hash of Template config to add to the config given to the
161# Template->new method.
162sub tt_config { () };
f5f03b78 1631;
164
165# -------------------------------------------------------------------
166
167=pod
168
169=head1 AUTHOR
170
171Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
172
173=head1 TODO
174
175Lots! But the next things include;
176
f5f03b78 177- Add support for a sqlf template repository somewhere, set as an INCLUDE_PATH,
178so that sub-classes can easily include file based templates.
179
180- Merge in TT::Table.
181
182=head1 SEE ALSO
183
184SQL::Translator.
185
186=cut