[Dbix-class] Re: Blessed objects as scalar values: what to do ?

Dami Laurent (PJ) laurent.dami at justice.ge.ch
Thu Nov 6 15:08:12 GMT 2008


 

>-----Message d'origine-----
>De : BUCHMULLER Norbert [mailto:norbi.lists at nix.hu] 
>Envoyé : jeudi, 6. novembre 2008 10:44
>À : dbix-class at lists.scsys.co.uk
>Objet : Re: [Dbix-class] update on DBIC / SQLA1.50 tests
>
>Hi,
>
>I've got an idea, but maybe I'm wrong: shouldn't we pass 
>through blessed
>objects in bind values unmodified (ie. without 
>stringification)? That way
>the user of SQLA can still decide what to do with them - in my 
>opinion the
>responsibility of SQLA is just to assemble the SQL string and data
>structure that holds the bind values, but not converting the 
>bind values
>(unless it's absolutely necessary).

>My proposed patch did pass them through, and it passed all 
>tests, but now
>yours explicity stringifies them (and SQLA explodes if such an 
>object is
>passed that has no stringification overload). I don't insist on my
>approach, just wanted to make sure that there are valid reasons why we
>should sacrifice flexibility.
>
>norbi
>

Hi, 

That's a question of defensive programming.

SQLA decides what to do on the basis of the 'refkind' of its arguments (is it a hashref, a scalarref, a plain scalar, etc.). If the argument is an object reference, what should be done ? My initial view was that this most probably an error from the client, so SQLA should complain.

Then your proposal was to treat it exactly like a scalar, and pass it through to DBI and then to the DBD driver. As far as SQLA is concerned, this could be OK, but then there are chances that this would result in strange errors in DBI or DBD::*, that would be harder to understand.

Therefore my proposal was to say : OK, let's treat objects like scalars, but only if they have a method to effectively transform themselves into a scalar. This makes it likely that such objects can indeed be inserted into databases.

I don't know, maybe this view is too strong ... but on the other hand, do you have any real-world use cases where object references are passed to DBI ?

If we really want more flexibility, maybe what could be done is to add a new option in the SQLA contructor
  SQL::Abstract->new(value_classes => [qw/Foo::Bar, etc/])
In that scenario, object references would be accepted as scalar values if and only if they belong to one of the given classes (or descendants).

What do you think ?

Laurent D.



More information about the DBIx-Class mailing list