1 package SQL::Translator::Parser::xSV;
3 # -------------------------------------------------------------------
4 # $Id: xSV.pm,v 1.9 2003-06-06 00:05:44 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
49 Indicates that the columns should be scanned to determine data types
50 and field sizes. True by default.
54 A shortcut to sending filters to Text::RecordParser, will create
55 callbacks that trim leading and trailing spaces from fields and headers.
60 Field names will automatically be normalized by
61 C<SQL::Translator::Utils::normalize>.
65 # -------------------------------------------------------------------
68 use vars qw($VERSION @EXPORT);
69 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
72 use Text::ParseWords qw(quotewords);
73 use Text::RecordParser;
74 use SQL::Translator::Utils qw(debug normalize_name);
76 use base qw(Exporter);
80 # Passed a SQL::Translator instance and a string containing the data
83 my ( $tr, $data ) = @_;
84 my $args = $tr->parser_args;
85 my $parser = Text::RecordParser->new(
86 field_separator => $args->{'field_separator'} || ',',
87 record_separator => $args->{'record_separator'} || "\n",
89 header_filter => \&normalize_name,
92 $parser->field_filter( sub { $_ = shift; s/^\s+|\s+$//g; $_ } )
93 unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
96 # Create skeleton structure, mostly empty.
106 my $schema = $tr->schema;
107 my $table = $schema->add_table( name => 'table1' );
110 # Get the field names from the first row.
112 $parser->bind_header;
113 my @field_names = $parser->field_list;
115 for ( my $i = 0; $i < @field_names; $i++ ) {
116 $parsed->{'table1'}{'fields'}{ $field_names[$i] } = {
119 name => $field_names[$i],
121 # Default datatype is "char"
124 # default size is 8bits; something more reasonable?
128 is_auto_inc => undef,
130 # field field is the primary key
131 is_primary_key => ($i == 0) ? 1 : undef,
134 my $field = $table->add_field(
135 name => $field_names[$i],
140 is_auto_increment => undef,
141 ) or die $table->error;
144 $table->primary_key( $field->name );
145 $field->is_primary_key(1);
150 # If directed, look at every field's values to guess size and type.
153 defined $args->{'scan_fields'} &&
154 $args->{'scan_fields'} == 0
156 my %field_info = map { $_, {} } @field_names;
157 while ( my $rec = $parser->fetchrow_hashref ) {
158 for my $field ( @field_names ) {
159 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
160 my $size = [ length $data ];
163 if ( $data =~ /^-?\d+$/ ) {
167 $data =~ /^-?[,\d]+\.[\d+]?$/
169 $data =~ /^-?[,\d]+?\.\d+$/
174 my ( $w, $d ) = map { s/,//g; $_ } split( /\./, $data );
175 $size = [ length $w, length $d ];
182 next unless defined $size->[ $i ];
183 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
184 if ( $size->[ $i ] > $fsize ) {
185 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
189 $field_info{ $field }{ $type }++;
193 for my $field ( keys %field_info ) {
194 my $size = $field_info{ $field }{'size'};
196 $field_info{ $field }{'char'} ? 'char' :
197 $field_info{ $field }{'float'} ? 'float' : 'integer';
199 $parsed->{'table1'}{'fields'}{ $field }{'size'} =
200 $field_info{ $field }{'size'};
202 $parsed->{'table1'}{'fields'}{ $field }{'data_type'} = $data_type;
204 my $field = $table->get_field( $field );
205 $field->size( $size );
206 $field->data_type( $data_type );
211 # Field 0 is primary key, by default, so add an index
213 for ( $parsed->{'table1'}->{'indices'}->[0] ) {
214 $_->{'type'} = 'primary_key';
215 $_->{'name'} = undef;
216 $_->{'fields'} = [ $field_names[0] ];
224 # -------------------------------------------------------------------
229 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
230 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.