Skip to content

Commit

Permalink
兼容clickhouse数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
veasion committed Oct 12, 2022
1 parent bb955a8 commit 6bcce3a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 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.6</version>
<version>1.1.7</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.6</version>
<version>1.1.7</version>

<name>veasion-db</name>
<url>https://github.com/veasion/veasion-db</url>
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/cn/veasion/db/jdbc/JdbcDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public static Object[] executeInsert(Connection connection, String sql, Object..
return keys.toArray();
}

/**
* 执行新增
*/
public static int executeInsertNoKeys(Connection connection, String sql, Object... params) {
int count;
PreparedStatement ps = null;
try {
ps = prepareStatement(connection, sql, params);
count = ps.executeUpdate();
LOGGER.info("<== Updates: {}", count);
} catch (SQLException e) {
throw new DbException("新增异常", e);
} finally {
closeAll(ps, null);
}
return count;
}

/**
* 列表查询
*
Expand Down Expand Up @@ -272,11 +290,12 @@ public static Object queryOnly(Connection connection, String sql, Object... para
}

private static PreparedStatement prepareStatement(Connection connection, String sql, Object... params) throws SQLException {
return prepareStatement(connection, Statement.NO_GENERATED_KEYS, sql, params);
// Statement.NO_GENERATED_KEYS
return prepareStatement(connection, null, sql, params);
}

private static PreparedStatement prepareStatement(Connection connection, int autoGeneratedKeys, String sql, Object... params) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, autoGeneratedKeys);
private static PreparedStatement prepareStatement(Connection connection, Integer autoGeneratedKeys, String sql, Object... params) throws SQLException {
PreparedStatement ps = autoGeneratedKeys == null ? connection.prepareStatement(sql) : connection.prepareStatement(sql, autoGeneratedKeys);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/cn/veasion/db/jdbc/JdbcEntityDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public ID add(EntityInsert entityInsert) {
return InterceptorUtils.intercept(new EntityDaoInvocation<>(this, "add", new Object[]{entityInsert}, () -> {
InsertSQL insertSQL = entityInsert.sqlValue();
return executeJdbc(JdbcTypeEnum.INSERT, connection -> {
Object[] objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues());
Object[] objects;
if (entityInsert.isUseGeneratedKeys()) {
objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues());
} else {
JdbcDao.executeInsertNoKeys(connection, insertSQL.getSQL(), insertSQL.getValues());
objects = new Object[0];
}
if (objects.length > 0) {
ID id = (ID) TypeUtils.convert(objects[0], idField.getType());
if (entity instanceof IBaseId) {
Expand All @@ -80,7 +86,13 @@ public ID[] batchAdd(BatchEntityInsert batchEntityInsert) {
Field idField = FieldUtils.getIdField(getEntityClass());
List<?> entityList = batchEntityInsert.getEntityList();
return executeJdbc(JdbcTypeEnum.INSERT, connection -> {
Object[] objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues());
Object[] objects;
if (batchEntityInsert.isUseGeneratedKeys()) {
objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues());
} else {
JdbcDao.executeInsertNoKeys(connection, insertSQL.getSQL(), insertSQL.getValues());
objects = new Object[0];
}
if (idField == null) {
return null;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/veasion/db/update/BatchEntityInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BatchEntityInsert {
private List<?> entityList;
private Class<?> entityClass;
private Set<String> skipFields;
private boolean useGeneratedKeys = true;
private AbstractQuery<?> insertSelectQuery;
private List<Map<String, Object>> fieldValueMapList;

Expand All @@ -43,6 +44,15 @@ public BatchEntityInsert(AbstractQuery<?> insertSelectQuery) {
this.insertSelectQuery = Objects.requireNonNull(insertSelectQuery);
}

public BatchEntityInsert setUseGeneratedKeys(boolean useGeneratedKeys) {
this.useGeneratedKeys = useGeneratedKeys;
return this;
}

public boolean isUseGeneratedKeys() {
return useGeneratedKeys;
}

public List<?> getEntityList() {
return entityList;
}
Expand Down
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 @@ -21,6 +21,7 @@ public class EntityInsert {
private Object entity;
private Class<?> entityClass;
private Set<String> skipFields;
private boolean useGeneratedKeys = true;
private Map<String, Object> fieldValueMap;

public EntityInsert(Object entity) {
Expand All @@ -42,6 +43,15 @@ public Map<String, Object> getFieldValueMap() {
return fieldValueMap;
}

public EntityInsert setUseGeneratedKeys(boolean useGeneratedKeys) {
this.useGeneratedKeys = useGeneratedKeys;
return this;
}

public boolean isUseGeneratedKeys() {
return useGeneratedKeys;
}

public Class<?> getEntityClass() {
return entityClass;
}
Expand Down

0 comments on commit 6bcce3a

Please sign in to comment.