Author image Masatoshi Mizuno
and 1 contributors

NAME

Egg::Plugin::EasyDBI - Plugin for Egg to use DBI easy.

SYNOPSIS

  use Egg qw/ EasyDBI /;
  
  # Acquisition of data base steering wheel.
  my $dbh= $e->dbh;
  
  # SELECT * FROM hoge WHERE id = ?
  my $hoge= $dbh->hashref(q{SELECT * FROM hoge WHERE id = ?}, $id)
         || die q{ Data is not found. };
  or
  my $hoge= $e->db->hoge->hashref('id = ?', $id)
         || die q{ Data is not found. };
  
  # SELECT * FROM hoge WHERE age > ?
  my $list= $dbh->arrayref(q{SELECT * FROM hoge WHERE age > ?}, 20)
         || die q{ Data is not found. };
  or
  my $list= $e->db->hoge->arrayref('age > ?', 20)
         || die q{ Data is not found. };
  
  # SELECT id FROM hoge WHERE user = ?
  my $id= $dbh->scalar(q{SELECT id FROM hoge WHERE user = ?}, 'boo')
         || die q{ Data is not found. };
  or
  my $id= $e->db->hoge->scalar(\'id', 'user = ?', 'boo');
  
  # The processed list is acquired.
  my $list= $e->db->hoge->arrayref('age > ?', [20], sub {
       my($array, %hash)= @_;
       push @$array, "$hash{id} : $hash{user} : $hash{age}";
    }) || die q{ Data is not found. };
    
  # The data that can be immediately used is acquired.
  my $text;
  $e->db->hoge->arrayref('age > ?', [20], sub {
       my($array, %hash)= @_;
       $text.= <<END_DATA;
  ID   : $hash{id}
  NAME : $hash{user}
  AGE  : $hash{age}
  END_DATA
    }) || "";
    
  # INSERT INTO hoge (id, user, age) VALUES (?, ?, ?);
  $dbh->do(
    q{INSERT INTO hoge (id, user, age) VALUES (?, ?, ?)},
    qw/ 1 zoo 21 /
    ) || die q{ Fails in regist of data. };
  or
  $e->db->hoge->insert( id=> 1, user=> 'zoo', age=> 20 )
      || die q{ Fails in regist of data. };
  
  # UPDATE hoge SET other = ?, age = age + 1 WHERE id = ?
  $dbh->do(
    q{UPDATE hoge SET other = ?, age = age + 1 WHERE id = ?},
    qw/ gao 1 /
    ) || die q{ Fails in regist of data. };
  or
  $e->db->hoge->update( id=> 1, other=> 'gao', age=> \1 )
      || die q{ Fails in regist of data. };
  
  or, I think that this is the best.
  $e->db->hoge->update(\'id = ?', { id=> [1], other=> 'gao', age=> \1 })
      || die q{ Fails in regist of data. };

A method different from 'do' of dbh of DBI is called about above-mentioned 'do'. It is $dbh->dbh. Therefore, usual 'do' is $dbh->dbh->do.

DESCRIPTION

It is a plugin to use module Egg::Mod::EasyDBI to use DBI easily.

It is necessary to setup Egg::Model::DBI to use it.

CONFIGURATION

Please go in the configuration of this plug-in with 'plugin_easydbi'.

All set values extend to Egg::Mod::EasyDBI as it is.

METHODS

dbh ([LABEL_NAME])

The object of Egg::Plugin::EasyDBI::handler is returned.

There is no argument needing and when two or more connection destination is set with Egg::Model::DBI, LABEL_NAME is passed usually.

  my $dbh= $e->dbh;

db

The db object of Egg::Plugin::EasyDBI::handler is returned.

  my $table= $e->db->hoge_table;

When tables unite, it becomes the following.

  my $table= $e->db(qw/ hoge = hoge1:a.id=b.id /);

see Egg::Mod::EasyDBI.

mysql_datetime ([TIME])

The time value is received and the character string of the datetime type is returned.

When TIME is omitted, a present time value is used.

  # It was timely the day before of the 30th.
  my $datetime= $e->mysql_datetime( time- (30* 24* 60* 60) );

close_dbh ([BOOL])

All the opened transactions are shut and the object is annulled.

AutoCommit However, the object is only annulled if it is invalid.

Please pass BOOL one usually. To do all rollbacks when 0 and undefined are passed, it is treated.

  # If commit_ok is effective, commit is done.
  $e->close_dbh(1);
  
  # Even if commit_ok is effective, it is rollback treatment of all.
  $e->close_dbh(0);

HANDLER METHODS

Egg::Plugin::EasyDBI::handler has succeeded to Egg::Mod::EasyDBI.

new

Constructor.

dbh and the configuration acquired from Egg::Model::DBI are passed to the constructor of the base class.

SEE ALSO

Egg::Release, Egg::Mod::EasyDBI, Egg::Model::DBI, DBI,

AUTHOR

Masatoshi Mizuno <lushe@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 Bee Flag, Corp. <http://egg.bomcity.com/>, All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.