DBIO-MySQL

 view release on metacpan or  search on metacpan

lib/DBIO/MySQL/SQLMaker.pm  view on Meta::CPAN

# FOR UPDATE | FOR SHARE [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]
#

my $lock_types = {
  update => 'FOR UPDATE',
  share => 'FOR SHARE',
};

my $lock_modifiers = {
  nowait => 'NOWAIT',
  skip_locked => 'SKIP LOCKED'
};

sub _lock_select {
  my ($self, $type) = @_;

  # Handle hash-based configuration to support new featureset
  if (ref $type eq 'HASH') {
    my $lock_type = $type->{type};
    my $tables = $type->{of};
    my $modifier = $type->{modifier};

lib/DBIO/MySQL/SQLMaker.pm  view on Meta::CPAN


=head2 _lock_select

Generates the locking clause appended to C<SELECT> statements. Accepts
either a plain string (C<'update'>, C<'share'>) or a hashref
for fine-grained control:

  $rs->search({}, { for => { type => 'update', of => ['tbl'], modifier => 'nowait' } });

Valid C<type> values: C<update>, C<share>.
Valid C<modifier> values: C<nowait>, C<skip_locked>.
The C<of> key takes a table name or arrayref of table names.

=seealso

=over 4

=item * L<DBIO::MySQL::Storage> - Storage class that uses this SQL maker

=item * L<DBIO::MySQL> - Schema component entry point

lib/DBIO/MySQL/SQLMaker/MariaDB.pm  view on Meta::CPAN


# Reuse the parent's lock_modifiers (NOWAIT / SKIP LOCKED) but override
# the lock_types mapping: 'share' means LOCK IN SHARE MODE on MariaDB.
my $lock_types = {
  update => 'FOR UPDATE',
  share  => 'LOCK IN SHARE MODE',
};

my $lock_modifiers = {
  nowait      => 'NOWAIT',
  skip_locked => 'SKIP LOCKED',
};

sub _lock_select {
  my ($self, $type) = @_;

  if (ref $type eq 'HASH') {
    my $lock_type = $type->{type};
    my $tables    = $type->{of};
    my $modifier  = $type->{modifier};

t/20-sqlmaker-mysql.t  view on Meta::CPAN

      SELECT `me`.`artistid`, `me`.`name`, `me`.`rank`, `me`.`charfield`
        FROM `artist` `me`
        FOR SHARE NOWAIT
    )',
    [],
    'FOR SHARE NOWAIT works correctly'
  );

  # Test SKIP LOCKED modifier
  is_same_sql_bind(
    $schema->resultset('Artist')->search({}, {for => {type => 'update', modifier => 'skip_locked'}})->as_query,
    '(
      SELECT `me`.`artistid`, `me`.`name`, `me`.`rank`, `me`.`charfield`
        FROM `artist` `me`
        FOR UPDATE SKIP LOCKED
    )',
    [],
    'FOR UPDATE SKIP LOCKED works correctly'
  );

  is_same_sql_bind(
    $schema->resultset('Artist')->search({}, {for => {type => 'share', modifier => 'skip_locked'}})->as_query,
    '(
      SELECT `me`.`artistid`, `me`.`name`, `me`.`rank`, `me`.`charfield`
        FROM `artist` `me`
        FOR SHARE SKIP LOCKED
    )',
    [],
    'FOR SHARE SKIP LOCKED works correctly'
  );

  # Test OF clause

t/20-sqlmaker-mysql.t  view on Meta::CPAN

    )',
    [],
    'FOR UPDATE OF table NOWAIT works correctly'
  );

  is_same_sql_bind(
    $schema->resultset('Artist')->search({}, {
      for => {
        type => 'share',
        of => ['artist', 'cd'],
        modifier => 'skip_locked'
      }
    })->as_query,
    '(
      SELECT `me`.`artistid`, `me`.`name`, `me`.`rank`, `me`.`charfield`
        FROM `artist` `me`
        FOR SHARE OF `artist`, `cd` SKIP LOCKED
    )',
    [],
    'FOR SHARE OF multiple tables SKIP LOCKED works correctly'
  );



( run in 2.055 seconds using v1.01-cache-2.11-cpan-bbe5e583499 )