fix the tests for Oracle nested tables / arrays

I messed up
This commit is contained in:
Gavin 2023-05-05 15:07:04 +02:00 committed by Gavin King
parent c922a10df2
commit 5f49441fcc
4 changed files with 155 additions and 11 deletions

View File

@ -218,9 +218,7 @@ public class OracleArrayJdbcType implements JdbcType {
}
String[] getDropArrayTypeCommand(String elementTypeName) {
// for some weird reason dropping the type declarations causes problem in the test suite
// return new String[] { "drop type " + elementTypeName };
return EMPTY_STRING_ARRAY;
return EMPTY_STRING_ARRAY; //new String[] { "drop type " + elementTypeName + " force" };
}
@Override

View File

@ -225,9 +225,7 @@ public class OracleNestedTableJdbcType implements JdbcType {
}
String[] getDropArrayTypeCommand(String elementTypeName) {
// for some weird reason dropping the type declarations causes problem in the test suite
// return new String[] { "drop type " + elementTypeName };
return EMPTY_STRING_ARRAY;
return EMPTY_STRING_ARRAY; //new String[] { "drop type " + elementTypeName + " force" };
}
@Override

View File

@ -40,32 +40,60 @@ public class OracleNestedTableTest {
@Test public void testSchema(SessionFactoryScope scope) {
scope.inSession( s -> {
try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) {
Connection c;
try {
c = s.getJdbcConnectionAccess().obtainConnection();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "STRINGS" );
while ( tableInfo.next() ) {
String type = tableInfo.getString(6);
assertEquals( "STRINGARRAY", type );
return;
}
fail("named enum column not exported");
fail("nested table column not exported");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
try {
s.getJdbcConnectionAccess().releaseConnection(c);
}
catch (SQLException e) {
}
}
});
scope.inSession( s -> {
try ( Connection c = s.getJdbcConnectionAccess().obtainConnection() ) {
Connection c;
try {
c = s.getJdbcConnectionAccess().obtainConnection();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "ACTIVITYTYPES" );
while ( tableInfo.next() ) {
String type = tableInfo.getString(6);
assertEquals( "ACTIVITYTYPEARRAY", type );
return;
}
fail("named enum column not exported");
fail("nested table column not exported");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
try {
s.getJdbcConnectionAccess().releaseConnection(c);
}
catch (SQLException e) {
}
}
});
}
@ -78,10 +106,11 @@ public class OracleNestedTableTest {
@Array(length = 33)
@Column(length = 25)
@JdbcTypeCode(SqlTypes.ARRAY)
@JdbcTypeCode(SqlTypes.TABLE)
String[] strings;
@Array(length = 2)
@JdbcTypeCode(SqlTypes.TABLE)
ActivityType[] activityTypes;
}

View File

@ -0,0 +1,119 @@
package org.hibernate.orm.test.type;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.annotations.Array;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.type.SqlTypes;
import org.junit.jupiter.api.Test;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@SessionFactory
@DomainModel(annotatedClasses = {OracleSqlArrayTest.Container.class})
@RequiresDialect(OracleDialect.class)
public class OracleSqlArrayTest {
@Test public void test(SessionFactoryScope scope) {
Container container = new Container();
container.activityKinds = new ActivityKind[] { ActivityKind.Work, ActivityKind.Play };
container.bigIntegers = new BigInteger[] { new BigInteger("123"), new BigInteger("345") };
scope.inTransaction( s -> s.persist( container ) );
Container c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where bigIntegers = ?1", Container.class ).setParameter(1, new BigInteger[] { new BigInteger("123"), new BigInteger("345") }).getSingleResult() );
assertArrayEquals( c.activityKinds, new ActivityKind[] { ActivityKind.Work, ActivityKind.Play } );
assertArrayEquals( c.bigIntegers, new BigInteger[] { new BigInteger("123"), new BigInteger("345") } );
c = scope.fromTransaction( s-> s.createQuery("from ContainerWithArrays where activityKinds = ?1", Container.class ).setParameter(1, new ActivityKind[] { ActivityKind.Work, ActivityKind.Play }).getSingleResult() );
}
@Test public void testSchema(SessionFactoryScope scope) {
scope.inSession( s -> {
Connection c;
try {
c = s.getJdbcConnectionAccess().obtainConnection();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "BIGINTEGERS" );
while ( tableInfo.next() ) {
String type = tableInfo.getString(6);
assertEquals( "BIGINTEGERARRAY", type );
return;
}
fail("named array column not exported");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
try {
s.getJdbcConnectionAccess().releaseConnection(c);
}
catch (SQLException e) {
}
}
});
scope.inSession( s -> {
Connection c;
try {
c = s.getJdbcConnectionAccess().obtainConnection();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ResultSet tableInfo = c.getMetaData().getColumns(null, null, "CONTAINERWITHARRAYS", "ACTIVITYKINDS" );
while ( tableInfo.next() ) {
String type = tableInfo.getString(6);
assertEquals( "ACTIVITYKINDARRAY", type );
return;
}
fail("named array column not exported");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
try {
s.getJdbcConnectionAccess().releaseConnection(c);
}
catch (SQLException e) {
}
}
});
}
public enum ActivityKind { Work, Play, Sleep }
@Entity(name = "ContainerWithArrays")
public static class Container {
@Id @GeneratedValue Long id;
@Array(length = 33)
@Column(length = 25)
@JdbcTypeCode(SqlTypes.ARRAY)
BigInteger[] bigIntegers;
@Array(length = 2)
@JdbcTypeCode(SqlTypes.ARRAY)
ActivityKind[] activityKinds;
}
}