« blib/lib/SQL/Translator/Parser/Oracle.pm
↑ Summary
blib/lib/SQL/Translator/Parser/XML/SQLFairy.pm »
| 1 | package SQL::Translator::Parser::xSV; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | SQL::Translator::Parser::xSV - parser for arbitrarily delimited text files | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | use SQL::Translator; | ||||||||||||||||||||||||||||||||||
| 10 | use SQL::Translator::Parser::xSV; | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | my $translator = SQL::Translator->new( | ||||||||||||||||||||||||||||||||||
| 13 | parser => 'xSV', | ||||||||||||||||||||||||||||||||||
| 14 | parser_args => { field_separator => "\t" }, | ||||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | Parses arbitrarily delimited text files. See the | ||||||||||||||||||||||||||||||||||
| 20 | Text::RecordParser manpage for arguments on how to parse the file | ||||||||||||||||||||||||||||||||||
| 21 | (e.g., C<field_separator>, C<record_separator>). Other arguments | ||||||||||||||||||||||||||||||||||
| 22 | include: | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | =over | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | =item * scan_fields | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | Indicates that the columns should be scanned to determine data types | ||||||||||||||||||||||||||||||||||
| 31 | and field sizes. True by default. | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | =item * trim_fields | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | A shortcut to sending filters to Text::RecordParser, will create | ||||||||||||||||||||||||||||||||||
| 36 | callbacks that trim leading and trailing spaces from fields and headers. | ||||||||||||||||||||||||||||||||||
| 37 | True by default. | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | =back | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | Field names will automatically be normalized by | ||||||||||||||||||||||||||||||||||
| 42 | C<SQL::Translator::Utils::normalize_name>. | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 48 | 1 | our @EXPORT; | |||||||||||||||||||||||||||||||||
| 49 | 1 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use Text::ParseWords qw(quotewords); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use Text::RecordParser; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use SQL::Translator::Utils qw(debug normalize_name); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 57 | 1 | @EXPORT = qw(parse); | |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||||||||
| 60 | # Passed a SQL::Translator instance and a string containing the data | ||||||||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=29 SCAR=34.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 63 | 1 | my ($tr, $data) = @_; | |||||||||||||||||||||||||||||||||
| 64 | 1 | my $args = $tr->parser_args; | |||||||||||||||||||||||||||||||||
| 65 | my $parser = Text::RecordParser->new( | ||||||||||||||||||||||||||||||||||
| 66 | field_separator => $args->{'field_separator'} || ',', | ||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | record_separator => $args->{'record_separator'} || "\n", | ||||||||||||||||||||||||||||||||
|
Condition50%2/4
$args->{'field_separator'} || ','
$args->{'record_separator'} || "\n"
MC/DC0%0/1
$args->{'field_separator'} || ','
$args->{'field_separator'}
MC/DC0%0/1
$args->{'record_separator'} || "\n"
$args->{'record_separator'}
Truth table50%1/2
A
A= $args->{'field_separator'}
Truth table50%1/2
A
A= $args->{'record_separator'}
| |||||||||||||||||||||||||||||||||||
| 68 | data => $data, | ||||||||||||||||||||||||||||||||||
| 69 | header_filter => \&normalize_name, | ||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 21 | ▶ | $parser->field_filter(sub { $_ = shift || ''; s/^\s+|\s+$//g; $_ }) | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | unless defined $args->{'trim_fields'} && $args->{'trim_fields'} == 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"trim_fields"} and $args->{"trim_fields"} == 0
MC/DC0%0/2
defined $args->{"trim_fields"} and $args->{"trim_fields"} == 0
defined $args->{"trim_fields"}
$args->{"trim_fields"} == 0
Truth table33%1/3
A and B
A= defined $args->{"trim_fields"}B= $args->{"trim_fields"} == 0
| |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 1 | my $schema = $tr->schema; | |||||||||||||||||||||||||||||||||
| 76 | 1 | my $table = $schema->add_table(name => 'table1'); | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||||||||
| 79 | # Get the field names from the first row. | ||||||||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||||||||
| 81 | 1 | $parser->bind_header; | |||||||||||||||||||||||||||||||||
| 82 | 1 | my @field_names = $parser->field_list; | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | for (my $i = 0; $i < @field_names; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 7 | ▶ | my $field = $table->add_field( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | name => $field_names[$i], | ||||||||||||||||||||||||||||||||||
| 87 | data_type => 'char', | ||||||||||||||||||||||||||||||||||
| 88 | default_value => '', | ||||||||||||||||||||||||||||||||||
| 89 | size => 255, | ||||||||||||||||||||||||||||||||||
| 90 | is_nullable => 1, | ||||||||||||||||||||||||||||||||||
| 91 | is_auto_increment => undef, | ||||||||||||||||||||||||||||||||||
| 92 | ) or die $table->error; | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 7 | ▶ | if ($i == 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 1 | $table->primary_key($field->name); | |||||||||||||||||||||||||||||||||
| 96 | 1 | $field->is_primary_key(1); | |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||||||||
| 101 | # If directed, look at every field's values to guess size and type. | ||||||||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | unless (defined $args->{'scan_fields'} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"scan_fields"} and $args->{"scan_fields"} == 0
MC/DC0%0/2
defined $args->{"scan_fields"} and $args->{"scan_fields"} == 0
defined $args->{"scan_fields"}
$args->{"scan_fields"} == 0
Truth table33%1/3
A and B
A= defined $args->{"scan_fields"}B= $args->{"scan_fields"} == 0
| |||||||||||||||||||||||||||||||||||
| 104 | && $args->{'scan_fields'} == 0) { | ||||||||||||||||||||||||||||||||||
| 105 | 1 | my %field_info = map { $_, {} } @field_names; | |||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | while (my $rec = $parser->fetchrow_hashref) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 2 | for my $field (@field_names) { | |||||||||||||||||||||||||||||||||
| 108 | 14 | ▶ | my $data = defined $rec->{$field} ? $rec->{$field} : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 14 | my $size = [ length $data ]; | |||||||||||||||||||||||||||||||||
| 110 | 14 | my $type; | |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 14 | ▶ | if ($data =~ /^-?\d+$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 113 | 2 | $type = 'integer'; | |||||||||||||||||||||||||||||||||
| 114 | ▶ | } elsif ($data =~ /^-?[,\d]+\.[\d+]?$/ | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/ or $data =~ /^-?\.\d+$/
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/
MC/DC66%2/3
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/ or $data =~ /^-?\.\d+$/
$data =~ /^-?[,\d]+\.[\d+]?$/
$data =~ /^-?[,\d]+?\.\d+$/
$data =~ /^-?\.\d+$/
Truth table75%3/4
A or B or C
A= $data =~ /^-?[,\d]+\.[\d+]?$/B= $data =~ /^-?[,\d]+?\.\d+$/C= $data =~ /^-?\.\d+$/
| |||||||||||||||||||||||||||||||||||
| 115 | || $data =~ /^-?[,\d]+?\.\d+$/ | ||||||||||||||||||||||||||||||||||
| 116 | || $data =~ /^-?\.\d+$/) { | ||||||||||||||||||||||||||||||||||
| 117 | 2 | $type = 'float'; | |||||||||||||||||||||||||||||||||
| 118 | my ($w, $d) | ||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | = map { s/,//g; length $_ || 1 } split(/\./, $data); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
length $_ || 1
MC/DC100%1/1
length $_ || 1
length $_
Truth table100%2/2
A
A= length $_
| |||||||||||||||||||||||||||||||||||
| 120 | 2 | $size = [ $w + $d, $d ]; | |||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||
| 122 | 10 | $type = 'char'; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 14 | for my $i (0, 1) { | |||||||||||||||||||||||||||||||||
| 126 | 28 | ▶ | next unless defined $size->[$i]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | 16 | ▶ | my $fsize = $field_info{$field}{'size'}[$i] || 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field_info{$field}{'size'}[$i] || 0
MC/DC100%1/1
$field_info{$field}{'size'}[$i] || 0
$field_info{$field}{'size'}[$i]
Truth table100%2/2
A
A= $field_info{$field}{'size'}[$i]
| |||||||||||||||||||||||||||||||||||
| 128 | 16 | ▶ | if ($size->[$i] > $fsize) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 11 | $field_info{$field}{'size'}[$i] = $size->[$i]; | |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | 14 | $field_info{$field}{$type}++; | |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 1 | for my $field (keys %field_info) { | |||||||||||||||||||||||||||||||||
| 138 | 7 | ▶ | my $size = $field_info{$field}{'size'} || [1]; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$field_info{$field}{'size'} || [1]
MC/DC0%0/1
$field_info{$field}{'size'} || [1]
$field_info{$field}{'size'}
Truth table50%1/2
A
A= $field_info{$field}{'size'}
| |||||||||||||||||||||||||||||||||||
| 139 | my $data_type | ||||||||||||||||||||||||||||||||||
| 140 | = $field_info{$field}{'char'} ? 'char' | ||||||||||||||||||||||||||||||||||
| 141 | : $field_info{$field}{'float'} ? 'float' | ||||||||||||||||||||||||||||||||||
| 142 | 7 | ▶ | : $field_info{$field}{'integer'} ? 'integer' | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 143 | : 'char'; | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 7 | ▶ | if ($data_type eq 'char' && scalar @$size == 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data_type eq "char" and scalar @$size == 2
MC/DC0%0/2
$data_type eq "char" and scalar @$size == 2
$data_type eq "char"
scalar @$size == 2
Truth table66%2/3
A and B
A= $data_type eq "char"B= scalar @$size == 2
| |||||||||||||||||||||||||||||||||||
| 146 | 0 | $size = [ $size->[0] + $size->[1] ]; | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | 7 | my $field = $table->get_field($field); | |||||||||||||||||||||||||||||||||
| 150 | 7 | $field->size($size); | |||||||||||||||||||||||||||||||||
| 151 | 7 | $field->data_type($data_type); | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 1 | return 1; | |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | =pod | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, | ||||||||||||||||||||||||||||||||||
| 165 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | Text::RecordParser, SQL::Translator. | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||||||||