VERSION
0.1.4DESCRIPTION
This class can be used as a base class for objects who holds all their information in a database. The class provides the base methods to add/delete/update/select the object to/from a database.RoPkg::DBObject inherits RoPkg::Object .
SYNOPSIS
package RoPkg::Person;
use strict;
use warnings;
use RoPkg::DBObject;
use RoPkg::Exceptions;
use vars qw($VERSION @ISA);
@ISA=qw(RoPkg::DBObject);
my $methods = {
id => '-',
Name => '-',
Addr => '-',
};
sub new {
my ($class, %opt) = @_;
my $self;
$opt{methods} = $methods;
$self = $class->SUPER::new(%opt);
return $self;
}
sub AddToDB {
my ($self) = @_;
OutsideClass->throw('Called from outside class')
if ( !$self or ref($self) ne $CLASS);
return $self->SQL_Insert();
}
1;
tester.pl
use warnings;
use strict;
use RoPkg::Person;
use RoPkg::DB;
sub main {
my ($p, $db);
$db = new RoPkg::DB();
$db->add('dsn...', 'user', 'pass', 'dbc');
$p = new RoPkg::Person(dbo => $db, dbo_method => 'db_dbc');
$p->id(1);
$p->Name('John Doe');
$p->Addr('home');
$p->AddToDB();
}
main();
SUBROUTINES/METHODS
new($hashref)
Constructor of the class. At this moment new() accepts 3 parameters:- *) dbo
- *) dbo_method
- *) quote_char
dbo and dbo_method must be given to the constructor. quote_char is optional. dbo is the database object (a instance of RoPkg::DB)and dbo_method dbo_method is the name of the method used to have access to the database handle. quote_char is the char used internally by to quote the table names, field names and so on. Default, the quote_char is empty (no quotation). If you are using PostgreSQL and you have upcase chars in table and fields names use quote_char => q{"} .
Exceptions:
RoPkg::DBObject uses the exceptions provided by RoPkg::Exceptions. new() throws the following exceptions:
- *) Param::Mising
- *) Param::Wrong
Param::Missing is raised when dbo or dbo_method parameters have not been specified in the parameters list.
Param::Wrong is raised when dbo or dbo_method parameters are not defined (or empty).
dbo($dbo)
get/set method. If is present (and defined), the internal object will be replaced with the new one. If is not present, the method acts as a get method, and will return the database object. The method will raise OutsideClass exception if is called outside the class instance.dbo_method($dbo_method)
get/set method. If is present (and defined), the internal value will be replaced with the one specified by parameter. Otherwise, the get behaviour will be selected, and the method will return the current value of . The method will raise OutsideClass exception if is called outside the class instance.dbh()
Returns the object used by this object.SQL METHODS
The following methods are responsable for all operations involving the database. All methods use SQL::Abstract to generate sql queries to ensure portability of the queries.The data that will be added/deleted to the database, is taken from the methods provided by $methods .
SQL_Insert()
add the current object to the database. Returns the value of >execute.SQL_Update(@fields)
update the data from database. The fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object).SQL_Delete(@fields)
update the data from database. The fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object).SQL_Select(@fields)
searches into the database for the object and initialize the current object with the values found. The fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object). This method is special. It will look only at the first record who meet the requirements (in sql ''). Also, if no records are found DB::NoResults exception is raised.DEPENDENCIES
RoPkg::Object require perl 5.008 or later and the following modules:- SQL::Abstract
- RoPkg::Object
- RoPkg::Exceptions
