/usr/share/perl5/Catalyst/Model/DBI.pm is in libcatalyst-modules-perl 43.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 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/ );
=head1 NAME
Catalyst::Model::DBI - DBI Model Class
=head1 SYNOPSIS
# 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 ...
=head1 DESCRIPTION
This is the C<DBI> model class.
=head1 METHODS
=over 4
=item new
Initializes DBI connection
=cut
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;
}
=item $self->dbh
Returns the current database handle.
=cut
sub dbh {
return shift->stay_connected;
}
=item $self->stay_connected
Returns a connected database handle.
=cut
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;
}
=item $self->connected
Returns true if the database handle is active and pingable.
=cut
sub connected {
my $self = shift;
return unless $self->_dbh;
return $self->_dbh->{Active} && $self->_dbh->ping;
}
=item $self->connect
Connects to the database and returns the handle.
=cut
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;
}
=item $self->disconnect
Executes rollback if AutoCommit is active,
disconnects and unsets the database handle.
=cut
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);
}
=back
=head1 SEE ALSO
L<Catalyst>, L<DBI>
=head1 AUTHOR
Alex Pavlovic, C<alex.pavlovic@taskforce-1.com>
=head1 COPYRIGHT
Copyright (c) 2005 - 2009
the Catalyst::Model::DBI L</AUTHOR>
as listed above.
=head1 LICENSE
This program is free software, you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|