});
$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
$schema->resultset('Artist')->create ({
name => 'root',
+ rank => 1,
cds => [],
children => [
{
name => 'child1',
+ rank => 2,
children => [
{
name => 'grandchild',
+ rank => 3,
cds => [
{
title => "grandchilds's cd" ,
children => [
{
name => 'greatgrandchild',
+ rank => 3,
}
],
}
},
{
name => 'child2',
+ rank => 3,
},
],
});
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
+ ORDER BY name ASC
+ ) me
+ ) me
+ WHERE rownum__index BETWEEN 1 AND 2
)',
[ [ name => 'root' ] ],
);
# 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' );
+ }
+
+ # combine a connect_by with group_by and having
+ {
+ my $rs = $schema->resultset('Artist')->search({}, {
+ 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 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({}, {
# select the whole cycle tree with nocylce
{
my $rs = $schema->resultset('Artist')->search({}, {
- nocycle => 1,
start_with => { name => 'cycle-root' },
'+select' => [ \ 'CONNECT_BY_ISCYCLE' ],
- connect_by => { parentid => { -prior => \ 'artistid' } },
+ connect_by_nocycle => { parentid => { -prior => \ 'artistid' } },
});
is_same_sql_bind (