],
});
+ $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({}, {
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'] ],
);
SELECT COUNT( * )
FROM artist me
START WITH name = ?
- CONNECT BY parentid = PRIOR( artistid )
+ CONNECT BY parentid = PRIOR artistid
)',
[ [ name => 'root'] ],
);
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'] ],
# get the root node
{
my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
- start_with => { name => 'greatgrandchild' },
- connect_by => { artistid => { -prior => \ 'parentid' } },
+ start_with => { name => 'root' },
+ connect_by => { parentid => { -prior => \ 'artistid' } },
});
is_same_sql_bind (
FROM artist me
WHERE ( parentid IS NULL )
START WITH name = ?
- CONNECT BY artistid = PRIOR( parentid )
+ CONNECT BY parentid = PRIOR artistid
)',
- [ [ name => 'greatgrandchild'] ],
+ [ [ name => 'root'] ],
);
is_deeply(
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' ] ],
);
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' ] ],
);
# combine a connect by with order_by
{
my $rs = $schema->resultset('Artist')->search({}, {
- start_with => { name => 'greatgrandchild' },
- connect_by => { artistid => { -prior => \ 'parentid' } },
- order_by => { -asc => 'name' },
+ start_with => { name => 'root' },
+ connect_by => { parentid => { -prior => \ 'artistid' } },
+ order_by => { -asc => [ 'LEVEL', 'name' ] },
});
is_same_sql_bind (
SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
FROM artist me
START WITH name = ?
- CONNECT BY artistid = PRIOR( parentid )
- ORDER BY name ASC
+ CONNECT BY parentid = PRIOR artistid
+ ORDER BY LEVEL ASC, name ASC
)',
- [ [ name => 'greatgrandchild' ] ],
+ [ [ name => 'root' ] ],
);
is_deeply (
[ $rs->get_column ('name')->all ],
- [ qw/child1 grandchild greatgrandchild root/ ],
+ [ qw/root child1 child2 grandchild greatgrandchild/ ],
'Connect By with a order_by - result name ok'
);
}
# limit a connect by
{
my $rs = $schema->resultset('Artist')->search({}, {
- start_with => { name => 'greatgrandchild' },
- connect_by => { artistid => { -prior => \ 'parentid' } },
- order_by => { -desc => 'name' },
+ start_with => { name => 'root' },
+ connect_by => { parentid => { -prior => \ 'artistid' } },
+ order_by => { -asc => 'name' },
rows => 2,
});
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 artistid = PRIOR( parentid )
- ORDER BY name DESC
- ) 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 => 'greatgrandchild' ] ],
+ [ [ name => 'root' ] ],
);
is_deeply (
[ $rs->get_column ('name')->all ],
- [qw/root greatgrandchild/],
+ [qw/child1 child2/],
'LIMIT a Connect By query - correct names'
);
# 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 artistid FROM (
+ # SELECT artistid, ROWNUM rownum__index FROM (
+ # SELECT
+ # me.artistid
+ # FROM artist me
+ # START WITH name = ?
+ # CONNECT BY artistid = PRIOR parentid
+ # ) me
+ # ) me
+ # WHERE rownum__index BETWEEN 1 AND 2
+ # ) me
+ # )',
+ # [ [ name => 'greatgrandchild' ] ],
+ # );
+ #
+ # is( $rs->count, 2, 'Connect By; LIMIT count ok' );
+ }
+
+ # 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({}, {
+ connect_by_nocycle => 1,
+ start_with => { name => 'cycle-root' },
+ '+select' => [ \ 'CONNECT_BY_ISCYCLE' ],
+ connect_by => { 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
+ )',
+ [ [ name => 'cycle-root'] ],
+ );
+ is_deeply (
+ [ $rs->get_column ('name')->all ],
+ [ 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)',
+ );
+
+
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
+ '(
+ SELECT COUNT( * )
+ FROM artist me
+ START WITH name = ?
+ CONNECT BY NOCYCLE parentid = PRIOR artistid
)',
- [ [ name => 'greatgrandchild' ] ],
+ [ [ name => 'cycle-root'] ],
);
- is( $rs->count, 2, 'Connect By; LIMIT count ok' );
+ is( $rs->count, 4, 'Connect By Nocycle count ok' );
}
}