Re: prefixing table names?

[prev] [thread] [next] [Date index for 2004/11/02]

From: Bowen Dwelle
Subject: Re: prefixing table names?
Date: 01:31 on 02 Nov 2004
I replaced my hack in Class::DBI::DATA::Schema::import with something 
slightly better:

                my $prefix = $caller->TABLE_PREFIX() || '';
                $sql =~ s/(CREATE TABLE IF NOT EXISTS) /$+ $prefix/sg;

I also put a sub table() in Email::Store::DBI that would applies the 
prefix in one place, like so:

sub table {
         my ($class, $table_name)  = @_;
         my $prefix = __PACKAGE__->TABLE_PREFIX();
	# sometimes table() is called with a null $table_name
	#   in this case, be sure to leave $table_name null
         $table_name = $prefix.$table_name
		unless( $table_name =~ /^$prefix/
		or $table_name eq '' );
         $class->SUPER::table($table_name)
}
instead of doing this in each object package:
   Email::Store::Mail->table(__PACKAGE__->TABLE_PREFIX() . "mail"); # feh

This only leftover crud is in Email::Store::Entity:

##############
# BOWEN - HACK
my $sql = q{
     SELECT     DISTINCT entity id
     FROM TABLE_PREFIX_addressing
     WHERE name = ?    AND address = ?};
my $prefix = Email::Store::DBI->TABLE_PREFIX();
$sql =~ s/TABLE_PREFIX_/$prefix/sg;
Email::Store::Entity->set_sql(distinct_entity => $sql);
##############

any clues?

Bowen Dwelle wrote:
> The only real ugliness so far is when Class::DBI::DATA::Schema is used 
> to create tables... For now I have this hard-coded, can someone advise 
> how to look in the right place in the calling stack?
> 
>     my $prefix = Email::Store::DBI->TABLE_PREFIX();
>     $sql =~ s/TABLE_PREFIX_/$prefix/sg;
> 
> thx
> 
> -- Bowen
> 
> Bowen Dwelle wrote:
> 
>> Bradley K. Embree wrote:
>>
>>> Could you not set up a TABLE_PREFIX in a parent CDBI class and have 
>>> your other classes inherit from that? The problem with this is 
>>> needing to be extra careful when rolling your own SQL to use the 
>>> prefix where necessary. Of course the __TABLE__ notation provided by 
>>> Ima::DBI makes that easier.
>>>
>>>
>>> package My::CDBI;
>>>
>>> sub TABLE_PREFIX {
>>>     return 'foo_';
>>> }
>>>
>>> package My::CDBI::Bar;
>>>
>>> use base 'My::CDBI';
>>>
>>> __PACKAGE__->table( __PACKAGE__->TABLE_PREFIX() . 'bar' );
>>>
>>>
>>> Hope that makes some sort of sense,
>>>
>>> Brad
>>
>>
>>
>> In this case the app uses set_up_table(), but that seems to do the 
>> same thing.
>>
>> I knew there was a simple answer. Thanks!
>>
> 

(message missing)

prefixing table names?
bowen 15:20 on 15 Oct 2004

Re: prefixing table names?
jason 15:36 on 15 Oct 2004

Re: prefixing table names?
Tony Bowden 18:45 on 15 Oct 2004

SQL Generation ( was Re: prefixing table names?)
jason scott gessner 00:08 on 19 Oct 2004

Re: SQL Generation ( was Re: prefixing table names?)
merlyn (Randal L. Schwartz) 09:46 on 19 Oct 2004

Re: SQL Generation ( was Re: prefixing table names?)
merlyn (Randal L. Schwartz) 10:00 on 19 Oct 2004

Re: prefixing table names?
Tony Bowden 15:36 on 15 Oct 2004

Re: prefixing table names?
Bowen Dwelle 22:05 on 15 Oct 2004

Re: prefixing table names?
Bradley K. Embree 22:16 on 15 Oct 2004

Re: prefixing table names?
Bowen Dwelle 22:48 on 15 Oct 2004

Re: prefixing table names?
Bowen Dwelle 23:20 on 15 Oct 2004

Re: prefixing table names?
Perrin Harkins 16:44 on 15 Oct 2004

FromCGI and Untaint question
Peter Speltz 17:23 on 19 Oct 2004

Re: prefixing table names?
Bowen Dwelle 01:31 on 02 Nov 2004

Re: prefixing table names?
Bowen Dwelle 16:36 on 02 Nov 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52