logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

RoPkg::DBObject - General pourpose database object.

Author

       Subredu Manuel <diablo@iasi.roedu.net>

Bugs And Limitations

       None known to the author

Configuration And Environment

       This  module  does not use any configuration files or environment variables. The dependencies however can
       use them. Please read the man page of each dependency to be sure.

Dependencies

       RoPkg::Object require perl 5.008 or later and the following modules:

       SQL::Abstract
       RoPkg::Object
       RoPkg::Exceptions

Description

       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 .

Diagnostics

       This module comes with his own tests. To run the tests unpack the source, and use 'make test' command.

Incompatibilities

       Do not install this version on the same machine with Simba <= 0.7.1

Name

        RoPkg::DBObject - General pourpose database object.

Perl Critic

       The code is perl critic level 2 compliant and almost level 1 compliant

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 DBI->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 'LIMIT 1'). Also, if no records are found DB::NoResults exception is raised.

Subroutines/Methods

new($hashref)
       Constructor of the class. At this moment new() accepts 3 parameters:

       *) dbo
       *) dbo_method
       *) quote_char

       dbo  and dbo_methodmust 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 DBI 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 $dbo is present (and defined), the internal $dbo object will be replaced with the  new
       one.  If  $dbo  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 $dbo_method is present (and defined), the internal $dbo_method value will be  replaced
       with  the  one specified by $dbo_method parameter. Otherwise, the get behaviour will be selected, and the
       method will return the current value of $dbo_method. The method will raise OutsideClass exception  if  is
       called outside the class instance.

   dbh()
       Returns the DBI object used by this 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();

Version

       0.1.4

See Also