Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

OOP + RDBMS == :D

by simeon2000 (Monk)
on Jul 19, 2002 at 13:06 UTC ( #183212=note: print w/replies, xml ) Need Help??


in reply to How to structure applications using a RDBMS

My personal favorite DB application design model is OOP. I usually end up making an object with the same field names as the database table to represent each object.

pseudo: package Project::User; my @FIELDS = qw /name age sex passwd/; sub new { my $type = shift; my $dar = shift or return undef; # data array ref, usually # a fetchrow_arrayref if (ref $dar = array) { # they already pulled the data, we're just holding # it for display or an update } elsif ($dar =~ /^\d+$/) { # if it's a unique ID, pull the data from the db } else { # it's just empty, we're empty } my %init = (); my @init{ @FIELDS } = @$dar; } sub pullFromDB { my $self = shift; my $uid = shift or return undef; # sql code to pull from DB } sub dbSync { my $self = shift; # db commit stuff here } sub assignArgs { my $self = shift; # utility function to assign data values to object properties }
... ok that's a bit much to write out, but I hope it presents the basic ideas I've used. Usually the object is used like

$user = new Project::User($uid); or $user = new Project::User($sql->fetchrow_arrayref); It makes it very easy to operate on the front-end. Or perhaps it's just needlessly complex :P

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://183212]
help
Chatterbox?
[Lady_Aleena]: Good morning Corion, I hope you have a wonderful day as well. 8)
[Corion]: Yesterday I encountered an interesting data structure problem. I have a remote program that emits events, and my client listens for these events with one-shot callbacks, that is, I register the callback and if the event gets generated that callback ...
[Lady_Aleena]: robby_dobby, every day. Chaos is my life with few controls.
[Corion]: ... gets called once. The data structure for that is just a hash of arrays, mapping the event type to a queue of registered one-shots, and the first one-shot from the queue gets removed and called.
[Corion]: But now I want to register a one-shot for two events, of which only one will arrive, so my data structure doesn't work anymore...
[Lady_Aleena]: Corion, ouchy.
[Corion]: (maybe I should write this up as a SoPW) - currently, the "most efficient" data structure I come up with is a single array which I scan for the first fitting one-shot. Not efficient but I don't expect more than five outstanding one-shots anyway
[choroba]: can't you create a meta-key corresponding to the disjunction of the events?
[robby_dobby]: Corion: Heh. This whole thing smells of Strategy Pattern or MVC pattern.
[Corion]: And performance linear to the number of registered one-shots doesn't feel that bad. Maybe I should collect statistics on how many callbacks are outstanding ;)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-05-29 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?