[Bast-commits] r6158 - SQL-Abstract/1.x/branches/special_op_handling/lib/SQL

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Thu May 7 09:55:12 GMT 2009


Author: ribasushi
Date: 2009-05-07 09:55:12 +0000 (Thu, 07 May 2009)
New Revision: 6158

Modified:
   SQL-Abstract/1.x/branches/special_op_handling/lib/SQL/Abstract.pm
Log:
Allow special op handlers to be a method name alongside a coderef
Switch built-in -in/-between handling to the method calling scheme, to facilitate overriding

Modified: SQL-Abstract/1.x/branches/special_op_handling/lib/SQL/Abstract.pm
===================================================================
--- SQL-Abstract/1.x/branches/special_op_handling/lib/SQL/Abstract.pm	2009-05-07 09:12:41 UTC (rev 6157)
+++ SQL-Abstract/1.x/branches/special_op_handling/lib/SQL/Abstract.pm	2009-05-07 09:55:12 UTC (rev 6158)
@@ -25,8 +25,8 @@
 # special operators (-in, -between). May be extended/overridden by user.
 # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
 my @BUILTIN_SPECIAL_OPS = (
-  {regex => qr/^(not )?between$/i, handler => \&_where_field_BETWEEN},
-  {regex => qr/^(not )?in$/i,      handler => \&_where_field_IN},
+  {regex => qr/^(not )?between$/i, handler => '_where_field_BETWEEN'},
+  {regex => qr/^(not )?in$/i,      handler => '_where_field_IN'},
 );
 
 #======================================================================
@@ -548,7 +548,19 @@
     # CASE: special operators like -in or -between
     my $special_op = first {$op =~ $_->{regex}} @{$self->{special_ops}};
     if ($special_op) {
-      ($sql, @bind) = $special_op->{handler}->($self, $k, $op, $val);
+      my $handler = $special_op->{handler};
+      if (! $handler) {
+        puke "No handler supplied for special operator matching $special_op->{regex}";
+      }
+      elsif (not ref $handler) {
+        ($sql, @bind) = $self->$handler ($k, $op, $val);
+      }
+      elsif (ref $handler eq 'CODE') {
+        ($sql, @bind) = $handler->($self, $k, $op, $val);
+      }
+      else {
+        puke "Illegal handler for special operator matching $special_op->{regex} - expecting a method name or a coderef";
+      }
     }
     else {
       $self->_SWITCH_refkind($val, {
@@ -2123,12 +2135,17 @@
 =head1 SPECIAL OPERATORS
 
   my $sqlmaker = SQL::Abstract->new(special_ops => [
-     {regex => qr/.../,
+     {
+      regex => qr/.../,
       handler => sub {
         my ($self, $field, $op, $arg) = @_;
         ...
-        },
+      },
      },
+     {
+      regex => qr/.../,
+      handler => 'method_name',
+     },
    ]);
 
 A "special operator" is a SQL syntactic clause that can be 
@@ -2140,12 +2157,13 @@
    WHERE MATCH(field) AGAINST (?, ?)
 
 Special operators IN and BETWEEN are fairly standard and therefore
-are builtin within C<SQL::Abstract>. For other operators,
-like the MATCH .. AGAINST example above which is 
-specific to MySQL, you can write your own operator handlers :
-supply a C<special_ops> argument to the C<new> method. 
-That argument takes an arrayref of operator definitions;
-each operator definition is a hashref with two entries
+are builtin within C<SQL::Abstract> (as the overridable methods
+C<_where_field_IN> and C<_where_field_BETWEEN>). For other operators,
+like the MATCH .. AGAINST example above which is specific to MySQL,
+you can write your own operator handlers - supply a C<special_ops>
+argument to the C<new> method. That argument takes an arrayref of
+operator definitions; each operator definition is a hashref with two
+entries:
 
 =over
 
@@ -2155,11 +2173,25 @@
 
 =item handler
 
-coderef that will be called when meeting that operator
-in the input tree. The coderef will be called with 
-arguments  C<< ($self, $field, $op, $arg) >>, and 
-should return a C<< ($sql, @bind) >> structure.
+Either a coderef or a plain scalar method name. In both cases
+the expected return is C<< ($sql, @bind) >>.
 
+When supplied with a method name, it is simply called on the
+L<SQL::Abstract/> object as:
+
+ $self->$method_name ($field, $op, $arg)
+
+ Where:
+
+  $op is the part that matched the handler regex
+  $field is the LHS of the operator
+  $arg is the RHS
+
+When supplied with a coderef, it is called as:
+
+ $coderef->($self, $field, $op, $arg)
+
+
 =back
 
 For example, here is an implementation 




More information about the Bast-commits mailing list