Skip to content

Commit

Permalink
BREAKING: Make DbType an enum and move it to yoj-databind
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Lavrukov <[email protected]>
  • Loading branch information
lavrukov and Alexander Lavrukov authored Feb 15, 2024
1 parent dffa13b commit e6e0f43
Show file tree
Hide file tree
Showing 40 changed files with 264 additions and 320 deletions.
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-bom</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>YOJ - Bill of Materials</name>
Expand Down
2 changes: 1 addition & 1 deletion databind/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
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(null),
/**
* 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 type;

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

public String typeString() {
return type;
}
}
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 @@ -435,12 +436,12 @@ private JavaField(JavaField javaField, JavaField parent) {
* @return the DB column type for data binding if specified, {@code null} otherwise
* @see Column
*/
public String getDbType() {
public DbType getDbType() {
Column annotation = field.getColumn();
if (annotation != null && !annotation.dbType().isEmpty()) {
if (annotation != null) {
return annotation.dbType();
}
return null;
return DbType.DEFAULT;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
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;
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;

import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;

public class ColumnTest {
Expand Down Expand Up @@ -48,15 +47,17 @@ public void plainFieldTypeTest() {
Schema<Plain> schema = newSchema(Plain.class);
assertThat(schema.flattenFields().stream()
.map(Schema.JavaField::getDbType)
.filter(Objects::nonNull)).isEmpty();
.filter(dbType -> dbType != DbType.DEFAULT)
).isEmpty();
}

@Test
public void annotatedFieldDbTypeTest() {
Schema<UInt32> schema = newSchema(UInt32.class);
assertThat(schema.flattenFields().stream()
.map(Schema.JavaField::getDbType))
.containsOnly("UInt32");
.map(DbType::typeString)
.containsOnly("UINT32");
}

@Test
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;
}
}
2 changes: 1 addition & 1 deletion json-jackson-v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>YDB ORM for Java (YOJ)</name>
Expand Down
2 changes: 1 addition & 1 deletion repository-inmemory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion repository-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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.Changefeed;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.repository.db.Entity;
Expand All @@ -21,7 +22,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 @@ -20,7 +21,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 @@ -29,28 +30,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 @@ -61,11 +62,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;

StringValueWrapper stringValueWrapper;
Expand Down
2 changes: 1 addition & 1 deletion repository-ydb-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>tech.ydb.yoj</groupId>
<artifactId>yoj-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Loading

0 comments on commit e6e0f43

Please sign in to comment.