Catalyst::Model::DBI - DBI Model Class


Catalyst-Model-DBI documentation Contained in the Catalyst-Model-DBI distribution.

Index


Code Index:

NAME

Top

Catalyst::Model::DBI - DBI Model Class

SYNOPSIS

Top

	# use the helper
	create model DBI DBI dsn username password

	# lib/MyApp/Model/DBI.pm
	package MyApp::Model::DBI;

	use base 'Catalyst::Model::DBI';

	__PACKAGE__->config(
		dsn           => 'dbi:Pg:dbname=myapp',
		password      => '',
		username      => 'postgres',
		options       => { AutoCommit => 1 },
	);

	1;

	my $dbh = $c->model('DBI')->dbh;
	#do something with $dbh ...

DESCRIPTION

Top

This is the DBI model class.

METHODS

Top

new

Initializes DBI connection

$self->dbh

Returns the current database handle.

$self->stay_connected

Returns a connected database handle.

$self->connected

Returns true if the database handle is active and pingable.

$self->connect

Connects to the database and returns the handle.

$self->disconnect

Executes rollback if AutoCommit is active, disconnects and unsets the database handle.

SEE ALSO

Top

Catalyst, DBI

AUTHOR

Top

Alex Pavlovic, alex.pavlovic@taskforce-1.com

COPYRIGHT

Top

LICENSE

Top

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


Catalyst-Model-DBI documentation Contained in the Catalyst-Model-DBI distribution.
package Catalyst::Model::DBI;

use strict;
use base 'Catalyst::Model';
use MRO::Compat;
use mro 'c3';
use DBI;

our $VERSION = '0.28';

__PACKAGE__->mk_accessors( qw/_dbh _pid _tid/ );

sub new {
	my $self  = shift->next::method(@_);
	my ( $c ) = @_;
	$self->{namespace}               ||= ref $self;
	$self->{additional_base_classes} ||= ();
	$self->{log} = $c->log;
	$self->{debug} = $c->debug;
	return $self;
}

sub dbh {
	return shift->stay_connected;
}

sub stay_connected {
	my $self = shift;
	if ( $self->_dbh ) {
		if ( defined $self->_tid && $self->_tid != threads->tid ) {
			$self->_dbh( $self->connect );
      		} elsif ( $self->_pid != $$ ) {
			$self->_dbh->{InactiveDestroy} = 1;
			$self->_dbh( $self->connect );
		} elsif ( ! $self->connected ) {
			$self->_dbh( $self->connect );
		}
	} else {
		$self->_dbh( $self->connect );
	}
	return $self->_dbh;
}

sub connected {
	my $self = shift;
	return unless $self->_dbh;
	return $self->_dbh->{Active} && $self->_dbh->ping;
}

sub connect {
	my $self = shift;
	my $dbh;
	eval {
		$dbh = DBI->connect(
			$self->{dsn},
			$self->{username} || $self->{user},
			$self->{password} || $self->{pass},
			$self->{options}
		);
	};
	if ($@) { $self->{log}->debug( qq{Couldn't connect to the database "$@"} ) if $self->{debug} }
	else { $self->{log}->debug ( 'Connected to the database via dsn:' . $self->{dsn} ) if $self->{debug}; }
	$self->_pid( $$ );
	$self->_tid( threads->tid ) if $INC{'threads.pm'};
	return $dbh;
}

sub disconnect {
	my $self = shift;
	if( $self->connected ) {
		$self->_dbh->rollback unless $self->_dbh->{AutoCommit};
		$self->_dbh->disconnect;
		$self->_dbh( undef );
	}
}

sub DESTROY {
	my $self = shift;
	$self->disconnect if (defined $self->_dbh);
}

1;