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