SQL::Abstract - boolean test support - should this merge?

3 messages Options
Embed this post
Permalink
Nigel Metheringham

SQL::Abstract - boolean test support - should this merge?

Reply Threaded More More options
Print post
Permalink
Boolean tests using SQLA (and hence DBIC) have always been a bit of a
pain - you can do a test for a true value using the hackish
     { boolcolumn => \'' }

which produces
     WHERE boolcolumn

but there has been no reasonable alternative that would allow you to
produce
     WHERE NOT boolcolumn


A few months back, I finally got around to building better support for
boolean tests in SQL::Abstract - you can see the branch at
     http://dev.catalyst.perl.org/svnweb/bast/browse/SQL-Abstract/1.x/branches/bool_operator/

This implements the following syntax:-
     { -bool => boolcolumn }
         WHERE boolcolumn

     { -not_bool => boolcolumn }
         WHERE NOT boolcolumn

     { -and => [-bool => 'foo', -bool => 'bar'] }
         WHERE foo AND bar

     { -bool => \'function(x)' }
         WHERE function(x)

This is slightly back to front compared to some other syntax, but
allows other syntax extensions - for example NOT of a subselect etc.

A side effect of this work is a significant re-factor of the SQLA code
handling unary (or possibly a better name would be prefix) operators,
which allows for subsequent expansion of those operators (currently the
operators are -and -or -nest).

So the big decision now is should this work be merged into the SQLA
mainline. That at least partly depends on whether ash, as master of
SQLA-v2 loves or hates it (no point adding syntax that he will prune).

If the -bool/-not_bool part is not merged, should the refactor be taken
in anyhow?

There is an alternative solution to the *basic* boolean SQL test, which
is to add it as a normal binary operation - which could be done like
this:-

     my $sqla = SQL::Abstract->new(
         special_ops => [
             {
                 regex   => qr/bool/,
                 handler => sub {
                     my ( $self, $field, $op, $arg ) = @_;
                     return ( $arg ? $field : "NOT $field" );
                 },
             },
         ]
     );

and leads to syntax like this:-
     { foo => {-bool, 1}, bar => {-bool, 0}}
         WHERE foo AND NOT bar

This will have some issues (subselects and parameterised functions
etc), although literal functions should work.

It is quite possible to add both forms of syntax (although this may
confuse the hell out of users unless we change the naming of one of
them)

So comments and votes (but this ain't a democracy) are welcome on the
best way to take this forward...

     Nigel.

[Just to be clear - none of this should break existing code - the test
suite all still passes]

--
[ Nigel Metheringham             [hidden email] ]
[ - Comments in this message are my own and not ITO opinion/policy - ]


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Peter Rabbitson-2

Re: SQL::Abstract - boolean test support - should this merge?

Reply Threaded More More options
Print post
Permalink
This is just to record my vote, which I shared with Nigel already.

Nigel Metheringham wrote:

> Boolean tests using SQLA (and hence DBIC) have always been a bit of a
> pain - you can do a test for a true value using the hackish
>     { boolcolumn => \'' }
>
> which produces
>     WHERE boolcolumn
>
> but there has been no reasonable alternative that would allow you to
> produce
>     WHERE NOT boolcolumn
>
>
> A few months back, I finally got around to building better support for
> boolean tests in SQL::Abstract - you can see the branch at
>    
> http://dev.catalyst.perl.org/svnweb/bast/browse/SQL-Abstract/1.x/branches/bool_operator/
>
>
> This implements the following syntax:-
>     { -bool => boolcolumn }
>         WHERE boolcolumn
>
>     { -not_bool => boolcolumn }
>         WHERE NOT boolcolumn
>
>     { -and => [-bool => 'foo', -bool => 'bar'] }
>         WHERE foo AND bar
>
>     { -bool => \'function(x)' }
>         WHERE function(x)
>
> This is slightly back to front compared to some other syntax, but
> allows other syntax extensions - for example NOT of a subselect etc.
>
> A side effect of this work is a significant re-factor of the SQLA code
> handling unary (or possibly a better name would be prefix) operators,
> which allows for subsequent expansion of those operators (currently the
> operators are -and -or -nest).
>
> So the big decision now is should this work be merged into the SQLA
> mainline. That at least partly depends on whether ash, as master of
> SQLA-v2 loves or hates it (no point adding syntax that he will prune).
>
> If the -bool/-not_bool part is not merged, should the refactor be taken
> in anyhow?

Probably not, the -and/-or machine is stable (albeit weird) the way it is, and -nest
may get a facelift before sqla2.

> There is an alternative solution to the *basic* boolean SQL test, which
> is to add it as a normal binary operation - which could be done like
> this:-
>
>     my $sqla = SQL::Abstract->new(
>         special_ops => [
>             {
>                 regex   => qr/bool/,
>                 handler => sub {
>                     my ( $self, $field, $op, $arg ) = @_;
>                     return ( $arg ? $field : "NOT $field" );
>                 },
>             },
>         ]
>     );
>
> and leads to syntax like this:-
>     { foo => {-bool, 1}, bar => {-bool, 0}}
>         WHERE foo AND NOT bar
>
> This will have some issues (subselects and parameterised functions
> etc), although literal functions should work.

This will _only_ work for column values, as you can not have references
as hash keys (thus literal functions are off the table).

> It is quite possible to add both forms of syntax (although this may
> confuse the hell out of users unless we change the naming of one of
> them)

I wouldn't do that, either one or the other.

> So comments and votes (but this ain't a democracy) are welcome on the
> best way to take this forward...
>

My vote is for what's in the branch.

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Alexander Hartmaier

Re: SQL::Abstract - boolean test support - should this merge?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Nigel Metheringham
Does someone know how Oracle handles this as it doesn't have a boolean
data type?
I use an integer column with a relationship to a bool_vals table to
limit the possible values.

Am Donnerstag, den 20.08.2009, 14:57 +0200 schrieb Nigel Metheringham:
> Boolean tests using SQLA (and hence DBIC) have always been a bit of a
> pain
>
> So comments and votes (but this ain't a democracy) are welcome on the
> best way to take this forward...
>
I like the second syntax more as it follows the column => value syntax,
but if we limit the possibilities like Peter said the one from the
branch is fine for me too.
Just don't make two ways!

--
BR Alex


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH   Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...