use warnings
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Latex.pm
1 package SQL::Translator::Producer::Latex;
2
3 =pod
4
5 =head1 NAME
6
7 SQL::Translator::Producer::Latex -
8     Produces latex formatted tables ready for import from schema.
9
10 =head1 SYNOPSIS
11
12   use SQL::Translator;
13   my $translator     = SQL::Translator->new(
14       from           => 'MySQL',
15       filename       => 'foo_schema.sql',
16       to             => 'Latex',
17   );
18   print $translator->translate;
19
20 =head1 DESCRIPTION
21
22 Currently you will get one class (with the a table
23 stereotype) generated per table in the schema. The fields are added as
24 attributes of the classes and their datatypes set. It doesn't currently set any
25 of the relationships. It doesn't do any layout, all the classses are in one big
26 stack. However it is still useful as you can use the layout tools in Dia to
27 automatically arrange them horizontally or vertically.
28
29 =head2 Producer Args
30
31 =over 4
32
33 =back
34
35 =cut
36
37 use strict;
38 use warnings;
39
40 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
41 $VERSION = '1.59';
42 $DEBUG   = 0 unless defined $DEBUG;
43
44 use SQL::Translator::Utils 'debug';
45
46 sub produce {
47     my $translator     = shift;
48     my $schema         = $translator->schema;
49     my $o = '';
50     for my $table ( $schema->get_tables ) {
51         my $table_name    = $table->name or next;
52         my $n = latex($table_name);
53         $o .=
54           sprintf '
55 \subsubsection{%s}
56 %s
57 \begin{table}[htb]
58 \caption{%s}
59 \label{tab:%s}
60 \center
61 { \small
62   \begin{tabular}{l l p{8cm}}
63   Column & Datatype & Description \\\\ \hline
64 ',
65  $n, latex($table->comments), $n, $table_name;
66
67         foreach my $f ($table->get_fields) {
68             $o .= sprintf '%s & %s & %s \\\\', map {latex($_)} ($f->name, $f->data_type, $f->comments || '');
69             $o .= "\n";
70
71         }
72 $o .= sprintf '
73 \end{tabular}
74 }
75 \end{table}
76 \clearpage
77 ';
78     }
79     return $o;
80 }
81 sub latex {
82     my $s = shift;
83     return '' unless defined $s;
84     $s =~ s/([\&\_\$\{\#])/\\$1/g;
85     return $s;
86 }
87
88 1;
89
90 =pod
91
92 =head1 AUTHOR
93
94 Chris Mungall
95
96 =head1 SEE ALSO
97
98 SQL::Translator.
99
100 =cut