Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from xezzon/develop
Browse files Browse the repository at this point in the history
0.14.0
  • Loading branch information
xezzon authored Jan 27, 2024
2 parents 7485666 + fcdfc04 commit 9fa24f4
Show file tree
Hide file tree
Showing 36 changed files with 712 additions and 418 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Maven Package

on:
release:
types: [released]
types: [ released ]

jobs:
build:
Expand All @@ -20,6 +20,7 @@ jobs:
distribution: 'temurin'
java-version: '17'
cache: 'maven'

- name: Build with Maven
run: mvn -B package --file pom.xml

Expand All @@ -33,8 +34,12 @@ jobs:
server-username: MAVEN_USERNAME # env variable for username in deploy
server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import

- name: Set Version
run: mvn versions:set -DnewVersion=$GITHUB_REF

- name: Publish to Apache Maven Central
run: mvn deploy -DskipTests=true
run: mvn deploy -P report,release -DskipTests=true
env:
MAVEN_USERNAME: ${{ vars.MAVEN_USERNAME }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
java-version: '17'
cache: 'maven'
- name: Test with Maven
run: mvn -B test --file pom.xml
run: mvn -B test -P test --file pom.xml
3 changes: 2 additions & 1 deletion micronaut-tao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jpa.version>3.1.0</jpa.version>
<lombok.version>1.18.24</lombok.version>
<micronaut.data.version>4.3.1</micronaut.data.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -134,7 +135,7 @@
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>${micronaut.version}</version>
<version>${micronaut.data.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected BaseExceptionHandler(String errorMessage) {
@Error(value = ClientException.class, global = true)
@Status(HttpStatus.BAD_REQUEST)
public Result<Void> handleClientException(ClientException e) {
return Result.fail(e.getCode(), e.getMessage());
return Result.fail(e);
}

/**
Expand All @@ -37,7 +37,7 @@ public Result<Void> handleClientException(ClientException e) {
@Error(value = ServerException.class, global = true)
@Status(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<Void> handleServerException(ServerException e) {
return Result.fail(e.getCode(), errorMessage);
return Result.fail(e, errorMessage);
}

/**
Expand All @@ -46,14 +46,15 @@ public Result<Void> handleServerException(ServerException e) {
@Error(value = ThirdPartyException.class, global = true)
@Status(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<Void> handleThirdPartyException(ThirdPartyException e) {
return Result.fail(e.getCode(), errorMessage);
return Result.fail(e, errorMessage);
}

/**
* 多消息异常
*/
@Error(MultiException.class)
@Status(HttpStatus.BAD_REQUEST)
@Deprecated
public Result<?> handleMultiException(MultiException e) {
return Result.fail(e.getCode(), errorMessage, e.getMessages());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import jakarta.persistence.Transient;
import jakarta.persistence.Version;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.Instant;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
* 数据库通用实体类生成基础方法
* @author xezzon
*/
@Getter
@Setter
@Accessors(chain = true)
@MappedSuperclass
public abstract class BaseEntity<ID> implements Serializable {

Expand All @@ -26,58 +32,31 @@ public abstract class BaseEntity<ID> implements Serializable {
*/
@Column(nullable = false, updatable = false)
@DateCreated
protected LocalDateTime createTime;
protected Instant createTime;
/**
* 记录最后更新时间
*/
@Column(nullable = false)
@DateUpdated
@Version
protected LocalDateTime updateTime;
protected Instant updateTime;
/**
* 逻辑删除标记 删除时间不为空且大于当前时间则认为已删除<br/>
* 注意 时间精度至少要到毫秒级
*/
@Column()
protected LocalDateTime deleteTime;
protected Instant deleteTime;

public abstract ID getId();

public abstract BaseEntity<ID> setId(ID id);

public LocalDateTime getCreateTime() {
return this.createTime;
}

public BaseEntity<ID> setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
return this;
}

public LocalDateTime getUpdateTime() {
return this.updateTime;
}

public BaseEntity<ID> setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
return this;
}

public LocalDateTime getDeleteTime() {
return this.deleteTime;
}

public BaseEntity<ID> setDeleteTime(LocalDateTime deleteTime) {
this.deleteTime = deleteTime;
return this;
}

@Transient
public boolean isDeleted() {
if (this.deleteTime == null) {
return false;
}
return !this.deleteTime.isAfter(LocalDateTime.now());
return !this.deleteTime.isAfter(Instant.now());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
import io.github.xezzon.tao.retrieval.CommonQueryFilterParser.ParenthesisContext;
import io.github.xezzon.tao.retrieval.CommonQueryFilterParser.PredicateContext;
import io.github.xezzon.tao.retrieval.FilterOperatorEnum;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -75,25 +79,25 @@ public BooleanExpression visitPredicate(PredicateContext ctx) {
String rawOperator = ctx.OP().getText();
String rawValue = ctx.VALUE().getText();
/* 解析字段 */
SimpleExpression<?> field =
SimpleExpression<?> column =
(SimpleExpression<?>) ReflectUtil.getFieldValue(dataObj, rawField);
if (field == null) {
if (column == null) {
throw nonexistentField(ctx.getText());
}
/* 解析操作符 */
FilterOperatorEnum op = FilterOperatorEnum.valueOf(rawOperator);
// 空操作符单独处理
if (op == FilterOperatorEnum.NULL) {
if (Boolean.parseBoolean(rawValue)) {
return field.isNull();
return column.isNull();
} else {
return field.isNotNull();
return column.isNotNull();
}
}
/* 解析值 */
rawValue = StrUtil.strip(rawValue, "'");
/* 组装查询语句 */
if (field instanceof StringPath f) {
if (column instanceof StringPath f) {
return switch (op) {
case EQ -> f.eq(rawValue);
case NE -> f.ne(rawValue);
Expand All @@ -102,61 +106,76 @@ public BooleanExpression visitPredicate(PredicateContext ctx) {
case OUT -> f.notIn(StrUtil.split(rawValue, ","));
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof EnumPath<?> f) {
} else if (column instanceof EnumPath f) {
Class<Enum> enumClazz = (Class<Enum>) ReflectUtil.getField(this.clazz, rawField).getType();
Set<Enum> values = StrUtil.split(rawValue, ",").parallelStream()
Set<Enum> values = Arrays.stream(rawValue.split(",")).parallel()
.map(o -> Enum.valueOf(enumClazz, o))
.collect(Collectors.toSet());
return switch (op) {
case EQ, IN -> ReflectUtil.invoke(f, "in", values);
case NE, OUT -> ReflectUtil.invoke(f, "notIn", values);
case EQ, IN -> f.in(values);
case NE, OUT -> f.notIn(values);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof NumberPath<?> f) {
} else if (column instanceof NumberPath f) {
BigDecimal value = new BigDecimal(rawValue);
return switch (op) {
case EQ -> ReflectUtil.invoke(f, "eq", value);
case NE -> ReflectUtil.invoke(f, "ne", value);
case EQ -> f.eq(value);
case NE -> f.ne(value);
case GT -> f.gt(value);
case LT -> f.lt(value);
case GE -> f.goe(value);
case LE -> f.loe(value);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof DateTimePath<?> f) {
LocalDateTime value = LocalDateTime.parse(rawValue);
return switch (op) {
case EQ -> ReflectUtil.invoke(f, "eq", value);
case NE -> ReflectUtil.invoke(f, "ne", value);
case GT -> ReflectUtil.invoke(f, "gt", value);
case LT -> ReflectUtil.invoke(f, "lt", value);
case GE -> ReflectUtil.invoke(f, "goe", value);
case LE -> ReflectUtil.invoke(f, "loe", value);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof DatePath<?> f) {
} else if (column instanceof DateTimePath f) {
Field field = clazz.getDeclaredField(rawField);
if (Objects.equals(field.getType(), LocalDateTime.class)) {
LocalDateTime value = LocalDateTime.parse(rawValue);
return switch (op) {
case EQ -> f.eq(value);
case NE -> f.ne(value);
case GT -> f.gt(value);
case LT -> f.lt(value);
case GE -> f.goe(value);
case LE -> f.loe(value);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (Objects.equals(field.getType(), Instant.class)) {
Instant value = Instant.parse(rawValue);
return switch (op) {
case EQ -> f.eq(value);
case NE -> f.ne(value);
case GT -> f.gt(value);
case LT -> f.lt(value);
case GE -> f.goe(value);
case LE -> f.loe(value);
default -> throw unsupportedOperator(ctx.getText());
};
}
throw uoe(ctx.getText());
} else if (column instanceof DatePath f) {
LocalDate value = LocalDate.parse(rawValue);
return switch (op) {
case EQ -> ReflectUtil.invoke(f, "eq", value);
case NE -> ReflectUtil.invoke(f, "ne", value);
case GT -> ReflectUtil.invoke(f, "gt", value);
case LT -> ReflectUtil.invoke(f, "lt", value);
case GE -> ReflectUtil.invoke(f, "goe", value);
case LE -> ReflectUtil.invoke(f, "loe", value);
case EQ -> f.eq(value);
case NE -> f.ne(value);
case GT -> f.gt(value);
case LT -> f.lt(value);
case GE -> f.goe(value);
case LE -> f.loe(value);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof TimePath<?> f) {
} else if (column instanceof TimePath f) {
LocalTime value = LocalTime.parse(rawValue);
return switch (op) {
case EQ -> ReflectUtil.invoke(f, "eq", value);
case NE -> ReflectUtil.invoke(f, "ne", value);
case GT -> ReflectUtil.invoke(f, "gt", value);
case LT -> ReflectUtil.invoke(f, "lt", value);
case GE -> ReflectUtil.invoke(f, "goe", value);
case LE -> ReflectUtil.invoke(f, "loe", value);
case EQ -> f.eq(value);
case NE -> f.ne(value);
case GT -> f.gt(value);
case LT -> f.lt(value);
case GE -> f.goe(value);
case LE -> f.loe(value);
default -> throw unsupportedOperator(ctx.getText());
};
} else if (field instanceof BooleanPath f) {
} else if (column instanceof BooleanPath f) {
boolean value = Boolean.parseBoolean(rawValue);
return switch (op) {
case EQ -> f.eq(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.Version;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -45,7 +45,7 @@ public static <T> JPAUpdateClause getUpdateClause(
EntityPathBase<T> dataObj
) {
JPAUpdateClause clause = queryFactory.update(dataObj);
LocalDateTime current = LocalDateTime.now();
Instant current = Instant.now();
Set<Field> fields = Arrays.stream(obj.getClass().getDeclaredFields())
.filter(field -> Objects.nonNull(field.getAnnotation(Column.class)))
.filter(field -> field.getAnnotation(Column.class).updatable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.micronaut.data.jpa.repository.JpaRepository;
import io.micronaut.transaction.annotation.Transactional;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.Instant;
import java.util.Optional;

/**
Expand All @@ -22,7 +22,7 @@ default void logicDelete(ID id) {
if (entity.isEmpty()) {
return;
}
entity.get().setDeleteTime(LocalDateTime.now());
entity.get().setDeleteTime(Instant.now());
update(entity.get());
};
}
Loading

0 comments on commit 9fa24f4

Please sign in to comment.