Remove copyright headers from individual scripts
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TTSchema.pm
CommitLineData
2b2601b5 1package SQL::Translator::Producer::TTSchema;
2
3=pod
4
5=head1 NAME
6
046d668a 7SQL::Translator::Producer::TTSchema -
fcc6f51d 8 Produces output using the Template Toolkit from a SQL schema
9
10=head1 SYNOPSIS
11
12 use SQL::Translator;
13 my $translator = SQL::Translator->new(
14 from => 'MySQL',
15 filename => 'foo_schema.sql',
16 to => 'TTSchema',
17 producer_args => {
046d668a 18 ttfile => 'foo_template.tt', # Template file to use
19
20 # Extra template variables
21 ttargs => {
22 author => "Mr Foo",
23 },
9d6bd3c7 24
25 # Template config options
26 ttargs => {
27 INCLUDE_PATH => '/foo/templates',
28 },
fcc6f51d 29 },
30 );
31 print $translator->translate;
32
33=head1 DESCRIPTION
34
35Produces schema output using a given Template Tookit template.
36
91c59bcf 37It needs one additional producer_arg of C<ttfile> which is the file
046d668a 38name of the template to use. This template will be passed a variable
39called C<schema>, which is the C<SQL::Translator::Producer::Schema> object
40created by the parser. You can then use it to walk the schema via the
41methods documented in that module.
91c59bcf 42
43Here's a brief example of what the template could look like:
44
45 database: [% schema.database %]
46 tables:
47 [% FOREACH table = schema.get_tables %]
48 [% table.name %]
49 ================
50 [% FOREACH field = table.get_fields %]
51 [% field.name %] [% field.data_type %]([% field.size %])
52 [% END -%]
53 [% END %]
fcc6f51d 54
55See F<t/data/template/basic.tt> for a more complete example.
56
046d668a 57The template will also get the set of extra variables given as a hashref via the
9d6bd3c7 58C<tt_vars> producer arg.
046d668a 59
60You can set any of the options used to initiallize the Template object by
9d6bd3c7 61adding a tt_conf producer_arg. See Template Toolkit docs for details of
fcc6f51d 62the options.
9d6bd3c7 63(Note that the old style of passing this config directly in the producer args
64has been deprecated).
65
fcc6f51d 66
67 $translator = SQL::Translator->new(
68 to => 'TT',
69 producer_args => {
70 ttfile => 'foo_template.tt',
10f36920 71 ttargs => {},
9d6bd3c7 72 tt_conf = {
73 INCLUDE_PATH => '/foo/templates/tt',
74 INTERPOLATE => 1,
75 }
fcc6f51d 76 },
77 );
2b2601b5 78
91c59bcf 79You can use this producer to create any type of text output you like,
80even using it to create your own versions of what the other producers
81make. For example, you could create a template that translates the
82schema into MySQL's syntax, your own HTML documentation, your own
83Class::DBI classes (or some other code) -- the opportunities are
84limitless!
85
046d668a 86=head2 Producer Args
87
88=over 4
89
90=item ttfile
91
92The template file to generate the output with.
93
9d6bd3c7 94=item tt_vars
046d668a 95
96A hash ref of extra variables you want to add to the template.
97
9d6bd3c7 98=item tt_conf
99
100A hash ref of configuration options to pass to the L<Template> object's
101constructor.
102
046d668a 103=back
104
2b2601b5 105=cut
106
fcc6f51d 107# -------------------------------------------------------------------
2b2601b5 108
109use strict;
2b2601b5 110
da06ac74 111use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
11ad2df9 112$VERSION = '1.59';
2b2601b5 113$DEBUG = 0 unless defined $DEBUG;
114
fcc6f51d 115use Template;
2b2601b5 116use Data::Dumper;
117use Exporter;
118use base qw(Exporter);
119@EXPORT_OK = qw(produce);
120
91c59bcf 121use SQL::Translator::Utils 'debug';
2b2601b5 122
123sub produce {
124 my $translator = shift;
fcc6f51d 125 local $DEBUG = $translator->debug;
126 my $scma = $translator->schema;
127 my $args = $translator->producer_args;
128 my $file = delete $args->{'ttfile'} or die "No template file!";
9d6bd3c7 129
130 my $tt_vars = delete $args->{'tt_vars'} || {};
329fc3f0 131 if ( exists $args->{ttargs} ) {
132 warn "Use of 'ttargs' producer arg is deprecated."
133 ." Please use 'tt_vars' instead.\n";
9d6bd3c7 134 %$tt_vars = { %{$args->{ttargs}}, %$tt_vars };
135 }
136
137 my %tt_conf = exists $args->{tt_conf} ? %{$args->{tt_conf}} : ();
138 # sqlt passes the producer args for _all_ producers in, so we use this
139 # grep hack to test for the old usage.
bced3fa7 140 debug(Dumper(\%tt_conf));
9d6bd3c7 141 if ( grep /^[A-Z_]+$/, keys %$args ) {
142 warn "Template config directly in the producer args is deprecated."
143 ." Please use 'tt_conf' instead.\n";
144 %tt_conf = ( %tt_conf, %$args );
329fc3f0 145 }
046d668a 146
2b2601b5 147 debug "Processing template $file\n";
148 my $out;
fcc6f51d 149 my $tt = Template->new(
150 DEBUG => $DEBUG,
91c59bcf 151 ABSOLUTE => 1, # Set so we can use from the command line sensibly
fcc6f51d 152 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
9d6bd3c7 153 %tt_conf,
bced3fa7 154 );
155 debug("Template ERROR: " . Template->error. "\n") if(!$tt);
156 $tt || die "Failed to initialize Template object: ".Template->error;
fcc6f51d 157
bced3fa7 158 my $ttproc = $tt->process(
046d668a 159 $file,
9d6bd3c7 160 { schema => $scma , %$tt_vars },
046d668a 161 \$out
bced3fa7 162 );
163 debug("ERROR: ". $tt->error. "\n") if(!$ttproc);
164 $ttproc or die "Error processing template '$file': ".$tt->error;
2b2601b5 165
166 return $out;
167};
168
1691;
170
fcc6f51d 171# -------------------------------------------------------------------
2b2601b5 172
173=pod
174
fcc6f51d 175=head1 AUTHOR
2b2601b5 176
fcc6f51d 177Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
2b2601b5 178
179=head1 TODO
180
181B<More template vars?> e.g. [% tables %] as a shortcut for
182[% schema.get_tables %].
183
fcc6f51d 184=head1 SEE ALSO
185
186SQL::Translator.
187
2b2601b5 188=cut