mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-11-30 02:48:17 +08:00
显示指定数据库方言,移除方言硬编码
This commit is contained in:
parent
a1c513e058
commit
56eb42cadc
@ -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);
|
||||
|
@ -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<MachineSshModel> 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());
|
||||
|
@ -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<T extends BaseDbModel> extends BaseDbCommonS
|
||||
* @return list
|
||||
*/
|
||||
public List<String> 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<T extends BaseDbModel> extends BaseDbCommonS
|
||||
* @return list
|
||||
*/
|
||||
public List<String> 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<T extends BaseDbModel> 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<T extends BaseDbModel> 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<T extends BaseDbModel> 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<T extends BaseDbModel> 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);
|
||||
}
|
||||
|
@ -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<T extends BaseWorkspaceModel> extends
|
||||
*/
|
||||
public List<String> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Entity> 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<String> 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<Entity> 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);
|
||||
}
|
||||
|
@ -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<T> {
|
||||
@Getter
|
||||
protected final String tableName;
|
||||
protected final Class<T> tClass;
|
||||
|
||||
protected final DbExtConfig.Mode dbMode;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BaseDbCommonService() {
|
||||
@ -68,6 +70,7 @@ public abstract class BaseDbCommonService<T> {
|
||||
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<T> {
|
||||
* @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<T> {
|
||||
if (CollUtil.isEmpty(t)) {
|
||||
return;
|
||||
}
|
||||
Db db = Db.use(this.getDataSource());
|
||||
Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode));
|
||||
try {
|
||||
List<Entity> entities = t.stream().map(this::dataBeanToEntity).collect(Collectors.toList());
|
||||
db.insert(entities);
|
||||
@ -123,7 +126,7 @@ public abstract class BaseDbCommonService<T> {
|
||||
public Entity dataBeanToEntity(T data) {
|
||||
Entity entity = new Entity(tableName);
|
||||
// 转换为 map
|
||||
Map<String, Object> beanToMap = BeanUtil.beanToMap(data, new LinkedHashMap<>(), true, s -> StrUtil.format("`{}`", s));
|
||||
Map<String, Object> beanToMap = BeanUtil.beanToMap(data, new LinkedHashMap<>(), true, DialectUtil::wrapField);
|
||||
entity.putAll(beanToMap);
|
||||
return entity;
|
||||
}
|
||||
@ -137,7 +140,7 @@ public abstract class BaseDbCommonService<T> {
|
||||
* @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<T> {
|
||||
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<T> {
|
||||
where.set(ID_STR, keyValue);
|
||||
List<Entity> 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<T> {
|
||||
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<T> {
|
||||
*/
|
||||
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<T> {
|
||||
*/
|
||||
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<T> {
|
||||
*/
|
||||
public final List<Entity> 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<T> {
|
||||
* @return List
|
||||
*/
|
||||
public final List<T> findByCondition(Condition... wheres) {
|
||||
Db db = Db.use(this.getDataSource());
|
||||
Db db = Db.use(this.getDataSource(),DialectUtil.getDialectByMode(dbMode));
|
||||
try {
|
||||
List<Entity> entities = db.findBy(getTableName(), wheres);
|
||||
return this.entityToBeanList(entities);
|
||||
@ -332,7 +335,7 @@ public abstract class BaseDbCommonService<T> {
|
||||
public final PageResultDto<T> listPageDb(Entity where, Page page, boolean fill) {
|
||||
where.setTableName(getTableName());
|
||||
PageResult<Entity> 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<T> {
|
||||
*/
|
||||
public final List<Entity> 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<T> {
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user