[Dbix-class] SELECT ROWS FOR UPDATE/SHARE

Oleg Pronin syber.rus at gmail.com
Sun Mar 18 15:44:30 GMT 2007


Hi. I use PostgreSQL database and want to use it's row locking system.
That's why i need to execute statements like this:
SELECT * FROM table WHERE condition FOR UPDATE (FOR SHARE);

I didn't find a nice solution for that.
I would like to use row-locking in some way like that:
$rs->search(
  {column =3D> $value},
  {
    rows =3D> 1,
    locking =3D> 'update',
  }
);

The only solution i found without copy-pasting code of  '_select' and
'_execute' functions of storage is

in my schema:
__PACKAGE__->storage_type('My::Storage');

in my::storage

package My::Storage;
use base qw/DBIx::Class::Storage::DBI::Pg/;
use strict;

our $_LOCKING =3D 0;

sub _select {
    my $self =3D shift;
    $_LOCKING =3D $_[3]->{locking} eq 'update' ? 1 : 2 if defined
$_[3]->{locking};
    $self->SUPER::_select(@_);
}

sub sth {
    my ($self, $sql) =3D @_;
    # 3 is the if_active parameter which avoids active sth re-use
    if ($_LOCKING) {
        $sql .=3D $_LOCKING =3D=3D 1 ? ' FOR UPDATE' : ' FOR SHARE';
        $_LOCKING =3D 0;
    }
    return $self->dbh->prepare_cached($sql, {}, 3);
}
I understand this is ugly.
Is there a better solution?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070318/a76=
7045b/attachment.htm


More information about the Dbix-class mailing list