From 56eb42cadcfd01176d840f409953df1b42f2e969 Mon Sep 17 00:00:00 2001 From: whz <1415238952@qq.com> Date: Sun, 17 Mar 2024 00:37:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=8C=87=E5=AE=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=96=B9=E8=A8=80=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=96=B9=E8=A8=80=E7=A1=AC=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MachineSshController.java | 3 +- .../func/assets/server/MachineSshServer.java | 3 +- .../jpom/service/h2db/BaseDbService.java | 23 +++++++------ .../service/h2db/BaseWorkspaceService.java | 6 ++-- .../dromara/jpom/system/db/DataInitEvent.java | 6 ++-- .../dromara/jpom/db/BaseDbCommonService.java | 33 ++++++++++--------- 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/modules/server/src/main/java/org/dromara/jpom/func/assets/controller/MachineSshController.java b/modules/server/src/main/java/org/dromara/jpom/func/assets/controller/MachineSshController.java index 549d02c3a..11fc274ad 100644 --- a/modules/server/src/main/java/org/dromara/jpom/func/assets/controller/MachineSshController.java +++ b/modules/server/src/main/java/org/dromara/jpom/func/assets/controller/MachineSshController.java @@ -35,6 +35,7 @@ import org.dromara.jpom.common.interceptor.PermissionInterceptor; import org.dromara.jpom.common.validator.ValidatorItem; import org.dromara.jpom.common.validator.ValidatorRule; import org.dromara.jpom.configuration.AssetsConfig; +import org.dromara.jpom.dialect.DialectUtil; import org.dromara.jpom.func.BaseGroupNameController; import org.dromara.jpom.func.assets.model.MachineSshModel; import org.dromara.jpom.func.assets.server.MachineSshServer; @@ -196,7 +197,7 @@ public class MachineSshController extends BaseGroupNameController { Entity entity = Entity.create(); entity.set("host", sshModel.getHost()); entity.set("port", sshModel.getPort()); - entity.set("`user`", sshModel.getUser()); + entity.set(DialectUtil.wrapField("user"), sshModel.getUser()); entity.set("connectType", sshModel.getConnectType()); Opt.ofBlankAble(id).ifPresent(s -> entity.set("id", StrUtil.format(" <> {}", s))); boolean exists = machineSshServer.exists(entity); diff --git a/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineSshServer.java b/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineSshServer.java index b9e2d5612..9038876f8 100644 --- a/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineSshServer.java +++ b/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineSshServer.java @@ -38,6 +38,7 @@ import org.dromara.jpom.common.ILoadEvent; import org.dromara.jpom.common.ServerConst; import org.dromara.jpom.configuration.AssetsConfig; import org.dromara.jpom.cron.CronUtils; +import org.dromara.jpom.dialect.DialectUtil; import org.dromara.jpom.func.assets.AssetsExecutorPoolService; import org.dromara.jpom.func.assets.model.MachineSshModel; import org.dromara.jpom.func.system.service.ClusterInfoService; @@ -142,7 +143,7 @@ public class MachineSshServer extends BaseDbService implements where.set("host", value.getHost()); where.set("port", value.getPort()); // 关键词,如果不加 ` 会查询不出结果 - where.set("`user`", value.getUser()); + where.set(DialectUtil.wrapField("user"), value.getUser()); where.set("connectType", value.getConnectType()); int update = sshService.update(entity, where); Assert.state(update > 0, "更新 SSH 表机器id 失败:" + value.getName()); diff --git a/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseDbService.java b/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseDbService.java index 2ec0ba0cd..fac452c88 100644 --- a/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseDbService.java +++ b/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseDbService.java @@ -28,6 +28,7 @@ import org.dromara.jpom.common.BaseServerController; import org.dromara.jpom.common.Const; import org.dromara.jpom.db.BaseDbCommonService; import org.dromara.jpom.db.DbExtConfig; +import org.dromara.jpom.dialect.DialectUtil; import org.dromara.jpom.model.BaseDbModel; import org.dromara.jpom.model.BaseUserModifyDbModel; import org.dromara.jpom.model.PageResultDto; @@ -88,7 +89,8 @@ public abstract class BaseDbService extends BaseDbCommonS * @return list */ public List listGroup() { - String sql = "select `GROUP` from " + getTableName() + " group by `GROUP`"; + String group = DialectUtil.wrapField("group"); + String sql = String.format("select %s from %s group by %s",group,getTableName(),group); return this.listGroupByName(sql, "group"); } @@ -99,7 +101,7 @@ public abstract class BaseDbService extends BaseDbCommonS * @return list */ public List listGroupName() { - String sql = "select `groupName` from " + this.getTableName() + " group by `groupName`"; + String sql = "select groupName from " + this.getTableName() + " group by groupName"; return this.listGroupByName(sql, "groupName"); } @@ -133,7 +135,8 @@ public abstract class BaseDbService extends BaseDbCommonS */ public void repairGroupFiled() { Assert.state(this.canGroup, "当前数据表不支持分组"); - String sql = "update " + getTableName() + " set `GROUP`=? where `GROUP` is null or `GROUP`=''"; + String group = DialectUtil.wrapField("group"); + String sql = String.format("update %s set %s =? where %s is null or %s = ''",getTableName(),group,group,group); super.execute(sql, Const.DEFAULT_GROUP_NAME); } @@ -221,7 +224,7 @@ public abstract class BaseDbService extends BaseDbCommonS private void removeUpdate(Entity entity) { for (String s : new String[]{ID_STR, "createTimeMillis", "createUser"}) { - entity.remove(StrUtil.format("`{}`", s)); + entity.remove(DialectUtil.wrapField(s)); entity.remove(s); } } @@ -591,11 +594,11 @@ public abstract class BaseDbService extends BaseDbCommonS } key = StrUtil.removeAll(key, "%"); if (StrUtil.startWith(stringStringEntry.getKey(), "%") && StrUtil.endWith(stringStringEntry.getKey(), "%")) { - where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '%{}%'", value)); + where.set(DialectUtil.wrapField(key), StrUtil.format(" like '%{}%'", value)); } else if (StrUtil.endWith(stringStringEntry.getKey(), "%")) { - where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '{}%'", value)); + where.set(DialectUtil.wrapField(key), StrUtil.format(" like '{}%'", value)); } else if (StrUtil.startWith(stringStringEntry.getKey(), "%")) { - where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '%{}'", value)); + where.set(DialectUtil.wrapField(key), StrUtil.format(" like '%{}'", value)); } else if (StrUtil.containsIgnoreCase(key, "time") && StrUtil.contains(value, "~")) { // 时间筛选 String[] val = StrUtil.splitToArray(value, "~"); @@ -632,15 +635,15 @@ public abstract class BaseDbService extends BaseDbCommonS ignoreField.add(timeKey); } else if (StrUtil.endWith(key, ":in")) { String inKey = StrUtil.removeSuffix(key, ":in"); - where.set(StrUtil.format("`{}`", inKey), StrUtil.split(value, StrUtil.COMMA)); + where.set(DialectUtil.wrapField(inKey), StrUtil.split(value, StrUtil.COMMA)); } else { - where.set(StrUtil.format("`{}`", key), value); + where.set(DialectUtil.wrapField(key), value); } } // 排序 if (StrUtil.isNotEmpty(orderField)) { orderField = StrUtil.removeAll(orderField, "%"); - pageReq.addOrder(new Order(StrUtil.format("`{}`", orderField), StrUtil.equalsIgnoreCase(order, "ascend") ? Direction.ASC : Direction.DESC)); + pageReq.addOrder(new Order(DialectUtil.wrapField(orderField), StrUtil.equalsIgnoreCase(order, "ascend") ? Direction.ASC : Direction.DESC)); } return this.listPage(where, pageReq, fill); } diff --git a/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseWorkspaceService.java b/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseWorkspaceService.java index c0b26da91..27ed531a2 100644 --- a/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseWorkspaceService.java +++ b/modules/server/src/main/java/org/dromara/jpom/service/h2db/BaseWorkspaceService.java @@ -22,6 +22,7 @@ import cn.hutool.extra.spring.SpringUtil; import org.dromara.jpom.common.BaseServerController; import org.dromara.jpom.common.Const; import org.dromara.jpom.common.ServerConst; +import org.dromara.jpom.dialect.DialectUtil; import org.dromara.jpom.exception.PermissionException; import org.dromara.jpom.model.BaseWorkspaceModel; import org.dromara.jpom.model.PageResultDto; @@ -357,7 +358,8 @@ public abstract class BaseWorkspaceService extends */ public List listGroup(HttpServletRequest request) { String workspaceId = getCheckUserWorkspace(request); - String sql = "select `GROUP` from " + getTableName() + " where workspaceId=? group by `GROUP`"; - return super.listGroupByName(sql, "GROUP", workspaceId); + String group = DialectUtil.wrapField("group"); + String sql = String.format("select %s from %s where workspaceId=? group by %s",group,getTableName(),group); + return super.listGroupByName(sql, group, workspaceId); } } diff --git a/modules/server/src/main/java/org/dromara/jpom/system/db/DataInitEvent.java b/modules/server/src/main/java/org/dromara/jpom/system/db/DataInitEvent.java index 9093c1809..52f9810b8 100644 --- a/modules/server/src/main/java/org/dromara/jpom/system/db/DataInitEvent.java +++ b/modules/server/src/main/java/org/dromara/jpom/system/db/DataInitEvent.java @@ -98,7 +98,7 @@ public class DataInitEvent implements ILoadEvent, ICacheTask { TableName tableName1 = parents.getAnnotation(TableName.class); Assert.notNull(tableName1, "父级表信息配置错误," + aClass); } - String sql = "select `workspaceId`,count(1) as allCount from " + tableName.value() + " group by `workspaceId`"; + String sql = "select workspaceId,count(1) as allCount from " + tableName.value() + " group by workspaceId"; List query = workspaceService.query(sql); for (Entity entity : query) { String workspaceId = (String) entity.get("workspaceId"); @@ -130,14 +130,14 @@ public class DataInitEvent implements ILoadEvent, ICacheTask { public void clearErrorWorkspace(String tableName) { Assert.state(errorWorkspaceTable.containsKey(tableName), "当前表没有错误数据"); Set workspaceIds = this.allowWorkspaceIds(); - String sql = "select `workspaceId`,count(1) as allCount from " + tableName + " group by `workspaceId`"; + String sql = "select workspaceId,count(1) as allCount from " + tableName + " group by workspaceId"; List query = workspaceService.query(sql); for (Entity entity : query) { String workspaceId = (String) entity.get("workspaceId"); if (workspaceIds.contains(workspaceId)) { continue; } - String deleteSql = "delete from " + tableName + " where `workspaceId`=?"; + String deleteSql = "delete from " + tableName + " where workspaceId=?"; int execute = workspaceService.execute(deleteSql, workspaceId); log.info("删除表 {} 中 {} 条工作空间id为:{} 的数据", tableName, execute, workspaceId); } diff --git a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/BaseDbCommonService.java b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/BaseDbCommonService.java index 05cbf4e77..d0c68ce16 100644 --- a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/BaseDbCommonService.java +++ b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/BaseDbCommonService.java @@ -21,8 +21,10 @@ import cn.hutool.db.Page; import cn.hutool.db.PageResult; import cn.hutool.db.ds.DSFactory; import cn.hutool.db.sql.Condition; +import cn.hutool.extra.spring.SpringUtil; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import org.dromara.jpom.dialect.DialectUtil; import org.dromara.jpom.model.PageResultDto; import org.dromara.jpom.system.JpomRuntimeException; import org.springframework.util.Assert; @@ -60,7 +62,7 @@ public abstract class BaseDbCommonService { @Getter protected final String tableName; protected final Class tClass; - + protected final DbExtConfig.Mode dbMode; @SuppressWarnings("unchecked") public BaseDbCommonService() { @@ -68,6 +70,7 @@ public abstract class BaseDbCommonService { TableName annotation = tClass.getAnnotation(TableName.class); Assert.notNull(annotation, "请配置 table Name"); this.tableName = annotation.value(); + this.dbMode = SpringUtil.getBean(DbExtConfig.class).getMode(); } public String getDataDesc() { @@ -87,7 +90,7 @@ public abstract class BaseDbCommonService { * @param t 数据 */ protected final int insertDb(T t) { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(), DialectUtil.getDialectByMode(dbMode)); try { Entity entity = this.dataBeanToEntity(t); return db.insert(entity); @@ -105,7 +108,7 @@ public abstract class BaseDbCommonService { if (CollUtil.isEmpty(t)) { return; } - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); try { List entities = t.stream().map(this::dataBeanToEntity).collect(Collectors.toList()); db.insert(entities); @@ -123,7 +126,7 @@ public abstract class BaseDbCommonService { public Entity dataBeanToEntity(T data) { Entity entity = new Entity(tableName); // 转换为 map - Map beanToMap = BeanUtil.beanToMap(data, new LinkedHashMap<>(), true, s -> StrUtil.format("`{}`", s)); + Map beanToMap = BeanUtil.beanToMap(data, new LinkedHashMap<>(), true, DialectUtil::wrapField); entity.putAll(beanToMap); return entity; } @@ -137,7 +140,7 @@ public abstract class BaseDbCommonService { * @return 影响行数 */ protected final int updateDb(Entity entity, Entity where) { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); if (where.isEmpty()) { throw new JpomRuntimeException("没有更新条件"); } @@ -166,7 +169,7 @@ public abstract class BaseDbCommonService { where.set(ID_STR, keyValue); Entity entity; try { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); if (consumer != null) { consumer.accept(where); } @@ -193,7 +196,7 @@ public abstract class BaseDbCommonService { where.set(ID_STR, keyValue); List entities; try { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); if (consumer != null) { consumer.accept(where); } @@ -216,7 +219,7 @@ public abstract class BaseDbCommonService { throw new JpomRuntimeException("没有删除条件"); } try { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); return db.del(where); } catch (Exception e) { throw warpException(e); @@ -231,7 +234,7 @@ public abstract class BaseDbCommonService { */ public final long count(Entity where) { where.setTableName(getTableName()); - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); try { return db.count(where); } catch (Exception e) { @@ -247,7 +250,7 @@ public abstract class BaseDbCommonService { */ public final long count(String sql, Object... params) { try { - return Db.use(this.getDataSource()).count(sql, params); + return Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)).count(sql, params); } catch (Exception e) { throw warpException(e); } @@ -262,7 +265,7 @@ public abstract class BaseDbCommonService { */ public final List queryList(Entity where) { where.setTableName(getTableName()); - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); try { return db.find(where); } catch (Exception e) { @@ -277,7 +280,7 @@ public abstract class BaseDbCommonService { * @return List */ public final List findByCondition(Condition... wheres) { - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); try { List entities = db.findBy(getTableName(), wheres); return this.entityToBeanList(entities); @@ -332,7 +335,7 @@ public abstract class BaseDbCommonService { public final PageResultDto listPageDb(Entity where, Page page, boolean fill) { where.setTableName(getTableName()); PageResult pageResult; - Db db = Db.use(this.getDataSource()); + Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)); try { pageResult = db.page(where, page); } catch (Exception e) { @@ -358,7 +361,7 @@ public abstract class BaseDbCommonService { */ public final List query(String sql, Object... params) { try { - return Db.use(this.getDataSource()).query(sql, params); + return Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)).query(sql, params); } catch (Exception e) { throw warpException(e); } @@ -382,7 +385,7 @@ public abstract class BaseDbCommonService { */ public final int execute(String sql, Object... params) { try { - return Db.use(this.getDataSource()).execute(sql, params); + return Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode)).execute(sql, params); } catch (Exception e) { throw warpException(e); }