remove commented copyright
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
index dff86b8..e243945 100644 (file)
@@ -1,26 +1,5 @@
 package SQL::Translator::Parser::xSV;
 
-# -------------------------------------------------------------------
-# $Id: xSV.pm,v 1.7 2003-05-09 17:15:30 kycl4rk Exp $
-# -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
-#                    darren chamberlain <darren@cpan.org>
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 =head1 NAME
 
 SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
@@ -37,11 +16,13 @@ SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
 
 =head1 DESCRIPTION
 
-Parses arbitrarily delimited text files.  See the 
+Parses arbitrarily delimited text files.  See the
 Text::RecordParser manpage for arguments on how to parse the file
 (e.g., C<field_separator>, C<record_separator>).  Other arguments
 include:
 
+=head1 OPTIONS
+
 =over
 
 =item * scan_fields
@@ -51,22 +32,20 @@ and field sizes.  True by default.
 
 =item * trim_fields
 
-A shortcut to sending filters to Text::RecordParser, will create 
+A shortcut to sending filters to Text::RecordParser, will create
 callbacks that trim leading and trailing spaces from fields and headers.
 True by default.
 
 =back
 
-Field names will automatically be normalized by 
-C<SQL::Translator::Utils::normalize>.
+Field names will automatically be normalized by
+C<SQL::Translator::Utils::normalize_name>.
 
 =cut
 
-# -------------------------------------------------------------------
-
 use strict;
 use vars qw($VERSION @EXPORT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.59';
 
 use Exporter;
 use Text::ParseWords qw(quotewords);
@@ -80,7 +59,7 @@ use base qw(Exporter);
 # Passed a SQL::Translator instance and a string containing the data
 #
 sub parse {
-    my ($tr, $data, $schema) = @_;
+    my ( $tr, $data )    = @_;
     my $args             = $tr->parser_args;
     my $parser           = Text::RecordParser->new(
         field_separator  => $args->{'field_separator'}  || ',',
@@ -89,20 +68,10 @@ sub parse {
         header_filter    => \&normalize_name,
     );
 
-    $parser->field_filter( sub { $_ = shift; s/^\s+|\s+$//g; $_ } ) 
+    $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } )
         unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
 
-    #
-    # Create skeleton structure, mostly empty.
-    #
-    my $parsed      =  {
-        table1      => {
-            type    => undef,
-            indices => [ { } ],
-            fields  => { },
-        },
-    };
-
+    my $schema = $tr->schema;
     my $table = $schema->add_table( name => 'table1' );
 
     #
@@ -112,24 +81,6 @@ sub parse {
     my @field_names = $parser->field_list;
 
     for ( my $i = 0; $i < @field_names; $i++ ) {
-        $parsed->{'table1'}{'fields'}{ $field_names[$i] } = {
-            type           => 'field',
-            order          => $i,
-            name           => $field_names[$i],
-
-            # Default datatype is "char"
-            data_type      => 'char',
-
-            # default size is 8bits; something more reasonable?
-            size           => [ 255 ],
-            null           => 1,
-            default        => '',
-            is_auto_inc    => undef,
-
-            # field field is the primary key
-            is_primary_key => ($i == 0) ? 1 : undef,
-        };
-
         my $field = $table->add_field(
             name              => $field_names[$i],
             data_type         => 'char',
@@ -148,7 +99,7 @@ sub parse {
     #
     # If directed, look at every field's values to guess size and type.
     #
-    unless ( 
+    unless (
         defined $args->{'scan_fields'} &&
         $args->{'scan_fields'} == 0
     ) {
@@ -156,22 +107,34 @@ sub parse {
         while ( my $rec = $parser->fetchrow_hashref ) {
             for my $field ( @field_names ) {
                 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
-                my $size = length $data;
+                my $size = [ length $data ];
                 my $type;
 
                 if ( $data =~ /^-?\d+$/ ) {
                     $type = 'integer';
                 }
-                elsif ( $data =~ /^-?[\d.]+$/ ) {
+                elsif (
+                    $data =~ /^-?[,\d]+\.[\d+]?$/
+                    ||
+                    $data =~ /^-?[,\d]+?\.\d+$/
+                    ||
+                    $data =~ /^-?\.\d+$/
+                ) {
                     $type = 'float';
+                    my ( $w, $d ) =
+                        map { s/,//g; length $_ || 1 } split( /\./, $data );
+                    $size = [ $w + $d, $d ];
                 }
                 else {
                     $type = 'char';
                 }
 
-                my $fsize = $field_info{ $field }{'size'} || 0;
-                if ( $size > $fsize ) {
-                    $field_info{ $field }{'size'} = $size;
+                for my $i ( 0, 1 ) {
+                    next unless defined $size->[ $i ];
+                    my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
+                    if ( $size->[ $i ] > $fsize ) {
+                        $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
+                    }
                 }
 
                 $field_info{ $field }{ $type }++;
@@ -179,15 +142,15 @@ sub parse {
         }
 
         for my $field ( keys %field_info ) {
-            my $size      = $field_info{ $field }{'size'};
-            my $data_type = 
-                $field_info{ $field }{'char'}  ? 'char'  : 
-                $field_info{ $field }{'float'} ? 'float' : 'integer';
-
-            $parsed->{'table1'}{'fields'}{ $field }{'size'} = 
-                [ $field_info{ $field }{'size'} ];
-
-            $parsed->{'table1'}{'fields'}{ $field }{'data_type'} = $data_type;
+            my $size      = $field_info{ $field }{'size'} || [ 1 ];
+            my $data_type =
+                $field_info{ $field }{'char'}    ? 'char'  :
+                $field_info{ $field }{'float'}   ? 'float' :
+                $field_info{ $field }{'integer'} ? 'integer' : 'char';
+
+            if ( $data_type eq 'char' && scalar @$size == 2 ) {
+                $size = [ $size->[0] + $size->[1] ];
+            }
 
             my $field = $table->get_field( $field );
             $field->size( $size );
@@ -195,30 +158,20 @@ sub parse {
         }
     }
 
-    #
-    # Field 0 is primary key, by default, so add an index
-    #
-    for ( $parsed->{'table1'}->{'indices'}->[0] ) {
-        $_->{'type'}   = 'primary_key';
-        $_->{'name'}   = undef;
-        $_->{'fields'} = [ $field_names[0] ];
-    }
-
-    return $parsed;
+    return 1;
 }
 
 1;
 
-# -------------------------------------------------------------------
 =pod
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =head1 SEE ALSO
 
-Text::RecordParser.
+Text::RecordParser, SQL::Translator.
 
 =cut