Egg::Model::DBI::Base - Base class for DBI model component.


Egg-Release-DBI documentation Contained in the Egg-Release-DBI distribution.

Index


Code Index:

NAME

Top

Egg::Model::DBI::Base - Base class for DBI model component.

DESCRIPTION

Top

It is a base class to use it from the component of Egg::Model::DBI.

The object of this module is only the one internally used when $e->model([LABEL_NAME]) is called. It is not necessary to consider it because of the application usually.

METHODS

Top

dbi

The Egg::Model::DBI object is returned.

dbh

The data base handler is returned.

The connection still calls the connect method.

connect

It connects with the data base and Egg::Model::dbh. The object is returned.

disconnect

The database connectivility is cut.

SEE ALSO

Top

Egg::Release, Egg::Model, Egg::Model::DBI, Egg::Model::DBI::dbh,

AUTHOR

Top

Masatoshi Mizuno, <lushe&64;cpan.org>

COPYRIGHT AND LICENSE

Top


Egg-Release-DBI documentation Contained in the Egg-Release-DBI distribution.

package Egg::Model::DBI::Base;
#
# Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
#
# $Id: Base.pm 302 2008-03-05 07:45:10Z lushe $
#
use strict;
use warnings;
use base qw/ Egg::Model /;
use Egg::Model::DBI::dbh;

our $VERSION= '0.02';

sub dbi {
	$_[0]->{dbi} ||= do {
		my $e= $_[0]->e || return 0;
		$e->model('dbi');
	  };
}
sub dbh {
	my($self)= @_;
	my $dbh;
	if (my $handlers= $self->dbi->handlers) {
		if ($dbh= $handlers->{$self->label_name}) {
			$dbh= $self->connect unless $dbh->_connected;
		}
	}
	$dbh || $self->connect;
}
sub connect {
	my($self)= @_;
	my $dbh;
	eval{ $dbh= $self->connect_db };
	$@ and die "Database Connect NG!! '@{[ $self->label_name ]}' at $@";
	my $handlers= $self->dbi->handlers || $self->dbi->handlers({});
	$handlers->{$self->label_name}= Egg::Model::DBI::dbh->_new($dbh);
}
sub disconnect {
	my($self)= @_;
	my $dbi= $self->dbi || return 0;
	my $handlers= $dbi->handlers || return 0;
	my $dbh= $handlers->{$self->label_name} || return 0;
	$dbh->_disconnect || return 0;
	delete $handlers->{$self->label_name};
	$self;
}
sub DESTROY {
	shift->disconnect;
}

1;

__END__