Help-Site Computer Manuals
Software
Hardware
Programming
Networking
  Algorithms & Data Structures   Programming Languages   Revision Control
  Protocols
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

Rose::DB::Object::Std
Standardized object representation of a single row in a database table.

Rose::DB::Object::Std - Standardized object representation of a single row in a database table.


NAME

Rose::DB::Object::Std - Standardized object representation of a single row in a database table.


SYNOPSIS


  package Category;

  use Rose::DB::Object::Std;

  our @ISA = qw(Rose::DB::Object::Std);

  __PACKAGE__->meta->table('categories');

  __PACKAGE__->meta->columns

  (

    id          => { type => 'int', primary_key => 1 },

    name        => { type => 'varchar', length => 255 },

    description => { type => 'text' },

  );

  __PACKAGE__->meta->add_unique_key('name');

  __PACKAGE__->meta->initialize;

  ...

  package Product;

  use Rose::DB::Object::Std;

  our @ISA = qw(Rose::DB::Object::Std);

  __PACKAGE__->meta->table('products');

  __PACKAGE__->meta->columns

  (

    id          => { type => 'int', primary_key => 1 },

    name        => { type => 'varchar', length => 255 },

    description => { type => 'text' },

    category_id => { type => 'int' },

    status => 

    {

      type      => 'varchar', 

      check_in  => [ 'active', 'inactive' ],

      default   => 'inactive',

    },

    start_date  => { type => 'datetime' },

    end_date    => { type => 'datetime' },

    date_created     => { type => 'timestamp', default => 'now' },  

    last_modified    => { type => 'timestamp', default => 'now' },

  );

  __PACKAGE__->meta->add_unique_key('name');

  __PACKAGE__->meta->foreign_keys

  (

    category =>

    {

      class       => 'Category',

      key_columns =>

      {

        category_id => 'id',

      }

    },

  );

  __PACKAGE__->meta->initialize;

  ...

  $product = Product->new(name        => 'GameCube',

                          status      => 'active',

                          start_date  => '11/5/2001',

                          end_date    => '12/1/2007',

                          category_id => 5);

  $product->save or die $product->error;

  $id = $product->id; # auto-generated on save

  ...

  $product = Product->new(id => $id);

  $product->load or die $product->error;

  print $product->category->name;

  $product->end_date->add(days => 45);

  $product->save or die $product->error;

  ...


DESCRIPTION

the Rose::DB::Object::Std manpage is a subclass of the Rose::DB::Object manpage that imposes a few more constraints on the tables it fronts. In addition to the constraints described in the the Rose::DB::Object manpage documentation, tables fronted by the Rose::DB::Object::Std manpage objects must also fulfill the following requirements:

Different databases provide for auto-generated column values in different ways. Some provide a native ``auto-increment'' or ``serial'' data type, others use sequences behind the scenes.

the Rose::DB::Object::Std manpage (in cooperation with the Rose::DB manpage and the Rose::DB::Object::Std::Metadata manpage) attempts to hide these details from you. All you have to do is omit the value for the primary key entirely. After the object is save()ed, you can retrieve the auto-selected primary key by calling the id() method.

You do have to correctly define the ``id'' column in the database, however. Here are examples of primary key column definitions that provide auto-generated values, one for each of the databases supported by the Rose::DB manpage.

  • PostgreSQL
  • 
        CREATE TABLE mytable
    
        (
    
          id   SERIAL NOT NULL PRIMARY KEY,
    
          ...
    
        );
  • MySQL
  • 
        CREATE TABLE mytable
    
        (
    
          id   INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    
          ...
    
        );
  • Informix
  • 
        CREATE TABLE mytable
    
        (
    
          id   SERIAL NOT NULL PRIMARY KEY,
    
          ...
    
        );

Other data definitions are possible, of course, but the three definitions above are used in the the Rose::DB::Object::Std manpage test suite and are therefore guaranteed to work. If you have success with alternative approaches, patches and/or new tests are welcome.

To achieve much of this functionality, the Rose::DB::Object::Std manpage uses the Rose::DB::Object::Std::Metadata manpage objects. The meta() method will create these form you. You should not need to do anything special if you use the idimoatic approach to defining metadata as shown in the synopsis.


METHODS

Only the methods that are overridden are documented here. See the the Rose::DB::Object manpage documentation for the rest.

meta
Returns the the Rose::DB::Object::Std::Metadata manpage object associated with this class. This object describes the database table whose rows are fronted by this class: the name of the table, its columns, unique keys, foreign keys, etc. See the the Rose::DB::Object::Std::Metadata manpage documentation for more information.

This can be used as both a class method and an object method.


AUTHOR

John C. Siracusa (siracusa@gmail.com)


COPYRIGHT

Copyright (c) 2007 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Programminig
Wy
Wy
yW
Wy
Programming
Wy
Wy
Wy
Wy