When things seem tedious, it’s time to get the computer to do more of the work.

" />
x
Loading
 Loading
Hello, Guest | Login | Register

Clever Class::DBI

When things seem tedious, it’s time to get the computer to do more of the work.

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:

GC::DB::PersonCruise->has_a(person_id => ‘GC::DB::Person’);
GC::DB::Person->has_many(personcruises => ‘GC::DB::PersonCruises’);
GC::DB::PersonCruise->has_a(cruise_id => ‘GC::DB::Cruise’);
GC::DB::Cruise->has_many(personcruises => ‘GC::DB::PersonCruises’);

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:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Developing for GPUs, Cell, and Multi-core CPUs Using a Unified Programming Model
  2. VMware to Present at the Bernstein Virtualization/Tech Long View Conference
  3. AmieStreet.com Launches in Japan
  4. Liferay and Sygnity Form Services Partnership
  5. Ball Aerospace Launches Opticks Open Source Software
Follow Linux Magazine
Rackspace