App::CLI::Plugin::DBI - for App::CLI::Extension database base module
1.1
# MyApp.pm package MyApp; use strict; use base qw(App::CLI::Extension); # extension method __PACKAGE__->load_plugins(qw(DBI)); __PACKAGE__->config(dbi => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]); 1; # MyApp/Hello.pm package MyApp::Hello; use strict; use base qw(App::CLI::Command); our $VERSION = '1.0'; sub run { my($self, @args) = @_; my $sql = "select id, name, age from member where id = ?"; my $sth = $self->dbh->prepare($sql); $sth->execute($args[0]); while (my $ref = $sth->fetchrow_hashref) { # anything to do... } $sth->finish; }
App::CLI::Extension DBI plugin module
dbh method setting
normal setting
# config (example: PostgreSQL) __PACKAGE__->config(dbi => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]); # get DBI handle my $dbh = $self->dbh;
multi db setting
# config __PACKAGE__->config(dbi => { default => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }] other => ["dbi:Pg:dbname=other_db;host=192.168.1.100;port=5432", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }] ); # get DBI handle my $default_dbh = $self->dbh; # same as $self->dbh("default") my $other_dbh = $self->dbh("other");
initialize DBI connect setting. setup phase to run normally with no need to perform explicitly.
However, the environment variable "APPCLI_DISABLE_DB_AUTO_CONNECT" If you have defined not to run the setup phase,
the need to call this method yourself
destroy DBI disconnect. finish phase to run normally with no need to perform explicitly.
get DBD::db. default handle name is "default"(actual value and the value returned dbi_default_handle method)
Example
# same as $self->dbh($self->dbi_default_handle); my $dbh = $self->dbh; # if you set up if you want to connect multiple databases my $default_dbh = $self->dbh; my $other1_dbh = $self->dbh("other1"); my $other2_dbh = $self->dbh("other2");
get default handle name.
get current default handle name
# $handle is "default" my $handle = $self->dbi_default_handle;
to change the default handle name
$self->dbi_default_handle("new_handle_name"); # get new_handle_name db handle my $dbh = $self->dbh;
Akira Horimoto
App::CLI::Extension DBI
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright (C) 2010 Akira Horimoto
To install App::CLI::Plugin::DBI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::CLI::Plugin::DBI
CPAN shell
perl -MCPAN -e shell install App::CLI::Plugin::DBI
For more information on module installation, please visit the detailed CPAN module installation guide.