dbix-objectmapper
dbix-objectmapper copied to clipboard
An implementation of the Data Mapper pattern (object-relational mapper).
=head1 NAME
DBIx::ObjectMapper - An implementation of the Data Mapper pattern (object-relational mapper).
=head1 SYNOPSIS
Create a engine and a mapper object.
use DBIx::ObjectMapper; use DBIx::ObjectMapper::Engine::DBI;
my $engine = DBIx::ObjectMapper::Engine::DBI->new({ dsn => 'dbi:SQLite:', username => undef, password => undef, });
my $mapper = DBIx::ObjectMapper->new( engine => $engine );
Create a ordinary perl class.
package My::User; use base qw(Class::Accessor::Fast); PACKAGE->mk_accessors(qw(id name));
1;
Get/Define metadata of the table.
my $user_meta = $mapper->metadata->table( 'user' => 'autoload' );
or
use DBIx::ObjectMapper::Metadata::Sugar qw(:all); my $user_meta = $mapper->metadata->table( 'user' => [ Col( id => Int(), PrimaryKey ), Col( name => String(128), NotNull ), ] );
Map the table metadata to the ordinary class.
$mapper->maps( $user_meta => 'My::User' );
Create session. And add My::User object to session object.
my $session = $mapper->begin_session; my $user = My::User->new({ id => 1, name => 'name1' }); $session->add($user);
When the $session is destroyed, the session object send a insert query to the database.
Get a My::User Object.
my $session = $mapper->begin_session; my $user = $session->get( 'My::User' => 1 ); $user->id; $user->name;
=head1 DESCRIPTION
DBIx::ObjectMapper is a implementation of the Data Mapper pattern. And abstraction layer for database access.
Concepts and interfaces of this module borrowed from SQLAlchemy. Lhttp://www.sqlalchemy.org/
=head1 METHODS
=head2 new(%args)
=over 5
=item B
LDBIx::ObjectMapper::Engine
=item B
By default LDBIx::ObjectMapper::Metadata. Set a LDBIx::ObjectMapper::Metadata based object if you want.
=item B<mapping_class>
By default LDBIx::ObjectMapper::Mapper. Set a LDBIx::ObjectMapper::Mapper based object if you want.
=item B<session_class>
By default LDBIx::ObjectMapper::Session. Set a LDBIx::ObjectMapper::Session based class if you want.
=item B<session_attr>
Set a hash reference of counstructor parameters of LDBIx::ObjectMapper::Session. When you call the L<begin_session> method, you get a LDBIx::ObjectMapper::Session object that this option is set up.
=back
=head2 begin_session(%session_option)
Gets a session object instance, and begins session. See the LDBIx::ObjectMapper::Session for more information.
=head2 maps(%map_config)
Sets a configuration of mapping. See the LDBIx::ObjectMapper::Mapper for more information.
=head2 relation( $relation_type => %relation_config )
LDBIx::ObjectMapper::Relation
=head2 metadata()
Returns the metadata object.
=head2 engine()
Returns the engine object.
=head2 mapping_class()
Returns the mapping_class.
=head2 session_class()
Returns the session_class.
=head1 AUTHOR
Eisuke Oishi
=head1 CONTRIBUTORS
nekokak: Atsushi Kobayashi
=head1 COPYRIGHT
Copyright 2010 Eisuke Oishi
=head1 LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.