Search in sources :

Example 1 with TableName

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());
}
Also used : TableName(io.jpom.service.h2db.TableName) JSONObject(com.alibaba.fastjson.JSONObject)

Example 2 with TableName

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);
        }
    }
}
Also used : TableName(io.jpom.service.h2db.TableName) BaseWorkspaceModel(io.jpom.model.BaseWorkspaceModel) NodeService(io.jpom.service.node.NodeService)

Example 3 with TableName

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);
}
Also used : TableName(io.jpom.service.h2db.TableName) Entity(cn.hutool.db.Entity) BaseWorkspaceModel(io.jpom.model.BaseWorkspaceModel) SystemPermission(io.jpom.permission.SystemPermission) GetMapping(org.springframework.web.bind.annotation.GetMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 4 with TableName

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);
}
Also used : PageResultDto(io.jpom.model.PageResultDto) SecureUtil(cn.hutool.crypto.SecureUtil) java.util(java.util) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) CollStreamUtil(cn.hutool.core.collection.CollStreamUtil) ClassUtil(cn.hutool.core.util.ClassUtil) Feature(io.jpom.permission.Feature) ServletUtil(cn.hutool.extra.servlet.ServletUtil) RequestBody(org.springframework.web.bind.annotation.RequestBody) JsonMessage(cn.jiangzeyin.common.JsonMessage) BackupInfoService(io.jpom.service.dblog.BackupInfoService) BackupInfoModel(io.jpom.model.data.BackupInfoModel) GetMapping(org.springframework.web.bind.annotation.GetMapping) ContentType(cn.hutool.http.ContentType) MethodFeature(io.jpom.permission.MethodFeature) SystemPermission(io.jpom.permission.SystemPermission) PostMapping(org.springframework.web.bind.annotation.PostMapping) DbConfig(io.jpom.system.db.DbConfig) ClassFeature(io.jpom.permission.ClassFeature) IOException(java.io.IOException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) JSON(com.alibaba.fastjson.JSON) BackupTypeEnum(io.jpom.model.enums.BackupTypeEnum) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) Const(io.jpom.common.Const) BackupStatusEnum(io.jpom.model.enums.BackupStatusEnum) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) TableName(io.jpom.service.h2db.TableName) BaseServerController(io.jpom.common.BaseServerController) TableName(io.jpom.service.h2db.TableName) JSONObject(com.alibaba.fastjson.JSONObject) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Aggregations

TableName (io.jpom.service.h2db.TableName)4 JSONObject (com.alibaba.fastjson.JSONObject)2 BaseWorkspaceModel (io.jpom.model.BaseWorkspaceModel)2 ClassFeature (io.jpom.permission.ClassFeature)2 Feature (io.jpom.permission.Feature)2 MethodFeature (io.jpom.permission.MethodFeature)2 SystemPermission (io.jpom.permission.SystemPermission)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 CollStreamUtil (cn.hutool.core.collection.CollStreamUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 ClassUtil (cn.hutool.core.util.ClassUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 SecureUtil (cn.hutool.crypto.SecureUtil)1 Entity (cn.hutool.db.Entity)1 ServletUtil (cn.hutool.extra.servlet.ServletUtil)1 ContentType (cn.hutool.http.ContentType)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 ValidatorItem (cn.jiangzeyin.common.validator.ValidatorItem)1 ValidatorRule (cn.jiangzeyin.common.validator.ValidatorRule)1 MultipartFileBuilder (cn.jiangzeyin.controller.multipart.MultipartFileBuilder)1