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