DBIx::DataModel 0.21 review

by rbytes.net on

DBIx::DataModel is a Perl module with Classes and UML-style Associations on top of DBI. SYNOPSIS in file "MySchema.pm" Decla

License: Perl Artistic License
File size: 41K
Developer: Laurent Dami
0 stars award from rbytes.net

DBIx::DataModel is a Perl module with Classes and UML-style Associations on top of DBI.


in file "MySchema.pm"

Declare the schema
use DBIx::DataModel;
DBIx::DataModel->Schema('MySchema'); # MySchema is now a Perl package

Declare the tables with (Perl name, DB name, primary key column(s)). Each table then becomes a Perl package.

MySchema->Table(qw/Employee Employee emp_id/);
MySchema->Table(qw/Department Department dpt_id/);
MySchema->Table(qw/Activity Activity act_id/);
Declare associations in UML style ( [table1 role1 multiplicity1 join1], [table2...]).
MySchema->Association([qw/Activity activities * emp_id/],
[qw/Employee employee 1 emp_id/]);
MySchema->Association([qw/Activity activities * dpt_id/],
[qw/Department department 1 dpt_id/]);
Declare a n-to-n association, on top of the linking table
MySchema->Association([qw/Department departments * activities department/]);
[qw/Employee employees * activities employee/]);
Declare "column types" with some handlers ..
# date conversion between database (yyyy-mm-dd) and user (dd.mm.yyyy)
MySchema->ColumnType(Date =>
fromDB => sub {$_[0] =~ s/(dddd)-(dd)-(dd)/$3.$2.$1/},
toDB => sub {$_[0] =~ s/(dd).(dd).(dddd)/$3-$2-$1/},
validate => sub {$_[0] =~ m/(dd).(dd).(dddd)/});

# 'percent' conversion between database (0.8) and user (80)
MySchema->ColumnType(Percent =>
fromDB => sub {$_[0] *= 100 if $_[0]},
toDB => sub {$_[0] /= 100 if $_[0]},
validate => sub {$_[0] =~ /1?d?d/});
.. and apply these "column types" to some of our columns
Employee->ColumnType(Date => qw/d_birth/);
Activity->ColumnType(Date => qw/d_begin d_end/);
Activity->ColumnType(Percent => qw/activity_rate/);
Declare a column that will be filled automatically at each update
MySchema->AutoUpdateColumns(last_modif =>
sub{$ENV{REMOTE_USER}.", ".scalar(localtime)});

For details that could not be expressed in a declarative way, just add a new method into the table class (but in that case, Schema and Table declarations should be in a BEGIN block, so that the table class is defined before you start adding methods to it).

package Activity;

sub activePeriod {
my $self = shift;
$self->{d_end} ? "from $self->{d_begin} to $self->{d_end}"
: "since $self->{d_begin}";

Declare how to automatically expand objects into data trees
Activity->AutoExpand(qw/employee department/);


DBIx::DataModel 0.21 search tags