1 package SQL::Translator;
3 # ----------------------------------------------------------------------
4 # $Id: Translator.pm,v 1.60 2004-11-09 02:09:52 grommit Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 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.60 $ =~ /(\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
78 # MOVED TO PRODUCER ARGS
80 #$self->format_table_name($config->{'format_table_name'});
81 #$self->format_package_name($config->{'format_package_name'});
82 #$self->format_fk_name($config->{'format_fk_name'});
83 #$self->format_pk_name($config->{'format_pk_name'});
86 # Set the parser_args and producer_args
88 for my $pargs ( qw[ parser_args producer_args ] ) {
89 $self->$pargs( $config->{$pargs} ) if defined $config->{ $pargs };
93 # Set the data source, if 'filename' or 'file' is provided.
95 $config->{'filename'} ||= $config->{'file'} || "";
96 $self->filename( $config->{'filename'} ) if $config->{'filename'};
99 # Finally, if there is a 'data' parameter, use that in
100 # preference to filename and file
102 if ( my $data = $config->{'data'} ) {
103 $self->data( $data );
107 # Set various other options.
109 $self->{'debug'} = defined $config->{'debug'} ? $config->{'debug'} : $DEBUG;
111 $self->add_drop_table( $config->{'add_drop_table'} );
113 $self->no_comments( $config->{'no_comments'} );
115 $self->show_warnings( $config->{'show_warnings'} );
117 $self->trace( $config->{'trace'} );
119 $self->validate( $config->{'validate'} );
124 # ----------------------------------------------------------------------
125 # add_drop_table([$bool])
126 # ----------------------------------------------------------------------
129 if ( defined (my $arg = shift) ) {
130 $self->{'add_drop_table'} = $arg ? 1 : 0;
132 return $self->{'add_drop_table'} || 0;
135 # ----------------------------------------------------------------------
136 # no_comments([$bool])
137 # ----------------------------------------------------------------------
141 if ( defined $arg ) {
142 $self->{'no_comments'} = $arg ? 1 : 0;
144 return $self->{'no_comments'} || 0;
148 # ----------------------------------------------------------------------
149 # producer([$producer_spec])
151 # Get or set the producer for the current translator.
152 # ----------------------------------------------------------------------
156 path => "SQL::Translator::Producer",
157 default_sub => "produce"
161 # ----------------------------------------------------------------------
164 # producer_type is an accessor that allows producer subs to get
165 # information about their origin. This is poptentially important;
166 # since all producer subs are called as subroutine references, there is
167 # no way for a producer to find out which package the sub lives in
168 # originally, for example.
169 # ----------------------------------------------------------------------
170 sub producer_type { $_[0]->{'producer_type'} }
172 # ----------------------------------------------------------------------
173 # producer_args([\%args])
175 # Arbitrary name => value pairs of paramters can be passed to a
176 # producer using this method.
178 # If the first argument passed in is undef, then the hash of arguments
179 # is cleared; all subsequent elements are added to the hash of name,
180 # value pairs stored as producer_args.
181 # ----------------------------------------------------------------------
182 sub producer_args { shift->_args("producer", @_); }
184 # ----------------------------------------------------------------------
185 # parser([$parser_spec])
186 # ----------------------------------------------------------------------
190 path => "SQL::Translator::Parser",
191 default_sub => "parse"
195 sub parser_type { $_[0]->{'parser_type'}; }
197 sub parser_args { shift->_args("parser", @_); }
199 # ----------------------------------------------------------------------
203 if ( defined $arg ) {
204 $self->{'show_warnings'} = $arg ? 1 : 0;
206 return $self->{'show_warnings'} || 0;
210 # filename - get or set the filename
214 my $filename = shift;
216 my $msg = "Cannot use directory '$filename' as input source";
217 return $self->error($msg);
218 } elsif (ref($filename) eq 'ARRAY') {
219 $self->{'filename'} = $filename;
220 $self->debug("Got array of files: ".join(', ',@$filename)."\n");
221 } elsif (-f _ && -r _) {
222 $self->{'filename'} = $filename;
223 $self->debug("Got filename: '$self->{'filename'}'\n");
225 my $msg = "Cannot use '$filename' as input source: ".
226 "file does not exist or is not readable.";
227 return $self->error($msg);
234 # ----------------------------------------------------------------------
237 # if $self->{'data'} is not set, but $self->{'filename'} is, then
238 # $self->{'filename'} is opened and read, with the results put into
240 # ----------------------------------------------------------------------
244 # Set $self->{'data'} based on what was passed in. We will
245 # accept a number of things; do our best to get it right.
248 if (isa($data, "SCALAR")) {
249 $self->{'data'} = $data;
252 if (isa($data, 'ARRAY')) {
253 $data = join '', @$data;
255 elsif (isa($data, 'GLOB')) {
259 elsif (! ref $data && @_) {
260 $data = join '', $data, @_;
262 $self->{'data'} = \$data;
266 # If we have a filename but no data yet, populate.
267 if (not $self->{'data'} and my $filename = $self->filename) {
268 $self->debug("Opening '$filename' to get contents.\n");
273 my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename);
275 foreach my $file (@files) {
276 unless (open FH, $file) {
277 return $self->error("Can't read file '$file': $!");
283 return $self->error("Can't close file '$file': $!");
287 $self->{'data'} = \$data;
290 return $self->{'data'};
293 # ----------------------------------------------------------------------
296 # Deletes the existing Schema object so that future calls to translate
297 # don't append to the existing.
300 $self->{'schema'} = undef;
304 # ----------------------------------------------------------------------
307 # Returns the SQL::Translator::Schema object
311 unless ( defined $self->{'schema'} ) {
312 $self->{'schema'} = SQL::Translator::Schema->new(
317 return $self->{'schema'};
320 # ----------------------------------------------------------------------
324 if ( defined $arg ) {
325 $self->{'trace'} = $arg ? 1 : 0;
327 return $self->{'trace'} || 0;
330 # ----------------------------------------------------------------------
331 # translate([source], [\%args])
333 # translate does the actual translation. The main argument is the
334 # source of the data to be translated, which can be a filename, scalar
335 # reference, or glob reference.
337 # Alternatively, translate takes optional arguements, which are passed
338 # to the appropriate places. Most notable of these arguments are
339 # parser and producer, which can be used to set the parser and
340 # producer, respectively. This is the applications last chance to set
343 # translate returns a string.
344 # ----------------------------------------------------------------------
347 my ($args, $parser, $parser_type, $producer, $producer_type);
348 my ($parser_output, $producer_output);
352 # Passed a reference to a hash?
353 if (isa($_[0], 'HASH')) {
355 $self->debug("translate: Got a hashref\n");
359 # Passed a GLOB reference, i.e., filehandle
360 elsif (isa($_[0], 'GLOB')) {
361 $self->debug("translate: Got a GLOB reference\n");
365 # Passed a reference to a string containing the data
366 elsif (isa($_[0], 'SCALAR')) {
367 # passed a ref to a string
368 $self->debug("translate: Got a SCALAR reference (string)\n");
372 # Not a reference; treat it as a filename
373 elsif (! ref $_[0]) {
374 # Not a ref, it's a filename
375 $self->debug("translate: Got a filename\n");
376 $self->filename($_[0]);
379 # Passed something else entirely.
381 # We're not impressed. Take your empty string and leave.
384 # Actually, if data, parser, and producer are set, then we
385 # can continue. Too bad, because I like my comment
387 return "" unless ($self->data &&
393 # You must pass in a hash, or you get nothing.
398 # ----------------------------------------------------------------------
399 # Can specify the data to be transformed using "filename", "file",
400 # "data", or "datasource".
401 # ----------------------------------------------------------------------
402 if (my $filename = ($args->{'filename'} || $args->{'file'})) {
403 $self->filename($filename);
406 if (my $data = ($args->{'data'} || $args->{'datasource'})) {
410 # ----------------------------------------------------------------
412 # ----------------------------------------------------------------
413 my $data = $self->data;
415 # ----------------------------------------------------------------
416 # Local reference to the parser subroutine
417 # ----------------------------------------------------------------
418 if ($parser = ($args->{'parser'} || $args->{'from'})) {
419 $self->parser($parser);
421 $parser = $self->parser;
422 $parser_type = $self->parser_type;
424 # ----------------------------------------------------------------
425 # Local reference to the producer subroutine
426 # ----------------------------------------------------------------
427 if ($producer = ($args->{'producer'} || $args->{'to'})) {
428 $self->producer($producer);
430 $producer = $self->producer;
431 $producer_type = $self->producer_type;
433 # ----------------------------------------------------------------
434 # Execute the parser, then execute the producer with that output.
435 # Allowances are made for each piece to die, or fail to compile,
436 # since the referenced subroutines could be almost anything. In
437 # the future, each of these might happen in a Safe environment,
438 # depending on how paranoid we want to be.
439 # ----------------------------------------------------------------
440 unless ( defined $self->{'schema'} ) {
441 eval { $parser_output = $parser->($self, $$data) };
442 if ($@ || ! $parser_output) {
443 my $msg = sprintf "translate: Error with parser '%s': %s",
444 $parser_type, ($@) ? $@ : " no results";
445 return $self->error($msg);
449 $self->debug("Schema =\n", Dumper($self->schema), "\n");
451 if ($self->validate) {
452 my $schema = $self->schema;
453 return $self->error('Invalid schema') unless $schema->is_valid;
456 eval { $producer_output = $producer->($self) };
457 if ($@ || ! $producer_output) {
458 my $err = $@ || $self->error || "no results";
459 my $msg = "translate: Error with producer '$producer_type': $err";
460 return $self->error($msg);
463 return $producer_output;
466 # ----------------------------------------------------------------------
469 # Hacky sort of method to list all available parsers. This has
472 # - Only finds things in the SQL::Translator::Parser namespace
474 # - Only finds things that are located in the same directory
475 # as SQL::Translator::Parser. Yeck.
477 # This method will fail in several very likely cases:
479 # - Parser modules in different namespaces
481 # - Parser modules in the SQL::Translator::Parser namespace that
482 # have any XS componenets will be installed in
483 # arch_lib/SQL/Translator.
485 # ----------------------------------------------------------------------
487 return shift->_list("parser");
490 # ----------------------------------------------------------------------
493 # See notes for list_parsers(), above; all the problems apply to
494 # list_producers as well.
495 # ----------------------------------------------------------------------
497 return shift->_list("producer");
501 # ======================================================================
503 # ======================================================================
505 # ----------------------------------------------------------------------
506 # _args($type, \%args);
508 # Gets or sets ${type}_args. Called by parser_args and producer_args.
509 # ----------------------------------------------------------------------
513 $type = "${type}_args" unless $type =~ /_args$/;
515 unless (defined $self->{$type} && isa($self->{$type}, 'HASH')) {
516 $self->{$type} = { };
520 # If the first argument is an explicit undef (remember, we
521 # don't get here unless there is stuff in @_), then we clear
522 # out the producer_args hash.
523 if (! defined $_[0]) {
525 %{$self->{$type}} = ();
528 my $args = isa($_[0], 'HASH') ? shift : { @_ };
529 %{$self->{$type}} = (%{$self->{$type}}, %$args);
535 # ----------------------------------------------------------------------
536 # Does the get/set work for parser and producer. e.g.
537 # return $self->_tool({
538 # name => 'producer',
539 # path => "SQL::Translator::Producer",
540 # default_sub => "produce",
542 # ----------------------------------------------------------------------
544 my ($self,$args) = (shift, shift);
545 my $name = $args->{name};
546 return $self->{$name} unless @_;
549 # passed an anonymous subroutine reference
550 if (isa($tool, 'CODE')) {
551 $self->{$name} = $tool;
552 $self->{"$name\_type"} = "CODE";
553 $self->debug("Got $name: code ref\n");
556 # Passed a module name or module and sub name
560 # Module name was passed directly
561 # We try to load the name; if it doesn't load, there's a
562 # possibility that it has a function name attached to it.
563 $tool =~ s/-/::/g if $tool !~ /::/;
564 if ( my $loaded = load($tool => $args->{path}) ) {
566 $func_name = $args->{default_sub};
569 # Passed Module::Name::function; try to recover
571 my @func_parts = split /::/, $tool;
572 $func_name = pop @func_parts;
573 $tool = join "::", @func_parts;
575 # If this doesn't work, then we have a legitimate
577 load($tool) or die "Can't load $tool: $@";
580 # get code reference and assign
581 $self->{$name} = \&{ "$tool\::$func_name" };
582 $self->{"$name\_type"} = $tool;
583 $self->debug("Got $name: $tool\::$func_name\n");
586 # At this point, $self->{$name} contains a subroutine
587 # reference that is ready to run
589 # Anything left? If so, it's args
590 my $meth = "$name\_args";
591 $self->$meth(@_) if (@_);
593 return $self->{$name};
596 # ----------------------------------------------------------------------
598 # ----------------------------------------------------------------------
601 my $type = shift || return ();
602 my $uctype = ucfirst lc $type;
605 # First find all the directories where SQL::Translator
606 # parsers or producers (the "type") appear to live.
608 load("SQL::Translator::$uctype") or return ();
609 my $path = catfile "SQL", "Translator", $uctype;
612 my $dir = catfile $_, $path;
613 $self->debug("_list_${type}s searching $dir\n");
619 # Now use File::File::find to look recursively in those
620 # directories for all the *.pm files, then present them
621 # with the slashes turned into dashes.
626 if ( -f && m/\.pm$/ ) {
629 my $cur_dir = $File::Find::dir;
630 my $base_dir = quotemeta catfile 'SQL', 'Translator', $uctype;
633 # See if the current directory is below the base directory.
635 if ( $cur_dir =~ m/$base_dir(.*)/ ) {
637 $cur_dir =~ s!^/!!; # kill leading slash
638 $cur_dir =~ s!/!-!g; # turn other slashes into dashes
644 $found{ join '-', map { $_ || () } $cur_dir, $mod } = 1;
650 return sort { lc $a cmp lc $b } keys %found;
653 # ----------------------------------------------------------------------
654 # load(MODULE [,PATH[,PATH]...])
656 # Loads a Perl module. Short circuits if a module is already loaded.
658 # MODULE - is the name of the module to load.
660 # PATH - optional list of 'package paths' to look for the module in. e.g
661 # If you called load(Bar => 'Foo', 'My::Modules') it will try to load the mod
662 # Bar then Foo::Bar then My::Modules::Bar.
664 # Returns package name of the module actually loaded or false and sets error.
666 # Note, you can't load a name from the root namespace (ie one without '::' in
667 # it), therefore a single word name without a path fails.
668 # ----------------------------------------------------------------------
672 push @path, "" if $name =~ /::/; # Empty path to check name on its own first
673 push @path, @_ if @_;
676 my $module = $_ ? "$_\::$name" : $name;
677 my $file = $module; $file =~ s[::][/]g; $file .= ".pm";
678 __PACKAGE__->debug("Loading $name as $file\n");
679 return $module if $INC{$file}; # Already loaded
681 eval { require $file };
682 next if $@ =~ /Can't locate $file in \@INC/;
683 eval { $file->import(@_) } unless $@;
684 return __PACKAGE__->error("Error loading $name as $module : $@") if $@;
686 return $module; # Module loaded ok
692 # ----------------------------------------------------------------------
693 sub format_table_name {
694 return shift->_format_name('_format_table_name', @_);
697 # ----------------------------------------------------------------------
698 sub format_package_name {
699 return shift->_format_name('_format_package_name', @_);
702 # ----------------------------------------------------------------------
704 return shift->_format_name('_format_fk_name', @_);
707 # ----------------------------------------------------------------------
709 return shift->_format_name('_format_pk_name', @_);
712 # ----------------------------------------------------------------------
713 # The other format_*_name methods rely on this one. It optionally
714 # accepts a subroutine ref as the first argument (or uses an identity
715 # sub if one isn't provided or it doesn't already exist), and applies
716 # it to the rest of the arguments (if any).
717 # ----------------------------------------------------------------------
723 if (ref($args[0]) eq 'CODE') {
724 $self->{$field} = shift @args;
726 elsif (! exists $self->{$field}) {
727 $self->{$field} = sub { return shift };
730 return @args ? $self->{$field}->(@args) : $self->{$field};
733 # ----------------------------------------------------------------------
736 # Calls UNIVERSAL::isa($ref, $type). I think UNIVERSAL::isa is ugly,
737 # but I like function overhead.
738 # ----------------------------------------------------------------------
740 my ($ref, $type) = @_;
741 return UNIVERSAL::isa($ref, $type);
744 # ----------------------------------------------------------------------
747 # Returns the $VERSION of the main SQL::Translator package.
748 # ----------------------------------------------------------------------
754 # ----------------------------------------------------------------------
756 my ( $self, $arg ) = @_;
757 if ( defined $arg ) {
758 $self->{'validate'} = $arg ? 1 : 0;
760 return $self->{'validate'} || 0;
765 # ----------------------------------------------------------------------
766 # Who killed the pork chops?
767 # What price bananas?
770 # ----------------------------------------------------------------------
776 SQL::Translator - manipulate structured data definitions (SQL and more)
782 my $translator = SQL::Translator->new(
785 # Print Parse::RecDescent trace
787 # Don't include comments in output
789 # Print name mutations, conflicts
791 # Add "drop table" statements
793 # Validate schema object
795 # Make all table names CAPS in producers which support this option
796 format_table_name => sub {my $tablename = shift; return uc($tablename)},
797 # Null-op formatting, only here for documentation's sake
798 format_package_name => sub {return shift},
799 format_fk_name => sub {return shift},
800 format_pk_name => sub {return shift},
803 my $output = $translator->translate(
806 # Or an arrayref of filenames, i.e. [ $file1, $file2, $file3 ]
808 ) or die $translator->error;
814 This documentation covers the API for SQL::Translator. For a more general
815 discussion of how to use the modules and scripts, please see
816 L<SQL::Translator::Manual>.
818 SQL::Translator is a group of Perl modules that converts
819 vendor-specific SQL table definitions into other formats, such as
820 other vendor-specific SQL, ER diagrams, documentation (POD and HTML),
821 XML, and Class::DBI classes. The main focus of SQL::Translator is
822 SQL, but parsers exist for other structured data formats, including
823 Excel spreadsheets and arbitrarily delimited text files. Through the
824 separation of the code into parsers and producers with an object model
825 in between, it's possible to combine any parser with any producer, to
826 plug in custom parsers or producers, or to manipulate the parsed data
827 via the built-in object model. Presently only the definition parts of
828 SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT,
833 The constructor is called C<new>, and accepts a optional hash of options.
884 All options are, well, optional; these attributes can be set via
885 instance methods. Internally, they are; no (non-syntactical)
886 advantage is gained by passing options to the constructor.
890 =head2 add_drop_table
892 Toggles whether or not to add "DROP TABLE" statements just before the
897 Toggles whether to print comments in the output. Accepts a true or false
898 value, returns the current value.
902 The C<producer> method is an accessor/mutator, used to retrieve or
903 define what subroutine is called to produce the output. A subroutine
904 defined as a producer will be invoked as a function (I<not a method>)
905 and passed its container C<SQL::Translator> instance, which it should
906 call the C<schema> method on, to get the C<SQL::Translator::Schema>
907 generated by the parser. It is expected that the function transform the
908 schema structure to a string. The C<SQL::Translator> instance is also useful
909 for informational purposes; for example, the type of the parser can be
910 retrieved using the C<parser_type> method, and the C<error> and
911 C<debug> methods can be called when needed.
913 When defining a producer, one of several things can be passed in: A
914 module name (e.g., C<My::Groovy::Producer>), a module name relative to
915 the C<SQL::Translator::Producer> namespace (e.g., C<MySQL>), a module
916 name and function combination (C<My::Groovy::Producer::transmogrify>),
917 or a reference to an anonymous subroutine. If a full module name is
918 passed in (for the purposes of this method, a string containing "::"
919 is considered to be a module name), it is treated as a package, and a
920 function called "produce" will be invoked: C<$modulename::produce>.
921 If $modulename cannot be loaded, the final portion is stripped off and
922 treated as a function. In other words, if there is no file named
923 F<My/Groovy/Producer/transmogrify.pm>, C<SQL::Translator> will attempt
924 to load F<My/Groovy/Producer.pm> and use C<transmogrify> as the name of
925 the function, instead of the default C<produce>.
927 my $tr = SQL::Translator->new;
929 # This will invoke My::Groovy::Producer::produce($tr, $data)
930 $tr->producer("My::Groovy::Producer");
932 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
933 $tr->producer("Sybase");
935 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
936 # assuming that My::Groovy::Producer::transmogrify is not a module
938 $tr->producer("My::Groovy::Producer::transmogrify");
940 # This will invoke the referenced subroutine directly, as
941 # $subref->($tr, $data);
942 $tr->producer(\&my_producer);
944 There is also a method named C<producer_type>, which is a string
945 containing the classname to which the above C<produce> function
946 belongs. In the case of anonymous subroutines, this method returns
949 Finally, there is a method named C<producer_args>, which is both an
950 accessor and a mutator. Arbitrary data may be stored in name => value
951 pairs for the producer subroutine to access:
953 sub My::Random::producer {
954 my ($tr, $data) = @_;
955 my $pr_args = $tr->producer_args();
957 # $pr_args is a hashref.
959 Extra data passed to the C<producer> method is passed to
962 $tr->producer("xSV", delimiter => ',\s*');
964 # In SQL::Translator::Producer::xSV:
965 my $args = $tr->producer_args;
966 my $delimiter = $args->{'delimiter'}; # value is ,\s*
970 The C<parser> method defines or retrieves a subroutine that will be
971 called to perform the parsing. The basic idea is the same as that of
972 C<producer> (see above), except the default subroutine name is
973 "parse", and will be invoked as C<$module_name::parse($tr, $data)>.
974 Also, the parser subroutine will be passed a string containing the
975 entirety of the data to be parsed.
977 # Invokes SQL::Translator::Parser::MySQL::parse()
978 $tr->parser("MySQL");
980 # Invokes My::Groovy::Parser::parse()
981 $tr->parser("My::Groovy::Parser");
983 # Invoke an anonymous subroutine directly
985 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
986 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
987 return $dumper->Dump;
990 There is also C<parser_type> and C<parser_args>, which perform
991 analogously to C<producer_type> and C<producer_args>
995 Toggles whether to print warnings of name conflicts, identifier
996 mutations, etc. Probably only generated by producers to let the user
997 know when something won't translate very smoothly (e.g., MySQL "enum"
998 fields into Oracle). Accepts a true or false value, returns the
1003 The C<translate> method calls the subroutines referenced by the
1004 C<parser> and C<producer> data members (described above). It accepts
1005 as arguments a number of things, in key => value format, including
1006 (potentially) a parser and a producer (they are passed directly to the
1007 C<parser> and C<producer> methods).
1009 Here is how the parameter list to C<translate> is parsed:
1015 1 argument means it's the data to be parsed; which could be a string
1016 (filename) or a reference to a scalar (a string stored in memory), or a
1017 reference to a hash, which is parsed as being more than one argument
1020 # Parse the file /path/to/datafile
1021 my $output = $tr->translate("/path/to/datafile");
1023 # Parse the data contained in the string $data
1024 my $output = $tr->translate(\$data);
1028 More than 1 argument means its a hash of things, and it might be
1029 setting a parser, producer, or datasource (this key is named
1030 "filename" or "file" if it's a file, or "data" for a SCALAR reference.
1032 # As above, parse /path/to/datafile, but with different producers
1033 for my $prod ("MySQL", "XML", "Sybase") {
1034 print $tr->translate(
1036 filename => "/path/to/datafile",
1040 # The filename hash key could also be:
1041 datasource => \$data,
1047 =head2 filename, data
1049 Using the C<filename> method, the filename of the data to be parsed
1050 can be set. This method can be used in conjunction with the C<data>
1051 method, below. If both the C<filename> and C<data> methods are
1052 invoked as mutators, the data set in the C<data> method is used.
1054 $tr->filename("/my/data/files/create.sql");
1058 my $create_script = do {
1060 open CREATE, "/my/data/files/create.sql" or die $!;
1063 $tr->data(\$create_script);
1065 C<filename> takes a string, which is interpreted as a filename.
1066 C<data> takes a reference to a string, which is used as the data to be
1067 parsed. If a filename is set, then that file is opened and read when
1068 the C<translate> method is called, as long as the data instance
1069 variable is not set.
1073 Returns the SQL::Translator::Schema object.
1077 Turns on/off the tracing option of Parse::RecDescent.
1081 Whether or not to validate the schema object after parsing and before
1086 Returns the version of the SQL::Translator release.
1090 The following people have contributed to the SQLFairy project:
1094 =item * Mark Addison <grommit@users.sourceforge.net>
1096 =item * Sam Angiuoli <angiuoli@users.sourceforge.net>
1098 =item * Dave Cash <dave@gnofn.org>
1100 =item * Darren Chamberlain <dlc@users.sourceforge.net>
1102 =item * Ken Y. Clark <kclark@cpan.org>
1104 =item * Allen Day <allenday@users.sourceforge.net>
1106 =item * Paul Harrington <phrrngtn@users.sourceforge.net>
1108 =item * Mikey Melillo <mmelillo@users.sourceforge.net>
1110 =item * Chris Mungall <cjm@fruitfly.org>
1112 =item * Ross Smith II <rossta@users.sf.net>
1114 =item * Gudmundur A. Thorisson <mummi@cshl.org>
1116 =item * Chris To <christot@users.sourceforge.net>
1118 =item * Jason Williams <smdwilliams@users.sourceforge.net>
1120 =item * Ying Zhang <zyolive@yahoo.com>
1124 If you would like to contribute to the project, you can send patches
1125 to the developers mailing list:
1127 sqlfairy-developers@lists.sourceforge.net
1129 Or send us a message (with your Sourceforge username) asking to be
1130 added to the project and what you'd like to contribute.
1135 This program is free software; you can redistribute it and/or modify
1136 it under the terms of the GNU General Public License as published by
1137 the Free Software Foundation; version 2.
1139 This program is distributed in the hope that it will be useful, but
1140 WITHOUT ANY WARRANTY; without even the implied warranty of
1141 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1142 General Public License for more details.
1144 You should have received a copy of the GNU General Public License
1145 along with this program; if not, write to the Free Software
1146 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
1151 Please use L<http://rt.cpan.org/> for reporting bugs.
1155 If you find this module useful, please use
1156 L<http://cpanratings.perl.org/rate/?distribution=SQL-Translator> to rate it.
1161 L<SQL::Translator::Parser>,
1162 L<SQL::Translator::Producer>,
1163 L<Parse::RecDescent>,
1166 L<Text::RecordParser>,