more readable tag names
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Manual.pod
CommitLineData
2e2fc2b4 1=head1 NAME
2
3SQL::Translator::Manual
4
5=head1 SYNOPSIS
6
cfdcb09e 7SQL::Translator (AKA "SQLFairy") is a collection of modules for
8transforming (mainly) SQL DDL files into a variety of other formats,
9including other SQL dialects, documentation, images, and code. In
10this manual, we will attempt to address how to use SQLFairy for common
11tasks. For a lower-level discussion of how the code works, please
12read the documentation for L<SQL::Translator>.
2e2fc2b4 13
cfdcb09e 14It may prove helpful to have a general understanding of the SQLFairy
15code before continuing. The code can be broken into three conceptual
16groupings:
2e2fc2b4 17
cfdcb09e 18=over 4
2e2fc2b4 19
cfdcb09e 20=item * Parsers
2e2fc2b4 21
cfdcb09e 22The parsers are responsible for reading the input files and describing
23them to the Schema object middleware.
2e2fc2b4 24
cfdcb09e 25=item * Producers
2e2fc2b4 26
cfdcb09e 27The producers create the output as described by the Schema middleware.
2e2fc2b4 28
cfdcb09e 29=item * Schema objects
30
31The Schema objects bridge the communication between the Parsers and
32Producers by representing any parsed file through a standard set of
33generic objects to represent concepts like Tables, Fields (columns),
34Indices, Constraints, etc.
35
36=back
37
38It's not necessary to understand how to write or manipulate any
39of these for most common tasks, but you should aware of the concepts
40as they will be referenced later in this document.
41
42=head1 SQLFAIRY SCRIPTS
43
44Most common tasks can be accomplished through the use of the script
45interfaces to the SQL::Translator code. All SQLFairy scripts begin
46with "sqlt." Here are the scripts and a description of what they each
47do:
48
49=over 4
50
51=item * sqlt
52
53This is the main interface for text-to-text translations, e.g.,
54converting a MySQL schema to Oracle.
55
56=item * sqlt-diagram
57
58This is a tailored interface for the Diagram producer and its many
59myriad options.
60
61=item * sqlt-diff
62
63This script will examine two schemas and report the SQL commands
64(ALTER, CREATE) needed to turn the first schema into the second.
65
66=item * sqlt-dumper
67
68This script generates a Perl script that can be used to connect to a
69database and dump the data in each table in different formats, similar
70to the "mysqldump" program.
71
72=item * sqlt-graph
73
74This is an interface to the GraphViz visualization tool and its myriad
75options.
76
77=item * sqlt.cgi
78
79This is a CGI script that presents an HTML form for uploading or
80pasting a schema and choosing an output and the output options.
81
82=back
83
84To read the full documentation for each script, use "perldoc" (or
85execute any of the command-line scripts with the "--help" flag).
86
87=head1 CONVERTING SQL DIALECTS
88
89Probably the most common task SQLFairy is used for is to convert one
90dialect of SQL to another. If you have a text description of an SQL
91database (AKA a "DDL" -- "Data Definition Language"), then you should
92use the "sqlt" script with switches to indicate the parser and
93producer and the name of the text file as the final argument. For
94example, to convert the "foo.sql" MySQL schema to a version suitable
95for PostgreSQL, you would do the following:
96
97 $ sqlt -f MySQL -t PostgreSQL foo.sql > foo-pg.sql
98
99The "from" and "to" options are case-sensitive and must match exactly
100the names of the Parser and Producer classes in SQL::Translator. For
101a complete listing of your options, execute "sqlt" with the "--list"
102flag.
103
104=head1 EXTRACT SQL SCHEMAS DIRECTLY FROM DATABASE
105
106It is possible to extract some schemas directly from the database
107without parsing a text file (the "foo.sql" in the above example).
108This can prove significantly faster than parsing a text file. To
109do this, use the "DBI" parser and provide the necessary arguments to
110connect to the database and indicate the producer class, like so:
111
112 $ sqlt -f DBI --dsn dbi:mysql:FOO --db-user guest \
113 --db-password p4ssw0rd -t PostgreSQL > foo
114
115The "--list" option to "sqlt" will show the databases supported by
116DBI parsers.
117
118=head1 HANDLING NON-SQL DATA
119
120Certain structured document formats can be easily thought of as
121tables. SQLFairy can parse Microsoft Excel spreadsheets and
122arbitrarily delimited text files just as if they were schemas which
123contained only one table definition. The column names are normalized
124to something sane for most databases (whitespace is converted to
125underscores and non-word characters are removed), and the data in each
126field is scanned to determine the appropriate data type (character,
127integer, or float) and size. For instance, to convert a
128comma-separated file to an SQLite database, do the following:
129
130 $ sqlt -f xSV --fs ',' -t SQLite foo.csv > foo-sqlite.sql
131
132Additionally, there are non-SQL represenations of relational schemas
133such as XML and XMI. Currently the XMI support in SQLFairy is
134experimental and not released. Additionally, the only XML supported
135is our own version; however, it would be fairly easy to add an XML
136parser for something like the TorqueDB (http://db.apache.org/torque/)
137project. The actual parsing of XML should be trivial given the number
138of XML parsers available, so all that would be left would be to map
139the specific concepts in the source file to the Schema objects in
140SQLFairy.
141
142To convert a schema in SQLFairy's XML dialect to Oracle, do the following:
143
144 $ sqlt -f XML-SQLFairy -t Oracle foo.xml > foo-oracle.sql
145
146=head1 SERIALIZING SCHEMAS
2e2fc2b4 147
cfdcb09e 148Parsing a schema is generally the most computationally expensive
149operation performed by SQLFairy, so it may behoove you to serialize a
150parsed schema if you need to perform repeated conversions. For
151example, as part of a build process the author converts a MySQL schema
152first to YAML, then to PostgreSQL, Oracle, SQLite and Sybase.
153Additionally, a variety of documention in HTML and images is produced.
154This can be accomplished like so:
155
156 $ sqlt -f MySQL -t YAML schema-mysql.sql > schema.yaml
157 $ sqlt -f YAML -t Oracle schema.yaml > schema-oracle.sql
158 $ sqlt -f YAML -t PostgreSQL schema.yaml > schema-postgresql.sql
159 $ ...
160
161SQLFairy has three serialization producers, none of which is superior
162to the other in their description of a schema.
163
164=over 4
165
166=item * XML-SQLFairy
167
168This is the aforementioned XML format. It is essentially a direct
169mapping of the Schema objects into XML. This can also provide a very
170convenient bridge to describing a schema to a non-Perl application.
171Providing a producer argument to "sqlt" of just "XML" will default to
172using "XML-SQLFairy."
173
174=item * Storable
175
176This producer stores the Schema object using Perl's Storable.pm module
177available on CPAN.
178
179=item * YAML
180
181This producer serialized the Schema object with the very readable
182structured data format of YAML (http://www.yaml.org/). Earlier
183examples show serializing to YAML.
184
185=back
186
187=head1 VISUALIZING SQL SCHEMAS
188
189The visualization tools in SQLFairy can graphically represent the
190tables, fields, datatypes and sizes, constraints, and foreign key
191relationships in a very compact and intuitive format. This can be
192very beneficial in understanding and document large or small schemas.
193Two producers in SQLFairy will create pseudo-E/R (entity-relationship)
194diagrams:
195
196=over 4
197
198=item * Diagram
199
200The first visualization tool in SQLFairy, this producer uses libgd to
201draw a picture of the schema. The tables are evenly distributed in
202definition order running in columns (i.e., no graphing algorithms are
203used), so the end result may result in many crossed lines showing the
204foreign key relationships. Please read the documentation of the
205"sqlt-diagram" script for all the options available to this producer.
206
207=item * GraphViz
208
209The layout of the GraphViz producer is far superior to the Diagram
210producer as it uses the Graphviz binary from Bell Labs to create very
211professional-looking graphs. There are several different layout
212algorithms and node shapes available. Please see the documentation of
213the "sqlt-graph" script for more information.
214
215=back
216
217=head1 AUTOMATED CODE-GENERATION
218
219Given that so many applications interact with SQL databases, it's no
220wonder that people have automated code to deal with this interaction.
221Class::DBI from CPAN is one such module that allows a developer to
222describe the relationships between tables and fields in class
223declarations and then generates all the SQL to interact (SELECT,
224UPDATE, DELETE, INSERT statements) at runtime. Obviously, the schema
225already describes itself, so it only makes sense that you should be
226able to generate this kind of code directly from the schema. The
227"ClassDBI" producer in SQLFairy does just this, creating a Perl module
228that inherits from Class::DBI and sets up most of the code needed to
229interact with the database. Here is an example of how to do this:
230
231 $ sqlt -f MySQL -t ClassDBI foo.sql > Foo.pm
232
233Then simply edit Foo.pm as needed and include it in your code.
234
21521f16 235=head1 CREATING A DATA DUMPER SCRIPT
236
237The Dumper producer creates a Perl script that can select the fields
238in each table and then create "INSERT" statements for each record in
239the database similar to the output generated by MySQL's "mysqldump"
240program:
241
242 $ sqlt -f YAML -t Dumper --dumper-db-user guest \
243 > --dumper-db-pass p4ssw0rd --dumper-dsn dbi:mysql:FOO \
244 > foo.yaml > foo-dumper.pl
245
246And then execute the resulting script to dump the data:
247
248 $ chmod +x foo-dumper.pl
249 $ ./foo-dumper.pl > foo-data.sql
250
251The dumper script also has a number of options available. Execute the
252script with the "--help" flag to read about them.
253
cfdcb09e 254=head1 DOCUMENTING WITH SQL::TRANSLATOR
2e2fc2b4 255
cfdcb09e 256SQLFairy offers two producers to help document schemas:
2e2fc2b4 257
cfdcb09e 258=over 4
2e2fc2b4 259
cfdcb09e 260=item * HTML
2e2fc2b4 261
cfdcb09e 262This producer creates a single HTML document which uses HTML
263formatting to describe the Schema objects and to create hyperlinks on
264foreign key relationships. This can be a surprisingly useful
265documentation aid as it creates a very readable format that allows one
266to jump easily to specific tables and fields. It's also possible to
267plugin your own CSS to further control the presentation of the HTML.
2e2fc2b4 268
cfdcb09e 269=item * POD
2e2fc2b4 270
cfdcb09e 271This is arguably not that useful of a producer by itself, but the
272number of POD-conversion tools could be used to further transform the
273POD into something more interesting. The schema is basically
274represented in POD sections where tables are broken down into fields,
275indices, constraints, foreign keys, etc.
2e2fc2b4 276
cfdcb09e 277=back
2e2fc2b4 278
cfdcb09e 279=head1 TEMPLATE-BASED MANIPULATION OF SCHEMA OBJECTS
2e2fc2b4 280
cfdcb09e 281All of the producers which create text output could have been coded
282using a templating system to mix in the dynamic output with static
283text. CPAN offers several diverse templating systems, but few are as
284powerful as Template Toolkit (http://www.template-toolkit.org/). You
285can easily create your own producer without writing any Perl code at
286all simply by writing a template using Template Toolkit's syntax. The
287template will be passed a reference to the Schema object briefly
288described at the beginning of this document and mentioned many times
289throughout. For example, you could create a template that simply
290prints the name of each table and field that looks like this:
2e2fc2b4 291
cfdcb09e 292 # file: schema.tt
293 [% FOREACH table IN schema.get_tables %]
294 Table: [% table.name %]
295 Fields:
296 [% FOREACH field IN table.get_fields -%]
297 [% field.name %]
298 [% END -%]
299 [% END %]
2e2fc2b4 300
cfdcb09e 301And the process it like so:
2e2fc2b4 302
cfdcb09e 303 $ sqlt -f YAML -t TTSchema --template schema.tt foo.yaml
2e2fc2b4 304
cfdcb09e 305To create output like this:
2e2fc2b4 306
cfdcb09e 307 Table: foo
308 Fields:
309 foo_id
310 foo_name
2e2fc2b4 311
cfdcb09e 312For more information on Template Toolkit, please install the
313"Template" module and read the POD.
2e2fc2b4 314
1b0fe900 315=head1 FINDING THE DIFFERENCES BETWEEN TWO SCHEMAS
316
317As mentioned above, the "sqlt-diff" schema examines two schemas and
318creates SQL schema modification statements that can be used to
319transform the first schema into the second. The flag syntax is
320somewhat quirky:
321
322 $ sqlt-diff foo-v1.sql=MySQL foo-v2.sql=Oracle > diff.sql
323
324As demonstrated, the schemas need not even be from the same vendor,
325though this is likely to produce some spurious results as
326datatypes are not currently viewed equivalent unless they match
327exactly, even if they would be converted to the same. For example,
328MySQL's "integer" data type would be converted to Oracle's "number,"
329but the differ isn't quite smart enough yet to figure this out. Also,
330as the SQL to ALTER a field definition varies from database vendor to
331vendor, these statements are made using just the keyword "CHANGE" and
332will likely need to be corrected for the target database.
333
334=head1 A UNIFIED GRAPHICAL INTERFACE
335
336Seeing all the above options and scripts, you may be pining for a
337single, graphical interface to handle all these transformations and
338choices. This is exactly what the "sqlt.cgi" script provides. Simply
339drop this script into your web server's CGI directory and enable the
340execute bit and you can point your web browser to an HTML form which
341provides a simple interface to all the SQLFairy parsers and producers.
342
cfdcb09e 343=head1 PLUGIN YOUR OWN PARSERS AND PRODUCERS
2e2fc2b4 344
cfdcb09e 345Now that you have seen how the parsers and producers interact via the
346Schema objects, you may wish to create your own versions to plugin.
2e2fc2b4 347
348=head1 AUTHOR
349
977651a5 350Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.