X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator.pm;h=9656ceae6c4a7781d9314b41adade82536f863cb;hb=c051e2bb5fafee0841da418402dbe5d31957521c;hp=96a8d5dd9e78919d0d35dfa7beaec0f743a9534d;hpb=841a3f1a241cff7e2f9fbc1bdc1e84aaa0f10909;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 96a8d5d..9656cea 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -1,11 +1,9 @@ package SQL::Translator; # ---------------------------------------------------------------------- -# $Id: Translator.pm,v 1.42 2003-08-21 18:12:56 kycl4rk Exp $ +# $Id: Translator.pm,v 1.54 2004-03-09 19:15:31 kycl4rk Exp $ # ---------------------------------------------------------------------- -# Copyright (C) 2003 Ken Y. Clark , -# darren chamberlain , -# Chris Mungall +# Copyright (C) 2002-4 The SQLFairy Authors # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -28,8 +26,8 @@ use base 'Class::Base'; require 5.004; -$VERSION = '0.02'; -$REVISION = sprintf "%d.%02d", q$Revision: 1.42 $ =~ /(\d+)\.(\d+)/; +$VERSION = '0.05'; +$REVISION = sprintf "%d.%02d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; $ERROR = ""; @@ -37,6 +35,7 @@ use Carp qw(carp); use Data::Dumper; use Class::Base; +use File::Find; use File::Spec::Functions qw(catfile); use File::Basename qw(dirname); use IO::Dir; @@ -74,13 +73,13 @@ sub init { $self->parser ($config->{'parser'} || $config->{'from'} || $DEFAULT_SUB); $self->producer($config->{'producer'} || $config->{'to'} || $DEFAULT_SUB); - # - # Set up callbacks for formatting of pk,fk,table,package names in producer - # - $self->format_table_name($config->{'format_table_name'}); - $self->format_package_name($config->{'format_package_name'}); - $self->format_fk_name($config->{'format_fk_name'}); - $self->format_pk_name($config->{'format_pk_name'}); + # + # Set up callbacks for formatting of pk,fk,table,package names in producer + # + $self->format_table_name($config->{'format_table_name'}); + $self->format_package_name($config->{'format_package_name'}); + $self->format_fk_name($config->{'format_fk_name'}); + $self->format_pk_name($config->{'format_pk_name'}); # # Set the parser_args and producer_args @@ -333,9 +332,9 @@ sub filename { if (-d $filename) { my $msg = "Cannot use directory '$filename' as input source"; return $self->error($msg); - } elsif (ref($filename) eq 'ARRAY') { - $self->{'filename'} = $filename; - $self->debug("Got array of files: ".join(', ',@$filename)."\n"); + } elsif (ref($filename) eq 'ARRAY') { + $self->{'filename'} = $filename; + $self->debug("Got array of files: ".join(', ',@$filename)."\n"); } elsif (-f _ && -r _) { $self->{'filename'} = $filename; $self->debug("Got filename: '$self->{'filename'}'\n"); @@ -388,21 +387,21 @@ sub data { local $/; my $data; - my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename); + my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename); - foreach my $file (@files) { - unless (open FH, $file) { - return $self->error("Can't read file '$file': $!"); - } + foreach my $file (@files) { + unless (open FH, $file) { + return $self->error("Can't read file '$file': $!"); + } - $data .= ; + $data .= ; - unless (close FH) { - return $self->error("Can't close file '$file': $!"); - } - } + unless (close FH) { + return $self->error("Can't close file '$file': $!"); + } + } - $self->{'data'} = \$data; + $self->{'data'} = \$data; } return $self->{'data'}; @@ -527,9 +526,6 @@ sub translate { # Get the data. # ---------------------------------------------------------------- my $data = $self->data; - unless (ref($data) eq 'SCALAR' and length $$data) { - return $self->error("Empty data file!"); - } # ---------------------------------------------------------------- # Local reference to the parser subroutine @@ -574,8 +570,8 @@ sub translate { eval { $producer_output = $producer->($self) }; if ($@ || ! $producer_output) { - my $msg = sprintf "translate: Error with producer '%s': %s", - $producer_type, ($@) ? $@ : " no results"; + my $err = $@ || $self->error || "no results"; + my $msg = "translate: Error with producer '$producer_type': $err"; return $self->error($msg); } @@ -655,26 +651,57 @@ sub _args { # _list($type) # ---------------------------------------------------------------------- sub _list { - my $self = shift; - my $type = shift || return (); + my $self = shift; + my $type = shift || return (); my $uctype = ucfirst lc $type; - my %found; + # + # First find all the directories where SQL::Translator + # parsers or producers (the "type") appear to live. + # load("SQL::Translator::$uctype") or return (); my $path = catfile "SQL", "Translator", $uctype; + my @dirs; for (@INC) { my $dir = catfile $_, $path; $self->debug("_list_${type}s searching $dir\n"); next unless -d $dir; - - my $dh = IO::Dir->new($dir); - for (grep /\.pm$/, $dh->read) { - s/\.pm$//; - $found{ join "::", "SQL::Translator::$uctype", $_ } = 1; - } + push @dirs, $dir; } - return keys %found; + # + # Now use File::File::find to look recursively in those + # directories for all the *.pm files, then present them + # with the slashes turned into dashes. + # + my %found; + find( + sub { + if ( -f && m/\.pm$/ ) { + my $mod = $_; + $mod =~ s/\.pm$//; + my $cur_dir = $File::Find::dir; + my $base_dir = quotemeta catfile 'SQL', 'Translator', $uctype; + + # + # See if the current directory is below the base directory. + # + if ( $cur_dir =~ m/$base_dir(.*)/ ) { + $cur_dir = $1; + $cur_dir =~ s!^/!!; # kill leading slash + $cur_dir =~ s!/!-!g; # turn other slashes into dashes + } + else { + $cur_dir = ''; + } + + $found{ join '-', map { $_ || () } $cur_dir, $mod } = 1; + } + }, + @dirs + ); + + return sort { lc $a cmp lc $b } keys %found; } # ---------------------------------------------------------------------- @@ -748,6 +775,16 @@ sub isa($$) { } # ---------------------------------------------------------------------- +# version +# +# Returns the $VERSION of the main SQL::Translator package. +# ---------------------------------------------------------------------- +sub version { + my $self = shift; + return $VERSION; +} + +# ---------------------------------------------------------------------- sub validate { my ( $self, $arg ) = @_; if ( defined $arg ) { @@ -820,6 +857,10 @@ via the built-in object model. Presently only the definition parts of SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT, UPDATE, DELETE). +This documentation covers the API for SQL::Translator. For a more general +discussion of how to use the modules and scripts, please see +L. + =head1 CONSTRUCTOR The constructor is called C, and accepts a optional hash of options. @@ -1072,6 +1113,10 @@ Turns on/off the tracing option of Parse::RecDescent. Whether or not to validate the schema object after parsing and before producing. +=head2 version + +Returns the version of the SQL::Translator release. + =head1 AUTHORS The following people have contributed to the SQLFairy project: @@ -1082,6 +1127,8 @@ The following people have contributed to the SQLFairy project: =item * Sam Angiuoli +=item * Dave Cash + =item * Darren Chamberlain =item * Ken Y. Clark