NAME

App::CLI::Plugin::DBI - for App::CLI::Extension database base module

VERSION

1.1

SYNOPSIS

  # 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;
  }

DESCRIPTION

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");

METHOD

dbi_connect

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

dbi_disconnect

destroy DBI disconnect. finish 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

dbh

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");

dbi_default_handle

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;

AUTHOR

Akira Horimoto

SEE ALSO

App::CLI::Extension DBI

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

COPYRIGHT

Copyright (C) 2010 Akira Horimoto