[Dbix-class] arbitrary table names - best practice?
Matt S Trout
dbix-class at trout.me.uk
Thu May 29 11:19:42 BST 2008
On Mon, May 12, 2008 at 05:51:00PM -0700, James Kiser wrote:
> I have a database that contains a few thousand tables that all share
> the same structure. They are all named as [db].[table][unique
> number]. The unique number is basically the primary key to another
> table which is used to define the 'relationship'.
>
> Obviously, I don't want to create a class for each table and would
> ideally like to have one class that will dynamically use the
> appropriate table based on the primary key (unique number) that I
> have. The right answer is to redesign the schema and there are plans
> to do this. However, I need a solution I can work with until those
> changes take place.
I don't think that's what you want.
What you want is one *result source* per table, but only one class. So.
Create a class My::Schema::Foo as normal. set ->table to '__VIRTUAL__' or
something.
Then, after load_classes()/load_namespaces(),
my $source = __PACKAGE__->source('Foo'); # get the resultsource created by the Foo class
foreach my $i (0 .. $max_table_numer) {
my $numbered_source = $source->new($source);
$numbered_source->table("foo_${i}");
__PACKAGE__->register_source("Foo_${i}" => $numbered_source);
}
Then your code can just do $schema->resultset("Foo_123") or whatever.
--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/
More information about the DBIx-Class
mailing list