DBIx-QuickORM
view release on metacpan or search on metacpan
lib/DBIx/QuickORM/DB.pm view on Meta::CPAN
sub db_name { $_[0]->{+DB_NAME} // $_[0]->{+NAME} }
=pod
=item $db->init
Object construction hook invoked by L<Object::HashBase>. Validates required
attributes and fills in default DBI attributes. Not called directly.
=cut
sub init {
my $self = shift;
croak "'dialect' is a required attribute" unless $self->{+DIALECT};
delete $self->{+NAME} unless defined $self->{+NAME};
$self->{+ATTRIBUTES} //= {};
$self->{+ATTRIBUTES}->{RaiseError} //= 1;
$self->{+ATTRIBUTES}->{PrintError} //= 1;
$self->{+ATTRIBUTES}->{AutoCommit} //= 1;
$self->{+ATTRIBUTES}->{AutoInactiveDestroy} //= 1;
croak "Cannot provide both a socket and a host" if $self->{+SOCKET} && $self->{+HOST};
}
=pod
=item $bool = $db->cas_count_reliable
True if a connection to this database reports the affected-row count that
compare-and-set needs (rows matched, not rows changed). The MySQL and MariaDB
drivers enable that by default; this is false only when the found-rows client
flag was explicitly turned off in the connect attributes.
=cut
sub cas_count_reliable { $_[0]->{+DIALECT}->cas_count_reliable($_[0]->attributes) }
=pod
=item $dsn = $db->dsn
Returns the DSN, building it from the dialect and caching it on first call.
=cut
sub dsn {
my $self = shift;
return $self->{+DSN} if $self->{+DSN};
return $self->{+DSN} = $self->{+DIALECT}->dsn($self);
}
=pod
=item $dbh = $db->new_dbh
Returns a new DBI handle, using the C<connect> callback when present or
C<< DBI->connect >> with the resolved DSN and credentials otherwise. Croaks if
the connection attempt throws or fails to produce a handle (possible when
C<RaiseError> is disabled or the callback misbehaves).
=back
=cut
sub new_dbh {
my $self = shift;
my $attrs = $self->attributes;
my $dbh;
my $ok = eval {
if ($self->{+CONNECT}) {
$dbh = $self->{+CONNECT}->();
}
else {
require DBI;
$dbh = DBI->connect($self->dsn, $self->user, $self->pass, $attrs);
}
1;
};
my $err = $@;
croak $err unless $ok;
unless (blessed($dbh)) {
my $reason = $DBI::errstr // 'connect did not return a handle';
croak "Could not connect to the database: $reason";
}
# A connect callback returns its own handle, so the attributes the DSN path
# passes to DBI->connect never reach it. Enforce the same attributes here
# (RaiseError, so the eval-based error handling throughout the codebase
# works; AutoCommit, so the dialect owns transaction control; and whatever
# else the DSN path was given) as the DSN path already gets, by copying
# every defined attribute onto the handle. Harmless on the DSN path, which
# already has them.
$dbh->{$_} = $attrs->{$_} for grep { defined $attrs->{$_} } keys %$attrs;
return $dbh;
}
1;
__END__
=head1 SOURCE
The source code repository for DBIx::QuickORM can be found at
L<https://github.com/exodist/DBIx-QuickORM>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist7@gmail.comE<gt>
=back
( run in 1.091 second using v1.01-cache-2.11-cpan-007c89162af )