None available.
sub create_and_load
{ my $db = shift;
my $tablename = shift;
my @cols = @_;
my $sql = "CREATE TABLE $tablename ( ";
my @col_defs;
my @idx_defs;
my @col_names;
foreach my $col (@cols) {
my ($name, $type) = split(/\s+/,$col);
push @col_names, $name;
if(defined($type) && $type =~ /i/) {
push @col_defs, "$name INT";
}
elsif (defined($type) && $type =~ /f/) {
push @col_defs, "$name FLOAT";
}
elsif (defined($type) && $type =~ /l/) {
push @col_defs, "$name TEXT";
}
else {
push @col_defs, "$name VARCHAR(255)";
}
if(defined($type) && $type =~ /\*/) {
push @idx_defs, "KEY ${name}_idx($name)";
}
}
my $create_cols = join( ",\n", @col_defs, @idx_defs);
$sql .= $create_cols.")";
$sql .= " MAX_ROWS = 100000000" if ($tablename =~ /^tmp.*gty$/); $db->do( $sql );
load( $db, $tablename, @col_names ); } |
sub debug
{ print STDERR @_, "\n";
}
1; } |
sub dumpSQL
{ my $db = shift;
my $sql = shift;
local *FH;
my $counter = 0;
open( FH, ">$TMP_DIR/$TMP_FILE" )
or die( "Cannot open $TMP_DIR/$TMP_FILE: $!" );
my $sth = $db->prepare( $sql);
$sth->{mysql_use_result} = 1;
$sth->execute();
my $first;
while ( my $aref = $sth->fetchrow_arrayref() ) {
my @a = map {defined($_) ? $_ : '\N'} @$aref;
print FH join("\t", @a), "\n";
}
close FH;
$sth->finish();
}
} |
sub load
{ my $db = shift;
my $tablename = shift;
my @colnames = @_;
my $cols = join( ",", @colnames );
my $table_file = "$TMP_DIR/$tablename\_$$\.txt";
rename("$TMP_DIR/$TMP_FILE", $table_file);
my $sql;
my $local_option = 'LOCAL'; if( `hostname` =~ /^bc/ ){ $local_option = '';
}
elsif( ! -e $table_file ){ $local_option = '';
}
if ( @colnames ) {
$sql = qq{
LOAD DATA $local_option INFILE "$table_file"
INTO TABLE $tablename( $cols )
};
} else {
$sql = qq{
LOAD DATA $local_option INFILE "$table_file"
INTO TABLE $tablename
};
}
$db->do( $sql );
unlink( "$table_file" );
}
} |