|
Class::Entity - Object interface for relational databases |
Class::Entity - Object interface for relational databases
Class::Entity allows a developer to create an object interface for a relational database by writing a minimal amount of code in a set of sub-classes which correspond to database tables.
Right now this module only implements a read only interface. Writes will probably come later.
package Table; use base qw(Class::Entity);
package main; use DBI; my $dbh = DBI->connect(...); my $table = Table->fetch(dbh => $dbh, key => 1234); print $table->Column;
for (Table->find(dbh => $dbh, where => "Name like 'foo%'")) {
printf "% %\n", $table->Name, $table->Date;
}
_primary_key()The value of this method is used to create the query that is run when a call to fetch is made.
_table()_relation()_object_map()The object map will only be used for method calls of the form get_Value.
Here's an example:
package Users; use base qw(Class::Entity);
package Departments;
use base qw(Class::Entity);
sub _object_map({
UserID => "Users"
})
package main;
use DBI;
my $dbh DBI->connect(...);
my @support = Departments->find(dbh => $dbh, where => "Name = 'Support'");
for (@support) {
printf "%s %s\n", $_->UserID, $_->get_UserID->name;
}
AUTOLOAD([$value])package Table; use base qw(Class::Entity);
package main; use DBI; my $dbh = DBI->connect(...); my $table = Table->fetch(dbh => $dbh, key => 10); print $table->Name . "\n"; print $table->Date . "\n"; print $table->Subject . "\n";
If you call an anonymous method of the form get_Value, where Value is a column represented by the current object, the autoloader will attempt to dispatch the call to the fetch method of the corresponding sub-class of Class::Entity, if it's listed in the _bject_map.
DBI
Paddy Newman, <pnewman@cpan.com>
This is basically a cut-down, slightly modified version of something an ex-colegue of mine wrote and introduced me to. His name is Dan Barlow and he's a much better programmer than me and he deserves all the credit.
|
Class::Entity - Object interface for relational databases |