take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
1 package SQL::Translator::Parser::xSV;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =head1 NAME
22
23 SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files
24
25 =head1 SYNOPSIS
26
27   use SQL::Translator;
28   use SQL::Translator::Parser::xSV;
29
30   my $translator  =  SQL::Translator->new(
31       parser      => 'xSV',
32       parser_args => { field_separator => "\t" },
33   );
34
35 =head1 DESCRIPTION
36
37 Parses arbitrarily delimited text files.  See the
38 Text::RecordParser manpage for arguments on how to parse the file
39 (e.g., C<field_separator>, C<record_separator>).  Other arguments
40 include:
41
42 =head1 OPTIONS
43
44 =over
45
46 =item * scan_fields
47
48 Indicates that the columns should be scanned to determine data types
49 and field sizes.  True by default.
50
51 =item * trim_fields
52
53 A shortcut to sending filters to Text::RecordParser, will create
54 callbacks that trim leading and trailing spaces from fields and headers.
55 True by default.
56
57 =back
58
59 Field names will automatically be normalized by
60 C<SQL::Translator::Utils::normalize_name>.
61
62 =cut
63
64 use strict;
65 use vars qw($VERSION @EXPORT);
66 $VERSION = '1.59';
67
68 use Exporter;
69 use Text::ParseWords qw(quotewords);
70 use Text::RecordParser;
71 use SQL::Translator::Utils qw(debug normalize_name);
72
73 use base qw(Exporter);
74 @EXPORT = qw(parse);
75
76 #
77 # Passed a SQL::Translator instance and a string containing the data
78 #
79 sub parse {
80     my ( $tr, $data )    = @_;
81     my $args             = $tr->parser_args;
82     my $parser           = Text::RecordParser->new(
83         field_separator  => $args->{'field_separator'}  || ',',
84         record_separator => $args->{'record_separator'} || "\n",
85         data             => $data,
86         header_filter    => \&normalize_name,
87     );
88
89     $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } )
90         unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
91
92     my $schema = $tr->schema;
93     my $table = $schema->add_table( name => 'table1' );
94
95     #
96     # Get the field names from the first row.
97     #
98     $parser->bind_header;
99     my @field_names = $parser->field_list;
100
101     for ( my $i = 0; $i < @field_names; $i++ ) {
102         my $field = $table->add_field(
103             name              => $field_names[$i],
104             data_type         => 'char',
105             default_value     => '',
106             size              => 255,
107             is_nullable       => 1,
108             is_auto_increment => undef,
109         ) or die $table->error;
110
111         if ( $i == 0 ) {
112             $table->primary_key( $field->name );
113             $field->is_primary_key(1);
114         }
115     }
116
117     #
118     # If directed, look at every field's values to guess size and type.
119     #
120     unless (
121         defined $args->{'scan_fields'} &&
122         $args->{'scan_fields'} == 0
123     ) {
124         my %field_info = map { $_, {} } @field_names;
125         while ( my $rec = $parser->fetchrow_hashref ) {
126             for my $field ( @field_names ) {
127                 my $data = defined $rec->{ $field } ? $rec->{ $field } : '';
128                 my $size = [ length $data ];
129                 my $type;
130
131                 if ( $data =~ /^-?\d+$/ ) {
132                     $type = 'integer';
133                 }
134                 elsif (
135                     $data =~ /^-?[,\d]+\.[\d+]?$/
136                     ||
137                     $data =~ /^-?[,\d]+?\.\d+$/
138                     ||
139                     $data =~ /^-?\.\d+$/
140                 ) {
141                     $type = 'float';
142                     my ( $w, $d ) =
143                         map { s/,//g; length $_ || 1 } split( /\./, $data );
144                     $size = [ $w + $d, $d ];
145                 }
146                 else {
147                     $type = 'char';
148                 }
149
150                 for my $i ( 0, 1 ) {
151                     next unless defined $size->[ $i ];
152                     my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
153                     if ( $size->[ $i ] > $fsize ) {
154                         $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
155                     }
156                 }
157
158                 $field_info{ $field }{ $type }++;
159             }
160         }
161
162         for my $field ( keys %field_info ) {
163             my $size      = $field_info{ $field }{'size'} || [ 1 ];
164             my $data_type =
165                 $field_info{ $field }{'char'}    ? 'char'  :
166                 $field_info{ $field }{'float'}   ? 'float' :
167                 $field_info{ $field }{'integer'} ? 'integer' : 'char';
168
169             if ( $data_type eq 'char' && scalar @$size == 2 ) {
170                 $size = [ $size->[0] + $size->[1] ];
171             }
172
173             my $field = $table->get_field( $field );
174             $field->size( $size );
175             $field->data_type( $data_type );
176         }
177     }
178
179     return 1;
180 }
181
182 1;
183
184 =pod
185
186 =head1 AUTHORS
187
188 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
189 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
190
191 =head1 SEE ALSO
192
193 Text::RecordParser, SQL::Translator.
194
195 =cut