perl-asp mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Philip Mak <>
Subject Object Oriented Databases with MySQL
Date Wed, 13 Jun 2001 23:06:55 GMT
Hello all,

I've been experimenting with a programming technique idea I had that I
call "Object Oriented Databases". The goal of the technique is to make
programming database driven websites easier by allowing the website code
(I use Apache::ASP, which is built on mod_perl) to access the database
contents as objects rather than having to write SQL statements. (The SQL
statements are in the .pm files that define the objects' methods.)

I'm using this technique to develop, a site that hosts
stories written by various online authors. There is a table called
"authors" that has fields such as "name" and "email". There is a table
called "fanfics" containing the stories, which is linked to the "authors"
table by the "aid" (author id number) field.

In perl/MySQL, if I wanted to list all the stories, along with their
authors, I would have to do something like:

my $sth = query("SELECT * FROM fanfics, authors
                 WHERE fanfics.aid = authors.aid
                 ORDER BY fanfics.title");
while (my $row = $sth->fetchrow_hashref) {
  print "<a href=\".$row->{handle}.".html\">".$row->{title}."</a> by".
        "<a href=\"mailto:".$row->{email}."\">".

In an object oriented style, I would do:

use Fanfics;
my @fanfics = Fanfics::loadall();
for my $fanfic (@fanfics) {
  print "<a href=\".$fanfic->handle.".html\">".$fanfic->title."</a> by".
        "<a href=\"mailto:".$fanfic->author->email."\">".

In the object oriented version, the resulting code is simpler (I don't
have to write MySQL statements in there). This simple example doesn't show
the full potential of this technique, because all the fields in the
database are simple ones - I don't have to combine data from multiple rows
in order to get a single piece of information about an entity. would look something like this:

sub loadall {
  my $sth = query("SELECT * FROM fanfics ORDER BY title");
  my @fanfics;
  while (my $row = $sth->fetchrow_hashref) {
    push(@fanfics, bless($row));
  return @fanfics;

sub title {
  my $self = shift;
  if (@_) { $self->{title} = shift }
  return Apache::Util::escape_html($self->{title});

sub author {
  my $self = shift;
  return Author::loadaid($self->{aid});

One of the early problems I'm running into with this style of programming
is that the flexibility of the MySQL queries is somewhat limited. In a
database driven website where the relationships between the database
tables/entities are well defined and known in advance, ad hoc queries are
probably not needed so that the design of the *.pm files can account for
every possible query that may need to be executed.

Has anyone ever done anything like this before? Do you have any comments
about this technique?


-Philip Mak (

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message