-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17246 - Guard against Sybase being configured for truncating trai…
…ling zeros Signed-off-by: Jan Schatteman <[email protected]>
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
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
57 changes: 57 additions & 0 deletions
57
hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/UUIDasVarBinaryJdbcType.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,57 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.type.descriptor.jdbc; | ||
|
||
import org.hibernate.type.descriptor.ValueExtractor; | ||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.JavaType; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.Arrays; | ||
|
||
import static org.hibernate.type.SqlTypes.VARBINARY_UUID; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
public class UUIDasVarBinaryJdbcType extends BinaryJdbcType { | ||
|
||
public static final UUIDasVarBinaryJdbcType INSTANCE = new UUIDasVarBinaryJdbcType(); | ||
|
||
@Override | ||
public int getDdlTypeCode() { | ||
return Types.VARBINARY; | ||
} | ||
|
||
@Override | ||
public int getDefaultSqlTypeCode() { | ||
return VARBINARY_UUID; | ||
} | ||
|
||
@Override | ||
public <X> ValueExtractor<X> getExtractor( JavaType<X> javaType ) { | ||
return new BasicExtractor<>( javaType, this ) { | ||
@Override | ||
protected X doExtract( ResultSet rs, int paramIndex, WrapperOptions options ) throws SQLException { | ||
return javaType.wrap( Arrays.copyOf(rs.getBytes( paramIndex), 16), options ); | ||
} | ||
|
||
@Override | ||
protected X doExtract( CallableStatement statement, int index, WrapperOptions options ) throws SQLException { | ||
return javaType.wrap( Arrays.copyOf(statement.getBytes(index), 16), options ); | ||
} | ||
|
||
@Override | ||
protected X doExtract( CallableStatement statement, String name, WrapperOptions options ) | ||
throws SQLException { | ||
return javaType.wrap( Arrays.copyOf(statement.getBytes(name), 16), options ); | ||
} | ||
}; | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
hibernate-core/src/test/java/org/hibernate/orm/test/id/uuid/SybaseASEUUIDTest.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,105 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.id.uuid; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import org.hibernate.dialect.SybaseASEDialect; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.RequiresDialect; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
@RequiresDialect(value = SybaseASEDialect.class) | ||
@DomainModel(annotatedClasses = { SybaseASEUUIDTest.Book.class }) | ||
@SessionFactory | ||
public class SybaseASEUUIDTest { | ||
|
||
private UUID uuid; | ||
|
||
@BeforeEach | ||
void setUp(SessionFactoryScope scope) { | ||
this.uuid = scope.fromTransaction( session -> { | ||
UUID uuid = UUID.randomUUID(); | ||
// Create a UUID with trailing zeros | ||
while ( SafeRandomUUIDGenerator.isSafeUUID(uuid) ) { | ||
uuid = UUID.randomUUID(); | ||
} | ||
final Book book = new Book(uuid, "John Doe"); | ||
session.persist( book ); | ||
return uuid; | ||
} ); | ||
} | ||
|
||
@AfterEach | ||
void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
session.createMutationQuery( "delete from Book" ).executeUpdate(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
@JiraKey( value = "" ) | ||
public void testTrailingZeroByteTruncation(SessionFactoryScope scope) { | ||
scope.inSession( | ||
session -> { | ||
session.doWork( | ||
connection -> { | ||
Statement st = connection.createStatement(); | ||
Check warning Code scanning / CodeQL Potential database resource leak Warning test
This Statement is not always closed on method exit.
|
||
ResultSet rs = st.executeQuery( "select id from Book" ); | ||
Check warning Code scanning / CodeQL Potential database resource leak Warning test
This ResultSet is not always closed on method exit.
|
||
rs.next(); | ||
byte[] barr = rs.getBytes( 1 ); | ||
assertEquals( 15, barr.length ); | ||
} | ||
); | ||
} | ||
); | ||
scope.inTransaction( | ||
session -> { | ||
Book b = session.createQuery( "from Book", Book.class ).getSingleResult(); | ||
UUID uuid = b.id; | ||
ByteBuffer bb = ByteBuffer.allocate( 8 ); | ||
bb.putLong( uuid.getLeastSignificantBits() ); | ||
byte[] arr = bb.array(); | ||
assertEquals(8, arr.length); | ||
assertEquals(this.uuid, uuid); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Book") | ||
static class Book { | ||
@Id | ||
UUID id; | ||
|
||
String author; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(UUID id, String author) { | ||
this.id = id; | ||
this.author = author; | ||
} | ||
} | ||
|
||
} |