Mapping Db Fields to Class Attributes

Depends on how complex your code is. I tried doing this once in Java, to
deal with really atrocious field names in the database (as far as i could
tell, their basic naming scheme was to remove all vowels except the silent
ones, but it was about as consistent as English). That idea went out the
window when i found myself trying to create objects that had over 300
fields! (the relational design was no better than the naming scheme). I
wound up just using their icky names.

One thing i CAN tell you is to write a code generator to read the table
DDLs and crank out wrapper classes that way (as the Pragmatic Programmers
would tell you, too). If you want to map better variable names over the
original database names, write them up as text metadata and feed that into
your code generator. But… The bigger problem i’ve always encountered on
sufficiently complex data is the “object-relational impedance mismatch”,
in which the difference between a good object model and a good relational
model for the same problem is enough to make things awkward. There is
lots of good discussion of this problem on the C2 wiki and elsewhere on
the web.

“Matthew, Graeme” Graeme.Matthew@mercer.com
09/19/02 05:59 PM
Please respond to ruby-talk

···

To: ruby-talk@ruby-lang.org (ruby-talk ML)
cc:
Subject: Mapping Db Fields to Class Attributes

Hi All, thanks who helped me yesterday …

I am currently designing my “object model” for a project management system
for software projects. One problem I have always encountered irrespective
of
wehter the language is Perl, Python or Ruby is the mapping of database
fields to class attributes. For example if I do a mysql db call and load a
hash for a specific tuple, in most cases the db table names will not be in
user firendly naming format eg prj_acc_id or prj_name, it would be silly
to
use these names as accesor’s in a class.

Has anyone come up with an easy yet effective way of doing this ? I was
thinking of having a corresponding hash that mapped the field name to the
attribute name

I looked at ruby’s alias option but am not sure if this is the appropriate
item to use as an alternative to hash mapping ?

Any advice would be most welcome

Graeme Matthew
Analyst Programmer
Mercer Investment Consulting
Level 29, 101 Collins Street, Melbourne, VIC, 3001, Australia
Tel - 61 3 9245 5352 Fax - 61 3 9245 5330
visit http://www.merceric.com

__


This e-mail and any attachments may be confidential or legally privileged.
If you received this message in error or are not the intended recipient,
you
should destroy the e-mail message and any attachments or copies, and you
are
prohibited from retaining, distributing, disclosing or using any
information
contained herein. Please inform us of the erroneous delivery by return
e-mail.

Thank you for your cooperation.


ec03/04

With ruby’s reflection you can avoid a code generator but create dynamic
proxys. Pseudo code follows:

class DBObject
def intialize(table, id_col, id_value)
stm = DB.prepare(“select * from #{table} where ? = ?”)
row = stm.execute(id_col, id_value).fetch
@data = {}
row.each_pair do |attr, value|
@data[attr] = value
self.type.module_eval %q{
def #{attr}
return @data[:#{attr}]
end
end
end
end
end

-billy.

···

On Sat, Sep 21, 2002 at 12:04:55AM +0900, David.Stagner@mpls.frb.org wrote:

One thing i CAN tell you is to write a code generator to read the table
DDLs and crank out wrapper classes that way (as the Pragmatic Programmers
would tell you, too). If you want to map better variable names over the
original database names, write them up as text metadata and feed that into
your code generator. […]


Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9