Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

DB persistence framework for Perl Classes

by gmpassos (Priest)
on Oct 17, 2004 at 00:10 UTC ( [id://399844]=perlmeditation: 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".

Replies are listed 'Best First'.
Re: DB persistence framework for Perl Classes
by dragonchild (Archbishop) on Oct 17, 2004 at 00:45 UTC
    How does this differ from very simple usages of Class::DBI?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Humm, in lines of code and DB portability?

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

        Lines of code don't matter as long as I'm not writing them.

        Portability? You can go all the way from SQLite through PostgreSQL through Oracle (we've done it) with Class::DBI. How much more portable do you want to be? Flat files? {grin}

        I won't be interested in your project unless you can give me better reasons than that. In fact, I'd actively discourage you from inventing your own shiny wheel, while not contributing to Class::DBI if you feel something is missing there.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: DB persistence framework for Perl Classes
by Arunbear (Prior) on Oct 17, 2004 at 01:43 UTC
    Is there an alternate interface for folks who don't like source filters?

    Also, are you seriously advocating the use of $obj->{attr} over $obj->attr ?

      Each attr can be accessed over $obj->{attr} or $obj->set_attr() and $obj->get_attr. And each key is tied to paste over the getter and setter methods too.

      About the source filtering, Class::HPLOO can generate .pm files from .hploo files automatically in your distributions, if you don't want to parse the source for each interpreter execution or make distributions. But I can work in something that enables the interface with HDB::Object without need to use Class::HPLOO source filtering.

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

Re: DB persistence framework for Perl Classes
by gmpassos (Priest) on Oct 17, 2004 at 19:14 UTC
    First of all, thanks for all your replies.

    Folks, don't be scared about new things, this is just a research work, and before release it I'm showing it to you.

    I receaved a lot of replies saying that X and Y already does this, etc... Well, please, show me with some sample code how to do this in Perl with already know modules. Maybe I can port some resources from here to there and have things working over components with more support.

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

      There really are quite a few modules now that do this sort of load/modify/save persistent objects stuff, the best known being Class::DBI and Tangram. Most of them show enough code in the synopsis that you should be able to see that they do these same things.

      Yours seems to create tables based on a class declaration. Tangram, XML::Comma, and maybe some others do that, but Class::DBI doesn't. It could be an interesting add-on for Class::DBI.

      Other than that, I'd say the most significant differences are the HPLOO syntax (neat experiment, but I wouldn't use it on my projects because it isn't really Perl and I would worry about other people understanding my code), and the features that other projects have which this one doesn't (triggers, constraints, ways to remap existing databases to accessors with different names, ways to use direct SQL, relationships between classes, etc.).

      There's nothing terribly wrong about your system, but I can't see much reason to recommend it right now when other systems have so much to offer.

Re: DB persistence framework for Perl Classes
by dragonchild (Archbishop) on Oct 17, 2004 at 14:45 UTC
    How does this differ from using Hibernate and similar tools?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I can be wrong, but Hibernate is for Java, not?

      And don't forget that in Java you need to do by hand your serialization of attributes. Only for J2SE 5 we have a new project called JDO that make things more automatic, and it will replace JDBC.

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

        Only for J2SE 5 we have a new project called JDO that make things more automatic, and it will replace JDBC.

        Not quite: JDO (Java Data Objects) is also available for J2SE 1.4 (and below perhaps). Furthermore, JDO is not intended as a replacement for JDBC! It is simply a persistent object store framework, mainly aimed at storing business objects. It therefore cannot (and will not) replace JDBC (which is just a means to hereogenously connect to different databases).

        -- JaWi

        "A chicken is an egg's way of producing more eggs."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://399844]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found