1 package SQL::Translator::Parser::xSV;
3 # -------------------------------------------------------------------
4 # $Id: xSV.pm,v 1.11 2003-10-09 16:38:51 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
26 SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
31 use SQL::Translator::Parser::xSV;
33 my $translator = SQL::Translator->new(
35 parser_args => { field_separator => "\t" },
40 Parses arbitrarily delimited text files. See the
41 Text::RecordParser manpage for arguments on how to parse the file
42 (e.g., C<field_separator>, C<record_separator>). Other arguments
51 Indicates that the columns should be scanned to determine data types
52 and field sizes. True by default.
56 A shortcut to sending filters to Text::RecordParser, will create
57 callbacks that trim leading and trailing spaces from fields and headers.
62 Field names will automatically be normalized by
63 C<SQL::Translator::Utils::normalize>.
67 # -------------------------------------------------------------------
70 use vars qw($VERSION @EXPORT);
71 $VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/;
74 use Text::ParseWords qw(quotewords);
75 use Text::RecordParser;
76 use SQL::Translator::Utils qw(debug normalize_name);
78 use base qw(Exporter);
82 # Passed a SQL::Translator instance and a string containing the data
85 my ( $tr, $data ) = @_;
86 my $args = $tr->parser_args;
87 my $parser = Text::RecordParser->new(
88 field_separator => $args->{'field_separator'} || ',',
89 record_separator => $args->{'record_separator'} || "\n",
91 header_filter => \&normalize_name,
94 $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } )
95 unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
97 my $schema = $tr->schema;
98 my $table = $schema->add_table( name => 'table1' );
101 # Get the field names from the first row.
103 $parser->bind_header;
104 my @field_names = $parser->field_list;
106 for ( my $i = 0; $i < @field_names; $i++ ) {
107 my $field = $table->add_field(
108 name => $field_names[$i],
113 is_auto_increment => undef,
114 ) or die $table->error;
117 $table->primary_key( $field->name );
118 $field->is_primary_key(1);
123 # If directed, look at every field's values to guess size and type.
126 defined $args->{'scan_fields'} &&
127 $args->{'scan_fields'} == 0
129 my %field_info = map { $_, {} } @field_names;
130 while ( my $rec = $parser->fetchrow_hashref ) {
131 for my $field ( @field_names ) {
132 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
133 my $size = [ length $data ];
136 if ( $data =~ /^-?\d+$/ ) {
140 $data =~ /^-?[,\d]+\.[\d+]?$/
142 $data =~ /^-?[,\d]+?\.\d+$/
147 my ( $w, $d ) = map { s/,//g; $_ } split( /\./, $data );
148 $size = [ length $w, length $d ];
155 next unless defined $size->[ $i ];
156 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
157 if ( $size->[ $i ] > $fsize ) {
158 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
162 $field_info{ $field }{ $type }++;
166 for my $field ( keys %field_info ) {
167 my $size = $field_info{ $field }{'size'};
169 $field_info{ $field }{'char'} ? 'char' :
170 $field_info{ $field }{'float'} ? 'float' : 'integer';
172 my $field = $table->get_field( $field );
173 $field->size( $size );
174 $field->data_type( $data_type );
183 # -------------------------------------------------------------------
188 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
189 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
193 Text::RecordParser, SQL::Translator.