Reenable additional tests

This commit is contained in:
Andrea Boriero 2021-12-15 10:51:29 +01:00 committed by Christian Beikov
parent b75c2117b2
commit 017d1a2dcd
3 changed files with 107 additions and 104 deletions

View File

@ -7,7 +7,7 @@
//$Id$
package org.hibernate.test.subquery;
package org.hibernate.orm.test.subquery;
import jakarta.persistence.*;

View File

@ -0,0 +1,106 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$Id$
package org.hibernate.orm.test.subquery;
import java.util.List;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.ArgumentsValidator;
import org.hibernate.query.sqm.produce.function.FunctionReturnTypeResolver;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Query;
/**
* Test some subquery scenarios.
*
* @author Christian Beikov
*/
@RequiresDialect(H2Dialect.class)
public class SubqueryTest extends BaseSessionFactoryFunctionalTest {
private static final SqmFunctionDescriptor LIMIT_FUNCTION = new LimitSqmSelfRenderingFunctionDescriptor();
public static class LimitSqmSelfRenderingFunctionDescriptor extends AbstractSqmSelfRenderingFunctionDescriptor {
public LimitSqmSelfRenderingFunctionDescriptor() {
this(
"limit",
StandardArgumentsValidators.exactly( 2 ),
StandardFunctionReturnTypeResolvers.useArgType( 1 )
);
}
public LimitSqmSelfRenderingFunctionDescriptor(
String name,
ArgumentsValidator argumentsValidator,
FunctionReturnTypeResolver returnTypeResolver) {
super( name, argumentsValidator, returnTypeResolver );
}
@Override
public void render(
SqlAppender sqlAppender, List<SqlAstNode> sqlAstArguments, SqlAstTranslator<?> walker) {
sqlAstArguments.get( 0 ).accept( walker );
sqlAppender.appendSql( " limit " + ( (QueryLiteral) sqlAstArguments.get( 1 ) ).getLiteralValue() );
}
}
@Override
protected void applyMetadataBuilder(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction( "limit", LIMIT_FUNCTION );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
EntityA.class
};
}
@Test
public void testNestedOrderBySubqueryInFunction() {
inSession(
session -> {
Query q = session.createQuery(
"SELECT a.id FROM EntityA a " +
"ORDER BY CASE WHEN (" +
"SELECT 1 FROM EntityA s1 " +
"WHERE s1.id IN(" +
"LIMIT(" +
"(" +
"SELECT 1 FROM EntityA sub " +
"ORDER BY " +
"CASE WHEN sub.name IS NULL THEN 1 ELSE 0 END, " +
"sub.name DESC, " +
"CASE WHEN sub.id IS NULL THEN 1 ELSE 0 END, " +
"sub.id DESC" +
")," +
"1)" +
")" +
") = 1 THEN 1 ELSE 0 END"
);
q.getResultList();
}
);
}
}

View File

@ -1,103 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$Id$
package org.hibernate.test.subquery;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.type.Type;
import org.junit.Test;
import jakarta.persistence.Query;
import java.util.List;
import java.util.function.Consumer;
/**
* Test some subquery scenarios.
*
* @author Christian Beikov
*/
@RequiresDialect(H2Dialect.class)
public class SubqueryTest extends BaseCoreFunctionalTestCase {
// private static final SQLFunction LIMIT_FUNCTION = new SQLFunction() {
// public boolean hasArguments() {
// return true;
// }
//
// public boolean hasParenthesesIfNoArguments() {
// return true;
// }
//
// public Type getReturnType(Type type, Mapping mpng) throws QueryException {
// return type;
// }
//
// public String render(Type type, List list, SessionFactoryImplementor sfi) throws QueryException {
// String subquery = list.get(0).toString();
// return subquery.substring(0, subquery.length() - 1) + " limit " + list.get(1) + ")";
// }
// };
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
EntityA.class
};
}
@Test
@FailureExpected
public void testNestedOrderBySubqueryInFunction() {
throw new NotYetImplementedFor6Exception( getClass() );
// withLimit(s -> {
// Query q = s.createQuery(
// "SELECT a.id FROM EntityA a " +
// "ORDER BY CASE WHEN (" +
// "SELECT 1 FROM EntityA s1 " +
// "WHERE s1.id IN(" +
// "LIMIT(" +
// "(" +
// "SELECT 1 FROM EntityA sub " +
// "ORDER BY " +
// "CASE WHEN sub.name IS NULL THEN 1 ELSE 0 END, " +
// "sub.name DESC, " +
// "CASE WHEN sub.id IS NULL THEN 1 ELSE 0 END, " +
// "sub.id DESC" +
// ")," +
// "1)" +
// ")" +
// ") = 1 THEN 1 ELSE 0 END"
// );
// q.getResultList();
// });
}
// private void withLimit(Consumer<Session> consumer) {
// rebuildSessionFactory( c -> c.addSqlFunction( "limit", LIMIT_FUNCTION ) );
//
// try {
// Session s = openSession();
// consumer.accept( s );
// } finally {
// // Rebuild to remove the function
// rebuildSessionFactory();
// }
// }
}