1 package SQL::Translator::Parser::xSV;
3 # -------------------------------------------------------------------
4 # $Id: xSV.pm,v 1.16 2004-02-09 22:23:40 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 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 # -------------------------------------------------------------------
25 SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
30 use SQL::Translator::Parser::xSV;
32 my $translator = SQL::Translator->new(
34 parser_args => { field_separator => "\t" },
39 Parses arbitrarily delimited text files. See the
40 Text::RecordParser manpage for arguments on how to parse the file
41 (e.g., C<field_separator>, C<record_separator>). Other arguments
50 Indicates that the columns should be scanned to determine data types
51 and field sizes. True by default.
55 A shortcut to sending filters to Text::RecordParser, will create
56 callbacks that trim leading and trailing spaces from fields and headers.
61 Field names will automatically be normalized by
62 C<SQL::Translator::Utils::normalize_name>.
66 # -------------------------------------------------------------------
69 use vars qw($VERSION @EXPORT);
70 $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/;
73 use Text::ParseWords qw(quotewords);
74 use Text::RecordParser;
75 use SQL::Translator::Utils qw(debug normalize_name);
77 use base qw(Exporter);
81 # Passed a SQL::Translator instance and a string containing the data
84 my ( $tr, $data ) = @_;
85 my $args = $tr->parser_args;
86 my $parser = Text::RecordParser->new(
87 field_separator => $args->{'field_separator'} || ',',
88 record_separator => $args->{'record_separator'} || "\n",
90 header_filter => \&normalize_name,
93 $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } )
94 unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
96 my $schema = $tr->schema;
97 my $table = $schema->add_table( name => 'table1' );
100 # Get the field names from the first row.
102 $parser->bind_header;
103 my @field_names = $parser->field_list;
105 for ( my $i = 0; $i < @field_names; $i++ ) {
106 my $field = $table->add_field(
107 name => $field_names[$i],
112 is_auto_increment => undef,
113 ) or die $table->error;
116 $table->primary_key( $field->name );
117 $field->is_primary_key(1);
122 # If directed, look at every field's values to guess size and type.
125 defined $args->{'scan_fields'} &&
126 $args->{'scan_fields'} == 0
128 my %field_info = map { $_, {} } @field_names;
129 while ( my $rec = $parser->fetchrow_hashref ) {
130 for my $field ( @field_names ) {
131 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
132 my $size = [ length $data ];
135 if ( $data =~ /^-?\d+$/ ) {
139 $data =~ /^-?[,\d]+\.[\d+]?$/
141 $data =~ /^-?[,\d]+?\.\d+$/
147 map { s/,//g; length $_ || 1 } split( /\./, $data );
148 $size = [ $w + $d, $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'} || [ 1 ];
169 $field_info{ $field }{'char'} ? 'char' :
170 $field_info{ $field }{'float'} ? 'float' :
171 $field_info{ $field }{'integer'} ? 'integer' : 'char';
173 if ( $data_type eq 'char' && scalar @$size == 2 ) {
174 $size = [ $size->[0] + $size->[1] ];
177 my $field = $table->get_field( $field );
178 $field->size( $size );
179 $field->data_type( $data_type );
188 # -------------------------------------------------------------------
193 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
194 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
198 Text::RecordParser, SQL::Translator.