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