Fix top-level PRIOR with missing '='
[dbsrgits/DBIx-Class.git] / t / oracle / connect_by.t
index b40ebce..1d36abf 100644 (file)
@@ -17,39 +17,44 @@ use DBIx::Class::SQLAHacks::Oracle;
 my @handle_tests = (
     {
         connect_by  => { 'parentid' => { '-prior' => \'artistid' } },
-        stmt        => " parentid = PRIOR artistid ",
+        stmt        => '"parentid" = PRIOR artistid',
         bind        => [],
-        msg         => 'Simple: parentid = PRIOR artistid',
+        msg         => 'Simple: "parentid" = PRIOR artistid',
     },
-    # {
-        # TODO: Can't handle this...
-        # connect_by  => { 'parentid' => { '!=' => { '-prior' => \'artistid' } } },
-        # connect_by  => [ \'parentid',  ],
-        # stmt        => "parentid != PRIOR artistid ",
-        # bind        => [],
-        # msg         => 'Simple: parentid != PRIOR artistid',
-    # },
+    {
+        connect_by  => { 'parentid' => { '!=' => { '-prior' => \'artistid' } } },
+        stmt        => '"parentid" != ( PRIOR artistid )',
+        bind        => [],
+        msg         => 'Simple: "parentid" != ( PRIOR artistid )',
+    },
+    # Examples from http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
 
-    # Excample from http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
+    # CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
     {
-        connect_by => [
-            'last_name' => { '!=' => 'King' },
-            '-prior' => [ \'employee_id', \'manager_id' ],
+        connect_by  => [
+            last_name => { '!=' => 'King' },
+            manager_id => { '-prior' => \'employee_id' },
         ],
-        stmt => "( last_name != ? AND PRIOR employee_id = manager_id )",
-        bind => ['King'],
+        stmt        => '( "last_name" != ? OR "manager_id" = PRIOR employee_id )',
+        bind        => ['King'],
+        msg         => 'oracle.com example #1',
     },
+    # CONNECT BY PRIOR employee_id = manager_id and 
+    #            PRIOR account_mgr_id = customer_id ...
     {
-        connect_by => [
-            '-prior' => [ \'employee_id', \'manager_id' ],
-            '-prior' => [ \'account_mgr_id', \'customer_id' ],
-        ],
-        stmt => "( PRIOR employee_id = manager_id AND PRIOR account_mgr_id = customer_id )",
-        bind => [],
+        connect_by  => {
+            manager_id => { '-prior' => \'employee_id' },
+            customer_id => { '>', { '-prior' => \'account_mgr_id' } },
+        },
+        stmt        => '( "customer_id" > ( PRIOR account_mgr_id ) AND "manager_id" = PRIOR employee_id )',
+        bind        => [],
+        msg         => 'oracle.com example #2',
     },
+    # CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4;
+    # TODO: NOCYCLE parameter doesn't work
 );
 
-my $sqla_oracle = DBIx::Class::SQLAHacks::Oracle->new();
+my $sqla_oracle = DBIx::Class::SQLAHacks::Oracle->new( quote_char => '"', name_sep => '.' );
 isa_ok($sqla_oracle, 'DBIx::Class::SQLAHacks::Oracle');
 
 
@@ -63,7 +68,7 @@ for my $case (@handle_tests) {
     );
     lives_ok(
         sub {
-            ( $stmt, @bind ) = $sqla_oracle->_recurse_where( $case->{connect_by}, 'and' );
+            ( $stmt, @bind ) = $sqla_oracle->_recurse_where( $case->{connect_by} );
             is_same_sql_bind( $stmt, \@bind, $case->{stmt}, $case->{bind},$msg )
               || diag "Search term:\n" . Dumper $case->{connect_by};
         }