Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

(jeffa) Re: Code factory

by jeffa (Bishop)
on Jul 07, 2003 at 19:45 UTC ( [id://272078]=note: print w/replies, xml ) Need Help??


in reply to Code factory

A simple naming convention could be a big help here. Anytime i create a database table, i always (99% of the time) use an "auto increment" id field named id. Not cust_id. Not user_id. Just id. After all, if i know the name of the table, i know what kind of id it is. Now, if that id is referenced in another table, then i append the full table name. Example:
foo: id
     name
     status

bar: id
     name
     status
     foo_id
By keeping a convention such as this, i look up field names less often, and i can also take advantage of this in my code, especially when having to insert into more than one table at a time (second and third normalized tables).

Class::DBI is a difficult module to master, but it powerfully abstracts such details away and allows you retrieve rows easily:

my $cd = CD->retrieve(1); my @cds = CD->retrieve_all; my @cds = CD->search(year => 1980); my @cds = CD->search_like(title => 'October%');
But once you have to start joining tables together, things can get a bit tricky. It is worth taking a look at, however.

UPDATE:
Oh, in the spirit of TIMTOWTDI, here is my version of a generic insert:
sub generic_id_fetch { my ($table,$id,@field) = @_; my $field = @field ? join(',',@field) : '*'; $dbh->selectrow_hashref("select $field from $table where id=?",unde +f,$id); }
And yes, i hardcoded the name of the id field. YMWV (your mileage will vary). Hope this helps. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found