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

NAME

HDB::Object - Base class for persistent Class::HPLOO objects.

DESCRIPTION

This is the base class for persistent Class::HPLOO objects.

This will automaticallt make Class::HPLOO classes persistent in any DB handled by HDB.

This persistence framework was built by a group of modules that handles specific parts of the problem:

Class::HPLOO

The class declaration and attribute handler.

HDB::Object

The object persistence and class proxy.

HDB

The DB connection and SQL communication for each DB type.

All of this will create a very automatic way to create persistent objects over a relational DB, what is perfect for fast and easy creation of good systems. For Web Systems this framework is automatically embeded into HPL, where you can count with a lot of resources for fast and powerful creations for the Web.

USAGE

Here's an example of a class built with HDB::Object persistence.

  use Class::HPLOO ;
  
  class User extends HDB::Object {
  
    use HDB::Object ;
  
    attr( user , pass , name , int age ) ;
    
    sub User( $user , $pass , $name , $age ) {
      $this->{user} = $user ;
      $this->{pass} = $pass ;
      $this->{name} = $name ;
      $this->{age} = time ;
    }
  
  }

You can see that is completly automatic and you don't need to care about the serialization or even about the DB connection, tables and column types.

METHODS

load (CONDITIONS)

Loads an object that exists in the database.

CONDITIONS can be a sequence of WHERE conditions for HDB:

  my $users_x = load User('id == 123') ;
  
  my @users = load User('id <= 10' , 'id == 123' , 'name eq "joe"') ;

If CONDITIONS is NOT paste, if wantarray it will return all the objects in the table, or it will return only the 1st:

  my $user1 = load User() ;
  
  my @all_users = load User() ;

hdb

Return the HDB object that handles the DB connection.

hdb_dump_table

Dump all the table content, returning that as a string.

hdb_table_exists

Retrun TRUE if the table for the HDB::Object was already created.

hdb_create_table

Create the table for the HDB::Object if it doesn't exists.

hdb_obj_changed

Return TRUE when the object was changed and need to be updated in the DB.

hdb_max_id

Return the max id in the object table.

hdb_delete

Delete the object from the database.

hdb_save

Save the object in the database.

** Note that you don't need to call this method directly, since when the object is destroied from the memory it will be saved automatically.

hdb_clean_unref

This will automatically clean objects from this table if they doesn't have references in the database from another object. So, only call this if you have a colection of objects that are referenced by others or you will lose all the objects from the table:

  Users::Level->hdb_clean_unref ;

You also can define the constant AUTO_CLEAN_UNREF to automatically clean unreferenced objects:

  class Users::Level extends HDB::Object {
    use constant AUTO_CLEAN_UNREF => 1 ;
    ...
  }

hdb_referenced_ids

Return the IDs that have a reference to it.

DESTROY and AUTO SAVE OBJECT

The object will be automatically saved when the object is destroied.

So if you want to use this automatically save resource you can't overload the sub DESTROY, or at least should call the super method:

  class User extends HDB::Object {
    ...
    
    sub DESTROY {
      $this->SUPER::DESTROY ;
      ... # your destroy stuffs.
    }
  }

OBJECT REFERENCES

HDB::Object will try to handle automatically attributes that make references to other objects or list of objects.

When an atribute make a reference to an object that extends HDB::Object, only the ID to the object will be saved, making a reference to the ID of the object table.

If an object doesn't extends HDB::Object it will be serialized, using Storable, and stored in the coloumn value.

When an attribute have a list of objects, a reference table is created automatically:

  attr( array Date::OBject dates )

OBJECT PROXY

The Object Proxy mechanism was inspirated on Class::LazyLoad. The main idea is that the object won't be created unless it's accessed. The proxy will be used only when the object is loaded from the DB, not when you create a new object. So, when you load an object from the DB, actually the DB will be accessed only when you use the object.

An Object Proxy is very important to save memory and DB access, since if we don't use a proxy is possible to have an object that loads all the DB in the memory due it's object tree.

SEE ALSO

HPL, HDB, Class::HPLOO, Class::LazyLoad.

AUTHOR

Graciliano M. P. <gm@virtuasites.com.br>

I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.