$dbh->{AutoCommit} = 0; #DO NOT USE THIS LINE
# $dbh->begin_work; #USE THIS LINE
####
$dbh->do("DROP TABLE IF EXISTS $tablename"); #ADDED THIS
$stmt = "CREATE TABLE IF NOT EXISTS $tablename.....
####
use SQL::Abstract;
use Data::Dumper;
use DBD::SQLite;
use Test::More;
use Data::Dump qw(pp);
use strict;
use warnings;
my $tablename = 'atable';
my $dbfile = 'tmp.sqlite';
if (-e $dbfile)
{
unlink $dbfile
or die "delete of existing $dbfile failed $!";
}
my %attr = ( RaiseError => 1); #auto die with error printout
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",\%attr)
or die "Couldn't connect to database $dbfile: " . DBI->errstr;
create_test_table( $dbh, $tablename);
print "Original Test Table Contents...\n";
dump_test_table ( $dbh, $tablename);
### TEST 1 ###
my $WHERE = undef; # (c3 = 'c') OR (c3 = 'ccc')
db_modify_column_value(
$dbh,
$tablename,
[qw/c1 c2/], # the columns to modify with below sub
sub {
my ($href, $colnames) = @_;
foreach (@$colnames)
{
$href->{$_}= uc ($href->{$_});
}
},
$WHERE
);
print "First Test Without a Where Clause...\n";
print "Test Table after modifications to cols c1 and c2....\n";
dump_test_table ( $dbh, $tablename);
### TEST 2 ###
create_test_table( $dbh, $tablename);
print "Second Test mods to c1 and c2 WHERE (c3 = 'c') OR (c3 = 'ccc')\n";
$WHERE = "WHERE (c3 = 'c') OR (c3 = 'ccc')";
db_modify_column_value(
$dbh,
$tablename,
[qw/c1 c2/], # the columns to modify with below sub
sub {
my ($href, $colnames) = @_;
foreach (@$colnames)
{
$href->{$_}= uc ($href->{$_});
}
},
$WHERE
);
print "Test Table after modifications....\n";
dump_test_table ( $dbh, $tablename);
#### subs #######
##
sub db_modify_column_value{
my ($dbh,$tablename,$col_arrayref,$exe,$WHERE)=@_;
my $result= fetchrows_subset_of_table($dbh,$tablename,$WHERE);
my $updateSQL = "UPDATE $tablename SET ".join("=?,",@$col_arrayref)."=?"." WHERE rowid =?";
# print "$updateSQL\n";
my $update = $dbh->prepare($updateSQL);
$dbh->begin_work;
foreach my $row_hash_ref (@$result){
$exe->($row_hash_ref,$col_arrayref);
$update->execute(@$row_hash_ref{@$col_arrayref},$row_hash_ref->{rowid});
}
$dbh->commit;
}
sub create_test_table {
my ($dbh, $tablename ) = @_;
my @tabledata = (
{'c1' => 'a', 'c2' => 'b', 'c3' => 'c'},
{'c1' => 'aa', 'c2' => 'bb', 'c3' => 'cc'},
{'c1' => 'aaa', 'c2' => 'bbb', 'c3' => 'ccc'},
# replicate a row
{'c1' => 'A', 'c2' => 'B', 'c3' => 'c'},
);
$dbh->do("DROP TABLE IF EXISTS $tablename");
$dbh->do("CREATE TABLE IF NOT EXISTS $tablename (c1 TEXT, c2 TEXT, c3 TEXT);");
my $insert = $dbh->prepare("INSERT INTO $tablename (c1,c2,c3) VALUES (?,?,?)");
$dbh->begin_work;
for my $rowref (@tabledata){
$insert->execute(@$rowref{qw(c1 c2 c3)});
}
$dbh->commit;
}
sub dump_test_table{
my ($dbh, $tablename)=@_;
my $result = fetchrows_subset_of_table ($dbh,$tablename);
print "Dumping Current Values in $tablename\n";
pp $result;
}
## fetchrows_subset_of_table()
# returns a ref to array of hashref of subset of a table
# specified by an SQL WHERE clause
sub fetchrows_subset_of_table {
my ($dbh, $tablename, $WHERE) = @_;
$WHERE //= '';
my $results = $dbh->selectall_arrayref(
"SELECT rowid, * FROM $tablename $WHERE ORDER BY rowid",
{ Slice => {} }
);
return $results; # $results is a ref to an array of hash ref's
}
__END__
Original Test Table Contents...
Dumping Current Values in atable
[
{ c1 => "a", c2 => "b", c3 => "c", rowid => 1 },
{ c1 => "aa", c2 => "bb", c3 => "cc", rowid => 2 },
{ c1 => "aaa", c2 => "bbb", c3 => "ccc", rowid => 3 },
{ c1 => "A", c2 => "B", c3 => "c", rowid => 4 },
]
First Test Without a Where Clause...
Test Table after modifications to cols c1 and c2....
Dumping Current Values in atable
[
{ c1 => "A", c2 => "B", c3 => "c", rowid => 1 },
{ c1 => "AA", c2 => "BB", c3 => "cc", rowid => 2 },
{ c1 => "AAA", c2 => "BBB", c3 => "ccc", rowid => 3 },
{ c1 => "A", c2 => "B", c3 => "c", rowid => 4 },
]
Second Test mods to c1 and c2 WHERE (c3 = 'c') OR (c3 = 'ccc')
Test Table after modifications....
Dumping Current Values in atable
[
{ c1 => "A", c2 => "B", c3 => "c", rowid => 1 },
{ c1 => "aa", c2 => "bb", c3 => "cc", rowid => 2 },
{ c1 => "AAA", c2 => "BBB", c3 => "ccc", rowid => 3 },
{ c1 => "A", c2 => "B", c3 => "c", rowid => 4 },
]