Egg::Model::DBIC - Model for DBIx::Class.


Egg-Release-DBIC documentation Contained in the Egg-Release-DBIC distribution.

Index


Code Index:

NAME

Top

Egg::Model::DBIC - Model for DBIx::Class.

SYNOPSIS

Top

  my $schema= $e->model('dbic::myschema');

  # If the transaction is effective.
  $schema->storage->txn_begin;

  my $table= $schema->resultset('hoge_master');
     Or
  my $table= $e->model('dbic::myschema::hoge_master');

  $table->search( ... );

  # And.
  $schema->storage->txn_rollback;
     Or
  $schema->storage->txn_commit;

DESCRIPTION

Top

It is MODEL to use DBIx::Class.

A series of Schema module is generated by using the helper for use.

  % cd /path/to/MyApp/bin
  % ./myapp_helper.pl M::DBIC [SCHEMA_NAME] -d dbi:SQLite:dbname=dbfile -u user -p passwd

The name that can be used as Perl module name in the part of SCHEMA_NAME is passed.

The option to continue is not indispensable.

Details are Egg::Helper::Model::DBIC. Please drink and refer to the document.

And, 'DBIC' is added to the MODEL setting of the project.

  % vi /path/to/MyApp/lib/MyApp/config.pm
  ..........
  ...
  MODEL => ['DBIC'],

Using this model by this becomes possible.

When the object of Schema is acquired from the application, as follows is done.

  my $schema= $e->model('dbic::schema_name');

The object to which DBIx::Class::Schema is succeeded to by this can be received.

And, the object of the table does as follows.

  my $table = $e->model('dbic::schema_name::table_name');

  # If you have already acquired the Schema object.
  my $table = $schema->resultset('table_name');

The object to which DBIx::Class::ResultSet is succeeded to by this can be received.

SEE ALSO

Top

Egg::Release, Egg::Model, Egg::Model::DBIC::Base, Egg::Helper::Model::DBIC, DBIx::Class, DBIx::Class::Schema, DBIx::Class::ResultSet, UNIVERSAL::require,

AUTHOR

Top

Masatoshi Mizuno, <lushe&64;cpan.org>

COPYRIGHT AND LICENSE

Top


Egg-Release-DBIC documentation Contained in the Egg-Release-DBIC distribution.

package Egg::Model::DBIC;
#
# Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
#
# $Id: DBIC.pm 324 2008-04-17 12:45:09Z lushe $
#
use strict;
use warnings;

our $VERSION = '3.01';

sub _setup {
	my($class, $e)= @_;
	Egg::Model::DBIC::handler->_setup($e);
	$class->next::method($e);
}

package Egg::Model::DBIC::handler;
use strict;
use UNIVERSAL::require;
use base qw/ Egg::Model /;
use Carp qw/ croak /;

sub _setup {
	my($class, $e)= @_;
	my $dbic_path= $e->path_to(qw{ lib_project Model/DBIC });
	-e $dbic_path || die __PACKAGE__. qq{ - '${dbic_path}'is not found. };
	my $schemas= $e->ixhash;
	no strict 'refs';  ## no critic.
	no warnings 'redefine';
	for (sort (grep /\.pm$/, <$dbic_path/*>)) {  ## no critic.
		m{([^\\\/\:]+)\.pm$} || next;
		my $pkg = $e->project_name. "::Model::DBIC::$1";
		my $name= lc $1;
		$pkg->require or die $@;
		my $c= $pkg->config || die __PACKAGE__. qq{ - '$pkg' config is empty. };
		$c->{dsn} || die __PACKAGE__. q{ - '$pkg' dsn is empty. };
		$c->{user}     ||= "";
		$c->{password} ||= "";
		$c->{options}  ||= {};
		my $label= lc( $c->{label_name} || "dbic::$name" );
		my $alias= $c->{label_source} || $c->{label_moniker} || {};
		   $alias= { map{ lc($_) => $alias->{$_} }keys %$alias };
		$e->model_manager->add_register(0, $label, $pkg);
		*{"${pkg}::new"}= $class->_mk_schema_closure
		     ($pkg, @{$c}{qw{ dsn user password options }});
		my $schema= $pkg->new
		     || die qq{ Schema of '$pkg' cannot be connected. };
		for my $moniker ($schema->sources) {
			my $m_class= "${pkg}::$moniker";
			my $m_label= $alias->{lc $moniker} || "${label}::$moniker";
			$e->model_manager->add_register(0, $m_label, $m_class);
			*{"${m_class}::ACCEPT_CONTEXT"}=
			    sub { $_[1]->model($label)->resultset($moniker) };
		}
		$schemas->{$label}= $pkg;
	}
	%$schemas or die __PACKAGE__. q{ - Schema module is not found. };
	$e->global->{dbic_schemas}= $schemas;
	@_;
}
sub _mk_schema_closure {
	my($class, $s_class, @source)= @_;
	my $schema;
	sub {
		return $schema if ( $schema
		  and $schema->storage->dbh->{Active}
		  and $schema->storage->dbh->ping
		  );
		$schema= $s_class->connect(@source);
	 };
}

1;

__END__