http://www.perlmonks.org?node_id=399844

I'm working into a framework to easy and fast create classes that have the object persistence over a basic relational DB.

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

With this 3 modules we have a framework were we don't need to care about anything in the DB side. Basically we just declare the class and attributes and all is done automatically, including the DB connection, table creation, insertion, coloumns update, commit, etc...

The best way to show a framework is a sample. So, first we declare our class:

use Class::HPLOO ; class User extends HDB::Object { use HDB::Object ; attr( user , pass , name , int age , int create_time ) ; sub User( $user , $pass , $name , $age ) { $this->{user} = $user ; $this->{pass} = $pass ; $this->{name} = $name ; $this->{age} = $age ; $this->{create_time} = time ; } }
And to use 1st we just define the global HDB connection (id => HPLOO) to be used by the HDB::Object classes:
BEGIN { use HDB ; HDB->new( id => 'HPLOO' , type => 'sqlite' , db => './hploo.db' ) ; } use User ; my $new_user = new User('joe' , 12345 , 'Joe Smith' , 30 ) ; $new_user = undef ; ## destroy to commit to the DB. Or just call $ne +w_user->hdb_save. print "-------------------------------------------\n" ; my $user_joe = load User("user eq 'joe'") ; foreach my $attr ( $user_joe->ATTRS ) { print "$attr: $user_joe->{$attr}\n" ; } $user_joe = undef ; ## commit print "-------------------------------------------\n" ; my @users = load User("id < 10") ; foreach my $users_i ( @users ) { ++$users_i->{age} ; } @users = () ; ## commit changes. print "-------------------------------------------\n" ; print User->hdb_dump_table ; print "-------------------------------------------\n" ;
Output:
------------------------------------------- user: joe pass: 12345 name: Joe Smith age: 30 create_time: 1097971836 ------------------------------------------- ------------------------------------------- TABLE User: user = TEXT pass = TEXT name = TEXT age = INTEGER create_time = INTEGER id = INTEGER PRIMARY KEY ROWS: joe::12345::Joe Smith::44::1097971836::1 joe::12345::Joe Smith::43::1097971838::2 joe::12345::Joe Smith::42::1097971839::3 joe::12345::Joe Smith::41::1097971839::4 joe::12345::Joe Smith::40::1097971840::5 joe::12345::Joe Smith::39::1097971840::6 joe::12345::Joe Smith::38::1097971841::7 joe::12345::Joe Smith::37::1097971842::8 joe::12345::Joe Smith::36::1097971843::9 joe::12345::Joe Smith::30::1097971844::10 joe::12345::Joe Smith::30::1097971845::11 joe::12345::Joe Smith::30::1097971846::12 joe::12345::Joe Smith::30::1097971847::13 joe::12345::Joe Smith::30::1097971848::14 -------------------------------------------

Graciliano M. P.
"Creativity is the expression of the liberty".