/usr/share/perl5/NetSDS/DBI.pm is in libnetsds-perl 1.301-3.
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | #===============================================================================
#
# FILE: DBI.pm
#
# DESCRIPTION: DBI wrapper for NetSDS
#
# AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
# COMPANY: Net.Style
# CREATED: 31.07.2009 13:56:33 UTC
#===============================================================================
=head1 NAME
NetSDS::DBI - DBI wrapper for NetSDS
=head1 SYNOPSIS
use NetSDS::DBI;
$dbh = NetSDS::DBI->new(
dsn => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432',
login => 'user',
passwd => 'topsecret',
);
print $db->call("select md5(?)", 'zuka')->fetchrow_hashref->{md5};
=head1 DESCRIPTION
C<NetSDS::DBI> module provides wrapper around DBI module.
=cut
package NetSDS::DBI;
use 5.8.0;
use strict;
use warnings;
use DBI;
use base 'NetSDS::Class::Abstract';
use version; our $VERSION = '1.301';
#===============================================================================
=head1 CLASS API
=over
=item B<new(%params)> - class constructor
$dbh = NetSDS::DBI->new(
dsn => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432',
login => 'user',
passwd => 'topsecret',
);
=cut
#-----------------------------------------------------------------------
sub new {
my ( $class, %params ) = @_;
# DBI handler attributes
my $attrs = { $params{attrs} ? %{ $params{attrs} } : () };
# Startup SQL queries
my $sets = $params{sets} || [];
# Prepare additional parameters
if ( $params{dsn} ) {
# Parse DSN to determine DBD driver and provide
my $dsn_scheme = undef;
my $dsn_driver = undef;
my $dsn_attr_str = undef;
my $dsn_attrs = undef;
my $dsn_dsn = undef;
if ( ( $dsn_scheme, $dsn_driver, $dsn_attr_str, $dsn_attrs, $dsn_dsn ) = DBI->parse_dsn( $params{dsn} ) ) {
# Set PostgreSQL default init queries
if ( 'Pg' eq $dsn_driver ) {
unshift( @{$sets}, "SET CLIENT_ENCODING TO 'UTF-8'" );
unshift( @{$sets}, "SET DATESTYLE TO 'ISO'" );
}
# Set UTF-8 support
$attrs = {
%{$attrs},
pg_enable_utf8 => 1,
};
} else {
return $class->error( "Can't parse DBI DSN: " . $params{dsn} );
}
} else {
return $class->error("Can't initialize DBI connection without DSN");
}
# initialize parent class
my $self = $class->SUPER::new(
dbh => undef,
dsn => $params{dsn},
login => $params{login},
passwd => $params{passwd},
attrs => {},
sets => [],
%params,
);
# Implement SQL debugging
if ( $params{debug_sql} ) {
$self->{debug_sql} = 1;
}
# Create object accessor for DBMS handler
$self->mk_accessors('dbh');
# Add initialization SQL queries
$self->_add_sets( @{$sets} );
$attrs->{PrintError} = 0;
$self->_add_attrs( %{$attrs} );
# Connect to DBMS
$self->_connect();
return $self;
} ## end sub new
#***********************************************************************
=item B<dbh()> - DBI connection handler accessor
Returns: DBI object
This method provides accessor to DBI object and for low level access
to database specific methods.
Example (access to specific method):
my $quoted = $db->dbh->quote_identifier(undef, 'auth', 'services');
# $quoted contains "auth"."services" now
=cut
#-----------------------------------------------------------------------
#***********************************************************************
=item B<call($sql, @bind_params)> - prepare and execute SQL query
Method C<call()> implements the following functionality:
* check connection to DBMS and restore it
* prepare chached SQL statement
* execute statement with bind parameters
Parameters:
* SQL query with placeholders
* bind parameters
Return:
* statement handler from DBI
Example:
$sth = $dbh->call("select * from users");
while (my $row = $sth->fetchrow_hashref()) {
print $row->{username};
}
=cut
#-----------------------------------------------------------------------
sub call {
my ( $self, $sql, @params ) = @_;
# Debug SQL
if ( $self->{debug_sql} ) {
$self->log( "debug", "SQL: $sql" );
}
# First check connection and try to restore if necessary
unless ( $self->_check_connection() ) {
return $self->error("Database connection error!");
}
# Prepare cached SQL query
# FIXME my $sth = $self->dbh->prepare_cached($sql);
my $sth = $self->dbh->prepare($sql);
unless ($sth) {
return $self->error("Can't prepare SQL query: $sql");
}
# Execute SQL query
$sth->execute(@params);
return $sth;
} ## end sub call
#***********************************************************************
=item B<fetch_call($sql, @params)> - call and fetch result
Paramters: SQL query, parameters
Returns: arrayref of records as hashrefs
Example:
# SQL DDL script:
# create table users (
# id serial,
# login varchar(32),
# passwd varchar(32)
# );
# Now we fetch all data to perl structure
my $table_data = $db->fetch_call("select * from users");
# Process this data
foreach my $user (@{$table_data}) {
print "User ID: " . $user->{id};
print "Login: " . $user->{login};
}
=cut
#-----------------------------------------------------------------------
sub fetch_call {
my ( $self, $sql, @params ) = @_;
# Try to prepare and execute SQL statement
if ( my $sth = $self->call( $sql, @params ) ) {
# Fetch all data as arrayref of hashrefs
return $sth->fetchall_arrayref( {} );
} else {
return $self->error("Can't execute SQL: $sql");
}
}
#***********************************************************************
=item B<begin()> - start transaction
=cut
sub begin {
my ($self) = @_;
return $self->dbh->begin_work();
}
#***********************************************************************
=item B<commit()> - commit transaction
=cut
sub commit {
my ($self) = @_;
return $self->dbh->commit();
}
#***********************************************************************
=item B<rollback()> - rollback transaction
=cut
sub rollback {
my ($self) = @_;
return $self->dbh->rollback();
}
#***********************************************************************
=item B<quote()> - quote SQL string
Example:
# Encode $str to use in queries
my $str = "some crazy' string; with (dangerous characters";
$str = $db->quote($str);
=cut
sub quote {
my ( $self, $str ) = @_;
return $self->dbh->quote($str);
}
#***********************************************************************
=back
=head1 INTERNAL METHODS
=over
=item B<_add_sets()> - add initial SQL query
Example:
$obj->_add_sets("set search_path to myscheme");
$obj->_add_sets("set client_encoding to 'UTF-8'");
=cut
#-----------------------------------------------------------------------
sub _add_sets {
my ( $self, @sets ) = @_;
push( @{ $self->{sets} }, @sets );
return 1;
}
#***********************************************************************
=item B<_add_attrs()> - add DBI handler attributes
$self->_add_attrs(AutoCommit => 1);
=cut
#-----------------------------------------------------------------------
sub _add_attrs {
my ( $self, %attrs ) = @_;
%attrs = ( %{ $self->{attrs} }, %attrs );
return %attrs;
}
#***********************************************************************
=item B<_check_connection()> - ping and reconnect
Internal method checking connection and implement reconnect
=cut
#-----------------------------------------------------------------------
sub _check_connection {
my ($self) = @_;
if ( $self->dbh ) {
if ( $self->dbh->ping() ) {
return 1;
} else {
return $self->_connect();
}
}
}
#***********************************************************************
=item B<_connect()> - connect to DBMS
Internal method starting connection to DBMS
=cut
#-----------------------------------------------------------------------
sub _connect {
my ($self) = @_;
# Try to connect to DBMS
$self->dbh( DBI->connect_cached( $self->{dsn}, $self->{login}, $self->{passwd}, $self->{attrs} ) );
if ( $self->dbh ) {
# All OK - drop error state
$self->error(undef);
# Call startup SQL queries
foreach my $row ( @{ $self->{sets} } ) {
unless ( $self->dbh->do($row) ) {
return $self->error( $self->dbh->errstr || 'Set error in connect' );
}
}
} else {
return $self->error( "Can't connect to DBMS: " . $DBI::errstr );
}
} ## end sub _connect
1;
__END__
=back
=head1 EXAMPLES
samples/testdb.pl
=head1 SEE ALSO
L<DBI>, L<DBD::Pg>
=head1 TODO
1. Make module less PostgreSQL specific.
=head1 AUTHOR
Michael Bochkaryov <misha@rattler.kiev.ua>
=head1 LICENSE
Copyright (C) 2008-2009 Net Style Ltd.
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
=cut
|