DBIx-SimpleQuery
view release on metacpan or search on metacpan
lib/DBIx/SimpleQuery.pm view on Meta::CPAN
# Try to get connection information from the following sources,
# in decreasing order of preference:
#
# - Specified as arguments to query (defeats the point, but a
# useful override nonetheless)
# - Given as parameters to new, which may be called explicitly by
# the module, instead of implicitly by query.
if (keys %params) {
$self->{"dsn"} = $params{"dsn"};
$self->{"user"} = $params{"user"};
$self->{"password"} = $params{"password"};
}
# - Stored as module-level variables
elsif ($default_dsn) {
$self->{"dsn"} = $default_dsn;
$self->{"user"} = $default_user;
$self->{"password"} = $default_password;
}
# - Stored as environment variables
elsif ($ENV{"DBIX_SIMPLEQUERY_DSN"}) {
$self->{"dsn"} = $ENV{"DBIX_SIMPLEQUERY_DSN"};
$self->{"user"} = $ENV{"DBIX_SIMPLEQUERY_USER"};
$self->{"password"} = $ENV{"DBIX_SIMPLEQUERY_PASSWORD"};
}
# - Read from /etc/simplequery.conf
elsif (-r '/etc/simplequery.conf') {
open my $config_file, '<', '/etc/simplequery.conf';
while (<$config_file>) {
chomp;
my $line = $_;
# Skip Comments
next if $line =~ /^\#/;
# Skip Blank Linkes
next if $line eq "";
next if $line =~ /^\s+$/;
# All remaining lines should be configuration values
unless ($line =~ /^\s*(dsn|user|password)\s*=\s*(\S+)\s*$/) {
croak "Bad config file format: $line";
}
else {
$self->{$1} = $2;
}
}
}
# - Default to the first available data source of the first
# available driver of Oracle, Pg, or mysql, in that order, using
# the current user's login name, and no password.
else {
my @available_drivers = DBI->available_drivers();
@available_drivers = grep { /^(?:Oracle|Pg|mysql)$/ } @available_drivers;
my @data_sources = DBI->data_sources(shift @available_drivers);
$self->{"dsn"} = shift(@data_sources);
$self->{"user"} = getpwuid($>);
$self->{"password"} = "";
}
return bless $self, $class;
}
sub setDefaults { return set_defaults(@_); }
sub set_defaults {
my %defaults = (ref($_[0]) eq "HASH" ? %{$_[0]} : @_);
$default_dsn = $defaults{"dsn"} if exists $defaults{"dsn"};
$default_user = $defaults{"user"} if exists $defaults{"user"};
$default_password = $defaults{"password"} if exists $defaults{"password"};
return;
}
sub getDsn { return get_dsn(@_); }
sub get_dsn {
my $self = shift();
$self = new DBIx::SimpleQuery unless (ref($self) eq "DBIx::SimpleQuery");
return $self->{"dsn"};
}
sub getUser { return get_user(@_); }
sub get_user {
my $self = shift();
$self = new DBIx::SimpleQuery unless (ref($self) eq "DBIx::SimpleQuery");
return $self->{"user"};
}
sub getPassword { return get_password(@_); }
sub get_password {
my $self = shift();
$self = new DBIx::SimpleQuery unless (ref($self) eq "DBIx::SimpleQuery");
return $self->{"password"};
}
# This can be called either as a class method or a function
sub query {
my $self = shift();
my $query;
if (ref($self) eq "DBIx::SimpleQuery") {
$query = shift();
}
else {
$query = $self;
$self = new DBIx::SimpleQuery(@_);
}
# Establish the connection
my $dbh;
if ($self->{"dsn"} =~ /^DBI:Pg/) {
$dbh = DBI->connect_cached($self->{"dsn"}, $self->{"user"}, $self->{"password"}, {
pg_server_prepare => 0,
});
}
else {
$dbh = DBI->connect_cached($self->{"dsn"}, $self->{"user"}, $self->{"password"});
}
croak "Unable to establish a database connection: $DBI::errstr" unless $dbh;
( run in 1.404 second using v1.01-cache-2.11-cpan-5623c5533a1 )