1 package SQL::Translator;
3 # ----------------------------------------------------------------------
4 # $Id: Translator.pm,v 1.51 2004-02-04 17:51:00 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 The SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
24 use vars qw( $VERSION $REVISION $DEFAULT_SUB $DEBUG $ERROR );
25 use base 'Class::Base';
30 $REVISION = sprintf "%d.%02d", q$Revision: 1.51 $ =~ /(\d+)\.(\d+)/;
31 $DEBUG = 0 unless defined $DEBUG;
39 use File::Spec::Functions qw(catfile);
40 use File::Basename qw(dirname);
42 use SQL::Translator::Schema;
44 # ----------------------------------------------------------------------
45 # The default behavior is to "pass through" values (note that the
46 # SQL::Translator instance is the first value ($_[0]), and the stuff
47 # to be parsed is the second value ($_[1])
48 # ----------------------------------------------------------------------
49 $DEFAULT_SUB = sub { $_[0]->schema } unless defined $DEFAULT_SUB;
51 # ----------------------------------------------------------------------
55 # new takes an optional hash of arguments. These arguments may
56 # include a parser, specified with the keys "parser" or "from",
57 # and a producer, specified with the keys "producer" or "to".
59 # The values that can be passed as the parser or producer are
60 # given directly to the parser or producer methods, respectively.
61 # See the appropriate method description below for details about
62 # what each expects/accepts.
63 # ----------------------------------------------------------------------
65 my ( $self, $config ) = @_;
67 # Set the parser and producer.
69 # If a 'parser' or 'from' parameter is passed in, use that as the
70 # parser; if a 'producer' or 'to' parameter is passed in, use that
71 # as the producer; both default to $DEFAULT_SUB.
73 $self->parser ($config->{'parser'} || $config->{'from'} || $DEFAULT_SUB);
74 $self->producer($config->{'producer'} || $config->{'to'} || $DEFAULT_SUB);
77 # Set up callbacks for formatting of pk,fk,table,package names in producer
79 $self->format_table_name($config->{'format_table_name'});
80 $self->format_package_name($config->{'format_package_name'});
81 $self->format_fk_name($config->{'format_fk_name'});
82 $self->format_pk_name($config->{'format_pk_name'});
85 # Set the parser_args and producer_args
87 for my $pargs ( qw[ parser_args producer_args ] ) {
88 $self->$pargs( $config->{$pargs} ) if defined $config->{ $pargs };
92 # Set the data source, if 'filename' or 'file' is provided.
94 $config->{'filename'} ||= $config->{'file'} || "";
95 $self->filename( $config->{'filename'} ) if $config->{'filename'};
98 # Finally, if there is a 'data' parameter, use that in
99 # preference to filename and file
101 if ( my $data = $config->{'data'} ) {
102 $self->data( $data );
106 # Set various other options.
108 $self->{'debug'} = defined $config->{'debug'} ? $config->{'debug'} : $DEBUG;
110 $self->add_drop_table( $config->{'add_drop_table'} );
112 $self->no_comments( $config->{'no_comments'} );
114 $self->show_warnings( $config->{'show_warnings'} );
116 $self->trace( $config->{'trace'} );
118 $self->validate( $config->{'validate'} );
123 # ----------------------------------------------------------------------
124 # add_drop_table([$bool])
125 # ----------------------------------------------------------------------
128 if ( defined (my $arg = shift) ) {
129 $self->{'add_drop_table'} = $arg ? 1 : 0;
131 return $self->{'add_drop_table'} || 0;
134 # ----------------------------------------------------------------------
135 # no_comments([$bool])
136 # ----------------------------------------------------------------------
140 if ( defined $arg ) {
141 $self->{'no_comments'} = $arg ? 1 : 0;
143 return $self->{'no_comments'} || 0;
147 # ----------------------------------------------------------------------
148 # producer([$producer_spec])
150 # Get or set the producer for the current translator.
151 # ----------------------------------------------------------------------
155 # producer as a mutator
157 my $producer = shift;
159 # Passed a module name (string containing "::")
160 if ($producer =~ /::/) {
163 # Module name was passed directly
164 # We try to load the name; if it doesn't load, there's
165 # a possibility that it has a function name attached to
167 if (load($producer)) {
168 $func_name = "produce";
171 # Module::function was passed
173 # Passed Module::Name::function; try to recover
174 my @func_parts = split /::/, $producer;
175 $func_name = pop @func_parts;
176 $producer = join "::", @func_parts;
178 # If this doesn't work, then we have a legitimate
180 load($producer) or die "Can't load $producer: $@";
183 # get code reference and assign
184 $self->{'producer'} = \&{ "$producer\::$func_name" };
185 $self->{'producer_type'} = $producer;
186 $self->debug("Got producer: $producer\::$func_name\n");
189 # passed an anonymous subroutine reference
190 elsif (isa($producer, 'CODE')) {
191 $self->{'producer'} = $producer;
192 $self->{'producer_type'} = "CODE";
193 $self->debug("Got producer: code ref\n");
196 # passed a string containing no "::"; relative package name
198 $producer =~ s/-/::/g;
199 my $Pp = sprintf "SQL::Translator::Producer::$producer";
200 load($Pp) or die "Can't load $Pp: $@";
201 $self->{'producer'} = \&{ "$Pp\::produce" };
202 $self->{'producer_type'} = $Pp;
203 $self->debug("Got producer: $Pp\n");
206 # At this point, $self->{'producer'} contains a subroutine
207 # reference that is ready to run
209 # Anything left? If so, it's producer_args
210 $self->producer_args(@_) if (@_);
213 return $self->{'producer'};
216 # ----------------------------------------------------------------------
219 # producer_type is an accessor that allows producer subs to get
220 # information about their origin. This is poptentially important;
221 # since all producer subs are called as subroutine references, there is
222 # no way for a producer to find out which package the sub lives in
223 # originally, for example.
224 # ----------------------------------------------------------------------
225 sub producer_type { $_[0]->{'producer_type'} }
227 # ----------------------------------------------------------------------
228 # producer_args([\%args])
230 # Arbitrary name => value pairs of paramters can be passed to a
231 # producer using this method.
233 # If the first argument passed in is undef, then the hash of arguments
234 # is cleared; all subsequent elements are added to the hash of name,
235 # value pairs stored as producer_args.
236 # ----------------------------------------------------------------------
239 return $self->_args("producer", @_);
242 # ----------------------------------------------------------------------
243 # parser([$parser_spec])
244 # ----------------------------------------------------------------------
248 # parser as a mutator
252 # Passed a module name (string containing "::")
253 if ($parser =~ /::/) {
256 # Module name was passed directly
257 # We try to load the name; if it doesn't load, there's
258 # a possibility that it has a function name attached to
261 $func_name = "parse";
264 # Module::function was passed
266 # Passed Module::Name::function; try to recover
267 my @func_parts = split /::/, $parser;
268 $func_name = pop @func_parts;
269 $parser = join "::", @func_parts;
271 # If this doesn't work, then we have a legitimate
273 load($parser) or die "Can't load $parser: $@";
276 # get code reference and assign
277 $self->{'parser'} = \&{ "$parser\::$func_name" };
278 $self->{'parser_type'} = $parser;
279 $self->debug("Got parser: $parser\::$func_name\n");
282 # passed an anonymous subroutine reference
283 elsif ( isa( $parser, 'CODE' ) ) {
284 $self->{'parser'} = $parser;
285 $self->{'parser_type'} = "CODE";
286 $self->debug("Got parser: code ref\n");
289 # passed a string containing no "::"; relative package name
292 my $Pp = "SQL::Translator::Parser::$parser";
293 load( $Pp ) or die "Can't load $Pp: $@";
294 $self->{'parser'} = \&{ "$Pp\::parse" };
295 $self->{'parser_type'} = $Pp;
296 $self->debug("Got parser: $Pp\n");
300 # At this point, $self->{'parser'} contains a subroutine
301 # reference that is ready to run
303 $self->parser_args( @_ ) if (@_);
306 return $self->{'parser'};
309 # ----------------------------------------------------------------------
310 sub parser_type { $_[0]->{'parser_type'} }
314 return $self->_args("parser", @_);
320 if ( defined $arg ) {
321 $self->{'show_warnings'} = $arg ? 1 : 0;
323 return $self->{'show_warnings'} || 0;
327 # filename - get or set the filename
331 my $filename = shift;
333 my $msg = "Cannot use directory '$filename' as input source";
334 return $self->error($msg);
335 } elsif (ref($filename) eq 'ARRAY') {
336 $self->{'filename'} = $filename;
337 $self->debug("Got array of files: ".join(', ',@$filename)."\n");
338 } elsif (-f _ && -r _) {
339 $self->{'filename'} = $filename;
340 $self->debug("Got filename: '$self->{'filename'}'\n");
342 my $msg = "Cannot use '$filename' as input source: ".
343 "file does not exist or is not readable.";
344 return $self->error($msg);
351 # ----------------------------------------------------------------------
354 # if $self->{'data'} is not set, but $self->{'filename'} is, then
355 # $self->{'filename'} is opened and read, with the results put into
357 # ----------------------------------------------------------------------
361 # Set $self->{'data'} based on what was passed in. We will
362 # accept a number of things; do our best to get it right.
365 if (isa($data, "SCALAR")) {
366 $self->{'data'} = $data;
369 if (isa($data, 'ARRAY')) {
370 $data = join '', @$data;
372 elsif (isa($data, 'GLOB')) {
376 elsif (! ref $data && @_) {
377 $data = join '', $data, @_;
379 $self->{'data'} = \$data;
383 # If we have a filename but no data yet, populate.
384 if (not $self->{'data'} and my $filename = $self->filename) {
385 $self->debug("Opening '$filename' to get contents.\n");
390 my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename);
392 foreach my $file (@files) {
393 unless (open FH, $file) {
394 return $self->error("Can't read file '$file': $!");
400 return $self->error("Can't close file '$file': $!");
404 $self->{'data'} = \$data;
407 return $self->{'data'};
410 # ----------------------------------------------------------------------
413 # Deletes the existing Schema object so that future calls to translate
414 # don't append to the existing.
417 $self->{'schema'} = undef;
421 # ----------------------------------------------------------------------
424 # Returns the SQL::Translator::Schema object
428 unless ( defined $self->{'schema'} ) {
429 $self->{'schema'} = SQL::Translator::Schema->new;
432 return $self->{'schema'};
435 # ----------------------------------------------------------------------
439 if ( defined $arg ) {
440 $self->{'trace'} = $arg ? 1 : 0;
442 return $self->{'trace'} || 0;
445 # ----------------------------------------------------------------------
446 # translate([source], [\%args])
448 # translate does the actual translation. The main argument is the
449 # source of the data to be translated, which can be a filename, scalar
450 # reference, or glob reference.
452 # Alternatively, translate takes optional arguements, which are passed
453 # to the appropriate places. Most notable of these arguments are
454 # parser and producer, which can be used to set the parser and
455 # producer, respectively. This is the applications last chance to set
458 # translate returns a string.
459 # ----------------------------------------------------------------------
462 my ($args, $parser, $parser_type, $producer, $producer_type);
463 my ($parser_output, $producer_output);
467 # Passed a reference to a hash?
468 if (isa($_[0], 'HASH')) {
470 $self->debug("translate: Got a hashref\n");
474 # Passed a GLOB reference, i.e., filehandle
475 elsif (isa($_[0], 'GLOB')) {
476 $self->debug("translate: Got a GLOB reference\n");
480 # Passed a reference to a string containing the data
481 elsif (isa($_[0], 'SCALAR')) {
482 # passed a ref to a string
483 $self->debug("translate: Got a SCALAR reference (string)\n");
487 # Not a reference; treat it as a filename
488 elsif (! ref $_[0]) {
489 # Not a ref, it's a filename
490 $self->debug("translate: Got a filename\n");
491 $self->filename($_[0]);
494 # Passed something else entirely.
496 # We're not impressed. Take your empty string and leave.
499 # Actually, if data, parser, and producer are set, then we
500 # can continue. Too bad, because I like my comment
502 return "" unless ($self->data &&
508 # You must pass in a hash, or you get nothing.
513 # ----------------------------------------------------------------------
514 # Can specify the data to be transformed using "filename", "file",
515 # "data", or "datasource".
516 # ----------------------------------------------------------------------
517 if (my $filename = ($args->{'filename'} || $args->{'file'})) {
518 $self->filename($filename);
521 if (my $data = ($args->{'data'} || $args->{'datasource'})) {
525 # ----------------------------------------------------------------
527 # ----------------------------------------------------------------
528 my $data = $self->data;
530 # ----------------------------------------------------------------
531 # Local reference to the parser subroutine
532 # ----------------------------------------------------------------
533 if ($parser = ($args->{'parser'} || $args->{'from'})) {
534 $self->parser($parser);
536 $parser = $self->parser;
537 $parser_type = $self->parser_type;
539 # ----------------------------------------------------------------
540 # Local reference to the producer subroutine
541 # ----------------------------------------------------------------
542 if ($producer = ($args->{'producer'} || $args->{'to'})) {
543 $self->producer($producer);
545 $producer = $self->producer;
546 $producer_type = $self->producer_type;
548 # ----------------------------------------------------------------
549 # Execute the parser, then execute the producer with that output.
550 # Allowances are made for each piece to die, or fail to compile,
551 # since the referenced subroutines could be almost anything. In
552 # the future, each of these might happen in a Safe environment,
553 # depending on how paranoid we want to be.
554 # ----------------------------------------------------------------
555 unless ( defined $self->{'schema'} ) {
556 eval { $parser_output = $parser->($self, $$data) };
557 if ($@ || ! $parser_output) {
558 my $msg = sprintf "translate: Error with parser '%s': %s",
559 $parser_type, ($@) ? $@ : " no results";
560 return $self->error($msg);
564 $self->debug("Schema =\n", Dumper($self->schema), "\n");
566 if ($self->validate) {
567 my $schema = $self->schema;
568 return $self->error('Invalid schema') unless $schema->is_valid;
571 eval { $producer_output = $producer->($self) };
572 if ($@ || ! $producer_output) {
573 my $err = $@ || $self->error || "no results";
574 my $msg = "translate: Error with producer '$producer_type': $err";
575 return $self->error($msg);
578 return $producer_output;
581 # ----------------------------------------------------------------------
584 # Hacky sort of method to list all available parsers. This has
587 # - Only finds things in the SQL::Translator::Parser namespace
589 # - Only finds things that are located in the same directory
590 # as SQL::Translator::Parser. Yeck.
592 # This method will fail in several very likely cases:
594 # - Parser modules in different namespaces
596 # - Parser modules in the SQL::Translator::Parser namespace that
597 # have any XS componenets will be installed in
598 # arch_lib/SQL/Translator.
600 # ----------------------------------------------------------------------
602 return shift->_list("parser");
605 # ----------------------------------------------------------------------
608 # See notes for list_parsers(), above; all the problems apply to
609 # list_producers as well.
610 # ----------------------------------------------------------------------
612 return shift->_list("producer");
616 # ======================================================================
618 # ======================================================================
620 # ----------------------------------------------------------------------
621 # _args($type, \%args);
623 # Gets or sets ${type}_args. Called by parser_args and producer_args.
624 # ----------------------------------------------------------------------
628 $type = "${type}_args" unless $type =~ /_args$/;
630 unless (defined $self->{$type} && isa($self->{$type}, 'HASH')) {
631 $self->{$type} = { };
635 # If the first argument is an explicit undef (remember, we
636 # don't get here unless there is stuff in @_), then we clear
637 # out the producer_args hash.
638 if (! defined $_[0]) {
640 %{$self->{$type}} = ();
643 my $args = isa($_[0], 'HASH') ? shift : { @_ };
644 %{$self->{$type}} = (%{$self->{$type}}, %$args);
650 # ----------------------------------------------------------------------
652 # ----------------------------------------------------------------------
655 my $type = shift || return ();
656 my $uctype = ucfirst lc $type;
659 # First find all the directories where SQL::Translator
660 # parsers or producers (the "type") appear to live.
662 load("SQL::Translator::$uctype") or return ();
663 my $path = catfile "SQL", "Translator", $uctype;
666 my $dir = catfile $_, $path;
667 $self->debug("_list_${type}s searching $dir\n");
673 # Now use File::File::find to look recursively in those
674 # directories for all the *.pm files, then present them
675 # with the slashes turned into dashes.
680 if ( -f && m/\.pm$/ ) {
683 my $cur_dir = $File::Find::dir;
684 my $base_dir = quotemeta catfile 'SQL', 'Translator', $uctype;
687 # See if the current directory is below the base directory.
689 if ( $cur_dir =~ m/$base_dir(.*)/ ) {
691 $cur_dir =~ s!^/!!; # kill leading slash
692 $cur_dir =~ s!/!-!g; # turn other slashes into dashes
698 $found{ join '-', map { $_ || () } $cur_dir, $mod } = 1;
704 return sort { lc $a cmp lc $b } keys %found;
707 # ----------------------------------------------------------------------
710 # Loads a Perl module. Short circuits if a module is already loaded.
711 # ----------------------------------------------------------------------
713 my $module = do { my $m = shift; $m =~ s[::][/]g; "$m.pm" };
714 return 1 if $INC{$module};
721 return __PACKAGE__->error($@) if ($@);
725 # ----------------------------------------------------------------------
726 sub format_table_name {
727 return shift->_format_name('_format_table_name', @_);
730 # ----------------------------------------------------------------------
731 sub format_package_name {
732 return shift->_format_name('_format_package_name', @_);
735 # ----------------------------------------------------------------------
737 return shift->_format_name('_format_fk_name', @_);
740 # ----------------------------------------------------------------------
742 return shift->_format_name('_format_pk_name', @_);
745 # ----------------------------------------------------------------------
746 # The other format_*_name methods rely on this one. It optionally
747 # accepts a subroutine ref as the first argument (or uses an identity
748 # sub if one isn't provided or it doesn't already exist), and applies
749 # it to the rest of the arguments (if any).
750 # ----------------------------------------------------------------------
756 if (ref($args[0]) eq 'CODE') {
757 $self->{$field} = shift @args;
759 elsif (! exists $self->{$field}) {
760 $self->{$field} = sub { return shift };
763 return @args ? $self->{$field}->(@args) : $self->{$field};
766 # ----------------------------------------------------------------------
769 # Calls UNIVERSAL::isa($ref, $type). I think UNIVERSAL::isa is ugly,
770 # but I like function overhead.
771 # ----------------------------------------------------------------------
773 my ($ref, $type) = @_;
774 return UNIVERSAL::isa($ref, $type);
777 # ----------------------------------------------------------------------
779 my ( $self, $arg ) = @_;
780 if ( defined $arg ) {
781 $self->{'validate'} = $arg ? 1 : 0;
783 return $self->{'validate'} || 0;
788 # ----------------------------------------------------------------------
789 # Who killed the pork chops?
790 # What price bananas?
793 # ----------------------------------------------------------------------
799 SQL::Translator - manipulate structured data definitions (SQL and more)
805 my $translator = SQL::Translator->new(
808 # Print Parse::RecDescent trace
810 # Don't include comments in output
812 # Print name mutations, conflicts
814 # Add "drop table" statements
816 # Validate schema object
818 # Make all table names CAPS in producers which support this option
819 format_table_name => sub {my $tablename = shift; return uc($tablename)},
820 # Null-op formatting, only here for documentation's sake
821 format_package_name => sub {return shift},
822 format_fk_name => sub {return shift},
823 format_pk_name => sub {return shift},
826 my $output = $translator->translate(
829 # Or an arrayref of filenames, i.e. [ $file1, $file2, $file3 ]
831 ) or die $translator->error;
837 SQL::Translator is a group of Perl modules that converts
838 vendor-specific SQL table definitions into other formats, such as
839 other vendor-specific SQL, ER diagrams, documentation (POD and HTML),
840 XML, and Class::DBI classes. The main focus of SQL::Translator is
841 SQL, but parsers exist for other structured data formats, including
842 Excel spreadsheets and arbitrarily delimited text files. Through the
843 separation of the code into parsers and producers with an object model
844 in between, it's possible to combine any parser with any producer, to
845 plug in custom parsers or producers, or to manipulate the parsed data
846 via the built-in object model. Presently only the definition parts of
847 SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT,
850 This documentation covers the API for SQL::Translator. For a more general
851 discussion of how to use the modules and scripts, please see
852 L<SQL::Translator::Manual>.
856 The constructor is called C<new>, and accepts a optional hash of options.
907 All options are, well, optional; these attributes can be set via
908 instance methods. Internally, they are; no (non-syntactical)
909 advantage is gained by passing options to the constructor.
913 =head2 add_drop_table
915 Toggles whether or not to add "DROP TABLE" statements just before the
920 Toggles whether to print comments in the output. Accepts a true or false
921 value, returns the current value.
925 The C<producer> method is an accessor/mutator, used to retrieve or
926 define what subroutine is called to produce the output. A subroutine
927 defined as a producer will be invoked as a function (I<not a method>)
928 and passed 2 parameters: its container C<SQL::Translator> instance and a
929 data structure. It is expected that the function transform the data
930 structure to a string. The C<SQL::Transformer> instance is provided for
931 informational purposes; for example, the type of the parser can be
932 retrieved using the C<parser_type> method, and the C<error> and
933 C<debug> methods can be called when needed.
935 When defining a producer, one of several things can be passed in: A
936 module name (e.g., C<My::Groovy::Producer>), a module name relative to
937 the C<SQL::Translator::Producer> namespace (e.g., C<MySQL>), a module
938 name and function combination (C<My::Groovy::Producer::transmogrify>),
939 or a reference to an anonymous subroutine. If a full module name is
940 passed in (for the purposes of this method, a string containing "::"
941 is considered to be a module name), it is treated as a package, and a
942 function called "produce" will be invoked: C<$modulename::produce>.
943 If $modulename cannot be loaded, the final portion is stripped off and
944 treated as a function. In other words, if there is no file named
945 F<My/Groovy/Producer/transmogrify.pm>, C<SQL::Translator> will attempt
946 to load F<My/Groovy/Producer.pm> and use C<transmogrify> as the name of
947 the function, instead of the default C<produce>.
949 my $tr = SQL::Translator->new;
951 # This will invoke My::Groovy::Producer::produce($tr, $data)
952 $tr->producer("My::Groovy::Producer");
954 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
955 $tr->producer("Sybase");
957 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
958 # assuming that My::Groovy::Producer::transmogrify is not a module
960 $tr->producer("My::Groovy::Producer::transmogrify");
962 # This will invoke the referenced subroutine directly, as
963 # $subref->($tr, $data);
964 $tr->producer(\&my_producer);
966 There is also a method named C<producer_type>, which is a string
967 containing the classname to which the above C<produce> function
968 belongs. In the case of anonymous subroutines, this method returns
971 Finally, there is a method named C<producer_args>, which is both an
972 accessor and a mutator. Arbitrary data may be stored in name => value
973 pairs for the producer subroutine to access:
975 sub My::Random::producer {
976 my ($tr, $data) = @_;
977 my $pr_args = $tr->producer_args();
979 # $pr_args is a hashref.
981 Extra data passed to the C<producer> method is passed to
984 $tr->producer("xSV", delimiter => ',\s*');
986 # In SQL::Translator::Producer::xSV:
987 my $args = $tr->producer_args;
988 my $delimiter = $args->{'delimiter'}; # value is ,\s*
992 The C<parser> method defines or retrieves a subroutine that will be
993 called to perform the parsing. The basic idea is the same as that of
994 C<producer> (see above), except the default subroutine name is
995 "parse", and will be invoked as C<$module_name::parse($tr, $data)>.
996 Also, the parser subroutine will be passed a string containing the
997 entirety of the data to be parsed.
999 # Invokes SQL::Translator::Parser::MySQL::parse()
1000 $tr->parser("MySQL");
1002 # Invokes My::Groovy::Parser::parse()
1003 $tr->parser("My::Groovy::Parser");
1005 # Invoke an anonymous subroutine directly
1007 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
1008 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
1009 return $dumper->Dump;
1012 There is also C<parser_type> and C<parser_args>, which perform
1013 analogously to C<producer_type> and C<producer_args>
1015 =head2 show_warnings
1017 Toggles whether to print warnings of name conflicts, identifier
1018 mutations, etc. Probably only generated by producers to let the user
1019 know when something won't translate very smoothly (e.g., MySQL "enum"
1020 fields into Oracle). Accepts a true or false value, returns the
1025 The C<translate> method calls the subroutines referenced by the
1026 C<parser> and C<producer> data members (described above). It accepts
1027 as arguments a number of things, in key => value format, including
1028 (potentially) a parser and a producer (they are passed directly to the
1029 C<parser> and C<producer> methods).
1031 Here is how the parameter list to C<translate> is parsed:
1037 1 argument means it's the data to be parsed; which could be a string
1038 (filename) or a reference to a scalar (a string stored in memory), or a
1039 reference to a hash, which is parsed as being more than one argument
1042 # Parse the file /path/to/datafile
1043 my $output = $tr->translate("/path/to/datafile");
1045 # Parse the data contained in the string $data
1046 my $output = $tr->translate(\$data);
1050 More than 1 argument means its a hash of things, and it might be
1051 setting a parser, producer, or datasource (this key is named
1052 "filename" or "file" if it's a file, or "data" for a SCALAR reference.
1054 # As above, parse /path/to/datafile, but with different producers
1055 for my $prod ("MySQL", "XML", "Sybase") {
1056 print $tr->translate(
1058 filename => "/path/to/datafile",
1062 # The filename hash key could also be:
1063 datasource => \$data,
1069 =head2 filename, data
1071 Using the C<filename> method, the filename of the data to be parsed
1072 can be set. This method can be used in conjunction with the C<data>
1073 method, below. If both the C<filename> and C<data> methods are
1074 invoked as mutators, the data set in the C<data> method is used.
1076 $tr->filename("/my/data/files/create.sql");
1080 my $create_script = do {
1082 open CREATE, "/my/data/files/create.sql" or die $!;
1085 $tr->data(\$create_script);
1087 C<filename> takes a string, which is interpreted as a filename.
1088 C<data> takes a reference to a string, which is used as the data to be
1089 parsed. If a filename is set, then that file is opened and read when
1090 the C<translate> method is called, as long as the data instance
1091 variable is not set.
1095 Returns the SQL::Translator::Schema object.
1099 Turns on/off the tracing option of Parse::RecDescent.
1103 Whether or not to validate the schema object after parsing and before
1108 The following people have contributed to the SQLFairy project:
1112 =item * Mark Addison <grommit@users.sourceforge.net>
1114 =item * Sam Angiuoli <angiuoli@users.sourceforge.net>
1116 =item * Dave Cash <dave@gnofn.org>
1118 =item * Darren Chamberlain <dlc@users.sourceforge.net>
1120 =item * Ken Y. Clark <kclark@cpan.org>
1122 =item * Allen Day <allenday@users.sourceforge.net>
1124 =item * Paul Harrington <phrrngtn@users.sourceforge.net>
1126 =item * Mikey Melillo <mmelillo@users.sourceforge.net>
1128 =item * Chris Mungall <cjm@fruitfly.org>
1130 =item * Ross Smith II <rossta@users.sf.net>
1132 =item * Gudmundur A. Thorisson <mummi@cshl.org>
1134 =item * Chris To <christot@users.sourceforge.net>
1136 =item * Jason Williams <smdwilliams@users.sourceforge.net>
1138 =item * Ying Zhang <zyolive@yahoo.com>
1142 If you would like to contribute to the project, you can send patches
1143 to the developers mailing list:
1145 sqlfairy-developers@lists.sourceforge.net
1147 Or send us a message (with your Sourceforge username) asking to be
1148 added to the project and what you'd like to contribute.
1153 This program is free software; you can redistribute it and/or modify
1154 it under the terms of the GNU General Public License as published by
1155 the Free Software Foundation; version 2.
1157 This program is distributed in the hope that it will be useful, but
1158 WITHOUT ANY WARRANTY; without even the implied warranty of
1159 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1160 General Public License for more details.
1162 You should have received a copy of the GNU General Public License
1163 along with this program; if not, write to the Free Software
1164 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
1169 Please use L<http://rt.cpan.org/> for reporting bugs.
1173 If you find this module useful, please use
1174 L<http://cpanratings.perl.org/rate/?distribution=SQL-Translator> to rate it.
1179 L<SQL::Translator::Parser>,
1180 L<SQL::Translator::Producer>,
1181 L<Parse::RecDescent>,
1184 L<Text::RecordParser>,