[Dbix-class] Select From Dual (Again)

Matt S Trout dbix-class at trout.me.uk
Fri Nov 9 19:02:53 GMT 2007


On Fri, Nov 09, 2007 at 04:55:30PM +0000, Richard Cox wrote:
> --- Matt S Trout <dbix-class at trout.me.uk> wrote:
> > On Fri, Nov 09, 2007 at 03:11:25PM +0000, Richard Cox wrote:
> > > So all that's cool but I hit a roadblock if I want to select more than one
> > > column at a time because we only have one 'dummy' column in our table class
> > so
> > > it gets autoviv'd
> > > 
> > > my @rs = $schema->resultset('Dual')->search(undef,
> > >    { select => [ \'sysdate', {initcap => '\'bar\''} ],
> > >      as     => [ 'dummy', 'dummy' ]
> > >    },
> > > );
> > > 
> > > for my $col (@rs) {
> > >    print $col->dummy."\n"; # Bar
> > > }
> > > 
> > > To get round this I've slung together an 'auto column' component which
> > > overrides get_column
> > 
> > Why bother?
> > 
> > as => [ 'sysdate', 'initcap' ]
> > 
> > ...
> > 
> > $obj->get_column('sysdate')
> > $obj->get_column('initcap')
> > 
> > DBIC intentionally doesn't validate the columns passed to inflate_result
> > and intentionally treats _column_data as supreme if the key exists precisely
> > to make this usage work without needing any extra custom code.
> > 
> > or you could sod off having a class at all and just use HashRefInflator so
> > @rs will contain { sysdate => $systdate, initcap => $initcap } in each
> > element.
> > 
> > Both seem simpler to me than adding+removing columns on the fly inside
> > get_column ...
>
> Thanks Matt that's certainly better than my stupid hack which is now in dev
> null, didn't know about HashRefInflator, will take a look

You underestimated the extent to which DBIC assumes you know what you're
doing when you ask it to do weird shit.

This is not stupidity. It's merely the assumption there'd be more of a
safety net than that.

Be aware that this discovery cuts both ways - the day DBIC lets you shoot
yourself in the head on the same assumption you knew what you were doing
you may not like the philosophy quite so much.

Either way, just creating a class for dual if you're going to be using it
heavily is a -lovely- idea.

Once you've finished experimenting, would you please write us up a quick
cookbook recipe over it? I think others would find it most enlightening
and the cookbook's much easier to find than a random thread in the
archives (even if your choice of subject line is pretty damn googlable :)

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list