Changed test to match what's returned for the size of a "text" field.
[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
7SQL::Translator::Producer::TTSchema - Produces output using the template toolkit
8from a SQL schema.
9
10=cut
11
12
13use strict;
14use warnings;
15
16use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
17#$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
18$VERSION = 0.1;
19$DEBUG = 0 unless defined $DEBUG;
20
21use Data::Dumper;
22use Exporter;
23use base qw(Exporter);
24@EXPORT_OK = qw(produce);
25
26use base qw/SQL::Translator::Producer/; # Doesn't do anything at the mo!
27use Template;
28
29sub debug {
30 warn @_,"\n" if $DEBUG;
31}
32
33sub produce {
34 my $translator = shift;
35 local $DEBUG = $translator->debug;
36 my $scma = $translator->schema;
37 my $args = $translator->producer_args;
38 my $file = delete $args->{ttfile} or die "No template file!";
39
40 debug "Processing template $file\n";
41 my $out;
42 my $tt = Template->new(
43 DEBUG => $DEBUG,
44 ABSOLUTE => 1, # Set so we can use from the command line sensible.
45 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
46 %$args, # Allow any TT opts to be passed in the producer_args
47
48 ) || die "Failed to initialize Template object: ".Template->error;
49 $tt->process($file,{ schema => $scma },\$out)
50 or die "Error processing template '$file': ".$tt->error;
51
52 return $out;
53};
54
551;
56
57__END__
58
59=pod
60
61=head1 SYNOPSIS
62
63 use SQL::Translator;
64 $translator = SQL::Translator->new(
65 from => "MySQL",
66 filename => "foo_schema.sql",
67 to => "TT",
68 producer_args => {
69 ttfile => "foo_template.tt",
70 },
71 );
72 print $translator->translate;
73
74=head1 DESCRIPTION
75
76Produces schema output using a given Template Tookit template.
77
78It needs one additional producer_arg of C<ttfile> that is the file name of the
79template to use. This template has one var added to it called C<schema>, which
80is the SQL::Translator::Producer::Schema object so you can then template via
81its methods.
82
83 database: [% schema.database %]
84 tables:
85 [% FOREACH table = schema.get_tables %]
86 [% table.name %]
87 ================
88 [% FOREACH field = table.get_fields %]
89 [% field.name %] [% field.datatype %]([% field.size %])
90 [% END -%]
91 [% END %]
92
93See F<t/data/template/basic.tt> for a more complete example.
94
95You can also set any of the options used to initiallize the Template object by
96adding them to your producer_args. See Template Toolkit docs for details of
97the options.
98
99 $translator = SQL::Translator->new(
100 to => "TT",
101 producer_args => {
102 ttfile => "foo_template.tt",
103 INCLUDE_PATH => "/foo/templates/tt",
104 INTERPOLATE => 1,
105 },
106 );
107
108=head1 TODO
109
110B<More template vars?> e.g. [% tables %] as a shortcut for
111[% schema.get_tables %].
112
113=cut