The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Egg::Plugin::DBI::Accessors::Extra - Accessor to convenient dbh that can write SQL short.

SYNOPSIS

  use MYPROJECT;
  use strict;
  use Egg qw/DBI::Accessors::Extra/;

Example code.

  my $scalar= $e->db->table_name->scalarref
    ('name', 'id = ?', $id) || die 'fails in acquisition.';
  
  my $array= $e->db->table_name->arrayref
    ([qw/id name email/], 'group = ?', $group) || die 'fails in acquisition.';
  for my $hash (@$array) {
        print "$hash->{id} : $hash->{name} : $hash->{email}";
  }
  
  my $hash= $e->db->table_name->hashref
    ([qw/id name email/], 'id = ?', $id); || die 'fails in acquisition.';
  print "$hash->{id} : $hash->{name} : $hash->{email}";
  
  $e->db->table_name->insert([qw/id name email/], $id, $name, $email);
  
  $e->db->table_name->update([qw/name email/], 'id = ?', $name, $email, $id);
  
  # All data is updated.
  $e->db->table_name->upgrade([qw/group/], 'general');
  
  $e->db->table_name->delete('group = ?', $group);
  
  # All data is deleted.
  $e->db->table_name->clear(1);

DESCRIPTION

This module is made to be able to write the inquiry of the data table easily.

Because this module has succeeded to Egg::Plugin::Accessors, it is not necessary to read separately.

The function to unite two or more tables is not provided. Only a single table can be treated.

When the application as DBIx::Class etc. are not used is made, it might be convenient.

When the table name that actually exists in $e->db is called as a method, Abject with the accessor to 'dbh' is restored.

  Table "mambers"
  Column    |  Type
  ----------+---------
  id        | serial
  user_name | verchar
  email     | verchar

  $e->db->members->insert([qw/user_name email/], $user_name, $email);
  
  my $array= $e->db->members->arrayref([qw/id user_name email/]);

METHODS

my $db= $e->db;

The object for handling to the data table is returned.

my $table= $db->[TABLE_NAME];

The object with the accessor to the data table is returned.

my $scalar= $table->scalarref('[FIELD_NAME]', '[WHERE]', [EXECUTE_VARS]);

The acquired single data is returned by the Scalar reference. When two or more data exists, the value of the data acquired first is returned.

0 returns when pertinent data is not found.

The name of the data field that wants to be acquired to the first argument is specified.

[WHERE] and [EXECUTE_VAR] can be omitted.

[WHERE] is not cared about including the statement such as 'ORDER BY' etc.

my $hash= $table->hashref([qw/[FIELD_NAME].../], '[WHERE]', [EXECUTE_VARS]);

The acquired data is returned by the HASH reference. When two or more data exists, the value of the data acquired first is returned.

0 returns when pertinent data is not found.

The data field name that wants to be acquired is given to the first argument with the ARRAY reference or Scalar.

The first value treats the first argument most as a field to check the presence of data.

[WHERE] and [EXECUTE_VAR] can be omitted.

[WHERE] is not cared about including the statement such as 'ORDER BY' etc.

my $array= $table->arrayref([qw/[FIELD_NAME].../], '[WHERE]', [EXECUTE_VARS]);

Two or more acquired data records are returned by the ARRAY reference. The data of each record becomes HASH reference.

0 returns when pertinent data is not found.

The data field name that wants to be acquired is given to the first argument with the ARRAY reference or Scalar.

[WHERE] and [EXECUTE_VAR] can be omitted.

[WHERE] is not cared about including the statement such as 'ORDER BY' etc.

$table->insert([qw/[FIELD_NAME].../], [EXECUTE_VARS]);

Data is added to the table.

$table->update([qw/[FIELD_NAME].../], [WHERE], [EXECUTE_VARS]);

Data is updated.

When [FIELD_NAME] is ARRAY reference, it converts it into an appropriate SET sentence.

  $table->update([qw/uid email address/] ...);

  'uid = ?, email = ?, address = ?'

When [FIELD_NAME] is given with Scalar, it is evaluated as it is.

  $table->update('uid = ?, email = ?, address = ?', ...)

$table->upgrade([qw/[FIELD_NAME]/], [EXECUTE_VARS]);

The value of the specified field on all the records is changed.

Similar $table->update is operated about [FIELD_NAME].

$table->any([SQL_STATEMENT], [EXECUTE_VARS]);

It is a general method.

The part written '-db-' of [SQL_STATEMENT] is replaced with the table name.

  $table->any("SELECT * FROM -db- WHERE a = ?", ...);
  
  'SELECT * FROM table_name WHERE a = ?';

$table->delete([WHERE], [EXECUTE_VARS]);

The specified record is deleted.

$table->clear([BOOLEAN]);

All the records are deleted. [WHERE] must be sure to be in $table->delete and there not be here needing though it is necessary.

If one is not given for safety, it doesn't operate.

SEE ALSO

Egg::Plugin::DBI::Accessors, Egg::Release,

AUTHOR

Masatoshi Mizuno <mizuno@bomcity.com>

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.