Fix top-level PRIOR with missing '='
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks / Oracle.pm
index ca6700e..5caf0a2 100644 (file)
@@ -1,16 +1,29 @@
 package # Hide from PAUSE
   DBIx::Class::SQLAHacks::Oracle;
 
+use warnings;
+use strict;
+
 use base qw( DBIx::Class::SQLAHacks );
 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
 
 # 
 #  TODO:
-#   - Check the parameter syntax of connect_by
 #   - Review by experienced DBIC/SQL:A developers :-)
 #   - Problem with count and connect_by look the TODO in t/73oracle.t
 # 
 
+sub new {
+  my $self = shift;
+  my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
+  push @{$opts{special_ops}}, {
+    regex => qr/^prior$/i,
+    handler => '_where_field_PRIOR',
+  };
+
+  $self->SUPER::new (\%opts);
+}
+
 sub select {
     my ($self, $table, $fields, $where, $order, @rest) = @_;
 
@@ -87,6 +100,21 @@ sub _order_siblings_by {
     return wantarray ? ( $sql, @bind ) : $sql;
 }
 
+# we need to add a '=' only when PRIOR is used against a column diretly
+# i.e. when it is invoked by a special_op callback
+sub _where_field_PRIOR {
+  my ($self, $lhs, $op, $rhs) = @_;
+  my ($sql, @bind) = $self->_recurse_where ($rhs);
+
+  $sql = sprintf ('%s = %s %s ',
+    $self->_convert($self->_quote($lhs)),
+    $self->_sqlcase ($op),
+    $sql
+  );
+
+  return ($sql, @bind);
+}
+
 1;
 
 __END__