[Bast-commits] r3932 - in DBIx-Class-Partitioned/1.000/trunk:
lib/DBIx/Class t t/lib/My t/lib/My/Schema
edenc at dev.catalyst.perl.org
edenc at dev.catalyst.perl.org
Sat Jan 12 19:27:13 GMT 2008
Author: edenc
Date: 2008-01-12 19:27:12 +0000 (Sat, 12 Jan 2008)
New Revision: 3932
Added:
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm
Removed:
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partition.pm
DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/ResultSet/
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo1.pm
DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo2.pm
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
DBIx-Class-Partitioned/1.000/trunk/t/test.db
Log:
selecting partition based on row data
Deleted: DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partition.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partition.pm 2008-01-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partition.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -1,9 +0,0 @@
-package DBIx::Class::Partition;
-
-use warnings;
-use strict;
-use base qw/DBIx::Class/;
-
-#__PACKAGE__->mk_accessors(qw/partition/);
-
-1;
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-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/lib/DBIx/Class/Partitioned.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -19,52 +19,70 @@
=head1 SYNOPSIS
- package My::Schema::Foo;
+ package My::Schema::User;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/Partitioned Core/);
- __PACKAGE__->partitions([qw/foo1 foo2/]);
+ __PACKAGE__->partitions(qw/active_user inactive_user/);
+ __PACKAGE__->add_columns(qw/id foo is_expired/);
+ __PACKAGE__->select_partition_via('is_active');
- package My::Schema::Foo1;
- use base qw/DBIx::Class/;
+ sub is_active { shift->is_expired ? 'active_user' : 'inactive_user' }
- __PACKAGE__->load_components(qw/Partition Core/);
- __PACKAGE__->partition_of('Foo');
-
- # [ insert fields here ]
-
- package My::Schema::Foo2;
- use base qw/DBIx::Class/;
-
- __PACKAGE__->load_components(qw/Partition Core/);
- __PACKAGE__->partition_of('Foo');
-
- # [ insert fields here ]
-
# meanwhile in a nearby piece of code...
- my $foo_rs = $schema->resultset('Foo');
- my @rows = $foo_rs->all; # selects rows from foo1 and foo2
- $foo_rs->new({ ... })->insert; # inserts to foo1
+ my $user_rs = $schema->resultset('User');
+ my @rows = $user_rs->all; # selects rows from active_user and inactive_user
+ $user_rs->create({ foo => 'bar', is_active => 1 }); # inserts to active_user
+ $user_rs->create({ foo => 'baz', is_active => 0 }); # inserts to inactive_user
-=for discussion with matt
-
-__PACKAGE__->table(\'...'); # requires patch to DBIC::ResultSourceProxy::Table and DBIC::Schema::deploy
-
-how am I supposed to know what table the object selected from a union belongs to?
-
=cut
__PACKAGE__->mk_classdata('_partitions');
+__PACKAGE__->mk_classdata('select_partition_via');
sub partitions {
my ( $class, @parts ) = @_;
- return $class->_partitions unless @parts;
- $class->resultset_class('DBIx::Class::ResultSet::Partitioned')
- unless $class->resultset_class->isa('DBIx::Class::ResultSet::Partitioned');
- $class->_partitions( \@parts );
+ $class->_partitions( {} );
+ my $union =
+ '(' . join( ' UNION ALL ', map { "SELECT * FROM $_" } @parts ) . ')';
+ my $source = $class->result_source_instance;
+ $source->name( \$union );
+ 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);
+ $class->_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};
+ 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 insert {
+ my $self = shift;
+ my $method = $self->select_partition_via;
+ my $partition_name = $self->$method;
+ $self->result_source( $self->partition($partition_name) );
+ return $self->next::method(@_);
+}
+
=head1 AUTHOR
Eden Cardim, C<< <edenc at shadowcatsystems.co.uk> >>
Modified: DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/t/01-basic.t 2008-01-12 19:27:12 UTC (rev 3932)
@@ -6,22 +6,32 @@
use My::Schema;
my $schema = My::Schema->connect('dbi:SQLite:dbname=t/test.db');
my $test = sub {
- my $foo_rs = $schema->resultset('Foo');
- my $data = { bar => 'bar 11', baz => 'baz 11' };
- isa_ok( $foo_rs->create($data), 'My::Schema::Foo' );
- ok( my $row = $foo_rs->search($data)->single );
- ok( my $foo1_row = $schema->resultset('Foo1')->search($data)->single );
- is( $row->id, $foo1_row->id );
- is_deeply( { $row->get_columns }, { $foo1_row->get_columns } );
+ my $user_rs = $schema->resultset('User');
+ my $data = { foo => 'bar', is_expired => 0 };
+ isa_ok( $user_rs->create($data), 'My::Schema::User' );
+ row_fields_equal(
+ $user_rs->search($data)->single,
+ $schema->resultset('ActiveUser')->search($data)->single,
+ [qw/id foo is_expired/]
+ );
- $foo_rs->_current_partition('Foo2');
- $data = { bar => 'bar 12', baz => 'baz 12' };
- isa_ok( $foo_rs->create($data), 'My::Schema::Foo' );
- ok( $row = $foo_rs->search($data)->single );
- ok( my $foo2_row = $schema->resultset('Foo2')->search($data)->single );
- is( $row->id, $foo2_row->id );
- is_deeply( { $row->get_columns }, { $foo2_row->get_columns } );
+ $data->{is_expired} = 1;
+ isa_ok( $user_rs->create($data), 'My::Schema::User' );
+ row_fields_equal(
+ $user_rs->search($data)->single,
+ $schema->resultset('InactiveUser')->search($data)->single,
+ [qw/id foo is_expired/]
+ );
die 'rollback';
};
eval { $schema->txn_do($test) };
die $@ if $@ !~ 'rollback';
+
+sub row_fields_equal {
+ my ( $row1, $row2, $fields ) = @_;
+ ok($row1);
+ ok($row2);
+ foreach my $field (@$fields) {
+ is( $row1->$field, $row2->$field );
+ }
+}
Copied: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm (from rev 3931, DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo1.pm)
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/ActiveUser.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -0,0 +1,16 @@
+package My::Schema::ActiveUser;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('active_user');
+
+__PACKAGE__->add_column(
+ id => { is_auto_increment => 1, data_type => 'integer' } );
+__PACKAGE__->add_columns(qw/foo is_expired/);
+__PACKAGE__->set_primary_key('id');
+
+1;
Deleted: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo.pm 2008-01-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -1,16 +0,0 @@
-package My::Schema::Foo;
-
-use warnings;
-use strict;
-
-use base qw/DBIx::Class/;
-
-__PACKAGE__->load_components(qw/Partitioned Core/);
-__PACKAGE__->table('(SELECT * FROM foo1 UNION ALL SELECT * FROM foo2)');
-__PACKAGE__->add_column(
- id => { is_auto_increment => 1, data_type => 'integer' } );
-__PACKAGE__->add_columns(qw/bar baz/);
-__PACKAGE__->set_primary_key('id');
-__PACKAGE__->partitions(qw/Foo1 Foo2/);
-
-1;
Deleted: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo1.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo1.pm 2008-01-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo1.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -1,15 +0,0 @@
-package My::Schema::Foo1;
-
-use warnings;
-use strict;
-
-use base qw/DBIx::Class/;
-
-__PACKAGE__->load_components(qw/Partition Core/);
-__PACKAGE__->table('foo1');
-__PACKAGE__->add_column(
- id => { is_auto_increment => 1, data_type => 'integer' } );
-__PACKAGE__->add_columns(qw/bar baz/);
-__PACKAGE__->set_primary_key('id');
-
-1;
Deleted: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo2.pm
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo2.pm 2008-01-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo2.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -1,15 +0,0 @@
-package My::Schema::Foo2;
-
-use warnings;
-use strict;
-
-use base qw/DBIx::Class/;
-
-__PACKAGE__->load_components(qw/Partition Core/);
-__PACKAGE__->table('foo2');
-__PACKAGE__->add_column(
- id => { is_auto_increment => 1, data_type => 'integer' } );
-__PACKAGE__->add_columns(qw/bar baz/);
-__PACKAGE__->set_primary_key('id');
-
-1;
Copied: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm (from rev 3931, DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo2.pm)
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/InactiveUser.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -0,0 +1,16 @@
+package My::Schema::InactiveUser;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Core/);
+__PACKAGE__->table('inactive_user');
+
+__PACKAGE__->add_column(
+ id => { is_auto_increment => 1, data_type => 'integer' } );
+__PACKAGE__->add_columns(qw/foo is_expired/);
+__PACKAGE__->set_primary_key('id');
+
+1;
Copied: DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm (from rev 3931, DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/Foo.pm)
===================================================================
--- DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm (rev 0)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema/User.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -0,0 +1,22 @@
+package My::Schema::User;
+
+use warnings;
+use strict;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/Partitioned Core/);
+__PACKAGE__->table('user');
+__PACKAGE__->partitions(qw/inactive_user active_user/);
+
+__PACKAGE__->add_column(
+ id => { is_auto_increment => 1, data_type => 'integer' } );
+__PACKAGE__->add_columns(qw/foo is_expired/);
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->select_partition_via('is_active');
+
+sub is_active {
+ return shift->is_expired ? 'inactive_user' : '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-12 17:06:08 UTC (rev 3931)
+++ DBIx-Class-Partitioned/1.000/trunk/t/lib/My/Schema.pm 2008-01-12 19:27:12 UTC (rev 3932)
@@ -6,7 +6,7 @@
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes();
-#__PACKAGE__->load_classes(qw/Foo1 Foo2/);
+#__PACKAGE__->load_classes(qw/ActiveUser InactiveUser/);
sub setup_test_data {
my ($self) = @_;
Modified: DBIx-Class-Partitioned/1.000/trunk/t/test.db
===================================================================
(Binary files differ)
More information about the Bast-commits
mailing list