Skip to content

Commit

Permalink
db-type-enum: YdbType
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lavrukov committed Feb 15, 2024
1 parent 51c3007 commit e1530ae
Show file tree
Hide file tree
Showing 21 changed files with 306 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
* The type of the DB column.<br>
* Defaults to automatically inferred from the field type.
*/
@Deprecated // Use ydbType() instead
String dbType() default "";

YdbType ydbType() default YdbType.DEFAULT;

/**
* Qualifier for refining type representation of the DB column.<br>
* Defaults to automatically inferred from the field type.
Expand Down
10 changes: 8 additions & 2 deletions databind/src/main/java/tech/ydb/yoj/databind/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,14 @@ private JavaField(JavaField javaField, JavaField parent) {
*/
public String getDbType() {
Column annotation = field.getColumn();
if (annotation != null && !annotation.dbType().isEmpty()) {
return annotation.dbType();
if (annotation != null) {
if (annotation.ydbType() != YdbType.DEFAULT) {
Preconditions.checkState(annotation.dbType().isEmpty(), "Can not set ydbType and dbType in one time");
return annotation.ydbType().getDbType();
}
if (!annotation.dbType().isEmpty()) {
return annotation.dbType();
}
}
return null;
}
Expand Down
98 changes: 98 additions & 0 deletions databind/src/main/java/tech/ydb/yoj/databind/schema/YdbType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tech.ydb.yoj.databind.schema;

/**
* Database column types supported by YDB.
*/
public enum YdbType {
DEFAULT(""),
/**
* Boolean value.
*/
BOOL("BOOL"),

/**
* Byte value.
*/
UINT8("UINT8"),

/**
* Integer value.
*/
INT32("INT32"),

/**
* Integer value stored in the db as Uint32.
*/
UINT32("UINT32"),

/**
* Long value.
*/
INT64("INT64"),

/**
* Long value stored in the db as Uint64.
*/
UINT64("UINT64"),

/**
* Float value.
*/
FLOAT("FLOAT"),

/**
* Double value.
*/
DOUBLE("DOUBLE"),

/**
* Date value, accurate to the day.
*/
DATE("DATE"),

/**
* Timestamp value, accurate to second.
*/
DATETIME("DATETIME"),

/**
* Timestamp value, accurate to microsecond.
*/
TIMESTAMP("TIMESTAMP"),

/**
* Interval value, accurate to microsecond.
*/
INTERVAL("INTERVAL"),

/**
* Binary data.
*/
LEGACY_BYTES("STRING"),

/**
* UTF-8 encoded string.
*/
UTF8("UTF8"),

/**
* JSON value, stored as a UTF-8 encoded string.
*/
JSON("JSON"),

/**
* JSON value, stored in an indexed representation permitting efficient query operations of the values inside the
* JSON value itself.
*/
JSON_DOCUMENT("JSON_DOCUMENT");

private final String dbType;

YdbType(String dbType) {
this.dbType = dbType;
}

public String getDbType() {
return dbType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.databind.schema.configuration.SchemaRegistry;
import tech.ydb.yoj.databind.schema.configuration.SchemaRegistry.SchemaKey;
import tech.ydb.yoj.databind.schema.reflect.Reflector;
Expand Down Expand Up @@ -112,7 +113,7 @@ private static class ColumnNameClashes {
@Column
Id id;

@Column(name = "id", dbType = "UTF8")
@Column(name = "id", ydbType = YdbType.UTF8)
String value;

@Value
Expand All @@ -127,7 +128,7 @@ private static class JavaFieldClashes {
@Column
Id id;

@Column(name = "value", dbType = "UTF8")
@Column(name = "value", ydbType = YdbType.UTF8)
String value;

@Value
Expand Down Expand Up @@ -171,7 +172,7 @@ private static final class Id {

@Value
private static final class UInt32 {
@Column(dbType = "UInt32", dbTypeQualifier = "Days")
@Column(ydbType = YdbType.UINT32, dbTypeQualifier = "Days")
int value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Value;
import tech.ydb.yoj.databind.schema.Changefeed;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.repository.db.Entity;

import static tech.ydb.yoj.databind.schema.Changefeed.Format.JSON;
Expand All @@ -14,7 +15,7 @@
public class ChangefeedEntity implements Entity<ChangefeedEntity> {
Id id;

@Column(dbType = "UTF8")
@Column(ydbType = YdbType.UTF8)
String stringField;

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Value;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.TTL;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.repository.db.Entity;

import java.time.Instant;
Expand All @@ -12,7 +13,7 @@
public class TtlEntity implements Entity<TtlEntity> {
Id id;

@Column(dbType = "TIMESTAMP")
@Column(ydbType = YdbType.TIMESTAMP)
Instant createdAt;

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Value;
import lombok.With;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.Table;

Expand All @@ -19,7 +20,7 @@ public class TypeFreak implements Entity<TypeFreak> {

boolean primitiveBoolean;
byte primitiveByte;
@Column(dbType = "UINT8")
@Column(ydbType = YdbType.UTF8)
byte primitiveByteUint8;
short primitiveShort;
int primitiveInt;
Expand All @@ -28,28 +29,28 @@ public class TypeFreak implements Entity<TypeFreak> {
double primitiveDouble;
Boolean boxedBoolean;
Byte boxedByte;
@Column(dbType = "UINT8")
@Column(ydbType = YdbType.UTF8)
Byte boxedByteUint8;
Short boxedShort;
Integer boxedInteger;
Long boxedLong;
Float boxedFloat;
Double boxedDouble;

@Column(dbType = "UTF8")
@Column(ydbType = YdbType.UTF8)
String utf8String;
String string;
byte[] bytes;
Status status;
@Column(dbType = "UTF8")
@Column(ydbType = YdbType.UTF8)
Status utf8Status;
@With
Embedded embedded;
@Column(flatten = false, dbType = "UTF8")
@Column(flatten = false, ydbType = YdbType.UTF8)
Embedded utf8embedded;
@Column(flatten = false, dbType = "STRING")
@Column(flatten = false, ydbType = YdbType.LEGACY_BYTES)
Embedded stringEmbedded;
@Column(flatten = false, dbType = "JSON_DOCUMENT")
@Column(flatten = false, ydbType = YdbType.JSON_DOCUMENT)
Embedded jsonDocumentEmbedded;
@Column(flatten = false)
Embedded jsonEmbedded;
Expand All @@ -60,11 +61,11 @@ public class TypeFreak implements Entity<TypeFreak> {
List<Embedded> list2;
Set<Embedded> list3;
Map<Integer, Embedded> map1;
@Column(dbType = "UTF8")
@Column(ydbType = YdbType.UTF8)
Map<Integer, Embedded> utf8map;
@Column(dbType = "STRING")
@Column(ydbType = YdbType.LEGACY_BYTES)
Map<Integer, Embedded> stringMap;
@Column(dbType = "JSON_DOCUMENT")
@Column(ydbType = YdbType.JSON_DOCUMENT)
Map<Integer, Embedded> jsonDocumentMap;

@Column(name = "custom_named_column")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Database column types supported by YDB.
*/
@Deprecated // use YdbType instead
public interface DbType {
/**
* Boolean value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import lombok.Value;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.YdbType;

@Value
public class Count {
@Column(dbType = "UINT64")
@Column(ydbType = YdbType.UINT64)
long count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tech.ydb.yoj.databind.schema.GlobalIndex;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.common.CommonConverters;
import tech.ydb.yoj.repository.db.exception.ConversionException;
Expand Down Expand Up @@ -279,34 +280,34 @@ enum SampleEnum {
@AllArgsConstructor
public static class BasicMapping {
String v1;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
String v2;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
String v3;
SampleEnum v4;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
SampleEnum v5;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
SampleEnum v6;
}

@AllArgsConstructor
public static class BasicMappingAndId implements Entity<BasicMappingAndId> {
BasicMappingAndId.Id id;
String v1;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
String v2;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
String v3;
SampleEnum v4;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
SampleEnum v5;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
SampleEnum v6;
UUID v7;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
UUID v8;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
UUID v9;

@Override
Expand All @@ -317,14 +318,14 @@ public BasicMappingAndId.Id getId() {
@AllArgsConstructor
public static class Id implements Entity.Id<BasicMappingAndId> {
String id1;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
String id2;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
String id3;
UUID id4;
@Column(dbType = DbType.STRING)
@Column(ydbType = YdbType.LEGACY_BYTES)
UUID id5;
@Column(dbType = DbType.UTF8)
@Column(ydbType = YdbType.UTF8)
UUID id6;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Value;
import lombok.With;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.YdbType;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.ydb.client.YdbTableHint;

Expand All @@ -17,7 +18,7 @@ public class HintUniform implements Entity<HintUniform> {

@Value
public static class Id implements Entity.Id<HintUniform> {
@Column(dbType = "UINT32")
@Column(ydbType = YdbType.UINT32)
long value;
}
}
Loading

0 comments on commit e1530ae

Please sign in to comment.