One class, multiple tables: create on the fly

4 messages Options
Embed this post
Permalink
Oleg Kostyuk-2

One class, multiple tables: create on the fly

Reply Threaded More More options
Print post
Permalink
Hello all,

I have a lot of similar tables, and need to access them via
DBIx::Class. I don't want create separate module for each table,
because there is lot of tables, and also this will not be DRY. Also, I
can't give DBIx::Class create classes for me on startup, because some
new tables can be created by other parts of system on the fly, and I
need to have possibility to query these new tables too. I searched
thru list for possible solution, and found similar (but not exactly
same) question:
http://www.mail-archive.com/dbix-class@.../msg03062.html
. At the end, mst@ says
(http://www.mail-archive.com/dbix-class@.../msg03068.html):

<cite>
You don't need a modification. Just create a result source object per
table on the fly and register them as using the same class.
</cite>

What is correct way to do this?

Currently, I already have schema model and class for base table (with
all needed columns and relations defined), in Model::DB and
Model::DB::BaseTable, respectively. I want to have some
Model::TableFactory, which will create new result sources, exactly
same as Model::DB::BaseTable, except they should use different tables.
What is correct way to create result sources on the fly and write
Model::TableFactory?

Finally, I want to use TableFactory like this (in Catalyst application):

my $tbl1 = $c->req->params->{table};
my $rs1  = $c->model('TableFactory')->table($tbl1)->search(...)->all;
my $tbl2 = $c->req->params->{table2};
my $rs2  = $c->model('TableFactory')->table($tbl2)->search(...)->count;

Of course, before use $tbk1 and $tbk2, I need to be sure that they
don't have illegal characters, represent existing tables etc etc -
this is skipped for simplicity here.

May be, you can propose different/better solution?
Any help welcome.
Thanks in advance.

--
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Oleg Kostyuk-2

Re: One class, multiple tables: create on the fly

Reply Threaded More More options
Print post
Permalink
Fixes for final code examples:

my $tbl1 = $c->req->params->{table};
my $all  = [ $c->model('TableFactory')->table($tbl1)->search(...)->all ];
my $tbl2 = $c->req->params->{table2};
my $cnt  = $c->model('TableFactory')->table($tbl2)->search(...)->count;

--
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Oleg Kostyuk-2

Re: One class, multiple tables: create on the fly

Reply Threaded More More options
Print post
Permalink
Any thoughts?

--
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Oleg Kostyuk-2

Re: One class, multiple tables: create on the fly

Reply Threaded More More options
Print post
Permalink
It seems that no one sees my messages :)

2009/11/2 Oleg Kostyuk <[hidden email]>:
> Any thoughts?
>
> --
> Sincerely yours,
> Oleg Kostyuk (CUB-UANIC)
>



--
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...