DBIx::Class::StorageReadOnly - Can't insert and update and delete for DBIC


DBIx-Class-StorageReadOnly documentation Contained in the DBIx-Class-StorageReadOnly distribution.

Index


Code Index:

NAME

Top

DBIx::Class::StorageReadOnly - Can't insert and update and delete for DBIC

SYNOPSIS

Top

    __PACKAGE__->load_components(qw/
        StorageReadOnly
        PK::Auto
        Core
    /);

    # create connection and set readonly info
    @connection_info = (
        'dbi:mysql:test',
        'foo',
        'bar',
        {read_only => 1},
    );
    my $schema = $schema_class->connect(@connection_info);

    my $user = $schema->resultset('User')->search({name => 'nomaneko'});
    $user->update({name => 'gikoneko'}); # die. Can't update.

DESCRIPTION

Top

If you try to write it in read only DB, the exception is generated.

METHOD

Top

insert

update

delete

_search_readonly_info

BUGS AND LIMITATIONS

Top

No bugs have been reported.

AUTHOR

Top

Atsushi Kobayashi <atsushi __at__ mobilefactory.jp>

LICENCE AND COPYRIGHT

Top


DBIx-Class-StorageReadOnly documentation Contained in the DBIx-Class-StorageReadOnly distribution.

package DBIx::Class::StorageReadOnly;
use strict;
use warnings;
use base 'DBIx::Class';
use Carp::Clan qw/^DBIx::Class/;

our $VERSION = '0.05';
use DBIx::Class::Storage::DBI;

{
    package DBIx::Class::Storage::DBI;
    no warnings 'redefine';
    no strict 'refs'; ## no critic
    for my $method (qw/insert update delete/) {
        my $code_org = DBIx::Class::Storage::DBI->can($method);
        *{"DBIx\::Class\::Storage\::DBI\::$method"} = sub {
            my $self = shift;
            if ($self->_search_readonly_info) {
                croak("This connection is read only. Can't $method.");
            }
            return $self->$code_org(@_);
        };
    }

    sub _search_readonly_info {
        my $self = shift;
        for my $info ( @{$self->connect_info} ) {
            if (ref $info eq 'HASH' ) {
                return 1 if $info->{read_only} == 1;
            }
        }
        return;
    }
}
1;
__END__