Skip to content

Commit

Permalink
Support time converter, support oplock exception
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed May 31, 2022
1 parent b0d9a5f commit feaedcd
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 13 deletions.
6 changes: 3 additions & 3 deletions example/jimmer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ plugins {
}

group 'org.babyfish.jimmer.example.core'
version '0.0.18'
version '0.0.19'

repositories {
mavenCentral()
}

dependencies {

implementation 'org.babyfish.jimmer:jimmer-core:0.0.18'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.18'
implementation 'org.babyfish.jimmer:jimmer-core:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'

implementation 'javax.validation:validation-api:2.0.1.Final'
}
Expand Down
6 changes: 3 additions & 3 deletions example/jimmer-sql-graphql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'org.babyfish.jimmer.sql.example'
version = '0.0.18'
version = '0.0.19'
sourceCompatibility = '1.8'

repositories {
Expand All @@ -14,8 +14,8 @@ repositories {

dependencies {

implementation 'org.babyfish.jimmer:jimmer-sql:0.0.18'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.18'
implementation 'org.babyfish.jimmer:jimmer-sql:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'

implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'org.springframework.boot:spring-boot-starter-web'
Expand Down
6 changes: 3 additions & 3 deletions example/jimmer-sql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ plugins {
}

group 'org.babyfish.jimmer.example.sql'
version '0.0.18'
version '0.0.19'

repositories {
mavenCentral()
}

dependencies {

implementation 'org.babyfish.jimmer:jimmer-sql:0.0.18'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.18'
implementation 'org.babyfish.jimmer:jimmer-sql:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'

implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.shell:spring-shell-starter:2.0.1.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.babyfish.jimmer.sql.runtime.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.babyfish.jimmer.sql.example.model;

import org.babyfish.jimmer.sql.Key;
import org.babyfish.jimmer.sql.meta.UUIDIdGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.List;
Expand All @@ -10,10 +14,13 @@
public interface Author {

@Id
@GeneratedValue(generator = UUIDIdGenerator.FULL_NAME)
UUID id();

@Key
String firstName();

@Key
String lastName();

Gender gender();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.babyfish.jimmer.sql.example.model;

import org.babyfish.jimmer.sql.Key;
import org.babyfish.jimmer.sql.meta.UUIDIdGenerator;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
Expand All @@ -9,10 +12,13 @@
public interface Book {

@Id
@GeneratedValue(generator = UUIDIdGenerator.FULL_NAME)
UUID id();

@Key
String name();

@Key
int edition();

BigDecimal price();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.babyfish.jimmer.sql.example.model;

import org.babyfish.jimmer.sql.Key;
import org.babyfish.jimmer.sql.meta.UUIDIdGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.Null;
Expand All @@ -11,8 +15,10 @@
public interface BookStore {

@Id
@GeneratedValue(generator = UUIDIdGenerator.FULL_NAME)
UUID id();

@Key
String name();

@Null
Expand Down
2 changes: 1 addition & 1 deletion project/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
allprojects {
group = "org.babyfish.jimmer"
version = "0.0.18"
version = "0.0.19"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.babyfish.jimmer.sql;

import org.babyfish.jimmer.meta.ImmutableType;

public class OptimisticLockException extends RuntimeException {

private final ImmutableType entityType;

private final Object entityId;

private final int entityVersion;

private final String path;

public OptimisticLockException(
ImmutableType entityType,
Object entityId,
int entityVersion,
String path
) {
super(
"Cannot update the entity whose type is \"" +
entityType +
"\", id is \"" +
entityId +
"\" and version is \"" +
entityVersion +
"\" at the path \"" +
path +
"\""
);
this.entityType = entityType;
this.entityId = entityId;
this.entityVersion = entityVersion;
this.path = path;
}

public ImmutableType getEntityType() {
return entityType;
}

public Object getEntityId() {
return entityId;
}

public String getPath() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.babyfish.jimmer.runtime.DraftSpi;
import org.babyfish.jimmer.runtime.ImmutableSpi;
import org.babyfish.jimmer.runtime.Internal;
import org.babyfish.jimmer.sql.OptimisticLockException;
import org.babyfish.jimmer.sql.ast.Expression;
import org.babyfish.jimmer.sql.ast.impl.query.Queries;
import org.babyfish.jimmer.sql.ast.mutation.AffectedTable;
Expand Down Expand Up @@ -483,6 +484,13 @@ private void update(DraftSpi draftSpi, boolean excludeKeyProps) {
increaseDraftVersion(draftSpi);
}
cache.save(draftSpi);
} else if (version != null) {
throw new OptimisticLockException(
type,
draftSpi.__get(type.getIdProp().getName()),
version,
path
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.*;
import java.util.Date;

public class Converts {

Expand Down Expand Up @@ -42,11 +46,69 @@ public static Object tryConvert(Object value, Class<?> expectedType) {
}
if (expectedType == BigDecimal.class) {
if (value instanceof BigInteger) {
return ((BigInteger)value).toString();
return new BigDecimal(value.toString());
}
if (value instanceof Double || value instanceof Float) {
return BigDecimal.valueOf(((Number) value).longValue());
}
return BigDecimal.valueOf(num.doubleValue());
}
}
if (value instanceof Instant) {
return tryConvertInstant((Instant) value, expectedType);
}
if (value instanceof Date) {
return tryConvertInstant(((Date) value).toInstant(), expectedType);
}
if (value instanceof LocalDate) {
return tryConvertInstant(Instant.from((LocalDate) value), expectedType);
}
if (value instanceof LocalTime) {
return tryConvertInstant(Instant.from((LocalTime) value), expectedType);
}
if (value instanceof LocalDateTime) {
return tryConvertInstant(Instant.from((LocalDateTime) value), expectedType);
}
if (value instanceof OffsetDateTime) {
return tryConvertInstant(Instant.from((OffsetDateTime) value), expectedType);
}
if (value instanceof ZonedDateTime) {
return tryConvertInstant(Instant.from((ZonedDateTime) value), expectedType);
}
return null;
}

private static Object tryConvertInstant(Instant instant, Class<?> expectedType) {
if (expectedType == Instant.class) {
return instant;
}
if (expectedType == Date.class) {
return new Date(instant.toEpochMilli());
}
if (expectedType == java.sql.Date.class) {
return new java.sql.Date(instant.toEpochMilli());
}
if (expectedType == Time.class) {
return new Time(instant.toEpochMilli());
}
if (expectedType == Timestamp.class) {
return new Timestamp(instant.toEpochMilli());
}
if (expectedType == LocalDate.class) {
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
}
if (expectedType == LocalTime.class) {
return instant.atZone(ZoneId.systemDefault()).toLocalTime();
}
if (expectedType == LocalDateTime.class) {
return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
}
if (expectedType == OffsetDateTime.class) {
return instant.atZone(ZoneId.systemDefault()).toOffsetDateTime();
}
if (expectedType == ZonedDateTime.class) {
return instant.atZone(ZoneId.systemDefault()).toOffsetDateTime().toZonedDateTime();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void testUpdateJoinByPostgresErrorByOuterJoin() {
@Test
public void testDelete() {
executeAndExpectRowCount(
getSqlClient().createDelete(BookTableEx.class, (d, book) -> {
getSqlClient().createDelete(BookTable.class, (d, book) -> {
d.where(book.name().eq("Learning GraphQL"));
}),
ctx -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.babyfish.jimmer.sql.mutation;

import org.babyfish.jimmer.sql.OptimisticLockException;
import org.babyfish.jimmer.sql.ast.mutation.AffectedTable;
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.babyfish.jimmer.sql.common.AbstractMutationTest;
import static org.babyfish.jimmer.sql.common.Constants.*;

import org.babyfish.jimmer.sql.model.*;
import org.babyfish.jimmer.sql.runtime.DbNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
Expand Down Expand Up @@ -646,4 +648,41 @@ public void testUpsertMatchedWithInverseManyToMany() {
}
);
}

@Test
public void test() {
executeAndExpectResult(
getSqlClient().getEntities().saveCommand(
BookStoreDraft.$.produce(store -> {
store.setName("MANNING").setVersion(1);
})
),
ctx -> {
ctx.statement(it -> {
it.sql(
"select tb_1_.ID, tb_1_.NAME " +
"from BOOK_STORE as tb_1_ " +
"where tb_1_.NAME = ?"
);
});
ctx.statement(it -> {
it.sql(
"update BOOK_STORE " +
"set VERSION = VERSION + 1 " +
"where ID = ? and VERSION = ?"
);
});
ctx.throwable(it -> {
it.message(
"Cannot update the entity whose " +
"type is \"org.babyfish.jimmer.sql.model.BookStore\", " +
"id is \"2fa3955e-3e83-49b9-902e-0465c109c779\" and " +
"version is \"1\" at the path " +
"\"<root>\""
);
it.type(OptimisticLockException.class);
});
}
);
}
}

0 comments on commit feaedcd

Please sign in to comment.