HDB::Object - Base class for persistent Class::HPLOO objects.
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:
The class declaration and attribute handler.
The object persistence and class proxy.
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.
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.
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() ;
Return the HDB object that handles the DB connection.
Dump all the table content, returning that as a string.
Retrun TRUE if the table for the HDB::Object was already created.
Create the table for the HDB::Object if it doesn't exists.
Return TRUE when the object was changed and need to be updated in the DB.
Return the max id in the object table.
Delete the object from the database.
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.
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 ; ... }
Return the IDs that have a reference to it.
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. } }
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 )
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.
HPL, HDB, Class::HPLOO, Class::LazyLoad.
Graciliano M. P. <gm@virtuasites.com.br>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HDB
CPAN shell
perl -MCPAN -e shell install HDB
For more information on module installation, please visit the detailed CPAN module installation guide.