Subscribed unsubscribe Subscribe Subscribe

Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

Data::Mapper 0.01 Released!

I just released Data::Mapper which implements Data Mapper Pattern described in PofEAA. Of course, I know there has been already existing implementation of that pattern: DBIx::ObjectMapper. Despite that, I've done it because I need simpler one for my new project.

Here's the basic usage of this module, it might be changed in the future, though:

use Data::Mapper;
use Data::Mapper::Adapter::DBI;

my $dbh     = DBI->connect($dsn, $username, $password, ...);
my $adapter = Data::Mapper::Adapter::DBI->new({ driver => $dbh });
my $mapper  = Data::Mapper->new({ adapter => $adapter });

# Create
my $data = $mapper->create(user => { name => 'kentaro', age => 34 });

# Retrieve just one item
$data = $mapper->find(user => { name => 'kentaro' });
$data->param('name'); #=> kentaro
$data->param('age');  #=> kentaro

# Search with some conditions
$result = $mapper->search(user => { age => 34 }, { order_by => 'id DESC' });

for my $data (@$result) {
    $data->param('name');
    ...
}

# Update
$data->param(age => 35);
my $sth = $mapper->update($data);
$sth->rows; #=> 1

# Destroy
my $sth = $mapper->delete($data);
$sth->rows; #=> 1

Any suggestions are welcome. Please contact me via GitHub.