Skip to content

Commit

Permalink
db-type-enum: enum DbType
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lavrukov committed Feb 15, 2024
1 parent 51c3007 commit 14a6948
Show file tree
Hide file tree
Showing 25 changed files with 106 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,87 +1,98 @@
package tech.ydb.yoj.repository.ydb;
package tech.ydb.yoj.databind;

/**
* Database column types supported by YDB.
*/
public interface DbType {
public enum DbType {
DEFAULT(""),
/**
* Boolean value.
*/
String BOOL = "BOOL";
BOOL("BOOL"),

/**
* Byte value.
*/
String UINT8 = "UINT8";
UINT8("UINT8"),

/**
* Integer value.
*/
String INT32 = "INT32";
INT32("INT32"),

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

/**
* Long value.
*/
String INT64 = "INT64";
INT64("INT64"),

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

/**
* Float value.
*/
String FLOAT = "FLOAT";
FLOAT("FLOAT"),

/**
* Double value.
*/
String DOUBLE = "DOUBLE";
DOUBLE("DOUBLE"),

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

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

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

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

/**
* Binary data.
*/
String STRING = "STRING";
STRING("STRING"),

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

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

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

private final String dbType;

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

public String getDbType() {
return dbType;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.yoj.databind.schema;

import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.FieldValueType;

import java.lang.annotation.Retention;
Expand All @@ -15,7 +16,7 @@
* <p>Usage Example:
* <blockquote><pre>
* // DB column will have name 'DESC' and DB-specific type 'UTF8'
* &#064;Column(name = "DESC", dbType = "UTF8")
* &#064;Column(name = "DESC", dbType = DbType.UTF8)
* String description;
*
* // Subobject's serialized representation will be written to a single BIG_SUBOBJ column
Expand All @@ -42,7 +43,7 @@
* The type of the DB column.<br>
* Defaults to automatically inferred from the field type.
*/
String dbType() default "";
DbType dbType() default DbType.DEFAULT;

/**
* Qualifier for refining type representation of the DB column.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.SneakyThrows;
import lombok.Value;
import lombok.With;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.FieldValueType;
import tech.ydb.yoj.databind.schema.configuration.SchemaRegistry.SchemaKey;
import tech.ydb.yoj.databind.schema.naming.NamingStrategy;
Expand Down Expand Up @@ -423,8 +424,8 @@ 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 && annotation.dbType() != DbType.DEFAULT) {
return annotation.dbType().getDbType();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Value;
import org.junit.Before;
import org.junit.Test;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.databind.schema.Schema;
Expand Down Expand Up @@ -112,7 +113,7 @@ private static class ColumnNameClashes {
@Column
Id id;

@Column(name = "id", dbType = "UTF8")
@Column(name = "id", dbType = DbType.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", dbType = DbType.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(dbType = DbType.UINT32, dbTypeQualifier = "Days")
int value;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.yoj.repository.test.sample.model;

import lombok.Value;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Changefeed;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.repository.db.Entity;
Expand All @@ -14,7 +15,7 @@
public class ChangefeedEntity implements Entity<ChangefeedEntity> {
Id id;

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

@Value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.yoj.repository.test.sample.model;

import lombok.Value;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.TTL;
import tech.ydb.yoj.repository.db.Entity;
Expand All @@ -12,7 +13,7 @@
public class TtlEntity implements Entity<TtlEntity> {
Id id;

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

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.NonNull;
import lombok.Value;
import lombok.With;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Column;
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(dbType = DbType.UINT8)
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(dbType = DbType.UINT8)
Byte boxedByteUint8;
Short boxedShort;
Integer boxedInteger;
Long boxedLong;
Float boxedFloat;
Double boxedDouble;

@Column(dbType = "UTF8")
@Column(dbType = DbType.UTF8)
String utf8String;
String string;
byte[] bytes;
Status status;
@Column(dbType = "UTF8")
@Column(dbType = DbType.UTF8)
Status utf8Status;
@With
Embedded embedded;
@Column(flatten = false, dbType = "UTF8")
@Column(flatten = false, dbType = DbType.UTF8)
Embedded utf8embedded;
@Column(flatten = false, dbType = "STRING")
@Column(flatten = false, dbType = DbType.STRING)
Embedded stringEmbedded;
@Column(flatten = false, dbType = "JSON_DOCUMENT")
@Column(flatten = false, dbType = DbType.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(dbType = DbType.UTF8)
Map<Integer, Embedded> utf8map;
@Column(dbType = "STRING")
@Column(dbType = DbType.STRING)
Map<Integer, Embedded> stringMap;
@Column(dbType = "JSON_DOCUMENT")
@Column(dbType = DbType.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 @@ -13,7 +13,6 @@
import org.slf4j.LoggerFactory;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.ydb.DbType;
import tech.ydb.yoj.repository.ydb.YdbConfig;
import tech.ydb.yoj.repository.ydb.YdbRepository;
import tech.ydb.yoj.repository.ydb.client.YdbPaths;
Expand Down Expand Up @@ -274,20 +273,20 @@ private static String builderDDLColumns(YdbSchemaOperations.Table table) {

private static String typeToDDL(String type) {
return switch (type) {
case DbType.BOOL -> "PrimitiveType.bool()";
case DbType.UINT8 -> "PrimitiveType.uint8()";
case DbType.INT32 -> "PrimitiveType.int32()";
case DbType.UINT32 -> "PrimitiveType.uint32()";
case DbType.INT64 -> "PrimitiveType.int64()";
case DbType.UINT64 -> "PrimitiveType.uint64()";
case DbType.FLOAT -> "PrimitiveType.float32()";
case DbType.DOUBLE -> "PrimitiveType.float64()";
case DbType.DATE -> "PrimitiveType.date()";
case DbType.DATETIME -> "PrimitiveType.datetime()";
case DbType.TIMESTAMP -> "PrimitiveType.timestamp()";
case DbType.STRING -> "PrimitiveType.string()";
case DbType.UTF8 -> "PrimitiveType.utf8()";
case DbType.JSON -> "PrimitiveType.json()";
case "BOOL" -> "PrimitiveType.bool()";
case "UINT8" -> "PrimitiveType.uint8()";
case "INT32" -> "PrimitiveType.int32()";
case "UINT32" -> "PrimitiveType.uint32()";
case "INT64" -> "PrimitiveType.int64()";
case "UINT64" -> "PrimitiveType.uint64()";
case "FLOAT" -> "PrimitiveType.float32()";
case "DOUBLE" -> "PrimitiveType.float64()";
case "DATE" -> "PrimitiveType.date()";
case "DATETIME" -> "PrimitiveType.datetime()";
case "TIMESTAMP" -> "PrimitiveType.timestamp()";
case "STRING" -> "PrimitiveType.string()";
case "UTF8" -> "PrimitiveType.utf8()";
case "JSON" -> "PrimitiveType.json()";
default -> throw new IllegalArgumentException("Unknown db type: " + type);
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tech.ydb.yoj.repository.ydb.statement;

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

@Value
public class Count {
@Column(dbType = "UINT64")
@Column(dbType = DbType.UINT64)
long count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Value;
import org.junit.Assert;
import org.junit.Test;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.FieldValueType;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.GlobalIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Value;
import lombok.With;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Column;
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(dbType = DbType.UINT32)
long value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.Table;
import tech.ydb.yoj.repository.BaseDb;
Expand All @@ -15,7 +16,6 @@
import tech.ydb.yoj.repository.db.TxManager;
import tech.ydb.yoj.repository.db.TxOptions;
import tech.ydb.yoj.repository.test.RepositoryTestSupport;
import tech.ydb.yoj.repository.ydb.DbType;
import tech.ydb.yoj.repository.ydb.TestYdbConfig;
import tech.ydb.yoj.repository.ydb.YdbRepository;
import tech.ydb.yoj.repository.ydb.YdbRepositoryTransaction;
Expand Down
Loading

0 comments on commit 14a6948

Please sign in to comment.