waitForIndexes();
// print = true;
String sql1 = "SELECT BASE.* from [nt:formInstVersion] as BASE " //
+ "JOIN [nt:formInst] AS FORMINST ON ISCHILDNODE(BASE,FORMINST)";
Query query = session.getWorkspace().getQueryManager().createQuery(sql1, Query.JCR_SQL2);
validateQuery().rowCount(2).validate(query, query.execute());
String sql2 = "SELECT BASE.* from [nt:formInstVersion] as BASE " //
+ "JOIN [nt:formInst] AS FORMINST ON ISCHILDNODE(BASE,FORMINST) " //
+ "JOIN [nt:formInstVersion] AS FORMINSTNEXT ON FORMINSTNEXT.previous_version = BASE.[jcr:uuid]";
query = session.getWorkspace().getQueryManager().createQuery(sql2, Query.JCR_SQL2);
validateQuery().rowCount(1).validate(query, query.execute());
String sql = sql1 + " UNION " + sql2;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(2).validate(query, query.execute());
sql = sql2 + " UNION " + sql1;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(2).validate(query, query.execute());
sql = sql1 + " INTERSECT " + sql2;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(1).validate(query, query.execute());
sql = sql2 + " INTERSECT " + sql1;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(1).validate(query, query.execute());
sql = sql1 + " EXCEPT " + sql2;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(1).validate(query, query.execute());
sql = sql2 + " EXCEPT " + sql1;
query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2);
validateQuery().rowCount(0).validate(query, query.execute());
}