Test for more internal object leaks (fails)
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
index 7647cf0..c13dfaa 100644 (file)
@@ -92,7 +92,7 @@ $dbh->do(qq{
 });
 $dbh->do(qq{
   CREATE OR REPLACE TRIGGER cd_insert_trg
-  BEFORE INSERT ON cd
+  BEFORE INSERT OR UPDATE ON cd
   FOR EACH ROW
   BEGIN
     IF :new.cdid IS NULL THEN
@@ -343,13 +343,16 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
 
     $schema->resultset('Artist')->create ({
         name => 'root',
+        rank => 1,
         cds => [],
         children => [
             {
                 name => 'child1',
+                rank => 2,
                 children => [
                     {
                         name => 'grandchild',
+                        rank => 3,
                         cds => [
                             {
                                 title => "grandchilds's cd" ,
@@ -365,6 +368,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
                         children => [
                             {
                                 name => 'greatgrandchild',
+                                rank => 3,
                             }
                         ],
                     }
@@ -372,10 +376,27 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
             },
             {
                 name => 'child2',
+                rank => 3,
             },
         ],
     });
 
+    $schema->resultset('Artist')->create(
+        {
+            name     => 'cycle-root',
+            children => [
+                {
+                    name     => 'cycle-child1',
+                    children => [ { name => 'cycle-grandchild' } ],
+                },
+                { name => 'cycle-child2' },
+            ],
+        }
+    );
+
+    $schema->resultset('Artist')->find({ name => 'cycle-root' })
+      ->update({ parentid => \'artistid' });
+
     # select the whole tree
     {
       my $rs = $schema->resultset('Artist')->search({}, {
@@ -389,7 +410,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
             FROM artist me
           START WITH name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
         )',
         [ [ name => 'root'] ],
       );
@@ -406,7 +427,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
           SELECT COUNT( * )
             FROM artist me
           START WITH name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
         )',
         [ [ name => 'root'] ],
       );
@@ -428,7 +449,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
             FROM artist me
           START WITH name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
           ORDER SIBLINGS BY name DESC
         )',
         [ [ name => 'root'] ],
@@ -455,7 +476,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
             FROM artist me
           WHERE ( parentid IS NULL )
           START WITH name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
         )',
         [ [ name => 'root'] ],
       );
@@ -486,7 +507,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
             LEFT JOIN cd cds ON cds.artist = me.artistid
           WHERE ( cds.title LIKE ? )
           START WITH me.name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
         )',
         [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
       );
@@ -506,7 +527,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
             LEFT JOIN cd cds ON cds.artist = me.artistid
           WHERE ( cds.title LIKE ? )
           START WITH me.name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
         )',
         [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
       );
@@ -528,7 +549,7 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
             FROM artist me
           START WITH name = ?
-          CONNECT BY parentid = PRIOR( artistid )
+          CONNECT BY parentid = PRIOR artistid 
           ORDER BY LEVEL ASC, name ASC
         )',
         [ [ name => 'root' ] ],
@@ -554,22 +575,21 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
       is_same_sql_bind (
         $rs->as_query,
         '( 
-            SELECT * FROM (
-                  SELECT A.*, ROWNUM r FROM (
+            SELECT artistid, name, rank, charfield, parentid FROM (
+                  SELECT artistid, name, rank, charfield, parentid, ROWNUM rownum__index FROM (
                       SELECT 
-                          me.artistid AS col1,
-                          me.name AS col2,
-                          me.rank AS col3,
-                          me.charfield AS col4,
-                          me.parentid AS col5 
+                          me.artistid,
+                          me.name,
+                          me.rank,
+                          me.charfield,
+                          me.parentid 
                       FROM artist me 
                       START WITH name = ? 
-                      CONNECT BY parentid = PRIOR( artistid )
-                      ORDER BY name ASC
-                  ) A
-                  WHERE ROWNUM < 3
-              ) B
-              WHERE r >= 1 
+                      CONNECT BY parentid = PRIOR artistid
+                      ORDER BY name ASC 
+                  ) me 
+            ) me
+            WHERE rownum__index BETWEEN 1 AND 2
         )',
         [ [ name => 'root' ] ],
       );
@@ -582,55 +602,103 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
 
       # TODO: 
       # prints "START WITH name = ? 
-      # CONNECT BY artistid = PRIOR( parentid )"
+      # CONNECT BY artistid = PRIOR parentid "
       # after count_subq, 
       # I will fix this later...
       # 
-      # is_same_sql_bind (
-      #   $rs->count_rs->as_query,
-      #   '( 
-      #       SELECT COUNT( * ) FROM (
-      #           SELECT * FROM (
-      #               SELECT A.*, ROWNUM r FROM (
-      #                   SELECT 
-      #                       me.artistid AS col1 
-      #                   FROM artist me 
-      #                   START WITH name = ? 
-      #                   CONNECT BY artistid = PRIOR( parentid ) 
-      #               ) A
-      #               WHERE ROWNUM < 3
-      #           ) B
-      #           WHERE r >= 1
-      #       ) count_subq 
-      #   )',
-      #   [ [ name => 'greatgrandchild' ] ],
-      # );
-      # 
-      # is( $rs->count, 2, 'Connect By; LIMIT count ok' );
+      is_same_sql_bind (
+        $rs->count_rs->as_query,
+        '( 
+            SELECT COUNT( * ) FROM (
+                SELECT artistid FROM (
+                    SELECT artistid, ROWNUM rownum__index FROM (
+                        SELECT 
+                            me.artistid
+                        FROM artist me 
+                        START WITH name = ? 
+                        CONNECT BY parentid = PRIOR artistid
+                    ) me
+                ) me 
+                WHERE rownum__index BETWEEN 1 AND 2
+            ) me
+        )',
+        [ [ name => 'root' ] ],
+      );
+
+      is( $rs->count, 2, 'Connect By; LIMIT count ok' );
     }
 
-    # select the whole tree with nocylce
+    # combine a connect_by with group_by and having
     {
       my $rs = $schema->resultset('Artist')->search({}, {
-        nocycle    => 1,
+        select => ['count(rank)'],
         start_with => { name => 'root' },
         connect_by => { parentid => { -prior => \ 'artistid' } },
+        group_by => ['rank'],
+        having => { 'count(rank)' => { '<', 2 } },
       });
 
       is_same_sql_bind (
         $rs->as_query,
         '(
-          SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
+            SELECT count(rank)
+            FROM artist me
+            START WITH name = ?
+            CONNECT BY parentid = PRIOR artistid
+            GROUP BY rank HAVING count(rank) < ?
+        )',
+        [ [ name => 'root' ], [ 'count(rank)' => 2 ] ],
+      );
+
+      is_deeply (
+        [ $rs->get_column ('count(rank)')->all ],
+        [1, 1],
+        'Group By a Connect By query - correct values'
+      );
+    }
+
+
+    # select the whole cycle tree without nocylce
+    {
+      my $rs = $schema->resultset('Artist')->search({}, {
+        start_with => { name => 'cycle-root' },
+        connect_by => { parentid => { -prior => \ 'artistid' } },
+      });
+      eval { $rs->get_column ('name')->all };
+      if ( $@ =~ /ORA-01436/ ){ # ORA-01436:  CONNECT BY loop in user data
+        pass "connect by initify loop detection without nocycle";
+      }else{
+        fail "connect by initify loop detection without nocycle, not detected by oracle";
+      }
+    }
+
+    # select the whole cycle tree with nocylce
+    {
+      my $rs = $schema->resultset('Artist')->search({}, {
+        start_with => { name => 'cycle-root' },
+        '+select'  => [ \ 'CONNECT_BY_ISCYCLE' ],
+        connect_by_nocycle => { parentid => { -prior => \ 'artistid' } },
+      });
+
+      is_same_sql_bind (
+        $rs->as_query,
+        '(
+          SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
             FROM artist me
           START WITH name = ?
-          CONNECT BY NOCYCLE parentid = PRIOR( artistid )
+          CONNECT BY NOCYCLE parentid = PRIOR artistid 
         )',
-        [ [ name => 'root'] ],
+        [ [ name => 'cycle-root'] ],
       );
       is_deeply (
         [ $rs->get_column ('name')->all ],
-        [ qw/root child1 grandchild greatgrandchild child2/ ],
-        'got artist tree with nocycle',
+        [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
+        'got artist tree with nocycle (name)',
+      );
+      is_deeply (
+        [ $rs->get_column ('CONNECT_BY_ISCYCLE')->all ],
+        [ qw/1 0 0 0/ ],
+        'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
       );
 
 
@@ -640,12 +708,12 @@ if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
           SELECT COUNT( * )
             FROM artist me
           START WITH name = ?
-          CONNECT BY NOCYCLE parentid = PRIOR( artistid )
+          CONNECT BY NOCYCLE parentid = PRIOR artistid 
         )',
-        [ [ name => 'root'] ],
+        [ [ name => 'cycle-root'] ],
       );
 
-      is( $rs->count, 5, 'Connect By Nocycle count ok' );
+      is( $rs->count, 4, 'Connect By Nocycle count ok' );
     }
 }