Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: sql query subroutine check

by Ovid (Cardinal)
on Mar 01, 2003 at 23:20 UTC ( #239793=note: print w/replies, xml ) Need Help??

in reply to sql query subroutine check

I've recently become a convert to object-oriented persistence modules. Specifically, I use Class::DBI, though there are others which are good. One of the many benefits of these modules is, when used properly, they allow for greater decoupling of code from the specific database implementation. For example, consider the following SQL:

my $cmd_ins = <<"END_SQL"; INSERT INTO product_metadata (desc, abbrv, prdct_id) VALUES (?, ?, ?) END_SQL

Later, as the programmer realizes that the field names in the database are not very desriptive, she thinks to change them to description, abbreviation, and product_id. This makes it easier to see what's happening in the database, but also forces her to change her code in every spot where this table is referenced.

As an alternative, I do something like the following:

my %column_map = ( description => 'desc', abbreviation => 'abbrv', product_id => 'prdct_id' );

Then, when I create an object that is persistent in the database, I use this mapping to map my desired accessor names to column names. Outside of the object, code relies on the accessor names and if I need to change the database column names, I just change them in %column_map and my code still works.

While you are probably well into your project, this could save you much grief in the future, particularly in projects where you are developing your database at the same time as you develop the code. For a more complete explanation of this, see my response at


New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: sql query subroutine check
by maksl (Pilgrim) on Mar 02, 2003 at 14:47 UTC

    i'm using hashes quite frequently to map changing file_names but never thought about using it for my database column. so sometimes, i carry with me strange names for field names .. their use has changed in between .. one that was thought for news_id .. is used for position in the referenced cat ;)

    my %column_map = ( position => 'news_id', );

    makes this code easier to read for shure
    /me is taking a serious look at Class::DBI
    thx ovid for your pointer .. :-))) maksl

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2023-05-31 00:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found