Strip evil svn:keywords
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
1 package SQL::Translator::Parser::xSV;
2
3 # -------------------------------------------------------------------
4 # $Id: xSV.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
7 #
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.
11 #
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.
16 #
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
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =head1 NAME
24
25 SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
26
27 =head1 SYNOPSIS
28
29   use SQL::Translator;
30   use SQL::Translator::Parser::xSV;
31
32   my $translator  =  SQL::Translator->new(
33       parser      => 'xSV',
34       parser_args => { field_separator => "\t" },
35   );
36
37 =head1 DESCRIPTION
38
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
42 include:
43
44 =head1 OPTIONS
45
46 =over
47
48 =item * scan_fields
49
50 Indicates that the columns should be scanned to determine data types
51 and field sizes.  True by default.
52
53 =item * trim_fields
54
55 A shortcut to sending filters to Text::RecordParser, will create 
56 callbacks that trim leading and trailing spaces from fields and headers.
57 True by default.
58
59 =back
60
61 Field names will automatically be normalized by 
62 C<SQL::Translator::Utils::normalize_name>.
63
64 =cut
65
66 # -------------------------------------------------------------------
67
68 use strict;
69 use vars qw(@EXPORT);
70
71 use Exporter;
72 use Text::ParseWords qw(quotewords);
73 use Text::RecordParser;
74 use SQL::Translator::Utils qw(debug normalize_name);
75
76 use base qw(Exporter);
77 @EXPORT = qw(parse);
78
79 #
80 # Passed a SQL::Translator instance and a string containing the data
81 #
82 sub parse {
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",
88         data             => $data,
89         header_filter    => \&normalize_name,
90     );
91
92     $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } ) 
93         unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
94
95     my $schema = $tr->schema;
96     my $table = $schema->add_table( name => 'table1' );
97
98     #
99     # Get the field names from the first row.
100     #
101     $parser->bind_header;
102     my @field_names = $parser->field_list;
103
104     for ( my $i = 0; $i < @field_names; $i++ ) {
105         my $field = $table->add_field(
106             name              => $field_names[$i],
107             data_type         => 'char',
108             default_value     => '',
109             size              => 255,
110             is_nullable       => 1,
111             is_auto_increment => undef,
112         ) or die $table->error;
113
114         if ( $i == 0 ) {
115             $table->primary_key( $field->name );
116             $field->is_primary_key(1);
117         }
118     }
119
120     #
121     # If directed, look at every field's values to guess size and type.
122     #
123     unless ( 
124         defined $args->{'scan_fields'} &&
125         $args->{'scan_fields'} == 0
126     ) {
127         my %field_info = map { $_, {} } @field_names;
128         while ( my $rec = $parser->fetchrow_hashref ) {
129             for my $field ( @field_names ) {
130                 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
131                 my $size = [ length $data ];
132                 my $type;
133
134                 if ( $data =~ /^-?\d+$/ ) {
135                     $type = 'integer';
136                 }
137                 elsif ( 
138                     $data =~ /^-?[,\d]+\.[\d+]?$/ 
139                     ||
140                     $data =~ /^-?[,\d]+?\.\d+$/  
141                     ||
142                     $data =~ /^-?\.\d+$/  
143                 ) {
144                     $type = 'float';
145                     my ( $w, $d ) = 
146                         map { s/,//g; length $_ || 1 } split( /\./, $data );
147                     $size = [ $w + $d, $d ];
148                 }
149                 else {
150                     $type = 'char';
151                 }
152
153                 for my $i ( 0, 1 ) {
154                     next unless defined $size->[ $i ];
155                     my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
156                     if ( $size->[ $i ] > $fsize ) {
157                         $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
158                     }
159                 }
160
161                 $field_info{ $field }{ $type }++;
162             }
163         }
164
165         for my $field ( keys %field_info ) {
166             my $size      = $field_info{ $field }{'size'} || [ 1 ];
167             my $data_type = 
168                 $field_info{ $field }{'char'}    ? 'char'  : 
169                 $field_info{ $field }{'float'}   ? 'float' :
170                 $field_info{ $field }{'integer'} ? 'integer' : 'char';
171
172             if ( $data_type eq 'char' && scalar @$size == 2 ) {
173                 $size = [ $size->[0] + $size->[1] ];
174             }
175
176             my $field = $table->get_field( $field );
177             $field->size( $size );
178             $field->data_type( $data_type );
179         }
180     }
181
182     return 1;
183 }
184
185 1;
186
187 # -------------------------------------------------------------------
188 =pod
189
190 =head1 AUTHORS
191
192 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
193 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
194
195 =head1 SEE ALSO
196
197 Text::RecordParser, SQL::Translator.
198
199 =cut