Commit | Line | Data |
aca7d777 |
1 | package SQL::Translator::Producer::HTML; |
2 | |
aca7d777 |
3 | use strict; |
a11fbaea |
4 | use Data::Dumper; |
da06ac74 |
5 | use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME); |
6 | |
11ad2df9 |
7 | $VERSION = '1.59'; |
64e36ec8 |
8 | |
f9c96971 |
9 | $NAME = __PACKAGE__; |
64e36ec8 |
10 | $NOWRAP = 0 unless defined $NOWRAP; |
11 | $NOLINKTABLE = 0 unless defined $NOLINKTABLE; |
12 | |
13 | # Emit XHTML by default |
14 | $CGI::XHTML = $CGI::XHTML = 42; |
aca7d777 |
15 | |
16 | use SQL::Translator::Schema::Constants; |
aca7d777 |
17 | |
18 | # ------------------------------------------------------------------- |
64e36ec8 |
19 | # Main entry point. Returns a string containing HTML. |
20 | # ------------------------------------------------------------------- |
aca7d777 |
21 | sub produce { |
22 | my $t = shift; |
84474a81 |
23 | my $args = $t->producer_args; |
aca7d777 |
24 | my $schema = $t->schema; |
25 | my $schema_name = $schema->name || 'Schema'; |
84474a81 |
26 | my $title = $args->{'title'} || "Description of $schema_name"; |
64e36ec8 |
27 | my $wrap = ! (defined $args->{'nowrap'} |
28 | ? $args->{'nowrap'} |
29 | : $NOWRAP); |
30 | my $linktable = ! (defined $args->{'nolinktable'} |
31 | ? $args->{'nolinktable'} |
32 | : $NOLINKTABLE); |
33 | my %stylesheet = defined $args->{'stylesheet'} |
34 | ? ( -style => { src => $args->{'stylesheet'} } ) |
35 | : ( ); |
36 | my @html; |
1ea530d4 |
37 | my $q = defined $args->{'pretty'} |
29ecbf4e |
38 | ? do { require CGI::Pretty; |
39 | import CGI::Pretty; |
40 | CGI::Pretty->new } |
41 | : do { require CGI; |
ea93df61 |
42 | import CGI; |
29ecbf4e |
43 | CGI->new }; |
64e36ec8 |
44 | my ($table, @table_names); |
45 | |
46 | if ($wrap) { |
47 | push @html, |
48 | $q->start_html({ |
49 | -title => $title, |
50 | %stylesheet, |
51 | -meta => { generator => $NAME }, |
c012e81a |
52 | }), |
64e36ec8 |
53 | $q->h1({ -class => 'SchemaDescription' }, $title), |
64e36ec8 |
54 | $q->hr; |
55 | } |
56 | |
ea93df61 |
57 | @table_names = grep { length $_->name } $schema->get_tables; |
aca7d777 |
58 | |
64e36ec8 |
59 | if ($linktable) { |
60 | # Generate top menu, with links to full table information |
61 | my $count = scalar(@table_names); |
62 | $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's'; |
aca7d777 |
63 | |
64e36ec8 |
64 | # Leading table of links |
ea93df61 |
65 | push @html, |
64e36ec8 |
66 | $q->comment("Table listing ($count)"), |
a3de76be |
67 | $q->a({ -name => 'top' }), |
68 | $q->start_table({ -width => '100%', -class => 'LinkTable'}), |
64e36ec8 |
69 | |
70 | # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader" |
71 | $q->Tr( |
72 | $q->td({ -class => 'LinkTableCell' }, |
73 | $q->h2({ -class => 'LinkTableTitle' }, |
74 | 'Tables' |
75 | ), |
76 | ), |
77 | ); |
ad02944a |
78 | |
d8b098e5 |
79 | for my $table (@table_names) { |
64e36ec8 |
80 | my $table_name = $table->name; |
ea93df61 |
81 | push @html, |
64e36ec8 |
82 | $q->comment("Start link to table '$table_name'"), |
83 | $q->Tr({ -class => 'LinkTableRow' }, |
84 | $q->td({ -class => 'LinkTableCell' }, |
85 | qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>] |
86 | ) |
87 | ), |
88 | $q->comment("End link to table '$table_name'"); |
ad02944a |
89 | } |
64e36ec8 |
90 | push @html, $q->end_table; |
ad02944a |
91 | } |
92 | |
64e36ec8 |
93 | for my $table ($schema->get_tables) { |
84474a81 |
94 | my $table_name = $table->name or next; |
aca7d777 |
95 | my @fields = $table->get_fields or next; |
64e36ec8 |
96 | push @html, |
a3de76be |
97 | $q->comment("Starting table '$table_name'"), |
98 | $q->a({ -name => $table_name }), |
64e36ec8 |
99 | $q->table({ -class => 'TableHeader', -width => '100%' }, |
100 | $q->Tr({ -class => 'TableHeaderRow' }, |
101 | $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)), |
102 | qq[<a name="$table_name">], |
103 | $q->td({ -class => 'TableHeaderCell', -align => 'right' }, |
104 | qq[<a href="#top">Top</a>] |
105 | ) |
106 | ) |
107 | ); |
aca7d777 |
108 | |
84474a81 |
109 | if ( my @comments = map { $_ ? $_ : () } $table->comments ) { |
64e36ec8 |
110 | push @html, |
111 | $q->b("Comments:"), |
112 | $q->br, |
113 | $q->em(map { $q->br, $_ } @comments); |
a11fbaea |
114 | } |
115 | |
aca7d777 |
116 | # |
117 | # Fields |
118 | # |
64e36ec8 |
119 | push @html, |
a3de76be |
120 | $q->start_table({ -border => 1 }), |
64e36ec8 |
121 | $q->Tr( |
122 | $q->th({ -class => 'FieldHeader' }, |
ea93df61 |
123 | [ |
124 | 'Field Name', |
125 | 'Data Type', |
126 | 'Size', |
127 | 'Default Value', |
128 | 'Other', |
129 | 'Foreign Key' |
64e36ec8 |
130 | ] |
ea93df61 |
131 | ) |
64e36ec8 |
132 | ); |
aca7d777 |
133 | |
3d4edd30 |
134 | my $i = 0; |
aca7d777 |
135 | for my $field ( @fields ) { |
84474a81 |
136 | my $name = $field->name || ''; |
aca7d777 |
137 | $name = qq[<a name="$table_name-$name">$name</a>]; |
84474a81 |
138 | my $data_type = $field->data_type || ''; |
139 | my $size = defined $field->size ? $field->size : ''; |
ea93df61 |
140 | my $default = defined $field->default_value |
84474a81 |
141 | ? $field->default_value : ''; |
142 | my $comment = $field->comments || ''; |
143 | my $fk = ''; |
aca7d777 |
144 | |
64e36ec8 |
145 | if ($field->is_foreign_key) { |
84474a81 |
146 | my $c = $field->foreign_key_reference; |
147 | my $ref_table = $c->reference_table || ''; |
148 | my $ref_field = ($c->reference_fields)[0] || ''; |
ea93df61 |
149 | $fk = |
aca7d777 |
150 | qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>]; |
151 | } |
152 | |
84474a81 |
153 | my @other = (); |
aca7d777 |
154 | push @other, 'PRIMARY KEY' if $field->is_primary_key; |
155 | push @other, 'UNIQUE' if $field->is_unique; |
156 | push @other, 'NOT NULL' unless $field->is_nullable; |
ad02944a |
157 | push @other, $comment if $comment; |
3d4edd30 |
158 | my $class = $i++ % 2 ? 'even' : 'odd'; |
64e36ec8 |
159 | push @html, |
160 | $q->Tr( |
3d4edd30 |
161 | { -class => "tr-$class" }, |
64e36ec8 |
162 | $q->td({ -class => "FieldCellName" }, $name), |
163 | $q->td({ -class => "FieldCellType" }, $data_type), |
164 | $q->td({ -class => "FieldCellSize" }, $size), |
165 | $q->td({ -class => "FieldCellDefault" }, $default), |
166 | $q->td({ -class => "FieldCellOther" }, join(', ', @other)), |
167 | $q->td({ -class => "FieldCellFK" }, $fk), |
168 | ); |
aca7d777 |
169 | } |
64e36ec8 |
170 | push @html, $q->end_table; |
aca7d777 |
171 | |
172 | # |
173 | # Indices |
174 | # |
175 | if ( my @indices = $table->get_indices ) { |
ea93df61 |
176 | push @html, |
64e36ec8 |
177 | $q->h3('Indices'), |
178 | $q->start_table({ -border => 1 }), |
179 | $q->Tr({ -class => 'IndexRow' }, |
ea93df61 |
180 | $q->th([ 'Name', 'Fields' ]) |
64e36ec8 |
181 | ); |
aca7d777 |
182 | |
183 | for my $index ( @indices ) { |
84474a81 |
184 | my $name = $index->name || ''; |
185 | my $fields = join( ', ', $index->fields ) || ''; |
186 | |
64e36ec8 |
187 | push @html, |
188 | $q->Tr({ -class => 'IndexCell' }, |
189 | $q->td( [ $name, $fields ] ) |
190 | ); |
aca7d777 |
191 | } |
192 | |
64e36ec8 |
193 | push @html, $q->end_table; |
aca7d777 |
194 | } |
195 | |
a3de76be |
196 | # |
197 | # Constraints |
198 | # |
ea93df61 |
199 | my @constraints = |
a3de76be |
200 | grep { $_->type ne PRIMARY_KEY } $table->get_constraints; |
201 | if ( @constraints ) { |
ea93df61 |
202 | push @html, |
a3de76be |
203 | $q->h3('Constraints'), |
204 | $q->start_table({ -border => 1 }), |
205 | $q->Tr({ -class => 'IndexRow' }, |
ea93df61 |
206 | $q->th([ 'Type', 'Fields' ]) |
a3de76be |
207 | ); |
208 | |
209 | for my $c ( @constraints ) { |
210 | my $type = $c->type || ''; |
211 | my $fields = join( ', ', $c->fields ) || ''; |
212 | |
213 | push @html, |
214 | $q->Tr({ -class => 'IndexCell' }, |
215 | $q->td( [ $type, $fields ] ) |
216 | ); |
217 | } |
218 | |
219 | push @html, $q->end_table; |
220 | } |
221 | |
64e36ec8 |
222 | push @html, $q->hr; |
aca7d777 |
223 | } |
224 | |
a3de76be |
225 | my $sqlt_version = $t->version; |
64e36ec8 |
226 | if ($wrap) { |
227 | push @html, |
228 | qq[Created by <a href="http://sqlfairy.sourceforge.net">], |
a3de76be |
229 | qq[SQL::Translator $sqlt_version</a>], |
64e36ec8 |
230 | $q->end_html; |
231 | } |
aca7d777 |
232 | |
64e36ec8 |
233 | |
234 | return join "\n", @html; |
aca7d777 |
235 | } |
236 | |
237 | 1; |
238 | |
239 | # ------------------------------------------------------------------- |
240 | # Always be ready to speak your mind, |
241 | # and a base man will avoid you. |
242 | # William Blake |
243 | # ------------------------------------------------------------------- |
244 | |
245 | =head1 NAME |
246 | |
247 | SQL::Translator::Producer::HTML - HTML producer for SQL::Translator |
248 | |
249 | =head1 SYNOPSIS |
250 | |
251 | use SQL::Translator::Producer::HTML; |
252 | |
253 | =head1 DESCRIPTION |
254 | |
255 | Creates an HTML document describing the tables. |
256 | |
64e36ec8 |
257 | The HTML produced is composed of a number of tables: |
258 | |
259 | =over 4 |
260 | |
261 | =item Links |
262 | |
263 | A link table sits at the top of the output, and contains anchored |
264 | links to elements in the rest of the document. |
265 | |
266 | If the I<nolinktable> producer arg is present, then this table is not |
267 | produced. |
268 | |
269 | =item Tables |
270 | |
271 | Each table in the schema has its own HTML table. The top row is a row |
272 | of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these |
273 | elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>, |
274 | I<Other> and I<Foreign Key>. Each successive row describes one field |
275 | in the table, and has a class of B<FieldCell$item>, where $item id |
276 | corresponds to the label of the column. For example: |
277 | |
278 | <tr> |
279 | <td class="FieldCellName"><a name="random-id">id</a></td> |
280 | <td class="FieldCellType">int</td> |
281 | <td class="FieldCellSize">11</td> |
282 | <td class="FieldCellDefault"></td> |
283 | <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td> |
284 | <td class="FieldCellFK"></td> |
285 | </tr> |
286 | |
287 | <tr> |
288 | <td class="FieldCellName"><a name="random-foo">foo</a></td> |
289 | <td class="FieldCellType">varchar</td> |
290 | <td class="FieldCellSize">255</td> |
291 | <td class="FieldCellDefault"></td> |
292 | <td class="FieldCellOther">NOT NULL</td> |
293 | <td class="FieldCellFK"></td> |
294 | </tr> |
295 | |
296 | <tr> |
297 | <td class="FieldCellName"><a name="random-updated">updated</a></td> |
298 | <td class="FieldCellType">timestamp</td> |
299 | <td class="FieldCellSize">0</td> |
300 | <td class="FieldCellDefault"></td> |
301 | <td class="FieldCellOther"></td> |
302 | <td class="FieldCellFK"></td> |
303 | </tr> |
304 | |
305 | =back |
306 | |
307 | Unless the I<nowrap> producer arg is present, the HTML will be |
308 | enclosed in a basic HTML header and footer. |
309 | |
310 | If the I<pretty> producer arg is present, the generated HTML will be |
311 | nicely spaced and human-readable. Otherwise, it will have very little |
312 | insignificant whitespace and be generally smaller. |
313 | |
314 | |
977651a5 |
315 | =head1 AUTHORS |
aca7d777 |
316 | |
f997b9ab |
317 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>, |
977651a5 |
318 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>. |
aca7d777 |
319 | |
320 | =cut |