Skip to content

Commit

Permalink
feat: Decimal (22, 9), (35, 0), (31, 9), (35, 9)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Nov 8, 2024
1 parent d97c393 commit 27c637b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import tech.ydb.hibernate.dialect.code.YdbJdbcCode;
import static tech.ydb.hibernate.dialect.code.YdbJdbcCode.DECIMAL_SHIFT;
import tech.ydb.hibernate.dialect.exporter.EmptyExporter;
import tech.ydb.hibernate.dialect.exporter.YdbIndexExporter;
import tech.ydb.hibernate.dialect.hint.IndexQueryHintHandler;
Expand All @@ -71,10 +72,8 @@
import tech.ydb.hibernate.dialect.types.LocalDateJdbcType;
import tech.ydb.hibernate.dialect.types.LocalDateTimeJavaType;
import tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType;
import tech.ydb.hibernate.dialect.types.Uint8JdbcType;

import static tech.ydb.hibernate.dialect.code.YdbJdbcCode.DECIMAL_SHIFT;
import static tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType.JDBC_TYPE_DATETIME_CODE;
import tech.ydb.hibernate.dialect.types.Uint8JdbcType;

/**
* @author Kirill Kurdyukov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public final class YdbJdbcCode {

public static final int DECIMAL_SHIFT = (1 << 14);

/**
* <a href="https://github.com/ydb-platform/ydb-jdbc-driver/blob/v2.3.3/jdbc/src/main/java/tech/ydb/jdbc/impl/YdbTypes.java#L37-L66">link</a>
*/
public static final int DECIMAL_22_9 = DECIMAL_SHIFT + (22 << 6) + 9;

public static final int DECIMAL_31_9 = DECIMAL_SHIFT + (31 << 6) + 9;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "hibernate_test")
@Table(name = "hibernate_test_bigdecimal")
public class TestEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.math.BigDecimal;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.exception.GenericJDBCException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import static tech.ydb.hibernate.TestUtils.SESSION_FACTORY;
Expand Down Expand Up @@ -46,5 +48,18 @@ primary key (id)

inTransaction(session -> session.persist(testEntity));
inTransaction(session -> assertEquals(testEntity, session.find(TestEntity.class, 1)));

testEntity.setBigDecimal35_0(new BigDecimal(123));

inTransaction(session -> session.merge(testEntity));
inTransaction(session -> assertEquals(testEntity, session.find(TestEntity.class, 1)));

testEntity.setBigDecimal31_9(new BigDecimal("123123456781231234567812312345678123123456781231234567812312345678123123456781231234567812312345678123"));

assertThrows(GenericJDBCException.class, () -> inTransaction(session -> session.merge(testEntity)), "Unable to bind parameter #1 - 123123456781231234567812312345678123123456781231234567812312345678123123456781231234567812312345678123 [Cannot cast to decimal type Decimal(31, 9): [class java.math.BigDecimal: 123123456781231234567812312345678123123456781231234567812312345678123123456781231234567812312345678123] is Infinite] [n/a]");

testEntity.setBigDecimal31_9(new BigDecimal("123451234512345123451234512345123"));

assertThrows(GenericJDBCException.class, () -> inTransaction(session -> session.merge(testEntity)), "Unable to bind parameter #1 - 123451234512345123451234512345123 [Cannot cast to decimal type Decimal(31, 9): [class java.math.BigDecimal: 123123456781231234567812312345678123123456781231234567812312345678123123456781231234567812312345678123] is Infinite] [n/a]");
}
}

0 comments on commit 27c637b

Please sign in to comment.