/usr/share/perl5/RoPkg/DB.pm is in libropkg-perl 0.4-1.2.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | ############################################################################
## Copyright (C) 2005 Subredu Manuel #
## Author Subredu Manuel <diablo@iasi.roedu.net> #
## #
## This program is free software; you can redistribute it and/or modify #
## it under the terms of the GNU General Public License as published by #
## the Free Software Foundation; either version 2 of the License, or #
## (at your option) any later version. #
## #
## This program is distributed in the hope that it will be useful, #
## but WITHOUT ANY WARRANTY; without even the implied warranty of #
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
## GNU General Public License for more details. #
## #
## You should have received a copy of the GNU General Public License #
## along with this program; if not, write to the Free Software #
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, #
## MA 02111-1307,USA. #
############################################################################
package RoPkg::DB;
use warnings;
use strict;
use Class::Singleton;
use DBI;
use Scalar::Util qw(blessed);
use English qw( -no_match_vars );
use RoPkg::Exceptions;
use RoPkg::Utils qw(CheckParam);
use vars qw($VERSION);
use base qw(Class::Singleton);
$VERSION = '0.1.12';
sub new {
shift;
return RoPkg::DB->instance(@_);
}
sub _new_instance {
my ($class, %opt) = @_;
my $self;
$self = bless { %opt }, $class;
$self->{conn} = ();
return $self;
}
sub Add {
my ($self, $dsn, $dbuser, $dbpass, $name) = @_;
my $dbh;
my %db;
if ( !blessed($self)) {
OutsideClass->throw(
error => 'Called outside class instance',
pkg_name => 'RoPkg::DB',
);
}
$db{dsn} = $dsn;
$db{dbuser} = $dbuser;
$db{dbpass} = $dbpass;
$db{name} = $name;
CheckParam(\%db, qw(dsn dbuser name));
if ( exists $self->{conn}->{$name} ) {
DB::ConnExists->throw(error => "Connection $name already exists",
dsn => $dsn,
user => $dbuser,
pass =>'***not-shown***');
}
$dbh = DBI->connect_cached($dsn, $dbuser, $dbpass, { RaiseError => 0, PrintError => 0 });
if ( !$dbh ) {
DB::Connect->throw(DBI::errstr);
}
$self->_set_dbh_defaults($dbh);
$db{dbh} = $dbh;
$self->{conn}->{$name} = \%db;
$self->_create_db_method($name);
return 1;
}
sub _set_dbh_defaults {
my ($self, $dbh) = @_;
$dbh->{HandleError} = sub {
DB->throw($_[0]);
};
# we set the error handle. Now is time to start throwing exceptions all around ;)
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;
return 1;
}
sub _create_db_method {
my ($self, $name) = @_;
no strict 'refs';
*{'RoPkg::DB::db_' . $name} = sub {
my ($self) = @_;
return $self->_reconnect($name);
};
use strict;
return 1;
}
sub Del {
my ($self, $name) = @_;
if ( !blessed($self)) {
OutsideClass->throw('Called outside class');
}
if (! exists $self->{conn}->{$name}) {
DB::ConnNotFound->throw("Connection $name not found");
}
$self->{conn}->{$name}->{dbh}->disconnect;
return delete $self->{conn}->{$name};
}
sub Has {
my ($self, $name) = @_;
if ( !blessed($self)) {
OutsideClass->throw('Called outside class');
}
if ( !$name ) {
Param::Missing->throw('name of the connection has not been specified');
}
return 0 if (! exists $self->{conn}->{$name});
return 1;
}
sub Reconnect {
my ($self, $name) = @_;
my $dbh;
my %db;
if ( !blessed($self)) {
OutsideClass->throw('Called outside class instance');
}
if ( !$name ) {
Param::Missing->throw('Connection name has not been specified');
}
if (( defined $name) && (! exists $self->{conn}->{$name})) {
DB::ConnNotFound->throw(error => "Connection $name does not exists",
dsn => $self->{conn}->{$name}->{dsn},
user => $self->{conn}->{$name}->{dbuser},
pass =>'***not-shown***');
}
if (defined $name) {
$self->_reconnect($name);
} else {
foreach(keys %{ $self->{conn} }) {
$self->_reconnect($_);
}
}
return 1;
}
sub _reconnect {
my ($self, $name) = @_;
my $conn_info;
$conn_info = $self->{conn}->{$name};
eval {
$conn_info->{dbh} = DBI->connect_cached(
$conn_info->{dsn},
$conn_info->{dbuser},
$conn_info->{dbpass}, {
RaiseError => 1,
PrintError => 0,
}
);
};
if ( Exception::Class->caught('DB') ) {
DB->Connect->throw($EVAL_ERROR->message);
} else {
die $EVAL_ERROR->message if (ref $EVAL_ERROR);
}
return $conn_info->{dbh};
}
sub TableExists {
my ($self, $name, $table) = @_;
if ( !blessed($self)) {
OutsideClass->throw('Call from outside class instance');
}
if ( !$name ) {
Param::Missing->throw('Connection name has not been specified');
}
if ( !$table ) {
Param::Missing->throw('Table name has not been specified');
}
if ( ! $self->Has($name) ) {
DB::ConnNotFound->throw("Connection $name not found");
}
return 0 if (! defined $table);
eval {
$self->{conn}->{$name}->{dbh}->do(qq{SELECT * FROM $table WHERE 1=0});
};
return 0 if ( Exception::Class->caught('DB') );
die $EVAL_ERROR if (ref $EVAL_ERROR);
return 1;
}
1;
__END__
=head1 NAME
RoPkg::DB - A Singleton database pool class
=head1 VERSION
0.1.12
=head1 DESCRIPTION
RoPkg::DB is a database pool class. Using this class you can
have access to multiple databases (no matter what kind of
database), from a single object. RoPkg::DB is ideal in
persistent environments because at base, is a singleton
class. This class is intensively used by Simba but it
can be used in any other project.
=head1 SYNOPSIS
use RoPkg::DB;
sub main {
my $dbp = new RoPkg::DB();
eval {
$dbp->Add('dbi:mysql:database=mysql;host=localhost',
'root',
'',
'local');
$dbp->Add('dbi:mysql:database=somedb;host=somehost',
'username',
'mypass',
'somedb');
};
die($@->package . ' said: ' . $@->message) if ref($@);
print 'Conn to localhost is:',$dbp->db_local,$/,
'Conn to remote is:',$dbp->db_somedb,$/;
$dbp->Del('somedb');
}
=head1 SUBROUTINES/METHODS
All methods (besides new), raise OutsideClass exception
when are not called within a class instance. Also, each
method may raise diferent exceptions from one case
to another. Please read the documentation for each
method to find out what are the exceptions raised by
each method.
=head2 new()
new is the contructor of the class. It does not do anything.
=head2 Add($dsn, $dbuser, $dbpass, $conn_name)
add and initialize a new connection to the pool. $dsn must
be in the DBI format. $dsn and $dbpass are the username
and password used to connect to the database. $conn_name
is the method who will be used to access the database handle.
Please note that the method that will be created will be
prefixed with "db_" .
Example:
$dbp->Add('dbi:mysql:database=mysql;host=localhost','root',q{},'local');
The method that will be used to get the database handle for this connection
is db_local .
Add() may raise the following exceptions:
=over 3
=item *) Param::Missing
=item *) DB::ConnExists
=item *) DB::Connect
=back
I<Param::Missing> is raised when $dsn, $dbuser or $conn_name
are not defined. I<DB::ConnExists> is raised when a connection
with the name $conn_name already exists. I<DB::Connect>
is raised when the connection with the database could not
be established. Besides this exceptions, each database handler
has RaiseError set to 1 and PrintError set to 0. Also, when
errors occurs, DBI will raise a I<DB> exception. The
method always returns 1.
=head2 Del($name)
removes the connection named $name from the pool. The
connection is closed first in a civilised manner. If the
connection $name is not found, DB::ConnNotFound is raised.
=head2 Has($name)
Returns 0 if the connection named $name does not exists,
1 otherwise. Param::Missing exception is raised if
$name is not defined.
=head2 Reconnect($name)
Force a reconnect of the connection named $name. If $name
is not defined, Param::Missing is raised. If a connection
named $name does not exists, DB::ConnNotFound is raised.
This method always returns 1.
=head2 TableExists($name, $table_name)
Returns 1 if the table $table_name exists in the database
specified by the connection named $name, 0 otherwise. If
$name or $table_name parameters are not defined, a
Param::Missing exception is raised. If the connection
$name does not exists, DB::ConnNotFound is raised.
=head1 DEPENDENCIES
RoPkg::DB requires perl 5.008 or later and the following modules:
=over 4
=item RoPkg >= 0.4.4
=item DBI
=item Class::Singleton
=item English
=back
=head1 DIAGNOSTICS
This module comes with tests. To run the tests, unpack the source
and run 'make test'
=head1 PERL CRITIC
This module is perl critic level 2 compliant (with 1 exception)
=head1 CONFIGURATION AND ENVIRONMENT
This module does not use any configuration files or environment
variables. The used modules however may use such things. Please
refer to each module man page for more information.
=head1 INCOMPATIBILITIES
None known to the author
=head1 BUGS AND LIMITATIONS
None known to the author
=head1 SEE ALSO
L<RoPkg::DBObject> L<RoPkg::Exceptions> L<RoPkg::Utils>
=head1 AUTHOR
Subredu Manuel <diablo@iasi.roedu.net>
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2005 Subredu Manuel. All Rights Reserved.
This module is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
|