| 115 |
|
|
| 116 |
sub open_table ($$$$$) { |
sub open_table ($$$$$) { |
| 117 |
my($self, $data, $table, $createMode, $lockMode) = @_; |
my($self, $data, $table, $createMode, $lockMode) = @_; |
| 118 |
|
|
| 119 |
|
# remember some attributes if scanning starts below |
| 120 |
|
$data->{Database}->{_cache}->{csv_tables}->{$table}->{'col_names'} = $data->{Database}->{csv_tables}->{$table}->{'col_names'} |
| 121 |
|
if $data->{Database}->{csv_tables}->{$table}->{'col_names'}; |
| 122 |
|
|
| 123 |
|
SCAN: |
| 124 |
|
if ($data->{f_stmt}->{command} eq 'SELECT' && $data->{Database}->{scan}) { |
| 125 |
|
# get rules from builtin rulebase if requested |
| 126 |
|
$data->{Database}->{'scan'} = _get_rules_autoscan() if $data->{Database}->{'scan'} == 1; |
| 127 |
|
delete $data->{Database}->{csv_tables}->{$table}; |
| 128 |
|
# rules left on stack? |
| 129 |
|
if (my $rule = shift @{$data->{Database}->{'scan'}}) { |
| 130 |
|
$data->{Database}->{scan_count}++; |
| 131 |
|
# merge csv-options to table metadata: |
| 132 |
|
# foreach (keys %{$rule}) { $data->{Database}->{csv_tables}->{$table}->{$_} = $rule->{$_}; } |
| 133 |
|
# overwrite table metadata, (re-)set csv-options: |
| 134 |
|
$data->{Database}->{csv_tables}->{$table} = $rule; |
| 135 |
|
} else { |
| 136 |
|
die "Missing first row or scanrule not applied"; |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
|
| 140 |
my $dbh = $data->{Database}; |
my $dbh = $data->{Database}; |
| 141 |
my $tables = $dbh->{csv_tables}; |
my $tables = $dbh->{csv_tables}; |
| 142 |
if (!exists($tables->{$table})) { |
if (!exists($tables->{$table})) { |
| 160 |
exists($meta->{'escape_char'}) ? $meta->{'escape_char'} : |
exists($meta->{'escape_char'}) ? $meta->{'escape_char'} : |
| 161 |
exists($dbh->{'csv_escape_char'}) ? $dbh->{'csv_escape_char'} : |
exists($dbh->{'csv_escape_char'}) ? $dbh->{'csv_escape_char'} : |
| 162 |
'"'; |
'"'; |
| 163 |
|
$opts{'always_quote'} = |
| 164 |
|
exists($meta->{'always_quote'}) ? $meta->{'always_quote'} : |
| 165 |
|
exists($dbh->{'csv_always_quote'}) ? $dbh->{'csv_always_quote'} : 0; |
| 166 |
$csv = $meta->{csv} = $class->new(\%opts); |
$csv = $meta->{csv} = $class->new(\%opts); |
| 167 |
} |
} |
| 168 |
my $file = $meta->{file} || $table; |
my $file = $meta->{file} || $table; |
| 193 |
} |
} |
| 194 |
if ($skipRows--) { |
if ($skipRows--) { |
| 195 |
if (!($array = $tbl->fetch_row($data))) { |
if (!($array = $tbl->fetch_row($data))) { |
| 196 |
die "Missing first row"; |
if ($data->{Database}->{'scan'}) { |
| 197 |
|
# if requested, try to help figuring out delimiters (just with SELECTs) |
| 198 |
|
$data->{Database}->{'scan_running'} = 1; |
| 199 |
|
goto SCAN; |
| 200 |
|
} |
| 201 |
|
my $die_msg = ''; |
| 202 |
|
if ($data->{f_stmt}->{command} ne 'SELECT') { |
| 203 |
|
$die_msg = ' - Note: scan does only work with a successful SELECT prior using ' . $data->{f_stmt}->{command}; |
| 204 |
|
} |
| 205 |
|
die "Missing first row" . $die_msg; |
| 206 |
} |
} |
| 207 |
$tbl->{col_names} = $array; |
$tbl->{col_names} = $array; |
| 208 |
while ($skipRows--) { |
while ($skipRows--) { |
| 210 |
} |
} |
| 211 |
} |
} |
| 212 |
$tbl->{first_row_pos} = $tbl->{fh}->tell(); |
$tbl->{first_row_pos} = $tbl->{fh}->tell(); |
| 213 |
|
$tbl->{size} = ($tbl->{fh}->stat)[7]; |
| 214 |
|
|
| 215 |
|
# checkpoint: did we already slurp to the end of the file? |
| 216 |
|
# is this correct to be assumed as an error |
| 217 |
|
# since it shouldn't occour while mungling with the first line(s)? |
| 218 |
|
if ( $tbl->{first_row_pos} == $tbl->{size} ) { |
| 219 |
|
$data->{Database}->{'scan_running'} = 1; |
| 220 |
|
$tbl->{fh}->setpos(0); |
| 221 |
|
goto SCAN; |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
# scan successful? |
| 225 |
|
if ($dbh->{'scan_running'}) { |
| 226 |
|
#print "matched rule: ", $dbh->{scan_count}, "\n"; |
| 227 |
|
# merge back cached attributes to local metadata |
| 228 |
|
foreach (keys %{$dbh->{_cache}->{csv_tables}->{$table}}) { |
| 229 |
|
$meta->{col_names} = $dbh->{_cache}->{csv_tables}->{$table}->{$_}; |
| 230 |
|
} |
| 231 |
|
# patch csv options from table metadata into the scope of the Text::CSV_XS object |
| 232 |
|
if ($data->{f_stmt}->{command} eq 'INSERT' || $data->{f_stmt}->{command} eq 'UPDATE') { |
| 233 |
|
my $rule = $data->{Database}->{csv_tables}->{$table}; |
| 234 |
|
foreach (keys %{$rule}) { $tbl->{csv_csv} = $rule->{$_}; } |
| 235 |
|
} |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
my $array; |
| 239 |
if (exists($meta->{col_names})) { |
if (exists($meta->{col_names})) { |
| 240 |
$array = $tbl->{col_names} = $meta->{col_names}; |
$array = $tbl->{col_names} = $meta->{col_names}; |
| 241 |
} elsif (!$tbl->{col_names} || !@{$tbl->{col_names}}) { |
} elsif (!$tbl->{col_names} || !@{$tbl->{col_names}}) { |