actualize has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone,

I have happily using Catalyst with DBIx::Class to grab data from tables in my database. However, the documentation in CPAN only shows how to get data out of rows. I hope to avoid hard coding the table names by grabbing them out of the database. Is it possible to use DBIx::Class this way?

-Actualize
  • Comment on Getting a table list from Catalyst/DBIx::Class

Replies are listed 'Best First'.
Re: Getting a table list from Catalyst/DBIx::Class
by kyle (Abbot) on Sep 16, 2008 at 19:31 UTC

    With a $schema object, you can ask for $schema->sources and get a list of every registered source. They may not all be tables, but I'm guessing they probably are.

    I'm guessing also that you can take the list of sources, instantiate each with $rs = $schema->resultset( $moniker ) and interrogate them for their properties—if you're expecting to find some non-tables in the list.

Re: Getting a table list from Catalyst/DBIx::Class
by Your Mother (Archbishop) on Sep 16, 2008 at 20:18 UTC

    Don't use this code in production! But play around with it to explore the issue and you can see how to go up and down the chain of the objects involved in a DBIC model within Cat.

    $c->model($dbic_package)->result_source->schema->storage->dbh->tables();

    (update: 30 June 2009) I realized that the path above is overly verbose, this should be equivalent (note, you want the model name, not any of the result class names; e.g., DBIC, not DBIC::User)-

    $c->model($model_name)->storage->dbh->tables();
Re: Getting a table list from Catalyst/DBIx::Class
by betterworld (Curate) on Sep 16, 2008 at 19:02 UTC
    I hope to avoid hard coding the table names by grabbing them out of the database.

    Do you plan to create the tables dynamically or why don't you want them in your code?

    Maybe DBIx::Class::Schema::Loader can be of help, it is designed to generate a schema from a running database.

      I already have the schema created. So far I am able to use table joins to get the data I need within the database. However, I want to search the data by category. It would be nice if I create a new table, I would only have to update schema files and already have the new table name available in my code.

      -Actualize