Added TTSchema producer.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / TTSchema.pm
1 package SQL::Translator::Producer::TTSchema;
2
3 =pod 
4
5 =head1 NAME
6
7 SQL::Translator::Producer::TTSchema - Produces output using the template toolkit
8 from a SQL schema.
9
10 =cut
11
12
13 use strict;
14 use warnings;
15
16 use 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
21 use Data::Dumper;
22 use Exporter;
23 use base qw(Exporter);
24 @EXPORT_OK = qw(produce);
25
26 use base qw/SQL::Translator::Producer/;  # Doesn't do anything at the mo!
27 use Template;
28
29 sub debug {
30     warn @_,"\n" if $DEBUG;
31 }
32
33 sub 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
55 1;
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
76 Produces schema output using a given Template Tookit template.
77
78 It needs one additional producer_arg of C<ttfile> that is the file name of the
79 template to use. This template has one var added to it called C<schema>, which 
80 is the SQL::Translator::Producer::Schema object so you can then template via 
81 its 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
93 See F<t/data/template/basic.tt> for a more complete example.
94
95 You can also set any of the options used to initiallize the Template object by 
96 adding them to your producer_args. See Template Toolkit docs for details of
97 the 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
110 B<More template vars?> e.g. [% tables %] as a shortcut for
111 [% schema.get_tables %].
112
113 =cut