-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...nate-core/src/main/java/org/hibernate/dialect/function/array/OracleArrayTrimFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 | ||
*/ | ||
package org.hibernate.dialect.function.array; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.query.ReturnableType; | ||
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; | ||
import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator; | ||
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; | ||
import org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers; | ||
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.Expression; | ||
|
||
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY; | ||
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.INTEGER; | ||
|
||
/** | ||
* Oracle array_trim function. | ||
*/ | ||
public class OracleArrayTrimFunction extends AbstractSqmSelfRenderingFunctionDescriptor { | ||
|
||
public OracleArrayTrimFunction() { | ||
super( | ||
"array_trim", | ||
StandardArgumentsValidators.composite( | ||
new ArgumentTypesValidator( null, ANY, INTEGER ), | ||
ArrayArgumentValidator.DEFAULT_INSTANCE | ||
), | ||
ArrayViaArgumentReturnTypeResolver.DEFAULT_INSTANCE, | ||
StandardFunctionArgumentTypeResolvers.composite( | ||
StandardFunctionArgumentTypeResolvers.invariant( ANY, INTEGER ), | ||
StandardFunctionArgumentTypeResolvers.IMPLIED_RESULT_TYPE | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> sqlAstArguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> walker) { | ||
final String arrayTypeName = DdlTypeHelper.getTypeName( | ||
( (Expression) sqlAstArguments.get( 0 ) ).getExpressionType(), | ||
walker | ||
); | ||
sqlAppender.append( arrayTypeName ); | ||
sqlAppender.append( "_trim(" ); | ||
sqlAstArguments.get( 0 ).accept( walker ); | ||
sqlAppender.append( ',' ); | ||
sqlAstArguments.get( 1 ).accept( walker ); | ||
sqlAppender.append( ')' ); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayTrimTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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>. | ||
*/ | ||
package org.hibernate.orm.test.function.array; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import org.hibernate.cfg.AvailableSettings; | ||
|
||
import org.hibernate.testing.orm.junit.DialectFeatureChecks; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.RequiresDialectFeature; | ||
import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.orm.junit.Setting; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.PersistenceException; | ||
import jakarta.persistence.Tuple; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* @author Christian Beikov | ||
*/ | ||
@DomainModel(annotatedClasses = EntityWithArrays.class) | ||
@SessionFactory | ||
@RequiresDialectFeature( feature = DialectFeatureChecks.SupportsStructuralArrays.class) | ||
// Make sure this stuff runs on a dedicated connection pool, | ||
// otherwise we might run into ORA-21700: object does not exist or is marked for delete | ||
// because the JDBC connection or database session caches something that should have been invalidated | ||
@ServiceRegistry(settings = @Setting(name = AvailableSettings.CONNECTION_PROVIDER, value = "")) | ||
public class ArrayTrimTest { | ||
|
||
@BeforeEach | ||
public void prepareData(SessionFactoryScope scope) { | ||
scope.inTransaction( em -> { | ||
em.persist( new EntityWithArrays( 1L, new String[]{} ) ); | ||
em.persist( new EntityWithArrays( 2L, new String[]{ "abc", null, "def" } ) ); | ||
em.persist( new EntityWithArrays( 3L, null ) ); | ||
} ); | ||
} | ||
|
||
@AfterEach | ||
public void cleanup(SessionFactoryScope scope) { | ||
scope.inTransaction( em -> { | ||
em.createMutationQuery( "delete from EntityWithArrays" ).executeUpdate(); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testTrimOne(SessionFactoryScope scope) { | ||
scope.inSession( em -> { | ||
//tag::hql-array-trim-example[] | ||
List<Tuple> results = em.createQuery( "select e.id, array_trim(e.theArray, 1) from EntityWithArrays e where e.id = 2", Tuple.class ) | ||
.getResultList(); | ||
//end::hql-array-trim-example[] | ||
assertEquals( 1, results.size() ); | ||
assertEquals( 2L, results.get( 0 ).get( 0 ) ); | ||
assertArrayEquals( new String[] { "abc", null }, results.get( 0 ).get( 1, String[].class ) ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testTrimAll(SessionFactoryScope scope) { | ||
scope.inSession( em -> { | ||
List<Tuple> results = em.createQuery( "select e.id, array_trim(e.theArray, 3) from EntityWithArrays e where e.id = 2", Tuple.class ) | ||
.getResultList(); | ||
assertEquals( 1, results.size() ); | ||
assertEquals( 2L, results.get( 0 ).get( 0 ) ); | ||
assertArrayEquals( new String[0], results.get( 0 ).get( 1, String[].class ) ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testTrimOutOfRange(SessionFactoryScope scope) { | ||
scope.inSession( em -> { | ||
try { | ||
em.createQuery( "select array_trim(e.theArray, 1) from EntityWithArrays e where e.id = 1" ) | ||
.getResultList(); | ||
fail( "Should fail because array is too small to trim!" ); | ||
} | ||
catch (PersistenceException ex) { | ||
assertInstanceOf( SQLException.class, ex.getCause() ); | ||
} | ||
} ); | ||
} | ||
|
||
} |