4cfd8bde35fef8ae67d9abf36519a5bee525bf93
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Latex.pm
1 package SQL::Translator::Producer::Latex;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-6 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =pod
22
23 =head1 NAME
24
25 SQL::Translator::Producer::Latex -
26     Produces latex formatted tables ready for import from schema.
27
28 =head1 SYNOPSIS
29
30   use SQL::Translator;
31   my $translator     = SQL::Translator->new(
32       from           => 'MySQL',
33       filename       => 'foo_schema.sql',
34       to             => 'Latex',
35   );
36   print $translator->translate;
37
38 =head1 DESCRIPTION
39
40 Currently you will get one class (with the a table
41 stereotype) generated per table in the schema. The fields are added as
42 attributes of the classes and their datatypes set. It doesn't currently set any
43 of the relationships. It doesn't do any layout, all the classses are in one big
44 stack. However it is still useful as you can use the layout tools in Dia to
45 automatically arrange them horizontally or vertically.
46
47 =head2 Producer Args
48
49 =over 4
50
51 =back
52
53 =cut
54
55 use strict;
56
57 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
58 $VERSION = '1.59';
59 $DEBUG   = 0 unless defined $DEBUG;
60
61 use SQL::Translator::Utils 'debug';
62
63 sub produce {
64     my $translator     = shift;
65     my $schema         = $translator->schema;
66     my $o = '';
67     for my $table ( $schema->get_tables ) {
68         my $table_name    = $table->name or next;
69         my $n = latex($table_name);
70         $o .=
71           sprintf '
72 \subsubsection{%s}
73 %s
74 \begin{table}[htb]
75 \caption{%s}
76 \label{tab:%s}
77 \center
78 { \small
79   \begin{tabular}{l l p{8cm}}
80   Column & Datatype & Description \\\\ \hline
81 ',
82  $n, latex($table->comments), $n, $table_name;
83
84         foreach my $f ($table->get_fields) {
85             $o .= sprintf '%s & %s & %s \\\\', map {latex($_)} ($f->name, $f->data_type, $f->comments || '');
86             $o .= "\n";
87
88         }
89 $o .= sprintf '
90 \end{tabular}
91 }
92 \end{table}
93 \clearpage
94 ';
95     }
96     return $o;
97 }
98 sub latex {
99     my $s = shift;
100     return '' unless defined $s;
101     $s =~ s/([\&\_\$\{\#])/\\$1/g;
102     return $s;
103 }
104
105 1;
106
107 =pod
108
109 =head1 AUTHOR
110
111 Chris Mungall
112
113 =head1 SEE ALSO
114
115 SQL::Translator.
116
117 =cut