in reply to [DSL] disabling packages in Perl?

I can't comment on your question regarding the disabling of package however I have spent some time trying to build an SQL DSL in Perl.

One issue you will likely face is dealing with things like "or" and "and" short-circuiting: as soon as Perl sees a true value on the left side of "or" or a false value on the left side of "and" it does not evaluate/execute the other term. Another issue is the flattening of "(" and ")" lists losing information you need to pass on to the database.

The best thing that I could come up with was parsing a list as special key/value pairs like so:

my $pinfo = $self->db->xhashref( select => [ 'n.id', 'p.hash' ], from => 'nodes n', inner_join => 'projects p', on => 'p.id = n.id', where => { 'n.uuid' => $uuid }, ); # Try and do this with an ORM! $dbw->xdo( insert_into => [ 'func_new_topic_status', qw/change_id id project_id tkind status rank def bill/, ], select => [ qv( $opts->{change_id} ), 'nextval("nodes")', qv( $opts->{id} ), 'ts.tkind', 'ts.status', 'ts.rank', 'ts.def', qv( $opts->{bill} // 1 ), ], from => 'topic_status ts', where => { 'ts.project_id' => $dup_pinfo->{id} }, order_by => [qw/ts.tkind ts.rank/] );

The DBIx::ThinSQL module contains my latest efforts. Unfortunately the documentation status is woefully wrong, but it functions (in my very humble opinion) far more powerfully and efficiently that any ORM or other SQL DSLs I've seen on CPAN.

Replies are listed 'Best First'.
Re^2: [DSL] disabling packages in Perl?
by LanX (Saint) on Apr 19, 2016 at 14:33 UTC
    > like "or" and "and" short-circuiting:

    Thanks, I'm fully aware of this! :)

    My initial plan was to patch B::Deparse in a way to take the op-tree of the code-block and to produce SQL instead of Perl (or rather a semantically correct intermediate Perl representation which produces SQL)

    > flattening of "(" and ")"

    likewise, true lists are flattened but precedence is still visible in the op-tree

    BUT for the moment I'm rather inclined to create subs OR() and AND() for a quick prototype.

    That is taking multiple expressions after a prefixed operator.

    WHERE { OR ( number EQ 3, AND ( customer LIKE '%...%', name IN qw/John Jane/, ) ) }

    Thats not really much overhead, because when mixing AND and OR parens should always be applied to highlight precedence.

    I'm very busy ATM but I'll show a proof of concept after next weekend.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      > Thats not really much overhead, because when mixing AND and OR parens should always be applied to highlight precedence.

      I would agree about the parens but there is a certain cognitive overhead in disassembling (or creating) the reverse polish notation which I have always shied away from. In the end for complicated expressions I think (I would have to check as it's been a while) I went for a mix of arrayrefs and hashrefs (implicit AND):

      select => [qw/expr1 expr2/], from => 'table', where => [ { number => 3 }, OR, { 'customer LIKE' => '%...%', name => [qw/John Jane/] }, },

      I'm looking forward to seeing what you come up with. Please post it!