take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
CommitLineData
046f18e5 1package SQL::Translator::Parser::xSV;
2
44659089 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
825ed07b 21=head1 NAME
22
23SQL::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
ea93df61 37Parses arbitrarily delimited text files. See the
825ed07b 38Text::RecordParser manpage for arguments on how to parse the file
39(e.g., C<field_separator>, C<record_separator>). Other arguments
40include:
41
5eb4a350 42=head1 OPTIONS
43
825ed07b 44=over
45
46=item * scan_fields
47
48Indicates that the columns should be scanned to determine data types
9d90f9cd 49and field sizes. True by default.
825ed07b 50
51=item * trim_fields
52
ea93df61 53A shortcut to sending filters to Text::RecordParser, will create
825ed07b 54callbacks that trim leading and trailing spaces from fields and headers.
9d90f9cd 55True by default.
825ed07b 56
57=back
58
ea93df61 59Field names will automatically be normalized by
7b09a302 60C<SQL::Translator::Utils::normalize_name>.
825ed07b 61
62=cut
63
046f18e5 64use strict;
da06ac74 65use vars qw($VERSION @EXPORT);
11ad2df9 66$VERSION = '1.59';
046f18e5 67
68use Exporter;
69use Text::ParseWords qw(quotewords);
825ed07b 70use Text::RecordParser;
71use SQL::Translator::Utils qw(debug normalize_name);
046f18e5 72
73use base qw(Exporter);
74@EXPORT = qw(parse);
75
825ed07b 76#
046f18e5 77# Passed a SQL::Translator instance and a string containing the data
825ed07b 78#
046f18e5 79sub parse {
70944bc5 80 my ( $tr, $data ) = @_;
825ed07b 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
ea93df61 89 $parser->field_filter( sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ } )
9d90f9cd 90 unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0;
825ed07b 91
70944bc5 92 my $schema = $tr->schema;
9d90f9cd 93 my $table = $schema->add_table( name => 'table1' );
94
825ed07b 95 #
96 # Get the field names from the first row.
97 #
98 $parser->bind_header;
99 my @field_names = $parser->field_list;
046f18e5 100
825ed07b 101 for ( my $i = 0; $i < @field_names; $i++ ) {
9d90f9cd 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);
046f18e5 114 }
115 }
116
825ed07b 117 #
118 # If directed, look at every field's values to guess size and type.
119 #
ea93df61 120 unless (
9d90f9cd 121 defined $args->{'scan_fields'} &&
122 $args->{'scan_fields'} == 0
123 ) {
825ed07b 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 } : '';
2008ecf3 128 my $size = [ length $data ];
825ed07b 129 my $type;
130
131 if ( $data =~ /^-?\d+$/ ) {
132 $type = 'integer';
133 }
ea93df61 134 elsif (
135 $data =~ /^-?[,\d]+\.[\d+]?$/
2008ecf3 136 ||
ea93df61 137 $data =~ /^-?[,\d]+?\.\d+$/
2008ecf3 138 ||
ea93df61 139 $data =~ /^-?\.\d+$/
2008ecf3 140 ) {
825ed07b 141 $type = 'float';
ea93df61 142 my ( $w, $d ) =
35ea7ccb 143 map { s/,//g; length $_ || 1 } split( /\./, $data );
144 $size = [ $w + $d, $d ];
825ed07b 145 }
146 else {
147 $type = 'char';
148 }
149
2008ecf3 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 }
825ed07b 156 }
157
158 $field_info{ $field }{ $type }++;
159 }
160 }
161
162 for my $field ( keys %field_info ) {
6808d3e9 163 my $size = $field_info{ $field }{'size'} || [ 1 ];
ea93df61 164 my $data_type =
165 $field_info{ $field }{'char'} ? 'char' :
35ea7ccb 166 $field_info{ $field }{'float'} ? 'float' :
167 $field_info{ $field }{'integer'} ? 'integer' : 'char';
9d90f9cd 168
6808d3e9 169 if ( $data_type eq 'char' && scalar @$size == 2 ) {
170 $size = [ $size->[0] + $size->[1] ];
171 }
172
9d90f9cd 173 my $field = $table->get_field( $field );
174 $field->size( $size );
175 $field->data_type( $data_type );
825ed07b 176 }
177 }
178
f62bd16c 179 return 1;
046f18e5 180}
181
046f18e5 1821;
825ed07b 183
825ed07b 184=pod
185
90075866 186=head1 AUTHORS
825ed07b 187
188Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
11ad2df9 189Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
825ed07b 190
191=head1 SEE ALSO
192
5eb4a350 193Text::RecordParser, SQL::Translator.
825ed07b 194
195=cut