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::DBCollection - base class who can be used for collections of database objects

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.  However, it is possible  that
       some dependencies to do so. Please read the man page of each dependency to find out more information.

Dependencies

       This module use the following modules:

       *) SQL::Abstract
       *) DBI
       *) Scalar::Util
       *) RoPkg

Description

       RoPkg::DBCollection is a class who can be used as a base class for database collection of objects. Is
       used by RoPkg::Simba::Mirrors ,RoPkg::Simba::Commands and RoPkg::Simba::Excludes. This class should not
       be used directly in applications but derived.

Diagnostics

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

Incompatibilities

       None known to the author

Name

       RoPkg::DBCollection - base class who can be used for collections of database objects

Perl Critic

       This modules is perlcritic level 2 compliant (with 1 exception)

See Also

       RoPkg::Exceptions RoPkg::DB RoPkg::DBObject RoPkg::Simba::Mirrors

Subroutines/Methods

new()
       The class constructor. Accepts a hash with parameters. The parameters who can be passed to new() are:

       dbo - database object (instance of RoPkg::DB)
       dbo_method - database method (for use with RoPkg::DB)
       table - the sql table name where the objects data can be found

       If you don't specify the dbo and dbo_method parameters, a Param::Missing exception is raised.

   dbh()
       returns the database handler (DBI object) used by the class.

   _count($fields)
       Returns  the number of records who match the fields specified in $fields. This method should be overriden
       by the child classes.  The $fields must me  specified  in  SQL::Abstract  format.  Please  refer  to  the
       SQL::Abstract documentation for more details about $fields format.

   _get($class_name,$fields,$order_by)
       Returns a array of initialized objects. The values are read from the database. $class_name is the name of
       the class who's gonna be instanciated. When creating the class instance dbo and dbo_method parameters are
       passed to the new() method.  The records from the database must match the $fields parameter and the order
       is  given  by  $order_by.  For  more  details  of  these  2  parameters  please  refer  to  SQL::Abstract
       documentation.
        Exceptions throwed:

       Param::Missing - when $class_name has not been specified
       Param::Wrong - when $class_name is not a class name
       DB::NoResults - when the query returned 0 results

Synopsis

        package RoPkg::Tester;

        use strict;
        use warnings;

        use Scalar::Util qw(blessed);
        use RoPkg::Exceptions;
        use RoPkg::DBCollection;
        use vars qw(@ISA);

        @ISA=qw(RoPkg::DBCollection);

        sub new {
          my ($class, %opt) = @_;
          my $self;

          $self = $class->SUPER::new(%opt);
          $self->{table} = 'Mirrors';

          return $self;
        }

        sub Count {
          my ($self) = @_;

          if (!blessed($self)) {
            OutsideClass->throw(
              error    => 'Called outside class instance',
              pkg_name => 'RoPkg::Tester',
            );
          }

          return $self->_count();
        }

        sub Get {
          my ($self, $fields) = @_;

          if (!blessed($self)) {
            OutsideClass->throw(
              error    => 'Called outside class instance',
              pkg_name => 'RoPkg::Tester'
            );
          }

          return $self->_get('RoPkg::Simba::Mirror');
        }

        1;

        sub main {
          my ($dbc, $dbp);

          $dbp = new RoPkg::DB();
          $dbp->Add(
                  'dbi:mysql:database=mirrors_db;host=localhost',
                  'root',
                  '',
                  'mirrors'
                );

          $dbc = new RoPkg::Tester(
                   dbo => $dbp,
                   dbo_method => 'db_mirrors'
                 );

          print $dbc->Count(),$/;

          my @mirrors = $dbc->Get();
        }

        main();

Version

       0.1.3

See Also