DBIx-QuickORM

 view release on metacpan or  search on metacpan

lib/DBIx/QuickORM/DB.pm  view on Meta::CPAN


Provenance metadata describing where this definition came from.

=item dialect

The L<DBIx::QuickORM> dialect object (required). Used to build the DSN.

=item dbi_driver

The DBI driver name associated with this database.

=back

=head1 PUBLIC METHODS

=over 4

=item $name = $db->db_name

The database name, defaulting to C<name> when C<db_name> is unset.

=cut

sub db_name { $_[0]->{+DB_NAME} // $_[0]->{+NAME} }

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 $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.

=back

=cut

sub new_dbh {
    my $self = shift;
    my (%params) = @_;

    my $attrs = $self->attributes;

    my $dbh;
    eval {
        if ($self->{+CONNECT}) {
            $dbh = $self->{+CONNECT}->();
        }
        else {
            require DBI;
            $dbh = DBI->connect($self->dsn, $self->user, $self->pass, $self->attributes);
        }

        1;
    } or confess $@;

    $dbh->{AutoInactiveDestroy} = 1 if $attrs->{AutoInactiveDestroy};

    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>exodist@cpan.orgE<gt>

=back

=head1 AUTHORS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 COPYRIGHT

Copyright Chad Granum E<lt>exodist7@gmail.comE<gt>.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.



( run in 0.563 second using v1.01-cache-2.11-cpan-5b529ec07f3 )