Nile
view release on metacpan or search on metacpan
lib/Nile/DBI.pm view on Meta::CPAN
# $arg{name}, $arg{user}, $arg{pass}
# if called without params, it will try to load from the default config vars.
# get app context
$app = $self->app;
$dbh = $app->db->connect(%arg);
=head1 DESCRIPTION
Nile::DBI - SQL database manager.
=cut
use Nile::Base;
use DBI;
use DBI::Profile;
use DBI::ProfileDumper;
use Hash::AsObject;
#my $hash = Hash::AsObject->new(\%hash); $hash->foo(27); print $hash->foo; print $hash->baz->quux;
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 dbh()
$app->db->dbh;
Get or set the current database connection handle.
=cut
has 'dbh' => (
is => 'rw',
);
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 connect()
$dbh = $app->db->connect(%arg);
Connect to the database. If %arg empty, it will try to get arg from the config object.
Returns the database connection handle is success.
=cut
sub connect {
my ($self, %arg) = @_;
my ($dbh, $dsn, $app);
$app = $self->app;
my $default = $app->config->get("dbi");
$default ||= +{};
%arg = (%{$default}, %arg);
$arg{driver} ||= "mysql";
$arg{dsn} ||= "";
$arg{host} ||= "localhost";
$arg{port} ||= 3306;
$arg{attr} ||= +{};
#$arg{attr} = {RaiseError => 0, PrintError => 0, mysql_enable_utf8 => 1}
if (!$arg{name}) {
$app->abort("Database error: Empty database name.");
}
#$self->dbh->disconnect if ($self->dbh);
if ($arg{driver} =~ m/ODBC/i) {
$dbh = DBI->connect("DBI:ODBC:$arg{dsn}", $arg{user}, $arg{pass}, $arg{attr})
or $self->db_error("$DBI::errstr, DSN: $arg{dsn}");
}
else {
$arg{dsn} ||= "DBI:$arg{driver}:database=$arg{name};host=$arg{host};port=$arg{port}";
$dbh = DBI->connect($arg{dsn}, $arg{user}, $arg{pass}, $arg{attr})
or $self->db_error("$DBI::errstr, DSN: $arg{dsn}");
}
$self->dbh($dbh);
return $dbh;
#$dbh->{'mysql_enable_utf8'} = 1;
#$dbh->do('SET NAMES utf8');
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 disconnect()
$app->db->disconnect;
Disconnect from this connection handle.
=cut
sub disconnect {
my ($self) = @_;
$self->dbh->disconnect if ($self->dbh);
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub table {
my ($self, $name) = @_;
$self->app->load_once("Nile::DBI::Table");
$self->app->object("Nile::DBI::Table", (name => $name));
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 run()
$app->db->run($qry);
Run query using the DBI do command or abort if error.
=cut
sub run {
my ($self, $qry) = @_;
$self->dbh->do($qry) or $self->db_error($qry);
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 do()
$app->db->do($qry);
Run query using the DBI do command and ignore errors.
=cut
sub do {
my ($self, $qry) = @_;
$self->dbh->do($qry);
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 exec()
$sth = $app->db->exec($qry);
Prepare and execute the query and return the statment handle.
=cut
sub exec {
my ($self, $qry) = @_;
my $sth = $self->dbh->prepare($qry) or $self->db_error($qry);
$sth->execute() or $self->db_error($qry);
( run in 0.659 second using v1.01-cache-2.11-cpan-df04353d9ac )