NAME

Egg::Plugin::DBI::Easy - Plugin of an easy doing as for treatment of DBI.

SYNOPSIS

  use Egg qw/ DBI::Easy /;

  # The result of SELECT is received with HASH.
  my $hash= $e->dbh_hashref( 'id',
     q{ SLECT id, name, email FROM myapp_table WHERE id = ? }, $id)
     || return 0;
  
  # The data table object is used.
  my $hash= $e->db->myapp_table->hashref
     ([qw/ id name email /], ' id = ? ', $id) || return 0;
  
  # Data is added.
  $e->db->myapp_table->insert
     ({ id => $id, name => 'myname', emai => 'myname@domain.name' });
  
  # Specified data is deleted.
  $e->db->myapp_table->delete(' id = ? ', $id);
  
  # All data is deleted.
  $e->db->myapp_table->clear(1);

DESCRIPTION

It is a plug-in to be able to write the description of the DBI processing that tends to become tedious easily.

* However, complex SQL cannot be treated.

METHODS

sql_datetime ( [TIME] )

Time::Piece::MySQL->mysql_datetime is returned.

An arbitrary date can be acquired by passing TIME the value of time.

  # Time of the day before of the seventh is acquired.
  $e->sql_datetime( time- (7* 24* 60* 60) );

* Please look at the document of Time::Piece::MySQL.

dbh_hashref ( [TRUE_CHECK_COLUMN], [SQL], [EXECUTE_ARGS] )

It is dbh->prepare( [SQL] ), and sth->execute( [EXECUTE_ARGS] ).

If the result is preserved in HASH and the value of TRUE_CHECK_COLUMN is effective, the HASH reference is returned.

 * The result returns only one record.
 * When two or more records become a hit, the first data is returned.

  my $hash= $e->dbh_hashref
            ('id', 'SELECT * FROM myapp_table WHERE id = ?', $id)
            || return 0;
  
  print $hash->{id};

dbh_arrayref ( [SQL], [EXECUTE_ARGS] )

The result is returned by the ARRAY reference.

* Each data of the ARRAY inside becomes HASH.

  my $array= $e->dbh_arrayref
           ('SELECT * FROM myapp_table WHERE email like ?', '%name%')
           || return 0;
  
  for my $db (@$array) {
    print "$db->{id} = $db->{name} : $db->{email} \n";
  }

dbh_scalarref ( [SQL], [EXECUTE_ARGS] )

The result is returned by the SCALAR reference.

* When two or more records become a hit, the first data is returned.

  my $scalar= $e->dbh_scalarref
            ('SELECT email FROM myapp_table WHERE id = ?', $id)
            || return 0;
  
  print $$scalar;

db

The object that generates the data table object is returned.

  $e->db->table_name->...

 * The table name is used and the comeback call is used for the method
   putting it.
 * When the table name is improper as the method name of Perl, it is not
   possible to use it.

DB METHODS

new

Constructor who returns DB object.

AUTOLOAD

The method of returning the handler object recognizing the table name the method of no existence is dynamically generated.

DESTROY

It doesn't do at all.

HANDLER METHODS

new

Constructor who returns handler object.

hashref ( [GET_COLUMN], [WHERE_SQL], [EXECUTE_ARGS] )

The result of dbh_hashref is returned.

If GET_COLUMN is ARRAY, the first column is treated most as TRUE_CHECK_COLUMN.

If GET_COLUMN is usual SCALAR, it is treated as TRUE_CHECK_COLUMN, and '*' is continued to SELECT.

* Please write ORDER BY etc. following WHERE in WHERE_SQL.

  my $hash= $e->db->myapp_table->hashref('id', 'id = ?', $id)
         || return 0;
  
  print $hash->{id};

arrayref ( [GET_COLUMN], [WHERE_SQL], [EXECUTE_ARGS] )

The result of dbh_arrayref is returned.

GET_COLUMN accepts the ARRAY reference.

When GET_COLUMN is omitted, '*' is used.

  my $array= $e->db->myapp_table->arrayref(0, 'email like ?', '%name%')
          || return 0;
  
  for my $db (@$array) {
    print "$db->{id} = $db->{name} : $db->{email} \n";
  }

scalarref ( [GET_COLUMN], [WHERE_SQL], [EXECUTE_ARGS] )

The result of dbh_scalarref is returned.

  my $scalar= $e->db->myapp_table->scalarref('email', 'id = ?', $id)
           || return 0;
  
  print $$scalar;

insert ( [DATA_HASH], [IGNOR_COLUMN] )

The data record is added.

IGNOR_COLUMN is included in DATA_HASH, and it specifies it when there is column that wanting the inclusion is not in SQL. * Being able to specify is 1 It is only column.

  # Do not include the data type of id in DATA_HASH for Serial etc.
  $e->db->myapp_table->insert({ name => 'myname', email => 'email@addr' });

update ( [PRIMARY_COLUMN], [DATA_HASH] )

The data record is updated.

* The value of PRIMARY_COLUMN is excluded from the set sentence.

  $e->db->myapp_table->update('id', { id => 1, name=> 'yurname' });

upgrade ( [DATA_HASH] )

Update that doesn't need WHERE is done. For two or more data update in a word.

  $e->db->myapp_table->upgrade({ age => 18 });

update_insert ( [PRIMARY_COLUMN], [DATA_HASH] )

If the data of PRIMARY_COLUMN exists, update is done and insert is done if not existing.

* Please pass it with ARRAY if PRIMARY_COLUMN is Serial type.

  $e->db->myapp_table->update_insert(['id'], { id => 1, name=> 'yurname' });

delete ( [WHERE_SQL] )

The data deletion with the condition is done.

  $e->db->myapp_table->delete('age < ?', 18);

clear ( [TRUE] )

All the data of the table inside is deleted.

* If TRUE is not given, the exception is generated.

  $e->db->myapp_table->clear(1);

SEE ALSO

Egg::Model::DBI, Egg::Plugin::DBI::Transaction, Egg::Release,

AUTHOR

Masatoshi Mizuno <lushe@cpan.org>

COPYRIGHT

Copyright (C) 2007 by 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.