.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "SQL::Translator 3pm" .TH SQL::Translator 3pm 2024-01-20 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME SQL::Translator \- manipulate structured data definitions (SQL and more) .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use SQL::Translator; \& \& my $translator = SQL::Translator\->new( \& # Print debug info \& debug => 1, \& # Print Parse::RecDescent trace \& trace => 0, \& # Don\*(Aqt include comments in output \& no_comments => 0, \& # Print name mutations, conflicts \& show_warnings => 0, \& # Add "drop table" statements \& add_drop_table => 1, \& # to quote or not to quote, thats the question \& quote_identifiers => 1, \& # Validate schema object \& validate => 1, \& # Make all table names CAPS in producers which support this option \& format_table_name => sub {my $tablename = shift; return uc($tablename)}, \& # Null\-op formatting, only here for documentation\*(Aqs sake \& format_package_name => sub {return shift}, \& format_fk_name => sub {return shift}, \& format_pk_name => sub {return shift}, \& ); \& \& my $output = $translator\->translate( \& from => \*(AqMySQL\*(Aq, \& to => \*(AqOracle\*(Aq, \& # Or an arrayref of filenames, i.e. [ $file1, $file2, $file3 ] \& filename => $file, \& ) or die $translator\->error; \& \& print $output; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This documentation covers the API for SQL::Translator. For a more general discussion of how to use the modules and scripts, please see SQL::Translator::Manual. .PP SQL::Translator is a group of Perl modules that converts vendor-specific SQL table definitions into other formats, such as other vendor-specific SQL, ER diagrams, documentation (POD and HTML), XML, and Class::DBI classes. The main focus of SQL::Translator is SQL, but parsers exist for other structured data formats, including Excel spreadsheets and arbitrarily delimited text files. Through the separation of the code into parsers and producers with an object model in between, it's possible to combine any parser with any producer, to plug in custom parsers or producers, or to manipulate the parsed data via the built-in object model. Presently only the definition parts of SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT, UPDATE, DELETE). .SH CONSTRUCTOR .IX Header "CONSTRUCTOR" .SS new .IX Subsection "new" The constructor is called \f(CW\*(C`new\*(C'\fR, and accepts a optional hash of options. Valid options are: .IP \(bu 4 parser / from .IP \(bu 4 parser_args .IP \(bu 4 producer / to .IP \(bu 4 producer_args .IP \(bu 4 filters .IP \(bu 4 filename / file .IP \(bu 4 data .IP \(bu 4 debug .IP \(bu 4 add_drop_table .IP \(bu 4 quote_identifiers .IP \(bu 4 quote_table_names (DEPRECATED) .IP \(bu 4 quote_field_names (DEPRECATED) .IP \(bu 4 no_comments .IP \(bu 4 trace .IP \(bu 4 validate .PP All options are, well, optional; these attributes can be set via instance methods. Internally, they are; no (non-syntactical) advantage is gained by passing options to the constructor. .SH METHODS .IX Header "METHODS" .SS add_drop_table .IX Subsection "add_drop_table" Toggles whether or not to add "DROP TABLE" statements just before the create definitions. .SS quote_identifiers .IX Subsection "quote_identifiers" Toggles whether or not to quote identifiers (table, column, constraint, etc.) with a quoting mechanism suitable for the chosen Producer. The default (true) is to quote them. .SS quote_table_names .IX Subsection "quote_table_names" DEPRECATED \- A legacy proxy to "quote_identifiers" .SS quote_field_names .IX Subsection "quote_field_names" DEPRECATED \- A legacy proxy to "quote_identifiers" .SS no_comments .IX Subsection "no_comments" Toggles whether to print comments in the output. Accepts a true or false value, returns the current value. .SS producer .IX Subsection "producer" The \f(CW\*(C`producer\*(C'\fR method is an accessor/mutator, used to retrieve or define what subroutine is called to produce the output. A subroutine defined as a producer will be invoked as a function (\fInot a method\fR) and passed its container \f(CW\*(C`SQL::Translator\*(C'\fR instance, which it should call the \f(CW\*(C`schema\*(C'\fR method on, to get the \f(CW\*(C`SQL::Translator::Schema\*(C'\fR generated by the parser. It is expected that the function transform the schema structure to a string. The \f(CW\*(C`SQL::Translator\*(C'\fR instance is also useful for informational purposes; for example, the type of the parser can be retrieved using the \f(CW\*(C`parser_type\*(C'\fR method, and the \f(CW\*(C`error\*(C'\fR and \&\f(CW\*(C`debug\*(C'\fR methods can be called when needed. .PP When defining a producer, one of several things can be passed in: A module name (e.g., \f(CW\*(C`My::Groovy::Producer\*(C'\fR), a module name relative to the \f(CW\*(C`SQL::Translator::Producer\*(C'\fR namespace (e.g., \f(CW\*(C`MySQL\*(C'\fR), a module name and function combination (\f(CW\*(C`My::Groovy::Producer::transmogrify\*(C'\fR), or a reference to an anonymous subroutine. If a full module name is passed in (for the purposes of this method, a string containing "::" is considered to be a module name), it is treated as a package, and a function called "produce" will be invoked: \f(CW$modulename::produce\fR. If \f(CW$modulename\fR cannot be loaded, the final portion is stripped off and treated as a function. In other words, if there is no file named \&\fIMy/Groovy/Producer/transmogrify.pm\fR, \f(CW\*(C`SQL::Translator\*(C'\fR will attempt to load \fIMy/Groovy/Producer.pm\fR and use \f(CW\*(C`transmogrify\*(C'\fR as the name of the function, instead of the default \f(CW\*(C`produce\*(C'\fR. .PP .Vb 1 \& my $tr = SQL::Translator\->new; \& \& # This will invoke My::Groovy::Producer::produce($tr, $data) \& $tr\->producer("My::Groovy::Producer"); \& \& # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data) \& $tr\->producer("Sybase"); \& \& # This will invoke My::Groovy::Producer::transmogrify($tr, $data), \& # assuming that My::Groovy::Producer::transmogrify is not a module \& # on disk. \& $tr\->producer("My::Groovy::Producer::transmogrify"); \& \& # This will invoke the referenced subroutine directly, as \& # $subref\->($tr, $data); \& $tr\->producer(\e&my_producer); .Ve .PP There is also a method named \f(CW\*(C`producer_type\*(C'\fR, which is a string containing the classname to which the above \f(CW\*(C`produce\*(C'\fR function belongs. In the case of anonymous subroutines, this method returns the string "CODE". .PP Finally, there is a method named \f(CW\*(C`producer_args\*(C'\fR, which is both an accessor and a mutator. Arbitrary data may be stored in name => value pairs for the producer subroutine to access: .PP .Vb 3 \& sub My::Random::producer { \& my ($tr, $data) = @_; \& my $pr_args = $tr\->producer_args(); \& \& # $pr_args is a hashref. .Ve .PP Extra data passed to the \f(CW\*(C`producer\*(C'\fR method is passed to \&\f(CW\*(C`producer_args\*(C'\fR: .PP .Vb 1 \& $tr\->producer("xSV", delimiter => \*(Aq,\es*\*(Aq); \& \& # In SQL::Translator::Producer::xSV: \& my $args = $tr\->producer_args; \& my $delimiter = $args\->{\*(Aqdelimiter\*(Aq}; # value is ,\es* .Ve .SS parser .IX Subsection "parser" The \f(CW\*(C`parser\*(C'\fR method defines or retrieves a subroutine that will be called to perform the parsing. The basic idea is the same as that of \&\f(CW\*(C`producer\*(C'\fR (see above), except the default subroutine name is "parse", and will be invoked as \f(CW\*(C`$module_name::parse($tr, $data)\*(C'\fR. Also, the parser subroutine will be passed a string containing the entirety of the data to be parsed. .PP .Vb 2 \& # Invokes SQL::Translator::Parser::MySQL::parse() \& $tr\->parser("MySQL"); \& \& # Invokes My::Groovy::Parser::parse() \& $tr\->parser("My::Groovy::Parser"); \& \& # Invoke an anonymous subroutine directly \& $tr\->parser(sub { \& my $dumper = Data::Dumper\->new([ $_[1] ], [ "SQL" ]); \& $dumper\->Purity(1)\->Terse(1)\->Deepcopy(1); \& return $dumper\->Dump; \& }); .Ve .PP There is also \f(CW\*(C`parser_type\*(C'\fR and \f(CW\*(C`parser_args\*(C'\fR, which perform analogously to \f(CW\*(C`producer_type\*(C'\fR and \f(CW\*(C`producer_args\*(C'\fR .SS filters .IX Subsection "filters" Set or retrieve the filters to run over the schema during the translation, before the producer creates its output. Filters are sub routines called, in order, with the schema object to filter as the 1st arg and a hash of options (passed as a list) for the rest of the args. They are free to do whatever they want to the schema object, which will be handed to any following filters, then used by the producer. .PP Filters are set as an array, which gives the order they run in. Like parsers and producers, they can be defined by a module name, a module name relative to the SQL::Translator::Filter namespace, a module name and function name together or a reference to an anonymous subroutine. When using a module name a function called \f(CW\*(C`filter\*(C'\fR will be invoked in that package to do the work. .PP To pass args to the filter set it as an array ref with the 1st value giving the filter (name or sub) and the rest its args. e.g. .PP .Vb 10 \& $tr\->filters( \& sub { \& my $schema = shift; \& # Do stuff to schema here! \& }, \& DropFKeys, \& [ "Names", table => \*(Aqlc\*(Aq ], \& [ "Foo", foo => "bar", hello => "world" ], \& [ "Filter5" ], \& ); .Ve .PP Although you normally set them in the constructor, which calls through to filters. i.e. .PP .Vb 8 \& my $translator = SQL::Translator\->new( \& ... \& filters => [ \& sub { ... }, \& [ "Names", table => \*(Aqlc\*(Aq ], \& ], \& ... \& ); .Ve .PP See \fIt/36\-filters.t\fR for more examples. .PP Multiple set calls to filters are cumulative with new filters added to the end of the current list. .PP Returns the filters as a list of array refs, the 1st value being a reference to the filter sub and the rest its args. .SS show_warnings .IX Subsection "show_warnings" Toggles whether to print warnings of name conflicts, identifier mutations, etc. Probably only generated by producers to let the user know when something won't translate very smoothly (e.g., MySQL "enum" fields into Oracle). Accepts a true or false value, returns the current value. .SS translate .IX Subsection "translate" The \f(CW\*(C`translate\*(C'\fR method calls the subroutine referenced by the \&\f(CW\*(C`parser\*(C'\fR data member, then calls any \f(CW\*(C`filters\*(C'\fR and finally calls the \f(CW\*(C`producer\*(C'\fR sub routine (these members are described above). It accepts as arguments a number of things, in key => value format, including (potentially) a parser and a producer (they are passed directly to the \f(CW\*(C`parser\*(C'\fR and \f(CW\*(C`producer\*(C'\fR methods). .PP Here is how the parameter list to \f(CW\*(C`translate\*(C'\fR is parsed: .IP \(bu 4 1 argument means it's the data to be parsed; which could be a string (filename) or a reference to a scalar (a string stored in memory), or a reference to a hash, which is parsed as being more than one argument (see next section). .Sp .Vb 2 \& # Parse the file /path/to/datafile \& my $output = $tr\->translate("/path/to/datafile"); \& \& # Parse the data contained in the string $data \& my $output = $tr\->translate(\e$data); .Ve .IP \(bu 4 More than 1 argument means its a hash of things, and it might be setting a parser, producer, or datasource (this key is named "filename" or "file" if it's a file, or "data" for a SCALAR reference. .Sp .Vb 7 \& # As above, parse /path/to/datafile, but with different producers \& for my $prod ("MySQL", "XML", "Sybase") { \& print $tr\->translate( \& producer => $prod, \& filename => "/path/to/datafile", \& ); \& } \& \& # The filename hash key could also be: \& datasource => \e$data, .Ve .Sp You get the idea. .SS "filename, data" .IX Subsection "filename, data" Using the \f(CW\*(C`filename\*(C'\fR method, the filename of the data to be parsed can be set. This method can be used in conjunction with the \f(CW\*(C`data\*(C'\fR method, below. If both the \f(CW\*(C`filename\*(C'\fR and \f(CW\*(C`data\*(C'\fR methods are invoked as mutators, the data set in the \f(CW\*(C`data\*(C'\fR method is used. .PP .Vb 1 \& $tr\->filename("/my/data/files/create.sql"); .Ve .PP or: .PP .Vb 6 \& my $create_script = do { \& local $/; \& open CREATE, "/my/data/files/create.sql" or die $!; \& ; \& }; \& $tr\->data(\e$create_script); .Ve .PP \&\f(CW\*(C`filename\*(C'\fR takes a string, which is interpreted as a filename. \&\f(CW\*(C`data\*(C'\fR takes a reference to a string, which is used as the data to be parsed. If a filename is set, then that file is opened and read when the \f(CW\*(C`translate\*(C'\fR method is called, as long as the data instance variable is not set. .SS schema .IX Subsection "schema" Returns the SQL::Translator::Schema object. .SS trace .IX Subsection "trace" Turns on/off the tracing option of Parse::RecDescent. .SS validate .IX Subsection "validate" Whether or not to validate the schema object after parsing and before producing. .SS version .IX Subsection "version" Returns the version of the SQL::Translator release. .SH AUTHORS .IX Header "AUTHORS" See the included AUTHORS file: .SH "GETTING HELP/SUPPORT" .IX Header "GETTING HELP/SUPPORT" If you are stuck with a problem or have doubts about a particular approach do not hesitate to contact us via any of the following options (the list is sorted by "fastest response time"): .IP \(bu 4 IRC: irc.perl.org#sql\-translator .IP \(bu 4 Mailing list: .IP \(bu 4 RT Bug Tracker: .SH "HOW TO CONTRIBUTE" .IX Header "HOW TO CONTRIBUTE" Contributions are always welcome, in all usable forms (we especially welcome documentation improvements). The delivery methods include git\- or unified-diff formatted patches, GitHub pull requests, or plain bug reports either via RT or the Mailing list. Contributors are generally granted access to the official repository after their first several patches pass successful review. Don't hesitate to contact us with any further questions you may have. .PP This project is maintained in a git repository. The code and related tools are accessible at the following locations: .IP \(bu 4 Official repo: .IP \(bu 4 Official gitweb: .IP \(bu 4 GitHub mirror: .IP \(bu 4 Authorized committers: .IP \(bu 4 Travis-CI log: .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright 2012 the SQL::Translator authors, as listed in "AUTHORS". .SH LICENSE .IX Header "LICENSE" This library is free software and may be distributed under the same terms as Perl 5 itself. .SH PRAISE .IX Header "PRAISE" If you find this module useful, please use to rate it. .SH "SEE ALSO" .IX Header "SEE ALSO" perl, SQL::Translator::Parser, SQL::Translator::Producer, Parse::RecDescent, GD, GraphViz, Text::RecordParser, Class::DBI, XML::Writer.