Bodo

Thank you very, very much for your e-mail. That's just what I needed. I 
know that I should have been using placeholders for a long time and never 
got round to it - it seemed complicated. I've just spent a couple of hours 
getting them into my brain for an application that I thought was too 
difficult for them - and it runs 4 time quicker on lots of little tables 
used to populate a web site from tsv files.

I had confused place holders with bind_parameters in my mind, so you've 
clarified that point (it worked without, by the way).

You can always learn from the perl code of others and your code is no 
exception. I've learnt a few new tricks. Thanks.

Ian

Here's some of the code that updates a complete database:

Notes:

Each tsv file has the column names in the first row, eg:

AirConditioningID       AirConditioning
2       'AC'
3       'ACC'
4       'None'

1) INSERT or UPDATE a row flagging it as updated
2) DELETE all rows without the flag set
3) RESET all flags ready for the next session

The interesting bit is sub update. I hope someone else finds these 
techniques useful :-)

------
#!/usr/bin/perl
use strict;
use DBI;
my $start = time;
my $errorlog;
my $error;
my $message;

my $webdata = 'WebData';
my $dbh;
my @tablelist = qw~     AirConditioning
                        BodyStyle
                        Country
                        EngineFuel
                        EngineSize
                        EngineType
                        ModelSeries
                        Transmission
                        Variant
                        MainCategory
                        Category
                        Part
                        PartIndex
                        PartWord
                        Year
                        ~;
&connect;
&runupdate;
&delete;
&reset;
        
$dbh->disconnect();

$message = "\n\n".sprintf ("\n\t%2.2d seconds to process\n",(time - $start));
$errorlog .= loggit (\$message);

my $fileout = "$webdata/Update.log";
open LOG, ">$fileout" or die "\nCan't open $fileout\n";
print LOG $errorlog;
close LOG;
print "\n\nLog written to $fileout\n";


# -----------------------------------------------------------------------------
# Connect
# -----------------------------------------------------------------------------

sub connect
{
        my $database = 'DBI:ODBC:webdatabase';
        my $user = '';
        my $pass = '';

        $dbh = DBI->connect($database, $user, $pass);
        $dbh->{RaiseError} = 1;
        $message = "Can't connect to database";
        $errorlog .= loggit (\$message) unless defined $dbh;
}


# -----------------------------------------------------------------------------
# Update tables
# -----------------------------------------------------------------------------

sub runupdate
{
        $errorlog .= "\n\nUpdating for tables\n";
        foreach my $table (@tablelist)
        {
                $message = "\n".sprintf ('%20s',$table);
                $errorlog .= loggit (\$message);
                update($table);
        }
}


# -----------------------------------------------------------------------------
# Update one table
# -----------------------------------------------------------------------------

sub update
{
        my $table = $_[0];
        my $filein = "$webdata/tbl$table.txt";

        @ARGV = $filein;
        $_ = <>;
        chomp;
        s/\t/,/g;
        my $column = $_;
        my $icolumn = "$_,Updated";
        my @column = split ',', $column;
        my @deleted = ();
        my $rowcount = 0;
        my $where;

        if ($table eq 'PartWord')
        {
                $where = "$column[0] = ? AND $column[1] = ?";
        } elsif ($table eq 'PartIndex') {
                $where = "$column[2] = ? AND $column[3] = ?";
        } else {
                $where = "$column[0] = ?";
        }

        my $update_sql = "UPDATE tbl$table SET " . join(', ',  map {$_ . '= ?'} 
@column) . ", Updated=True $where";
        my $insert_sql = "INSERT INTO tbl$table (" . join(', ', @column) . ", 
Updated) VALUES (" . join(', ', map {'?'} @column) . ", True)";

        my $update_sth;
        if (!($update_sth = $dbh->prepare($update_sql)))
        {
                $message = "\nCould not prepare $update_sql\n";
                $errorlog .= loggit (\$message);
                $error = 1;
        }
        my $insert_sth;
        if (!($insert_sth = $dbh->prepare($insert_sql)))
        {
                $message = "\nCould not prepare $insert_sql\n";
                $errorlog .= loggit (\$message);
                $error = 1;
        }
        
        while (<>)
        {
                chomp;
                my @value = split "\t", $_;
                $rowcount++;
                my $placevalue;

                my $field = $column[0];
                if ($table eq 'PartCarCompanion' || $table eq 'PartWord' || $table eq 
'Promotion')
                {
                        $placevalue = "$value[0],$value[1]";
                } elsif ($table =~ 'Promotion') {
                        $placevalue = "$value[0],$value[1],$value[2]";
                } elsif ($table eq 'PartIndex') {
                        $placevalue = "$value[2],$value[3]";
                } else {
                        $placevalue = "$value[0]";
                }
                my @value = split "\t", $_;
                eval { $insert_sth->execute(@value) };
                if ($@)
                {
                        eval { $update_sth->execute(@value) };
                        print "\nUPDATE failed on values:\n\@values:\n$@\n" if $@;
                }
                print sprintf('%8d',$rowcount).(chr(8) x 8);
        }
        $message = sprintf('%8d',$rowcount).' records';
        $errorlog .= loggit (\$message);
}


# -----------------------------------------------------------------------------
# Delete
# -----------------------------------------------------------------------------

sub delete
{
        $message = "\n\nDeleting deleted records\n";
        $errorlog .= loggit (\$message);
        foreach my $table (@tablelist)
        {
                $errorlog .= "\n".sprintf ('%20s',$table);
                my $sql = "DELETE FROM tbl$table WHERE Updated=False";
                if (!($dbh->do($sql)))
                {
                        $message = "\nProblem doing $sql";
                        $errorlog .= loggit (\$message);
                }
        }
}


# -----------------------------------------------------------------------------
# Reset
# -----------------------------------------------------------------------------

sub reset
{
        $message = "\n\nReseting all rows\n";
        $errorlog .= loggit (\$message);
        foreach my $table (@tablelist)
        {
                $errorlog .= "\n".sprintf ('%20s',$table);
                my $sql = "UPDATE tbl$table SET Updated=False";
                if (!($dbh->do($sql)))
                {
                        $message = "\nProblem doing $sql";
                        $errorlog .= loggit (\$message);
                }
        }
}


# -----------------------------------------------------------------------------
# Log
# -----------------------------------------------------------------------------

sub loggit
{
        my ($message_ref) = @_;
        print $$message_ref;
        $$message_ref;
}

Reply via email to