Firewall-DBI
view release on metacpan or search on metacpan
lib/Firewall/DBI/Pg.pm view on Meta::CPAN
package Firewall::DBI::Pg;
#------------------------------------------------------------------------------
# å è½½æ©å±æ¨¡å
#------------------------------------------------------------------------------
use Moose;
use namespace::autoclean;
use DBIx::Custom;
#------------------------------------------------------------------------------
# ç»§æ¿ Firewall::DBI::Role æ¹æ³å±æ§
#------------------------------------------------------------------------------
with 'Firewall::DBI::Role';
has option => ( is => 'ro', isa => 'Undef | HashRef[Str]', default => undef, );
has '+dbi' => ( isa => 'DBIx::Custom', handles => qr/^(?:select|update|insert|delete|execute|user).*/, );
for my $func (qw( execute delete update insert batchExecute )) {
around $func => sub {
my $orig = shift;
my $self = shift;
my $result;
eval {
$result = $self->$orig(@_);
$self->dbi->dbh->commit;
};
if ( !!$@ ) {
if ( $self->dbi->dbh->rollback ) {
confess "ERROR: $@";
}
else {
confess "ERROR: $@\n" . $self->dbi->dbh->errstr;
}
}
else {
return $result;
}
};
}
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my %param = ( @_ > 0 and ref( $_[0] ) eq 'HASH' ) ? %{$_[0]} : @_;
if ( not defined $param{dsn} and defined $param{host} and defined $param{port} and defined $param{dbname} ) {
$param{dsn} = "dbi:Pg:dbname=$param{dbname};host=$param{host};port=$param{port}";
}
return $class->$orig(%param);
};
sub clone {
my $self = shift;
return __PACKAGE__->new( dsn => $self->dsn, user => $self->user, password => $self->password,
option => $self->option );
}
sub batchExecute {
my $self = shift;
$self->_rawExecute(@_);
}
# æ¯multipleInsertç¥å¿«ï¼éå大æ¹éæè¯å¥è¾å¤æçæä½
sub _rawExecute {
my ( $self, $paramRef, $sqlString ) = @_;
my $num = 0;
my $sth = $self->dbi->dbh->prepare($sqlString);
for my $param ( @{$paramRef} ) {
$sth->execute( @{$param} );
$self->dbi->dbh->commit if ++$num % 5000 == 0;
}
}
( run in 1.705 second using v1.01-cache-2.11-cpan-99c4e6809bf )