You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Kee Hinckley <na...@somewhere.com> on 2002/02/14 21:40:15 UTC

A patch for partial LongNames in DBIx::Recordset

I don't know if this is of general interest, or if this is the way you'd
do it.  But for what it's worth.

Currently DBIx::Recordset uses the short names of fields by default. 
So if you do a join and have two fields with the same name, one gets 
lost.  This behavior can be overridden by setting !LongNames to 1, in 
which case you must use the fully table-qualified names to look up 
values.

I wanted something in-between.  I only wanted to use qualified names 
if the name was non-unique.  Now ideally I'd like both to work at the 
same time, but that was a bit much to ask.  So I made the following 
change.  With this patch, setting !LongNames to 2 will cause it to 
use unqualified names when possible, and full names the rest of the 
time.

In case none of that's clear. Given two tables, both with a field
called "id", and one with a field called "name". 

!LongNames = 0
$set{id}	returns one of the ids, no way to tell which
$set{'tab1.id'}	returns null
$set{'name'}	returns name

!LongNames = 1
$set{id}	returns null
$set{'tab1.id'}	returns id
$set{'name'}	returns null

!LongNames = 2
$set{id}	returns null
$set{'tab1.id'}	returns id
$set{'name'}	returns name


One thing that may or may not be "correct" in this patch.  If you set LongNames to 2, and you pass field names to the select (e.g. "$fields = 'ab1.id,name') then I assume you know what you're doing and I just use whatever you passed and do no checking. I believe that's correct, because the underlying SQL engine should complain if you don't specify unique values.

966% diff -c /Library/Perl/DBIx/Recordset.pm DBIx/Recordset.pm  wraith.somewhere.com:data
*** /Library/Perl/DBIx/Recordset.pm     Fri Sep 22 01:50:15 2000
--- DBIx/Recordset.pm   Thu Feb 14 15:26:06 2002
***************
*** 1379,1385 ****
      
  
  
!     if ($self->{'*LongNames'})
          {
          if ($fields eq '*')
            {
--- 1379,1415 ----
      
  
  
!     if ($self->{'*LongNames'} == 2)
!         {
!       # longnames only when required for uniqueness
!         if ($fields eq '*')
!           {
!               my ($val, %names, @result, $i);
!               # first get a count of each name
!               foreach $val (@$names) {
!                   ++$names{$val};
!               }
!               for ($i = 0; $i < @$names; ++$i) {
!                   if ($names{$names->[$i]} > 1) {
!                       # there's more than one, use the full name
!                       push(@result, $self->{'*FullNames'}->[$i]);
!                   } else {
!                       # just use the partial name
!                       push(@result, $names->[$i]);
!                   }
!               }
!               # assign the resul
!               $self->{'*SelectFields'} = \@result;
!           }
!         else
!             {
!           # nothing special, just use the names passed, SQL will do the rest
!             my @allfields = split (/\s*,\s*/, $fields) ;
!             shift @allfields if (lc($allfields[0]) eq 'distinct') ;
!             $self->{'*SelectFields'} = \@allfields ;
!             }
!         }
!     elsif ($self->{'*LongNames'})
          {
          if ($fields eq '*')
            {
-- 

Kee Hinckley - Somewhere.Com, LLC
http://consulting.somewhere.com/
nazgul@somewhere.com

I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: A patch for partial LongNames in DBIx::Recordset

Posted by Gerald Richter <ri...@ecos.de>.
>
> In case none of that's clear. Given two tables, both with a field
> called "id", and one with a field called "name".
>
> !LongNames = 0
> $set{id} returns one of the ids, no way to tell which

It returns the id of the last table you specify in !Table

>

Thanks for the patch, I will put it in the next relase of DBIx::Recordset
(which is really overdue, but Embperl 2.0 takes up all my time at the
moment)

Gerald

-------------------------------------------------------------
Gerald Richter    ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post:       Tulpenstrasse 5         D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de         Voice:    +49 6133 925131
WWW:        http://www.ecos.de      Fax:      +49 6133 925152
-------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org