use of io.jpom.service.h2db.TableName in project Jpom by dromara.
the class LoadBuildJsonToDB method initInsertSql.
/**
* init insert SQL with param map and table name
*
* @param paramMap
* @param clazz 实体类
* @return
*/
private String initInsertSql(Map<String, Object> paramMap, Class<?> clazz) {
TableName tableName = clazz.getAnnotation(TableName.class);
Assert.notNull(tableName, "not find table name");
// 构造 insert SQL 语句
StringBuffer sqlBuffer = new StringBuffer("merge into {} ( ");
StringBuilder sqlFieldNameBuffer = new StringBuilder();
StringBuilder sqlFieldValueBuffer = new StringBuilder();
for (int i = 0; i < paramMap.size(); i++) {
sqlFieldNameBuffer.append("`{}`,");
sqlFieldValueBuffer.append("'{}',");
}
sqlBuffer.append(sqlFieldNameBuffer.substring(0, sqlFieldNameBuffer.length() - 1)).append(" )").append(" values ( ").append(sqlFieldValueBuffer.substring(0, sqlFieldValueBuffer.length() - 1)).append(" )");
// 构造 SQL 参数
List<Object> params = new ArrayList<>();
params.add(tableName.value());
params.addAll(paramMap.keySet());
params.addAll(paramMap.values());
return StrUtil.format(sqlBuffer, params.toArray());
}
use of io.jpom.service.h2db.TableName in project Jpom by dromara.
the class WorkspaceService method convertNullWorkspaceId.
/**
* 将没有工作空间ID 的数据添加默认值
*/
public void convertNullWorkspaceId() {
Set<Class<?>> classes = ClassUtil.scanPackage("io.jpom.model", BaseWorkspaceModel.class::isAssignableFrom);
for (Class<?> aClass : classes) {
TableName tableName = aClass.getAnnotation(TableName.class);
if (tableName == null) {
continue;
}
String sql = "update " + tableName.value() + " set workspaceId=? where workspaceId is null";
NodeService nodeService = SpringUtil.getBean(NodeService.class);
int execute = nodeService.execute(sql, Const.WORKSPACE_DEFAULT_ID);
if (execute > 0) {
DefaultSystemLog.getLog().info("convertNullWorkspaceId {} {}", tableName.value(), execute);
}
}
}
use of io.jpom.service.h2db.TableName in project Jpom by dromara.
the class WorkspaceController method delete.
/**
* 删除工作空间
*
* @param id 工作空间 ID
* @return json
*/
@GetMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
@SystemPermission(superUser = true)
public Object delete(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "数据 id 不能为空") String id) {
//
Assert.state(!StrUtil.equals(id, Const.WORKSPACE_DEFAULT_ID), "不能删除默认工作空间");
// 判断是否存在关联数据
Set<Class<?>> classes = ClassUtil.scanPackage("io.jpom.model", BaseWorkspaceModel.class::isAssignableFrom);
StringBuilder autoDelete = new StringBuilder(StrUtil.EMPTY);
for (Class<?> aClass : classes) {
TableName tableName = aClass.getAnnotation(TableName.class);
if (tableName == null) {
continue;
}
if (aClass == UserOperateLogV1.class) {
// 用户操作日志
String sql = "delete from " + tableName.value() + " where workspaceId=?";
int execute = workspaceService.execute(sql, id);
if (execute > 0) {
autoDelete.append(StrUtil.format(" 自动删除 {} 表中数据 {} 条数据", execute));
}
continue;
}
String sql = "select count(1) as cnt from " + tableName.value() + " where workspaceId=?";
List<Entity> query = workspaceService.query(sql, id);
Entity first = CollUtil.getFirst(query);
if (first != null) {
Assert.notEmpty(first, "没有对应的用户信息");
Integer cnt = first.getInt("cnt");
Assert.state(cnt == null || cnt <= 0, "当前工作空间下还存在关联数据:" + tableName.name());
}
}
// 判断用户绑定关系
boolean workspace = userBindWorkspaceService.existsWorkspace(id);
Assert.state(!workspace, "当前工作空间下还绑定着用户信息");
// 删除信息
workspaceService.delByKey(id);
return JsonMessage.toJson(200, "删除成功 " + autoDelete);
}
use of io.jpom.service.h2db.TableName in project Jpom by dromara.
the class BackupInfoController method loadTableNameList.
/**
* 读取数据库表名称列表
*
* @return json
*/
@PostMapping(value = "/system/backup/table-name-list")
@Feature(method = MethodFeature.LIST)
public Object loadTableNameList() {
// 从数据库加载表名称列表
List<String> tableNameList = backupInfoService.h2TableNameList();
// 扫描程序,拿到表名称和别名
if (TABLE_NAME_MAP.isEmpty()) {
Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation("io.jpom", TableName.class);
TABLE_NAME_MAP = CollStreamUtil.toMap(classes, aClass -> {
TableName tableName = aClass.getAnnotation(TableName.class);
return tableName.value();
}, aClass -> {
TableName tableName = aClass.getAnnotation(TableName.class);
return tableName.name();
});
}
List<JSONObject> list = tableNameList.stream().map(s -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tableName", s);
jsonObject.put("tableDesc", StrUtil.emptyToDefault(TABLE_NAME_MAP.get(s), s));
return jsonObject;
}).collect(Collectors.toList());
return JsonMessage.toJson(200, "", list);
}
Aggregations