Re: abstract class with Class::DBI

[prev] [thread] [next] [Date index for 2004/10/21]

From: Kingsley Kerce
Subject: Re: abstract class with Class::DBI
Date: 20:01 on 21 Oct 2004
Edward Betts writes:
 > I've got a bunch of activities, they are of different types like e-mail,
 > SMS, and registration. They are represented by an abstract class, called
 > Activity, with subclasses for each type, called ActivityEmail, ActivitySms,
 > and ActivityReg. I'm trying to work out how to store them using Class::DBI.

Class::DBI::Relationship::IsA seems to work fine.  For the following
example, I had to tweak the data model to fit SQLite.

Kingsley

$ cat activity.sql
create table activity (
    id            integer primary key,
    title         varchar(50),
    activitydate  varchar(25)
		       );

create table activity_email (
    activity   integer primary key,
    subject    varchar(80),
    body       text,
    whofrom    varchar(80),
    whoto      varchar(80)
			     );

create table activity_sms (
    activity   integer primary key,
    message    varchar(160)
			   );

create table activity_reg (
    activity    integer primary key,
    purpose     varchar(15)
			   );
$ dbish dbi:SQLite:activity.db < activity.sql
DBI::Shell 11.93 using DBI 1.42 in batch mode
Connecting to 'dbi:SQLite:activity.db' as ''...
[0E0 rows affected] 
[0E0 rows affected] 
[0E0 rows affected] 
[0E0 rows affected] 
Use of uninitialized value in chomp at /usr/lib/perl/lib/site_perl/5.8.3/DBI/Shell.pm line 650, <STDIN> line 23.
Disconnecting from dbi:SQLite:activity.db.
$ cat activity
#!/usr/bin/perl -w

package ActivityDBI;
use base 'Class::DBI';
use Class::DBI::Loader;

Class::DBI::Loader->new(
			dsn => "dbi:SQLite:activity.db",
			user => "",
			password => "",
			namespace => "",
			);

package Activity;
use base 'ActivityDBI';

__PACKAGE__->add_relationship_type(is_a => 'Class::DBI::Relationship::IsA');

foreach my $package (qw/Email Sms Reg/) {
  eval "
    package Activity$package;
    use base 'Activity';

    __PACKAGE__->is_a(activity => 'Activity');
  ";
  die $@ if $@;
}

package main;

use strict;
use warnings;

ActivityEmail->create({
  title	=> 'An email activity',
  activitydate => '2004-10-21',
  subject => 'the subject says it all',
  body => 'body of evidence',
  whofrom => 'Squeaky',
  whoto => 'Desmond',
});

ActivityEmail->create({
  title	=> 'Another email activity',
  activitydate => '2004-10-19',
  subject => 'CDBI',
  body => 'rocks',
  whofrom => 'Desmond',
  whoto => 'Squeaky',
});

print '-' x 5, "\n";
print "Email:\n";
print '-' x 5, "\n";

foreach my $activity_email (ActivityEmail->retrieve_all) {
  foreach my $field (ActivityEmail->columns) {
    print $field, ': ', $activity_email->$field, "\n";
  }
  print '-' x 5, "\n";
}

print "SMS:\n";
print '-' x 5, "\n";
ActivitySms->create({
  title	=> 'An SMS activity',
  activitydate => '2004-09-18',
  message => 'CDBI has an IsA relationship module',
});

foreach my $activity_sms (ActivitySms->search(activitydate => '2004-09-18')) {
  foreach my $field (ActivitySms->columns) {
    print $field, ': ', $activity_sms->$field, "\n";
  }
  print '-' x 5, "\n";
}

print "Reg:\n";
print '-' x 5, "\n";

ActivityReg->create({
  title	=> 'A reg activity',
  activitydate => '2002-08-07',
  purpose => 'not much of one',
});

ActivityReg->create({
  title	=> 'Another reg activity',
  activitydate => '2002-08-08',
  purpose => 'not much of two',
});

ActivityReg->create({
  title	=> 'Yet another reg activity',
  activitydate => '2001-03-02',
  purpose => 'too much of one thing',
});

foreach my $activity_reg (ActivityReg->search_like(purpose => 'not much%')) {
  foreach my $field (ActivityReg->columns) {
    print $field, ': ', $activity_reg->$field, "\n";
  }
  print '-' x 5, "\n";
}

foreach my $activity_reg (ActivityReg->search_like(purpose => 'too much%')) {
  foreach my $field (ActivityReg->columns) {
    print $field, ': ', $activity_reg->$field, "\n";
  }
  print '-' x 5, "\n";
}

$ ./activity
-----
Email:
-----
body: body of evidence
whofrom: Squeaky
subject: the subject says it all
activity: 1
whoto: Desmond
activitydate: 2004-10-21
title: An email activity
-----
body: rocks
whofrom: Desmond
subject: CDBI
activity: 2
whoto: Squeaky
activitydate: 2004-10-19
title: Another email activity
-----
SMS:
-----
activity: 3
message: CDBI has an IsA relationship module
activitydate: 2004-09-18
title: An SMS activity
-----
Reg:
-----
purpose: not much of one
activity: 4
activitydate: 2002-08-07
title: A reg activity
-----
purpose: not much of two
activity: 5
activitydate: 2002-08-08
title: Another reg activity
-----
purpose: too much of one thing
activity: 6
activitydate: 2001-03-02
title: Yet another reg activity
-----

(message missing)

abstract class with Class::DBI
Edward Betts 15:09 on 20 Oct 2004

Re: abstract class with Class::DBI
Tom Hukins 16:40 on 20 Oct 2004

Re: abstract class with Class::DBI
William McKee 13:00 on 21 Oct 2004

Re: abstract class with Class::DBI
Tom Hukins 14:17 on 21 Oct 2004

Re: abstract class with Class::DBI
Perrin Harkins 16:09 on 22 Oct 2004

Re: abstract class with Class::DBI
Tony Bowden 14:41 on 21 Oct 2004

Re: abstract class with Class::DBI
William McKee 18:54 on 21 Oct 2004

Re: abstract class with Class::DBI
Michael 19:17 on 21 Oct 2004

Re: abstract class with Class::DBI
Andreas Fromm 07:00 on 21 Oct 2004

Re: abstract class with Class::DBI
William McKee 19:42 on 21 Oct 2004

Re: abstract class with Class::DBI
Kingsley Kerce 20:01 on 21 Oct 2004

Re: abstract class with Class::DBI
Perrin Harkins 16:14 on 22 Oct 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52