1 package SQL::Abstract::Plugin::BangOverrides;
5 with 'SQL::Abstract::Role::Plugin';
7 sub register_extensions {
8 my ($self, $sqla) = @_;
9 foreach my $stmt ($sqla->statement_list) {
10 $sqla->wrap_expander($stmt => sub {
13 my ($self, $name, $args) = @_;
16 (ref($args->{order_by}) eq 'HASH'
17 ? %{$args->{order_by}}
21 foreach my $clause (map /^!(.*)$/, keys %args) {
22 my $override = delete $args{"!${clause}"};
23 $overrides{$clause} = (
24 ref($override) eq 'CODE'
25 ? $self->$override($args{$clause})
29 $self->$orig($name, { %$args, %overrides });
41 SQL::Abstract::Plugin::BangOverrides
45 $sqla->plugin('+BangOverrides');
51 =head2 register_extensions
53 Wraps all currently existing clause based statements such that when a clause
54 of '!name' is encountered, if its value is a coderef, it's called with the
55 original value of the 'name' clause and expected to return a replacement, and
56 if not, it's simply used as a direct replacement.
58 This allows for passing data through existing systems that attempt to have
59 their own handling for thing but whose capabilities are now superceded by
60 L<SQL::Abstract>, and is primarily useful to provide access to experimental
61 feature bundles such as L<SQL::Abstract::Plugin::ExtraClauses>.
63 As an example of such a thing, given an appropriate DBIC setup
64 (see C<examples/bangdbic.pl>):
66 $s->storage->sqlmaker->plugin('+ExtraClauses')->plugin('+BangOverrides');
68 my $rs2 = $s->resultset('Foo')->search({
69 -op => [ '=', { -ident => 'outer.y' }, { -ident => 'me.x' } ]
71 # (SELECT me.x, me.y, me.z FROM foo me WHERE ( outer.y = me.x ))
73 my $rs3 = $rs2->search({}, {
74 '!from' => sub { my ($sqla, $from) = @_;
75 my $base = $sqla->expand_expr({ -old_from => $from });
76 return [ $base, -join => [ 'wub', on => [ 'me.z' => 'wub.z' ] ] ];
79 # (SELECT me.x, me.y, me.z FROM foo me JOIN wub ON me.z = wub.z WHERE ( outer.y = me.x ))
81 my $rs4 = $rs3->search({}, {
82 '!with' => [ [ qw(wub x y z) ], $s->resultset('Bar')->as_query ],
84 # (WITH wub(x, y, z) AS (SELECT me.a, me.b, me.c FROM bar me) SELECT me.x, me.y, me.z FROM foo me JOIN wub ON me.z = wub.z WHERE ( outer.y = me.x ))
86 my $rs5 = $rs->search({}, { select => [ { -coalesce => [ { -ident => 'x' }, { -value => 7 } ] } ] });
87 # (SELECT -COALESCE( -IDENT( x ), -VALUE( 7 ) ) FROM foo me WHERE ( z = ? ))
89 my $rs6 = $rs->search({}, { '!select' => [ { -coalesce => [ { -ident => 'x' }, { -value => 7 } ] } ] });
90 # (SELECT COALESCE(x, ?) FROM foo me WHERE ( z = ? ))