[Bast-commits] r3947 - in DBIx-Class-Partitioned/1.000/trunk:
lib/DBIx/Class lib/DBIx/Class/ResultSource
lib/DBIx/Class/ResultSource/Table lib/DBIx/Class/Schema t t/lib/My
edenc at dev.catalyst.perl.org
edenc at dev.catalyst.perl.org
Wed Jan 16 23:27:47 GMT 2008
Author: edenc
Date: 2008-01-16 23:27:47 +0000 (Wed, 16 Jan 2008)
New Revision: 3947
Added:
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partition.pm
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partitioned.pm
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Schema/
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Schema/Partitioned.pm
DBIx-Class-Partitioned/1.000/trunk/t/02-restrict_schema.t
Removed:
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSet/
Modified:
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm
DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm
Log:
added source restriction
Modified: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-16 18:16:06 UTC (rev 3946)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-16 23:27:47 UTC (rev 3947)
@@ -3,7 +3,6 @@
use warnings;
use strict;
use Storable ();
-use DBIx::Class::ResultSet::Table::Partition;
use base qw/DBIx::Class/;
=head1 NAME
@@ -40,49 +39,19 @@
=cut
-__PACKAGE__->mk_classdata('_partitions');
__PACKAGE__->mk_group_accessors(
'simple' => qw/_current_partition is_partition_row/ );
-sub partitions {
- my ( $class, @parts ) = @_;
- $class->_partitions( {} );
- my $source = $class->result_source_instance;
- foreach my $partition (@parts) {
- my $new_source = $source->new($source);
- my $new_source_name = join '::', map { ucfirst } split '_', $partition;
- $new_source->source_name($new_source_name);
- $new_source->name($partition);
- $new_source->_relationships(
- Storable::dclone( $new_source->_relationships ) );
- $new_source->relationship_info($_)->{attrs}{cascade_delete} = 0
- for $new_source->relationships;
- $class->_partitions->{$partition} = $new_source;
- }
-
-# $class->ensure_class_loaded('DBIx::Class::ResultSource::Table::Partitioned');
-# $class->table_class();
- $class->ensure_class_loaded('DBIx::Class::ResultSet::Partitioned');
- $class->resultset_class('DBIx::Class::ResultSet::Partitioned')
- unless $class->can('DBIx::Class::ResultSet::Partitioned');
+sub table {
+ my $class = shift;
+ my $table_class = 'DBIx::Class::ResultSource::Table::Partitioned';
+ $class->ensure_class_loaded($table_class), $class->table_class($table_class)
+ unless $class->table_class->isa($table_class);
+ $class->next::method(@_);
}
-sub partition {
- my $self = shift;
- if (@_) {
- my ($moniker) = @_;
- my $partitions = $self->_partitions;
- return unless exists $partitions->{$moniker};
- my $partition_source = $partitions->{$moniker};
- my $partition_source_name = $partition_source->source_name;
- my $schema = $self->result_source->schema;
- if ( !exists $schema->source_registrations->{$partition_source_name} ) {
- $schema->register_source(
- $partition_source_name => $partition_source );
- }
- return $partition_source;
- }
- return;
+sub partitions {
+ shift->result_source_instance->partitions(@_);
}
sub new {
@@ -109,7 +78,7 @@
&& !$self->is_partition_row
&& ( my $part = $self->_partition_name ) )
{
- my $partition = $self->partition($part)
+ my $partition = $self->result_source->partition($part)
or confess("couldn't find partition '${part}'");
my $row = $partition->resultset->new_result( { $self->get_columns } );
$row->is_partition_row(1);
@@ -132,7 +101,7 @@
if ( $old_part ne $new_part ) {
$part_row->delete;
my $row =
- $self->partition($new_part)
+ $self->result_source->partition($new_part)
->resultset->new_result( { $self->get_columns } );
$row->is_partition_row(1);
$row->insert;
@@ -145,7 +114,7 @@
}
elsif ( !$old_part && $new_part ) {
my $row =
- $self->partition($new_part)
+ $self->result_source->partition($new_part)
->resultset->new_result( { $self->get_columns } );
$row->is_partition_row(1);
$row->insert;
@@ -153,7 +122,7 @@
}
elsif ( $old_part && !$new_part ) {
my $row =
- $self->partition($old_part)
+ $self->result_source->partition($old_part)
->resultset->find( { $self->get_columns } );
$row->is_partition_row(1);
$row->delete;
Added: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partition.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partition.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partition.pm 2008-01-16 23:27:47 UTC (rev 3947)
@@ -0,0 +1,22 @@
+package DBIx::Class::ResultSource::Table::Partition;
+
+use warnings;
+use strict;
+
+use Scalar::Util ();
+
+use base qw/DBIx::Class::ResultSource::Table/;
+
+__PACKAGE__->mk_group_accessors( simple => qw/original_source/ );
+
+sub new {
+ my $self = shift->next::method(@_);
+ Scalar::Util::weaken( $self->{original_source} );
+ return $self;
+}
+
+sub partition {
+ return shift->original_source->partition(@_);
+}
+
+1;
Added: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partitioned.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partitioned.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSource/Table/Partitioned.pm 2008-01-16 23:27:47 UTC (rev 3947)
@@ -0,0 +1,54 @@
+package DBIx::Class::ResultSource::Table::Partitioned;
+
+use warnings;
+use strict;
+
+use DBIx::Class::ResultSource::Table::Partition;
+use base qw/DBIx::Class::ResultSource::Table/;
+
+__PACKAGE__->mk_group_accessors( 'simple' => qw/_partitions _is_registered/ );
+
+sub schema {
+ my $self = shift;
+ my ($schema) = @_;
+ # ugh...
+ if ( !$self->_is_registered && ref $schema) {
+ foreach my $partition_source ( values %{ $self->_partitions } ) {
+ $schema->register_source(
+ $partition_source->source_name => $partition_source );
+ }
+ $self->_is_registered(1);
+ }
+ return $self->next::method(@_);
+}
+
+sub partitions {
+ my ( $self, @parts ) = @_;
+ $self->_partitions( {} );
+ foreach my $partition (@parts) {
+ my $new_source =
+ DBIx::Class::ResultSource::Table::Partition->new($self);
+ my $new_source_name = join '::', map { ucfirst } split '_', $partition;
+ $new_source->source_name($new_source_name);
+ $new_source->name($partition);
+ $new_source->_relationships(
+ Storable::dclone( $new_source->_relationships ) );
+ $new_source->relationship_info($_)->{attrs}{cascade_delete} = 0
+ for $new_source->relationships;
+ $self->_partitions->{$partition} = $new_source;
+ }
+}
+
+sub partition {
+ my $self = shift;
+ if (@_) {
+ my ($moniker) = @_;
+ my $partitions = $self->_partitions;
+ return unless exists $partitions->{$moniker};
+ my $partition_source = $partitions->{$moniker};
+ return $partition_source;
+ }
+ return;
+}
+
+1;
Added: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Schema/Partitioned.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Schema/Partitioned.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Schema/Partitioned.pm 2008-01-16 23:27:47 UTC (rev 3947)
@@ -0,0 +1,30 @@
+package DBIx::Class::Schema::Partitioned;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components('Schema::RestrictWithObject');
+
+sub source {
+ my $self = shift;
+ my $source = $self->next::method(@_);
+ if ( $self->can('restricting_object')
+ && ( my $obj = $self->restricting_object ) )
+ {
+ my $s = $source->source_name;
+ $s =~ s/::/_/g;
+ my $pre = $self->restricted_prefix;
+ my $meth = "restrict_${s}_source";
+
+ #if a prefix was set, try that first
+ if ($pre) {
+ my $meth_pre = "restrict_${pre}_${s}_source";
+ return $obj->$meth_pre($source) if $obj->can($meth_pre);
+ }
+ $source = $obj->$meth($source) if $obj->can($meth);
+ }
+ return $source;
+}
+
+1;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-16 18:16:06 UTC (rev 3946)
+++ DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-16 23:27:47 UTC (rev 3947)
@@ -7,7 +7,6 @@
my $schema = My::Schema->connect('dbi:SQLite:dbname=t/test.db');
my $test = sub {
my $user_rs = $schema->resultset('User');
- $user_rs->partition('active_user');
my $data = { foo => 'bar', is_expired => 0 };
isa_ok( my $user = $user_rs->create($data), 'My::Schema::User' );
$user->add_to_affiliations( { name => 'foo' } );
@@ -44,15 +43,9 @@
$user->update;
ok( !$schema->resultset('ActiveUser')->search( { $user->get_columns } )
->count );
- ok( !$user_rs->search( { $user->get_columns } )->count );
- $user_rs->partition('');
+ ok( !$user_rs->result_source->partition('active_user')
+ ->resultset->search( { $user->get_columns } )->count );
ok( $user_rs->search( { $user->get_columns } )->count );
- $user_rs->partition('active_user');
- is(
- $schema->resultset('UserAffiliation')->search( {}, { join => 'user' } )
- ->count,
- 1
- );
die 'rollback';
};
eval { $schema->txn_do($test) };
Added: DBIx-Class-Partitioned/1.000/trunk/t/02-restrict_schema.t
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/02-restrict_schema.t (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/02-restrict_schema.t 2008-01-16 23:27:47 UTC (rev 3947)
@@ -0,0 +1,24 @@
+use warnings;
+use strict;
+
+use lib 't/lib';
+
+use Test::More qw/no_plan/;
+use My::Schema;
+my $schema = My::Schema->connect('dbi:SQLite:dbname=t/test.db');
+isa_ok(
+ my $restricted =
+ $schema->restrict_with_object( bless {}, 'Test::RestrictingObject' ),
+ 'DBIx::Class::Schema::RestrictWithObject::RestrictComp::Schema'
+);
+is( $restricted->source('User')->from, 'active_user' );
+is( $restricted->source('Affiliation')->from, 'affiliation' );
+
+package Test::RestrictingObject;
+
+sub restrict_User_source {
+ my ( $self, $source ) = @_;
+ return $source->partition('active_user');
+}
+
+1;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm 2008-01-16 18:16:06 UTC (rev 3946)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm 2008-01-16 23:27:47 UTC (rev 3947)
@@ -4,8 +4,9 @@
use strict;
use base qw/DBIx::Class::Schema/;
+__PACKAGE__->load_components(qw/Schema::Partitioned/);
+__PACKAGE__->load_classes();
-__PACKAGE__->load_classes();
#__PACKAGE__->load_classes(
# qw/UserIdent ActiveUser InactiveUser UserAffiliation Affiliation Post/);
More information about the Bast-commits
mailing list