1 package SQL::Translator;
4 our ( $DEFAULT_SUB, $DEBUG, $ERROR );
8 our $VERSION = '0.11013';
9 $DEBUG = 0 unless defined $DEBUG;
12 use Carp qw(carp croak);
16 use File::Spec::Functions qw(catfile);
17 use File::Basename qw(dirname);
19 use SQL::Translator::Producer;
20 use SQL::Translator::Schema;
21 use SQL::Translator::Utils qw(throw ex2err);
23 $DEFAULT_SUB = sub { $_[0]->schema } unless defined $DEFAULT_SUB;
26 SQL::Translator::Role::Debug
27 SQL::Translator::Role::Error
28 SQL::Translator::Role::BuildArgs
31 around BUILDARGS => sub {
34 my $config = $self->$orig(@_);
36 # If a 'parser' or 'from' parameter is passed in, use that as the
37 # parser; if a 'producer' or 'to' parameter is passed in, use that
38 # as the producer; both default to $DEFAULT_SUB.
39 $config->{parser} ||= $config->{from} if defined $config->{from};
40 $config->{producer} ||= $config->{to} if defined $config->{to};
42 $config->{filename} ||= $config->{file} if defined $config->{file};
45 if (defined $config->{quote_identifiers}) {
46 $quote = $config->{quote_identifiers};
48 for (qw/quote_table_names quote_field_names/) {
49 carp "Ignoring deprecated parameter '$_', since 'quote_identifiers' is supplied"
50 if defined $config->{$_}
53 # Legacy one set the other is not
55 defined $config->{'quote_table_names'}
57 defined $config->{'quote_field_names'}
59 if (defined $config->{'quote_table_names'}) {
60 carp "Explicitly disabling the deprecated 'quote_table_names' implies disabling 'quote_identifiers' which in turn implies disabling 'quote_field_names'"
61 unless $config->{'quote_table_names'};
62 $quote = $config->{'quote_table_names'} ? 1 : 0;
65 carp "Explicitly disabling the deprecated 'quote_field_names' implies disabling 'quote_identifiers' which in turn implies disabling 'quote_table_names'"
66 unless $config->{'quote_field_names'};
67 $quote = $config->{'quote_field_names'} ? 1 : 0;
71 elsif(defined $config->{'quote_table_names'}) {
72 croak 'Setting quote_table_names and quote_field_names to conflicting values is no longer supported'
73 if ($config->{'quote_table_names'} xor $config->{'quote_field_names'});
75 $quote = $config->{'quote_table_names'} ? 1 : 0;
78 $config->{quote_identifiers} = $quote if defined $quote;
85 # Make sure all the tool-related stuff is set up
86 foreach my $tool (qw(producer parser)) {
87 $self->$tool($self->$tool);
94 coerce => sub { $_[0] ? 1 : 0 },
95 ) foreach qw(add_drop_table no_comments show_warnings trace validate);
97 # quote_identifiers is on by default, use a 0-but-true as indicator
98 # so we can allow individual producers to change the default
99 has quote_identifiers => (
101 default => sub { '0E0' },
102 coerce => sub { $_[0] || 0 },
105 sub quote_table_names {
106 (@_ > 1 and ($_[1] xor $_[0]->quote_identifiers) )
107 ? croak 'Using quote_table_names as a setter is no longer supported'
108 : $_[0]->quote_identifiers;
111 sub quote_field_names {
112 (@_ > 1 and ($_[1] xor $_[0]->quote_identifiers) )
113 ? croak 'Using quote_field_names as a setter is no longer supported'
114 : $_[0]->quote_identifiers;
117 after quote_identifiers => sub {
119 # synchronize for old code reaching directly into guts
120 $_[0]->{quote_table_names}
121 = $_[0]->{quote_field_names}
126 has producer => ( is => 'rw', default => sub { $DEFAULT_SUB } );
128 around producer => sub {
133 path => "SQL::Translator::Producer",
134 default_sub => "produce",
138 has producer_type => ( is => 'rwp', init_arg => undef );
140 has producer_args => ( is => 'rw', default => sub { +{} } );
142 around producer_args => sub {
144 shift->_args($orig, @_);
147 has parser => ( is => 'rw', default => sub { $DEFAULT_SUB } );
149 around parser => sub {
154 path => "SQL::Translator::Parser",
155 default_sub => "parse",
159 has parser_type => ( is => 'rwp', init_arg => undef );
161 has parser_args => ( is => 'rw', default => sub { +{} } );
163 around parser_args => sub {
165 shift->_args($orig, @_);
170 default => sub { [] },
173 # Set. Convert args to list of [\&code,@args]
174 foreach (@{$_[0]||[]}) {
175 my ($filt,@args) = ref($_) eq "ARRAY" ? @$_ : $_;
176 if ( isa($filt,"CODE") ) {
177 push @filters, [$filt,@args];
181 __PACKAGE__->debug("Adding $filt filter. Args:".Dumper(\@args)."\n");
182 $filt = _load_sub("$filt\::filter", "SQL::Translator::Filter")
183 || throw(__PACKAGE__->error);
184 push @filters, [$filt,@args];
191 around filters => sub {
194 return @{$self->$orig([@{$self->$orig}, @_])} if @_;
195 return @{$self->$orig};
201 my $filename = shift;
203 throw("Cannot use directory '$filename' as input source");
204 } elsif (not -f _ && -r _) {
205 throw("Cannot use '$filename' as input source: ".
206 "file does not exist or is not readable.");
211 around filename => \&ex2err;
218 # Set $self->data based on what was passed in. We will
219 # accept a number of things; do our best to get it right.
221 if (isa($data, 'ARRAY')) {
222 $data = join '', @$data;
224 elsif (isa($data, 'GLOB')) {
225 seek ($data, 0, 0) if eof ($data);
229 return isa($data, 'SCALAR') ? $data : \$data;
237 if (@_ > 1 && !ref $_[0]) {
238 return $self->$orig(\join('', @_));
241 return $self->$orig(@_);
243 return ex2err($orig, $self);
248 # If we have a filename but no data yet, populate.
249 if (my $filename = $self->filename) {
250 $self->debug("Opening '$filename' to get contents.\n");
254 my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename);
256 foreach my $file (@files) {
257 open my $fh, '<', $file
258 or throw("Can't read file '$file': $!");
262 close $fh or throw("Can't close file '$file': $!");
273 predicate => '_has_schema',
276 around reset => sub {
283 sub _build_schema { SQL::Translator::Schema->new(translator => shift) }
287 my ($args, $parser, $parser_type, $producer, $producer_type);
288 my ($parser_output, $producer_output, @producer_output);
292 # Passed a reference to a hash?
293 if (isa($_[0], 'HASH')) {
295 $self->debug("translate: Got a hashref\n");
299 # Passed a GLOB reference, i.e., filehandle
300 elsif (isa($_[0], 'GLOB')) {
301 $self->debug("translate: Got a GLOB reference\n");
305 # Passed a reference to a string containing the data
306 elsif (isa($_[0], 'SCALAR')) {
307 # passed a ref to a string
308 $self->debug("translate: Got a SCALAR reference (string)\n");
312 # Not a reference; treat it as a filename
313 elsif (! ref $_[0]) {
314 # Not a ref, it's a filename
315 $self->debug("translate: Got a filename\n");
316 $self->filename($_[0]);
319 # Passed something else entirely.
321 # We're not impressed. Take your empty string and leave.
324 # Actually, if data, parser, and producer are set, then we
325 # can continue. Too bad, because I like my comment
327 return "" unless ($self->data &&
333 # You must pass in a hash, or you get nothing.
338 # ----------------------------------------------------------------------
339 # Can specify the data to be transformed using "filename", "file",
340 # "data", or "datasource".
341 # ----------------------------------------------------------------------
342 if (my $filename = ($args->{'filename'} || $args->{'file'})) {
343 $self->filename($filename);
346 if (my $data = ($args->{'data'} || $args->{'datasource'})) {
350 # ----------------------------------------------------------------
352 # ----------------------------------------------------------------
353 my $data = $self->data;
355 # ----------------------------------------------------------------
356 # Local reference to the parser subroutine
357 # ----------------------------------------------------------------
358 if ($parser = ($args->{'parser'} || $args->{'from'})) {
359 $self->parser($parser);
361 $parser = $self->parser;
362 $parser_type = $self->parser_type;
364 # ----------------------------------------------------------------
365 # Local reference to the producer subroutine
366 # ----------------------------------------------------------------
367 if ($producer = ($args->{'producer'} || $args->{'to'})) {
368 $self->producer($producer);
370 $producer = $self->producer;
371 $producer_type = $self->producer_type;
373 # ----------------------------------------------------------------
374 # Execute the parser, the filters and then execute the producer.
375 # Allowances are made for each piece to die, or fail to compile,
376 # since the referenced subroutines could be almost anything. In
377 # the future, each of these might happen in a Safe environment,
378 # depending on how paranoid we want to be.
379 # ----------------------------------------------------------------
382 unless ( $self->_has_schema ) {
383 eval { $parser_output = $parser->($self, $$data) };
384 if ($@ || ! $parser_output) {
385 my $msg = sprintf "translate: Error with parser '%s': %s",
386 $parser_type, ($@) ? $@ : " no results";
387 return $self->error($msg);
390 $self->debug("Schema =\n", Dumper($self->schema), "\n");
392 # Validate the schema if asked to.
393 if ($self->validate) {
394 my $schema = $self->schema;
395 return $self->error('Invalid schema') unless $schema->is_valid;
400 foreach ($self->filters) {
402 my ($code,@args) = @$_;
403 eval { $code->($self->schema, @args) };
404 my $err = $@ || $self->error || 0;
405 return $self->error("Error with filter $filt_num : $err") if $err;
409 # Calling wantarray in the eval no work, wrong scope.
410 my $wantarray = wantarray ? 1 : 0;
413 @producer_output = $producer->($self);
415 $producer_output = $producer->($self);
418 if ($@ || !( $producer_output || @producer_output)) {
419 my $err = $@ || $self->error || "no results";
420 my $msg = "translate: Error with producer '$producer_type': $err";
421 return $self->error($msg);
424 return wantarray ? @producer_output : $producer_output;
428 return shift->_list("parser");
432 return shift->_list("producer");
436 # ======================================================================
438 # ======================================================================
440 # ----------------------------------------------------------------------
441 # _args($type, \%args);
443 # Gets or sets ${type}_args. Called by parser_args and producer_args.
444 # ----------------------------------------------------------------------
450 # If the first argument is an explicit undef (remember, we
451 # don't get here unless there is stuff in @_), then we clear
452 # out the producer_args hash.
453 if (! defined $_[0]) {
458 my $args = isa($_[0], 'HASH') ? shift : { @_ };
459 return $self->$orig({ %{$self->$orig}, %$args });
465 # ----------------------------------------------------------------------
466 # Does the get/set work for parser and producer. e.g.
467 # return $self->_tool({
468 # name => 'producer',
469 # path => "SQL::Translator::Producer",
470 # default_sub => "produce",
472 # ----------------------------------------------------------------------
474 my ($self,$args) = (shift, shift);
475 my $name = $args->{name};
476 my $orig = $args->{orig};
477 return $self->{$name} unless @_; # get accessor
479 my $path = $args->{path};
480 my $default_sub = $args->{default_sub};
483 # passed an anonymous subroutine reference
484 if (isa($tool, 'CODE')) {
486 $self->${\"_set_${name}_type"}("CODE");
487 $self->debug("Got $name: code ref\n");
490 # Module name was passed directly
491 # We try to load the name; if it doesn't load, there's a
492 # possibility that it has a function name attached to it,
493 # so we give it a go.
495 $tool =~ s/-/::/g if $tool !~ /::/;
497 ($code,$sub) = _load_sub("$tool\::$default_sub", $path);
499 if ( __PACKAGE__->error =~ m/Can't find module/ ) {
500 # Mod not found so try sub
501 ($code,$sub) = _load_sub("$tool", $path) unless $code;
502 die "Can't load $name subroutine '$tool' : ".__PACKAGE__->error
506 die "Can't load $name '$tool' : ".__PACKAGE__->error;
510 # get code reference and assign
511 my (undef,$module,undef) = $sub =~ m/((.*)::)?(\w+)$/;
513 $self->${\"_set_$name\_type"}($sub eq "CODE" ? "CODE" : $module);
514 $self->debug("Got $name: $sub\n");
517 # At this point, $self->{$name} contains a subroutine
518 # reference that is ready to run
520 # Anything left? If so, it's args
521 my $meth = "$name\_args";
522 $self->$meth(@_) if (@_);
524 return $self->{$name};
527 # ----------------------------------------------------------------------
529 # ----------------------------------------------------------------------
532 my $type = shift || return ();
533 my $uctype = ucfirst lc $type;
536 # First find all the directories where SQL::Translator
537 # parsers or producers (the "type") appear to live.
539 load("SQL::Translator::$uctype") or return ();
540 my $path = catfile "SQL", "Translator", $uctype;
543 my $dir = catfile $_, $path;
544 $self->debug("_list_${type}s searching $dir\n");
550 # Now use File::File::find to look recursively in those
551 # directories for all the *.pm files, then present them
552 # with the slashes turned into dashes.
557 if ( -f && m/\.pm$/ ) {
560 my $cur_dir = $File::Find::dir;
561 my $base_dir = quotemeta catfile 'SQL', 'Translator', $uctype;
564 # See if the current directory is below the base directory.
566 if ( $cur_dir =~ m/$base_dir(.*)/ ) {
568 $cur_dir =~ s!^/!!; # kill leading slash
569 $cur_dir =~ s!/!-!g; # turn other slashes into dashes
575 $found{ join '-', map { $_ || () } $cur_dir, $mod } = 1;
581 return sort { lc $a cmp lc $b } keys %found;
584 # ----------------------------------------------------------------------
585 # load(MODULE [,PATH[,PATH]...])
587 # Loads a Perl module. Short circuits if a module is already loaded.
589 # MODULE - is the name of the module to load.
591 # PATH - optional list of 'package paths' to look for the module in. e.g
592 # If you called load('Super::Foo' => 'My', 'Other') it will
593 # try to load the mod Super::Foo then My::Super::Foo then Other::Super::Foo.
595 # Returns package name of the module actually loaded or false and sets error.
597 # Note, you can't load a name from the root namespace (ie one without '::' in
598 # it), therefore a single word name without a path fails.
599 # ----------------------------------------------------------------------
603 push @path, "" if $name =~ /::/; # Empty path to check name on its own first
604 push @path, @_ if @_;
607 my $module = $_ ? "$_\::$name" : $name;
608 my $file = $module; $file =~ s[::][/]g; $file .= ".pm";
609 __PACKAGE__->debug("Loading $name as $file\n");
610 return $module if $INC{$file}; # Already loaded
612 eval { require $file };
613 next if $@ =~ /Can't locate $file in \@INC/;
614 eval { $module->import() } unless $@;
615 return __PACKAGE__->error("Error loading $name as $module : $@")
616 if $@ && $@ !~ /"SQL::Translator::Producer" is not exported/;
618 return $module; # Module loaded ok
621 return __PACKAGE__->error("Can't find module $name. Path:".join(",",@path));
624 # ----------------------------------------------------------------------
625 # Load the sub name given (including package), optionally using a base package
626 # path. Returns code ref and name of sub loaded, including its package.
627 # (\&code, $sub) = load_sub( 'MySQL::produce', "SQL::Translator::Producer" );
628 # (\&code, $sub) = load_sub( 'MySQL::produce', @path );
629 # ----------------------------------------------------------------------
631 my ($tool, @path) = @_;
633 my (undef,$module,$func_name) = $tool =~ m/((.*)::)?(\w+)$/;
634 if ( my $module = load($module => @path) ) {
635 my $sub = "$module\::$func_name";
636 return wantarray ? ( \&{ $sub }, $sub ) : \&$sub;
641 sub format_table_name {
642 return shift->_format_name('_format_table_name', @_);
645 sub format_package_name {
646 return shift->_format_name('_format_package_name', @_);
650 return shift->_format_name('_format_fk_name', @_);
654 return shift->_format_name('_format_pk_name', @_);
657 # ----------------------------------------------------------------------
658 # The other format_*_name methods rely on this one. It optionally
659 # accepts a subroutine ref as the first argument (or uses an identity
660 # sub if one isn't provided or it doesn't already exist), and applies
661 # it to the rest of the arguments (if any).
662 # ----------------------------------------------------------------------
668 if (ref($args[0]) eq 'CODE') {
669 $self->{$field} = shift @args;
671 elsif (! exists $self->{$field}) {
672 $self->{$field} = sub { return shift };
675 return @args ? $self->{$field}->(@args) : $self->{$field};
679 my ($ref, $type) = @_;
680 return UNIVERSAL::isa($ref, $type);
688 # Must come after all 'has' declarations
689 around new => \&ex2err;
693 # ----------------------------------------------------------------------
694 # Who killed the pork chops?
695 # What price bananas?
698 # ----------------------------------------------------------------------
704 SQL::Translator - manipulate structured data definitions (SQL and more)
710 my $translator = SQL::Translator->new(
713 # Print Parse::RecDescent trace
715 # Don't include comments in output
717 # Print name mutations, conflicts
719 # Add "drop table" statements
721 # to quote or not to quote, thats the question
722 quote_identifiers => 1,
723 # Validate schema object
725 # Make all table names CAPS in producers which support this option
726 format_table_name => sub {my $tablename = shift; return uc($tablename)},
727 # Null-op formatting, only here for documentation's sake
728 format_package_name => sub {return shift},
729 format_fk_name => sub {return shift},
730 format_pk_name => sub {return shift},
733 my $output = $translator->translate(
736 # Or an arrayref of filenames, i.e. [ $file1, $file2, $file3 ]
738 ) or die $translator->error;
744 This documentation covers the API for SQL::Translator. For a more general
745 discussion of how to use the modules and scripts, please see
746 L<SQL::Translator::Manual>.
748 SQL::Translator is a group of Perl modules that converts
749 vendor-specific SQL table definitions into other formats, such as
750 other vendor-specific SQL, ER diagrams, documentation (POD and HTML),
751 XML, and Class::DBI classes. The main focus of SQL::Translator is
752 SQL, but parsers exist for other structured data formats, including
753 Excel spreadsheets and arbitrarily delimited text files. Through the
754 separation of the code into parsers and producers with an object model
755 in between, it's possible to combine any parser with any producer, to
756 plug in custom parsers or producers, or to manipulate the parsed data
757 via the built-in object model. Presently only the definition parts of
758 SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT,
763 The constructor is called C<new>, and accepts a optional hash of options.
810 quote_table_names (DEPRECATED)
814 quote_field_names (DEPRECATED)
830 All options are, well, optional; these attributes can be set via
831 instance methods. Internally, they are; no (non-syntactical)
832 advantage is gained by passing options to the constructor.
836 =head2 add_drop_table
838 Toggles whether or not to add "DROP TABLE" statements just before the
841 =head2 quote_identifiers
843 Toggles whether or not to quote identifiers (table, column, constraint, etc.)
844 with a quoting mechanism suitable for the chosen Producer. The default (true)
847 =head2 quote_table_names
849 DEPRECATED - A legacy proxy to L</quote_identifiers>
851 =head2 quote_field_names
853 DEPRECATED - A legacy proxy to L</quote_identifiers>
857 Toggles whether to print comments in the output. Accepts a true or false
858 value, returns the current value.
862 The C<producer> method is an accessor/mutator, used to retrieve or
863 define what subroutine is called to produce the output. A subroutine
864 defined as a producer will be invoked as a function (I<not a method>)
865 and passed its container C<SQL::Translator> instance, which it should
866 call the C<schema> method on, to get the C<SQL::Translator::Schema>
867 generated by the parser. It is expected that the function transform the
868 schema structure to a string. The C<SQL::Translator> instance is also useful
869 for informational purposes; for example, the type of the parser can be
870 retrieved using the C<parser_type> method, and the C<error> and
871 C<debug> methods can be called when needed.
873 When defining a producer, one of several things can be passed in: A
874 module name (e.g., C<My::Groovy::Producer>), a module name relative to
875 the C<SQL::Translator::Producer> namespace (e.g., C<MySQL>), a module
876 name and function combination (C<My::Groovy::Producer::transmogrify>),
877 or a reference to an anonymous subroutine. If a full module name is
878 passed in (for the purposes of this method, a string containing "::"
879 is considered to be a module name), it is treated as a package, and a
880 function called "produce" will be invoked: C<$modulename::produce>.
881 If $modulename cannot be loaded, the final portion is stripped off and
882 treated as a function. In other words, if there is no file named
883 F<My/Groovy/Producer/transmogrify.pm>, C<SQL::Translator> will attempt
884 to load F<My/Groovy/Producer.pm> and use C<transmogrify> as the name of
885 the function, instead of the default C<produce>.
887 my $tr = SQL::Translator->new;
889 # This will invoke My::Groovy::Producer::produce($tr, $data)
890 $tr->producer("My::Groovy::Producer");
892 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
893 $tr->producer("Sybase");
895 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
896 # assuming that My::Groovy::Producer::transmogrify is not a module
898 $tr->producer("My::Groovy::Producer::transmogrify");
900 # This will invoke the referenced subroutine directly, as
901 # $subref->($tr, $data);
902 $tr->producer(\&my_producer);
904 There is also a method named C<producer_type>, which is a string
905 containing the classname to which the above C<produce> function
906 belongs. In the case of anonymous subroutines, this method returns
909 Finally, there is a method named C<producer_args>, which is both an
910 accessor and a mutator. Arbitrary data may be stored in name => value
911 pairs for the producer subroutine to access:
913 sub My::Random::producer {
914 my ($tr, $data) = @_;
915 my $pr_args = $tr->producer_args();
917 # $pr_args is a hashref.
919 Extra data passed to the C<producer> method is passed to
922 $tr->producer("xSV", delimiter => ',\s*');
924 # In SQL::Translator::Producer::xSV:
925 my $args = $tr->producer_args;
926 my $delimiter = $args->{'delimiter'}; # value is ,\s*
930 The C<parser> method defines or retrieves a subroutine that will be
931 called to perform the parsing. The basic idea is the same as that of
932 C<producer> (see above), except the default subroutine name is
933 "parse", and will be invoked as C<$module_name::parse($tr, $data)>.
934 Also, the parser subroutine will be passed a string containing the
935 entirety of the data to be parsed.
937 # Invokes SQL::Translator::Parser::MySQL::parse()
938 $tr->parser("MySQL");
940 # Invokes My::Groovy::Parser::parse()
941 $tr->parser("My::Groovy::Parser");
943 # Invoke an anonymous subroutine directly
945 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
946 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
947 return $dumper->Dump;
950 There is also C<parser_type> and C<parser_args>, which perform
951 analogously to C<producer_type> and C<producer_args>
955 Set or retreive the filters to run over the schema during the
956 translation, before the producer creates its output. Filters are sub
957 routines called, in order, with the schema object to filter as the 1st
958 arg and a hash of options (passed as a list) for the rest of the args.
959 They are free to do whatever they want to the schema object, which will be
960 handed to any following filters, then used by the producer.
962 Filters are set as an array, which gives the order they run in.
963 Like parsers and producers, they can be defined by a module name, a
964 module name relative to the SQL::Translator::Filter namespace, a module
965 name and function name together or a reference to an anonymous subroutine.
966 When using a module name a function called C<filter> will be invoked in
967 that package to do the work.
969 To pass args to the filter set it as an array ref with the 1st value giving
970 the filter (name or sub) and the rest its args. e.g.
975 # Do stuff to schema here!
978 [ "Names", table => 'lc' ],
979 [ "Foo", foo => "bar", hello => "world" ],
983 Although you normally set them in the constructor, which calls
984 through to filters. i.e.
986 my $translator = SQL::Translator->new(
990 [ "Names", table => 'lc' ],
995 See F<t/36-filters.t> for more examples.
997 Multiple set calls to filters are cumulative with new filters added to
998 the end of the current list.
1000 Returns the filters as a list of array refs, the 1st value being a
1001 reference to the filter sub and the rest its args.
1003 =head2 show_warnings
1005 Toggles whether to print warnings of name conflicts, identifier
1006 mutations, etc. Probably only generated by producers to let the user
1007 know when something won't translate very smoothly (e.g., MySQL "enum"
1008 fields into Oracle). Accepts a true or false value, returns the
1013 The C<translate> method calls the subroutine referenced by the
1014 C<parser> data member, then calls any C<filters> and finally calls
1015 the C<producer> sub routine (these members are described above).
1016 It accepts as arguments a number of things, in key => value format,
1017 including (potentially) a parser and a producer (they are passed
1018 directly to the C<parser> and C<producer> methods).
1020 Here is how the parameter list to C<translate> is parsed:
1026 1 argument means it's the data to be parsed; which could be a string
1027 (filename) or a reference to a scalar (a string stored in memory), or a
1028 reference to a hash, which is parsed as being more than one argument
1031 # Parse the file /path/to/datafile
1032 my $output = $tr->translate("/path/to/datafile");
1034 # Parse the data contained in the string $data
1035 my $output = $tr->translate(\$data);
1039 More than 1 argument means its a hash of things, and it might be
1040 setting a parser, producer, or datasource (this key is named
1041 "filename" or "file" if it's a file, or "data" for a SCALAR reference.
1043 # As above, parse /path/to/datafile, but with different producers
1044 for my $prod ("MySQL", "XML", "Sybase") {
1045 print $tr->translate(
1047 filename => "/path/to/datafile",
1051 # The filename hash key could also be:
1052 datasource => \$data,
1058 =head2 filename, data
1060 Using the C<filename> method, the filename of the data to be parsed
1061 can be set. This method can be used in conjunction with the C<data>
1062 method, below. If both the C<filename> and C<data> methods are
1063 invoked as mutators, the data set in the C<data> method is used.
1065 $tr->filename("/my/data/files/create.sql");
1069 my $create_script = do {
1071 open CREATE, "/my/data/files/create.sql" or die $!;
1074 $tr->data(\$create_script);
1076 C<filename> takes a string, which is interpreted as a filename.
1077 C<data> takes a reference to a string, which is used as the data to be
1078 parsed. If a filename is set, then that file is opened and read when
1079 the C<translate> method is called, as long as the data instance
1080 variable is not set.
1084 Returns the SQL::Translator::Schema object.
1088 Turns on/off the tracing option of Parse::RecDescent.
1092 Whether or not to validate the schema object after parsing and before
1097 Returns the version of the SQL::Translator release.
1101 See the included AUTHORS file:
1102 L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
1104 If you would like to contribute to the project, you can send patches
1105 to the developers mailing list:
1107 sqlfairy-developers@lists.sourceforge.net
1109 Or send us a message (with your Sourceforge username) asking to be
1110 added to the project and what you'd like to contribute.
1115 This program is free software; you can redistribute it and/or modify
1116 it under the terms of the GNU General Public License as published by
1117 the Free Software Foundation; version 2.
1119 This program is distributed in the hope that it will be useful, but
1120 WITHOUT ANY WARRANTY; without even the implied warranty of
1121 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1122 General Public License for more details.
1124 You should have received a copy of the GNU General Public License
1125 along with this program; if not, write to the Free Software
1126 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
1131 Please use L<http://rt.cpan.org/> for reporting bugs.
1135 If you find this module useful, please use
1136 L<http://cpanratings.perl.org/rate/?distribution=SQL-Translator> to rate it.
1141 L<SQL::Translator::Parser>,
1142 L<SQL::Translator::Producer>,
1143 L<Parse::RecDescent>,
1146 L<Text::RecordParser>,