|
| 1 | +package tech.ydb.hibernate.types; |
| 2 | + |
| 3 | +import jakarta.persistence.Column; |
| 4 | +import jakarta.persistence.Entity; |
| 5 | +import jakarta.persistence.Id; |
| 6 | +import jakarta.persistence.Table; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.time.Duration; |
| 9 | +import java.time.Instant; |
| 10 | +import java.time.LocalDate; |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.time.ZoneOffset; |
| 13 | +import java.util.UUID; |
| 14 | +import lombok.AllArgsConstructor; |
| 15 | +import lombok.Data; |
| 16 | +import lombok.NoArgsConstructor; |
| 17 | +import org.hibernate.annotations.JdbcTypeCode; |
| 18 | +import org.hibernate.cfg.AvailableSettings; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 23 | +import tech.ydb.hibernate.TestUtils; |
| 24 | +import static tech.ydb.hibernate.TestUtils.basedConfiguration; |
| 25 | +import static tech.ydb.hibernate.TestUtils.inTransaction; |
| 26 | +import static tech.ydb.hibernate.TestUtils.jdbcUrl; |
| 27 | +import tech.ydb.hibernate.dialect.code.YdbJdbcCode; |
| 28 | +import tech.ydb.test.junit5.YdbHelperExtension; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Kirill Kurdyukov |
| 32 | + */ |
| 33 | +public class YdbJdbcCodeTest { |
| 34 | + |
| 35 | + @RegisterExtension |
| 36 | + private static final YdbHelperExtension ydb = new YdbHelperExtension(); |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + static void beforeAll() { |
| 40 | + TestUtils.SESSION_FACTORY = basedConfiguration() |
| 41 | + .addAnnotatedClass(YdbAllTypes.class) |
| 42 | + .setProperty(AvailableSettings.URL, jdbcUrl(ydb)) |
| 43 | + .buildSessionFactory(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void integrationTest() { |
| 48 | + /* |
| 49 | + * create table ydb_all_types ( |
| 50 | + * aDouble Double, |
| 51 | + * aFloat Float, |
| 52 | + * bool Bool, |
| 53 | + * bytes Bytes, |
| 54 | + * date Date, |
| 55 | + * date32 Date32, |
| 56 | + * datetime Datetime, |
| 57 | + * datetime64 Datetime64, |
| 58 | + * int16 Int16, |
| 59 | + * int32 Int32, |
| 60 | + * int64 Int64, |
| 61 | + * int8 Int8, |
| 62 | + * interval Interval, |
| 63 | + * interval64 Interval64, |
| 64 | + * json Json, |
| 65 | + * jsonDocument JsonDocument, |
| 66 | + * text Text, |
| 67 | + * timestamp Timestamp, |
| 68 | + * timestamp64 Timestamp64, |
| 69 | + * uint16 Uint16, |
| 70 | + * uint32 Uint32, |
| 71 | + * uint64 Uint64, |
| 72 | + * uint8 Uint8, |
| 73 | + * uuid Uuid not null, |
| 74 | + * yson Yson, |
| 75 | + * primary key (uuid) |
| 76 | + * ) |
| 77 | + */ |
| 78 | + var ydbAllEntity = new YdbAllTypes( |
| 79 | + UUID.randomUUID(), |
| 80 | + true, 1, 1, 1, 1, 1, 1, 1, 1, |
| 81 | + LocalDate.now(), |
| 82 | + LocalDateTime.of(2000, 1, 1, 1, 1, 1), |
| 83 | + Instant.parse("2025-10-02T11:06:32.779087Z"), |
| 84 | + Duration.ofSeconds(1), |
| 85 | + LocalDate.ofYearDay(1000, 1), |
| 86 | + LocalDateTime.of(1000, 1, 1, 1, 1, 1), |
| 87 | + LocalDateTime.of(1000, 1, 1, 1, 1, 1).toInstant(ZoneOffset.UTC), |
| 88 | + Duration.ofSeconds(-1), |
| 89 | + "text", |
| 90 | + "{\"a\":\"a\"}", |
| 91 | + "{\"a\":\"a\"}", |
| 92 | + new byte[]{10, 20}, |
| 93 | + "{a=1u}".getBytes(StandardCharsets.UTF_8), |
| 94 | + 1.1f, 1.1 |
| 95 | + ); |
| 96 | + |
| 97 | + inTransaction(session -> session.persist(ydbAllEntity)); |
| 98 | + inTransaction(session -> assertEquals(ydbAllEntity, session.find(YdbAllTypes.class, ydbAllEntity.getUuid()))); |
| 99 | + |
| 100 | + var newYdbAllEntity = new YdbAllTypes( |
| 101 | + ydbAllEntity.getUuid(), |
| 102 | + false, 2, 2, 2, 2, 2, 2, 2, 2, |
| 103 | + LocalDate.now(), |
| 104 | + LocalDateTime.of(2000, 2, 2, 2, 2, 2), |
| 105 | + Instant.parse("2023-10-02T11:06:32.779087Z"), |
| 106 | + Duration.ofSeconds(2), |
| 107 | + LocalDate.ofYearDay(1000, 2), |
| 108 | + LocalDateTime.of(1000, 2, 2, 2, 2, 2), |
| 109 | + LocalDateTime.of(1000, 2, 2, 2, 2, 2).toInstant(ZoneOffset.UTC), |
| 110 | + Duration.ofSeconds(-2), |
| 111 | + "new_text", |
| 112 | + "{\"b\":\"b\"}", |
| 113 | + "{\"b\":\"b\"}", |
| 114 | + new byte[]{20, 20}, |
| 115 | + "{a=2u}".getBytes(StandardCharsets.UTF_8), |
| 116 | + 2.2f, 2.2 |
| 117 | + ); |
| 118 | + inTransaction(session -> { |
| 119 | + session.createQuery( |
| 120 | + """ |
| 121 | + UPDATE YdbAllTypes e |
| 122 | + SET |
| 123 | + e.bool = :bool, |
| 124 | + e.uint8 = :uint8, |
| 125 | + e.int8 = :int8, |
| 126 | + e.uint16 = :uint16, |
| 127 | + e.int16 = :int16, |
| 128 | + e.uint32 = :uint32, |
| 129 | + e.int32 = :int32, |
| 130 | + e.uint64 = :uint64, |
| 131 | + e.int64 = :int64, |
| 132 | + e.date = :date, |
| 133 | + e.datetime = :datetime, |
| 134 | + e.timestamp = :timestamp, |
| 135 | + e.interval = :interval, |
| 136 | + e.date32 = :date32, |
| 137 | + e.datetime64 = :datetime64, |
| 138 | + e.timestamp64 = :timestamp64, |
| 139 | + e.interval64 = :interval64, |
| 140 | + e.text = :text, |
| 141 | + e.json = :json, |
| 142 | + e.jsonDocument = :jsonDocument, |
| 143 | + e.bytes = :bytes, |
| 144 | + e.yson = :yson, |
| 145 | + e.aFloat = :aFloat, |
| 146 | + e.aDouble = :aDouble |
| 147 | + """) |
| 148 | + .setParameter("bool", newYdbAllEntity.isBool()) |
| 149 | + .setParameter("uint8", newYdbAllEntity.getUint8()) |
| 150 | + .setParameter("int8", newYdbAllEntity.getInt8()) |
| 151 | + .setParameter("uint16", newYdbAllEntity.getUint16()) |
| 152 | + .setParameter("int16", newYdbAllEntity.getInt16()) |
| 153 | + .setParameter("uint32", newYdbAllEntity.getUint32()) |
| 154 | + .setParameter("int32", newYdbAllEntity.getInt32()) |
| 155 | + .setParameter("uint64", newYdbAllEntity.getUint64()) |
| 156 | + .setParameter("int64", newYdbAllEntity.getInt64()) |
| 157 | + .setParameter("date", newYdbAllEntity.getDate()) |
| 158 | + .setParameter("datetime", newYdbAllEntity.getDatetime()) |
| 159 | + .setParameter("timestamp", newYdbAllEntity.getTimestamp()) |
| 160 | + .setParameter("interval", newYdbAllEntity.getInterval()) |
| 161 | + .setParameter("date32", newYdbAllEntity.getDate32()) |
| 162 | + .setParameter("datetime64", newYdbAllEntity.getDatetime64()) |
| 163 | + .setParameter("timestamp64", newYdbAllEntity.getTimestamp64()) |
| 164 | + .setParameter("interval64", newYdbAllEntity.getInterval64()) |
| 165 | + .setParameter("text", newYdbAllEntity.getText()) |
| 166 | + .setParameter("json", newYdbAllEntity.getJson()) |
| 167 | + .setParameter("jsonDocument", newYdbAllEntity.getJsonDocument()) |
| 168 | + .setParameter("bytes", newYdbAllEntity.getBytes()) |
| 169 | + .setParameter("yson", newYdbAllEntity.getYson()) |
| 170 | + .setParameter("aFloat", newYdbAllEntity.getAFloat()) |
| 171 | + .setParameter("aDouble", newYdbAllEntity.getADouble()) |
| 172 | + .executeUpdate(); |
| 173 | + assertEquals(newYdbAllEntity, session.find(YdbAllTypes.class, newYdbAllEntity.getUuid())); |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + @Entity(name = "YdbAllTypes") |
| 178 | + @Table(name = "ydb_all_types") |
| 179 | + @Data |
| 180 | + @AllArgsConstructor |
| 181 | + @NoArgsConstructor |
| 182 | + public static class YdbAllTypes { |
| 183 | + @Id |
| 184 | + @JdbcTypeCode(YdbJdbcCode.UUID) |
| 185 | + private UUID uuid; |
| 186 | + |
| 187 | + @Column |
| 188 | + @JdbcTypeCode(YdbJdbcCode.BOOL) |
| 189 | + private boolean bool; |
| 190 | + |
| 191 | + @Column |
| 192 | + @JdbcTypeCode(YdbJdbcCode.UINT8) |
| 193 | + private int uint8; |
| 194 | + |
| 195 | + @Column |
| 196 | + @JdbcTypeCode(YdbJdbcCode.INT8) |
| 197 | + private int int8; |
| 198 | + |
| 199 | + @Column |
| 200 | + @JdbcTypeCode(YdbJdbcCode.UINT16) |
| 201 | + private int uint16; |
| 202 | + |
| 203 | + @Column |
| 204 | + @JdbcTypeCode(YdbJdbcCode.INT16) |
| 205 | + private int int16; |
| 206 | + |
| 207 | + @Column |
| 208 | + @JdbcTypeCode(YdbJdbcCode.UINT32) |
| 209 | + private int uint32; |
| 210 | + |
| 211 | + @Column |
| 212 | + @JdbcTypeCode(YdbJdbcCode.INT32) |
| 213 | + private int int32; |
| 214 | + |
| 215 | + @Column |
| 216 | + @JdbcTypeCode(YdbJdbcCode.UINT64) |
| 217 | + private int uint64; |
| 218 | + |
| 219 | + @Column |
| 220 | + @JdbcTypeCode(YdbJdbcCode.INT64) |
| 221 | + private int int64; |
| 222 | + |
| 223 | + @Column |
| 224 | + @JdbcTypeCode(YdbJdbcCode.DATE) |
| 225 | + private LocalDate date; |
| 226 | + |
| 227 | + @Column |
| 228 | + @JdbcTypeCode(YdbJdbcCode.DATETIME) |
| 229 | + private LocalDateTime datetime; |
| 230 | + |
| 231 | + @Column |
| 232 | + @JdbcTypeCode(YdbJdbcCode.TIMESTAMP) |
| 233 | + private Instant timestamp; |
| 234 | + |
| 235 | + @Column |
| 236 | + @JdbcTypeCode(YdbJdbcCode.INTERVAL) |
| 237 | + private Duration interval; |
| 238 | + |
| 239 | + @Column |
| 240 | + @JdbcTypeCode(YdbJdbcCode.DATE_32) |
| 241 | + private LocalDate date32; |
| 242 | + |
| 243 | + @Column |
| 244 | + @JdbcTypeCode(YdbJdbcCode.DATETIME_64) |
| 245 | + private LocalDateTime datetime64; |
| 246 | + |
| 247 | + @Column |
| 248 | + @JdbcTypeCode(YdbJdbcCode.TIMESTAMP_64) |
| 249 | + private Instant timestamp64; |
| 250 | + |
| 251 | + @Column |
| 252 | + @JdbcTypeCode(YdbJdbcCode.INTERVAL_64) |
| 253 | + private Duration interval64; |
| 254 | + |
| 255 | + @Column |
| 256 | + @JdbcTypeCode(YdbJdbcCode.TEXT) |
| 257 | + private String text; |
| 258 | + |
| 259 | + @Column |
| 260 | + @JdbcTypeCode(YdbJdbcCode.JSON) |
| 261 | + private String json; |
| 262 | + |
| 263 | + @Column |
| 264 | + @JdbcTypeCode(YdbJdbcCode.JSON_DOCUMENT) |
| 265 | + private String jsonDocument; |
| 266 | + |
| 267 | + @Column |
| 268 | + @JdbcTypeCode(YdbJdbcCode.BYTES) |
| 269 | + private byte[] bytes; |
| 270 | + |
| 271 | + @Column |
| 272 | + @JdbcTypeCode(YdbJdbcCode.YSON) |
| 273 | + private byte[] yson; |
| 274 | + |
| 275 | + @Column |
| 276 | + @JdbcTypeCode(YdbJdbcCode.FLOAT) |
| 277 | + private float aFloat; |
| 278 | + |
| 279 | + @Column |
| 280 | + @JdbcTypeCode(YdbJdbcCode.DOUBLE) |
| 281 | + private double aDouble; |
| 282 | + } |
| 283 | +} |
0 commit comments