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:
- Class::HPLOO
The class declaration and attribute handler.
- HDB::Object
The object persistence and class proxy.
- HDB
The DB connection and SQL communication for any DB type.
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".
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|