Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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".


In reply to DB persistence framework for Perl Classes by gmpassos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found