I didn't get any feedback on my recent post (Subject: More convention?) so I
went ahead and added support for what I'm calling the "convention manager."
It's in CVS now.

Each RDBO-derived class has a convention manager.  By default, the
convention manager will be used to automagically set any missing primary key
column names, table names, related class names, and foreign/related column
names according to a set of conventions that I decided were reasonable.
(Sorry, no docs yet.  I'm writing them now.)

If you don't like the conventions I chose, you can always subclass the
default convention manager and create your own rules.  If you set everything
explicitly, then the convention manager will never come into play.   You can
also set the convention manager to undef to totally disable it.

Here's a complete example that shows off most of the features of the
convention manager.  Let's start with the schema:

---
CREATE TABLE vendors
(
  id    SERIAL NOT NULL PRIMARY KEY,
  name  VARCHAR(255)
);

CREATE TABLE colors
(
  code  CHAR(3) NOT NULL PRIMARY KEY,
  name  VARCHAR(255)
);

CREATE TABLE products
(
  id        SERIAL NOT NULL PRIMARY KEY,
  name      VARCHAR(255),
  vendor_id INT NOT NULL REFERENCES vendors (id)
);

CREATE TABLE prices
(
  price_id    SERIAL NOT NULL PRIMARY KEY,
  product_id  INT NOT NULL REFERENCES products (id),
  region      CHAR(2) NOT NULL DEFAULT 'US',
  price       DECIMAL(10,2) NOT NULL
);

CREATE TABLE product_colors
(
  id           SERIAL NOT NULL PRIMARY KEY,
  product_id   INT NOT NULL REFERENCES products (id),
  color_code   CHAR(3) NOT NULL REFERENCES colors (code)
);
---

Now onto the Perl classes.  I've omitted "use strict", the "1;" true value
at the end of each class, and the column data type specifications for the
sake of brevity.  (Replace the all the calls to meta->initialize() with
calls to auto_initialize(no_warnings => 1) and the column details would be
filled in automatically as well.)

When looking at this code, note all the things that I've left out: table
names, column mappings, etc.  And yet it all actually works.  You can run
this code for yourself by creating the schema and data found at the __END__
of the convention-test.pl script in CVS:

    rose/modules/Rose-DB-Object/t/sandbox/convention/convention-test.pl

(I'm using a Postgres database; adjust as necessary.)  Then run the
convention-test.pl script to watch it go.

Anyway, the classes:

---

# Rose::DB subclass to handle the db connection
package My::DB;
use base 'Rose::DB';
My::DB->register_db
(
  type     => 'default',
  domain   => 'default',
  driver   => 'Pg',
  database => 'test',
  username => 'postgres',
);

---

# Common RDBO-derived base class for the other objects
package My::Object;
use My::DB;
use base 'Rose::DB::Object';
sub init_db { My::DB->new }

---

package My::Price;
use base 'My::Object';
__PACKAGE__->meta->columns(qw(price_id product_id region price));
__PACKAGE__->meta->foreign_keys(qw(product));
__PACKAGE__->meta->initialize;

---

package My::Vendor;
use base 'My::Object';
__PACKAGE__->meta->columns(qw(id name));
__PACKAGE__->meta->initialize;

---

package My::Color;
use base 'My::Object';
__PACKAGE__->meta->columns(qw(code name));
__PACKAGE__->meta->primary_key_columns('code');
__PACKAGE__->meta->initialize;

---

package My::Product;
use base 'My::Object';
__PACKAGE__->meta->columns(qw(id name vendor_id));
__PACKAGE__->meta->foreign_keys(qw(vendor));
__PACKAGE__->meta->relationships
(
  prices => { type => 'one to many' },
  colors => { type => 'many to many' },
);
__PACKAGE__->meta->initialize;

---

package My::ProductColors;
use base 'My::Object';
__PACKAGE__->meta->columns(qw(id product_id color_code));
__PACKAGE__->meta->foreign_keys(qw(product color));
__PACKAGE__->meta->initialize;

---

I'm not sure how I feel about all this "by convention" stuff, but some
people seem to like it.  And it does save a lot of typing :)  Anyway, it'll
be in 0.77, which I plan to release as soon as I write the docs for the
convention manager stuff.

-John




-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to