Skip to content

Commit

Permalink
支持REPLACE & 优化SPI扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
veasion committed Oct 22, 2022
1 parent 6bcce3a commit 41a7cf3
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ veasion-db 是一个轻量级持久层db框架,除slf4j-api外不依赖任何
<dependency>
<groupId>cn.veasion</groupId>
<artifactId>veasion-db</artifactId>
<version>1.1.7</version>
<version>1.1.8</version>
</dependency>
```
支持sql解析生成veasion-db代码
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>cn.veasion</groupId>
<artifactId>veasion-db</artifactId>
<version>1.1.7</version>
<version>1.1.8</version>

<name>veasion-db</name>
<url>https://github.com/veasion/veasion-db</url>
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/cn/veasion/db/jdbc/AbstractSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public abstract class AbstractSQL<T> {
protected List<Object> values = new ArrayList<>();
protected DynamicTableExt dynamicTableExt = ServiceLoaderUtils.dynamicTableExt();

static TypeConvert typeConvert;

static {
typeConvert = ServiceLoaderUtils.loadOne(TypeConvert.class);
}
static TypeConvert typeConvert = ServiceLoaderUtils.typeConvert();

public String getSQL() {
return sql.toString();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/veasion/db/jdbc/DynamicTableExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public interface DynamicTableExt {

String getTableName(String tableName, Class<?> entityClazz, AbstractFilter<?> filter, Object source);

default int sort() {
return 0;
}

}
6 changes: 5 additions & 1 deletion src/main/java/cn/veasion/db/jdbc/InsertSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ private void insert(Class<?> entityClazz, List<Map<String, Object>> fieldValueMa
Map<String, String> fieldColumns = FieldUtils.entityFieldColumns(entityClazz);
Set<String> fields = fieldValueMapList.get(0).keySet();

sql.append("INSERT INTO ");
if (source instanceof EntityInsert && ((EntityInsert) source).isReplace()) {
sql.append("REPLACE INTO ");
} else {
sql.append("INSERT INTO ");
}
sql.append(getTableName(entityClazz, null, source)).append(" (");
for (String field : fields) {
appendInsertColumn(fieldColumns.get(field));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/veasion/db/query/AbstractQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public T page(PageParam pageParam) {
}

public T page(int page, int size) {
PageParam pageParam = ServiceLoaderUtils.loadOne(PageParam.class);
PageParam pageParam = ServiceLoaderUtils.pageParam();
if (pageParam != null) {
this.pageParam = pageParam;
this.pageParam.setPage(page);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cn/veasion/db/query/PageParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public int getSize() {
public void setSize(int size) {
this.size = size;
}

public int sort() {
return 0;
}

}
10 changes: 10 additions & 0 deletions src/main/java/cn/veasion/db/update/EntityInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class EntityInsert {
private Set<String> skipFields;
private boolean useGeneratedKeys = true;
private Map<String, Object> fieldValueMap;
private boolean replace;

public EntityInsert(Object entity) {
this.entity = Objects.requireNonNull(entity);
Expand Down Expand Up @@ -60,6 +61,15 @@ public void setEntityClass(Class<?> entityClass) {
this.entityClass = entityClass;
}

public EntityInsert withReplace() {
this.replace = true;
return this;
}

public boolean isReplace() {
return replace;
}

public void check(Class<?> mainEntityClass) {
if (entityClass == null) {
setEntityClass(mainEntityClass);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/veasion/db/utils/FieldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static boolean setValue(Object object, String field, Object value, boolea
return f != null;
}
} catch (Exception e) {
throw new DbException("字段赋值异常: " + field, e);
throw new DbException("字段赋值异常: " + field + " [" + (value == null ? null : value.getClass().getName()) + "]", e);
}
}

Expand Down
58 changes: 56 additions & 2 deletions src/main/java/cn/veasion/db/utils/ServiceLoaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.veasion.db.jdbc.DataSourceProvider;
import cn.veasion.db.jdbc.DynamicTableExt;
import cn.veasion.db.query.PageParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,6 +22,8 @@ public class ServiceLoaderUtils {

private static DataSourceProvider dataSourceProvider;
private static DynamicTableExt dynamicTableExt;
private static TypeConvert typeConvert;
private static PageParam pageParam;

public synchronized static DataSourceProvider dataSourceProvider() {
if (dataSourceProvider != null) {
Expand All @@ -39,13 +42,64 @@ public synchronized static DataSourceProvider dataSourceProvider() {
return dataSourceProvider;
}

public synchronized static DynamicTableExt dynamicTableExt() {
public static DynamicTableExt dynamicTableExt() {
if (dynamicTableExt != null) {
return dynamicTableExt;
}
return dynamicTableExt = loadOne(DynamicTableExt.class);
synchronized (ServiceLoaderUtils.class) {
if (dynamicTableExt != null) {
return dynamicTableExt;
}
List<DynamicTableExt> list = loadList(DynamicTableExt.class);
if (list.size() > 0) {
if (list.size() > 1) {
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
}
dynamicTableExt = list.get(0);
}
return dynamicTableExt;
}
}

public static TypeConvert typeConvert() {
if (typeConvert != null) {
return typeConvert;
}
synchronized (ServiceLoaderUtils.class) {
if (typeConvert != null) {
return typeConvert;
}
List<TypeConvert> list = loadList(TypeConvert.class);
if (list.size() > 0) {
if (list.size() > 1) {
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
}
typeConvert = list.get(0);
}
return typeConvert;
}
}

public static PageParam pageParam() {
if (pageParam != null) {
return pageParam;
}
synchronized (ServiceLoaderUtils.class) {
if (pageParam != null) {
return pageParam;
}
List<PageParam> list = loadList(PageParam.class);
if (list.size() > 0) {
if (list.size() > 1) {
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
}
pageParam = list.get(0);
}
return pageParam;
}
}

@Deprecated
public static <T> T loadOne(Class<T> clazz) {
List<T> list = loadList(clazz);
return list.size() > 0 ? list.get(0) : null;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/veasion/db/utils/TypeConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ default Object convertValue(Object value) {
return value;
}

default int sort() {
return 0;
}

}
6 changes: 1 addition & 5 deletions src/main/java/cn/veasion/db/utils/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
@SuppressWarnings("unchecked")
public class TypeUtils {

static TypeConvert typeConvert;

static {
typeConvert = ServiceLoaderUtils.loadOne(TypeConvert.class);
}
static TypeConvert typeConvert = ServiceLoaderUtils.typeConvert();

public static String getTableName(Class<?> entityClazz) {
Table annotation = entityClazz.getAnnotation(Table.class);
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/cn/veasion/db/update/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public static void main(String[] args) {
.eq("sno", "s001")
.notExists(SubQueryParam.build(new Q("1").eq("sno", "copy_s001")))
)));

// replace into
StudentPO student = getStudent();
student.setId(System.currentTimeMillis());
student.setDesc("replace: add");
studentDao.add(new EntityInsert(student).withReplace());
student.setDesc("replace: update");
studentDao.add(new EntityInsert(student).withReplace());
}

private static StudentPO getStudent() {
Expand Down

0 comments on commit 41a7cf3

Please sign in to comment.