d655e1f77fe20880cc3fec9e9bfbe573378289c3
[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 # -------------------------------------------------------------------
38
39 use strict;
40
41 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
42 $VERSION = '1.59';
43 $DEBUG   = 0 unless defined $DEBUG;
44
45 use SQL::Translator::Utils 'debug';
46
47 sub produce {
48     my $translator     = shift;
49     my $schema         = $translator->schema;
50     my $o = '';
51     for my $table ( $schema->get_tables ) {
52         my $table_name    = $table->name or next;
53         my $n = latex($table_name);
54         $o .=
55           sprintf '
56 \subsubsection{%s}
57 %s
58 \begin{table}[htb]
59 \caption{%s}
60 \label{tab:%s}
61 \center
62 { \small
63   \begin{tabular}{l l p{8cm}}
64   Column & Datatype & Description \\\\ \hline
65 ',
66  $n, latex($table->comments), $n, $table_name;
67
68         foreach my $f ($table->get_fields) {
69             $o .= sprintf '%s & %s & %s \\\\', map {latex($_)} ($f->name, $f->data_type, $f->comments || '');
70             $o .= "\n";
71
72         }
73 $o .= sprintf '
74 \end{tabular}
75 }
76 \end{table}
77 \clearpage
78 ';
79     }
80     return $o;
81 }
82 sub latex {
83     my $s = shift;
84     return '' unless defined $s;
85     $s =~ s/([\&\_\$\{\#])/\\$1/g;
86     return $s;
87 }
88  
89 1;
90
91 # -------------------------------------------------------------------
92
93 =pod
94
95 =head1 AUTHOR
96
97 Chris Mungall 
98
99 =head1 SEE ALSO
100
101 SQL::Translator.
102
103 =cut