Brother monks, I seek your wisdom.

I am trying to write a DBIx::Class based script that runs queries on a table in a MySQL database where the name of the table is not known till runtime. The the table definition (the columns and their types) is known beforehand.

I thought I could just make use of the __PACKAGE__->table('some_name'); hook defined in DBIx::Class::Table, to set the name of the table at runtime, but that does not work. I wrote:

package MyCompany::Schema::LogEntry; use base 'DBIx::Class::Core'; __PACKAGE__->add_columns( 'id' => { data_type=>'int', is_auto_increment=>1 +}, # Other rows. (deleted) ); __PACKAGE__->set_primary_key('id');

My main script looks like:

use Getopt::Long; use MyCompany::Schema::LogEntry; GetOptions( $opts, 'help|h','man','verbose+', 'quiet', 'dbic_dsn=s', 'dbic_user=s', 'dbic_password=s', 'db_table=s', ) or pod2usage(-verbose=>0, -message => "incorrect options"); # Change the log table to the real one. MyCompany::Schema::LogEntry->table( $opts->{'db_table'} ); my $log_schema = MyCompany::Schema->connect( @{$opts}{'dbic_dsn','dbi +c_user','dbic_password'} ) my $matching_log_RS = $log_schema->resultset('LogEntry')->search({%sea +rchTerms}); print $matching_log_RS->as_query();

I intended to call MyCompany::Schema::LogEntry->table() at runtime, but the code above would not compile. I got the error message:

DBIx::Class::Schema::throw_exception(): Can't locate object method "re +sult_source_instance" via package "MyCompany::Schema::LogEntry" at /u +sr/local/lib/perl/5.10.1/DBIx/Class/ResultSourceProxy.pm line 37.

I then thought, I could give the table a dummy name, and then change it later, so I added __PACKAGE__->table('__CHANGE_ME__'); to the source just before the add_columns call.

That still did not work, as internally DBIx::Class had stored the dummy table name, and not updated to the replacement name. The query returned by $matching_log_RS->as_query(); on the last line of my main program refers to the dummy table name rather than the updated one.

Can you offer any help or assistance?


In reply to DBIx::Class - Define a table name at runtime by chrestomanci

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.