Sorry about that - PostgreSQL does let you concatenate ('||') text fields. It's been awhile since I had to deal with SQLServer weirdness.
Well then you're either left with running a
transaction around your read/append/write or CAST the text field to varchar then concat (assuming the data can be cast).
I would go for the transaction approach, you'll be sure it'll work with any kind of data.
As for the ugliness it should be easy to make a method to hide the $dbh->{AutoCommit} = 0 (or $dbh->begin_work), read, append, update, $dbh->commit. Don't forget to turn AutoCommit off if it's on. In fact I would just add that in there to be safe.
local $dbh->{AutoCommit} = 0;
local $dbh->{RaiseError} = 1;
# You can RaiseError and eval the transaction
# or you can check $sth->err each time and rollback
eval {
##Do your read and write here
$dbh->commit;
};
if ($@) {
warn "append failed $@";
$dbh->rollback;
}
UPDATE: something similar to this
sub append_to_text {
my $dbh = shift;
my $table = shift;
my $id = shift;
my $col = shift;
my $data = shift;
local $dbh->{RaiseError} = 1;
eval {
$dbh->begin_work();
my $read = qq|
SELECT $col FROM $table
WHERE id = ?
|;
my $update = qq|
UPDATE $table SET $col = ?
WHERE id = ?
|;
my $sth_read = $dbh->prepare($read);
$sth_read->execute($id);
my $row = $sth_read->fetchrow_hashref();
$sth_read->finish();
my $current = $row->{$col};
$current .= $data;
my $rtn = $dbh->do($update,undef,$current,$id);
$dbh->commit() if ($rtn);
};
if ($@) {
warn "Append Failed: $@\nStatement: $DBI::lasth->{Statement}
+";
eval { $dbh->rollback };
}
}
grep
|
One dead unjugged rabbit fish later |
|