Acrux-DBI
view release on metacpan or search on metacpan
lib/Acrux/DBI.pm view on Meta::CPAN
This is the L<Mojo::URL/port> that will be used for generating the connection DSN
Default: none
=head2 query
my $res = $dbi->query('select * from test');
my $res = $dbi->query('insert into test values (?, ?)', @values);
Execute a blocking statement and return a L<Acrux::DBI::Res> object with the results.
You can also append a 'bind_callback' to perform binding value manually:
my $res = $dbi->query('insert into test values (?, ?)', {
bind_callback => sub {
my $sth = shift;
$sth->bind_param( ... );
}
});
=head2 rollback
$dbi->begin;
# ...
$dbi->rollback;
This is a transaction method!
lib/Acrux/DBI.pm view on Meta::CPAN
$dbh->rollback;
return $self;
}
# Request methods
sub query { # SQL, { args }
my $self = shift;
my $sql = shift // '';
my $args = @_
? @_ > 1
? {bind_values => [@_]}
: ref($_[0]) eq 'HASH'
? {%{$_[0]}}
: {bind_values => [@_]}
: {};
$self->{error} = '';
return unless my $dbh = $self->dbh;
unless (length($sql)) {
$self->error("No statement specified");
return;
}
# Prepare
my $sth = $dbh->prepare($sql);
unless ($sth) {
$self->error(sprintf("Can't prepare statement \"%s\": %s", $sql,
$dbh->errstr || $DBI::errstr || 'unknown error'));
return;
}
# HandleError
local $sth->{HandleError} = sub { $_[0] = Carp::shortmess($_[0]); 0 };
# Binding params and execute
my $bind_values = $args->{bind_values} || [];
unless (is_array_ref($bind_values)) {
$self->error("Invalid list of binding values. Array ref expected");
return;
}
my $rv;
my $argb = '';
if (scalar @$bind_values) {
$argb = sprintf(" with bind values: %s",
join(", ", map {defined($_) ? sprintf("'%s\'", $_) : 'undef'} @$bind_values));
$rv = $sth->execute(@$bind_values);
} elsif (my $cb = $args->{bind_callback} || $args->{bind_cb}) {
unless (is_code_ref($cb)) {
$self->error("Invalid binding callback function. Code ref expected");
return;
}
$cb->($sth); # Callback! bind params
$rv = $sth->execute;
} else {
$rv = $sth->execute; # Without bindings
}
unless (defined $rv) {
$self->error(sprintf("Can't execute statement \"%s\"%s: %s", $sql, $argb,
$sth->errstr || $dbh->errstr || $DBI::errstr || 'unknown error'));
return;
}
# Result
return Acrux::DBI::Res->new(
dbi => $self,
( run in 3.315 seconds using v1.01-cache-2.11-cpan-2398b32b56e )