When things seem tedious, it’s time to get the computer to do more of the work.
Tuesday, February 1st, 2005
Share This:
Recently, I was contracted by Geekcruises (http://www.geekcruises.com) to update their online booking and administration system, which currently uses a simple CGI architecture and a lot of hand-rolled SQL to talk to their PostgreSQL database.
My first design decision was to adopt a more generic, flexible web abstraction, creating CGI::Prototype as a result. (Geekcruises allowed me to upload that to the CPAN, and I’ll be writing more about that in the future.) For the “model” part of the abstraction, I settled in on the wildly popular Class::DBI framework. And that brings us to today’s problem and solution.
The existing Geekcruises database consists of fifty tables, with typical foreign key mappings. For example, a person_cruise record links a person with a cruise. In Class::DBI, setting up these foreign key relationships so that they could be easily accessed looked simple enough: I’d just have to set up a bunch of statements like:
And that was fine — for the first ten or fifteen records. Then it started to get boring. And then tedious. And then error-prone. I started to think, “Hey, wait a minute! PostgreSQL knows these relationships! Why am I reentering that information?” And then I thought, “Oh, someone else must’ve done this already! ”
When I started looking in the CPAN for some reusable solutions, I quickly found Class::DBI::Loader. Joy! This module solves the problem of querying the database for all of the records…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: