Search in sources :

Example 1 with ValidateResult

use of com.ctrip.platform.dal.daogen.sql.validate.ValidateResult in project dal by ctripcorp.

the class GenTaskByFreeSqlResource method validateSQL.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("sqlValidate")
public Status validateSQL(@FormParam("db_name") String set_name, @FormParam("crud_type") String crud_type, @FormParam("sql_content") String sql_content, @FormParam("params") String params, @FormParam("pagination") boolean pagination, @FormParam("mockValues") String mockValues) {
    Status status = Status.OK();
    try {
        Map<String, Parameter> map = new LinkedHashMap<>();
        List<Parameter> list = new ArrayList<>();
        Matcher matcher = pattern.matcher(sql_content);
        while (matcher.find()) {
            String parameter = matcher.group();
            Parameter p = new Parameter();
            // trim @
            p.setName(parameter.substring(1));
            list.add(p);
        }
        String[] values = mockValues.split(";");
        String[] parameters = params.split(";");
        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; i++) {
                if (parameters[i].isEmpty()) {
                    continue;
                }
                String[] array = parameters[i].split(",");
                if (array != null && array.length > 0) {
                    String name = array[0];
                    if (name.isEmpty()) {
                        continue;
                    }
                    int type = Integer.valueOf(array[1]);
                    if (!map.containsKey(name)) {
                        Parameter p = new Parameter();
                        p.setType(type);
                        p.setValue(values[i]);
                        map.put(name, p);
                    }
                }
            }
        }
        if (list.size() > 0) {
            for (Parameter p : list) {
                String name = p.getName();
                Parameter temp = map.get(name);
                if (temp != null) {
                    p.setType(temp.getType());
                    p.setValue(temp.getValue());
                }
            }
        } else {
            for (Map.Entry<String, Parameter> entry : map.entrySet()) {
                Parameter p = new Parameter();
                Parameter temp = entry.getValue();
                p.setType(temp.getType());
                p.setValue(temp.getValue());
                list.add(p);
            }
        }
        sql_content = sql_content.replaceAll(expression, "?");
        int[] sqlTypes = getTypes(list);
        values = getValues(list);
        DatabaseSetEntry databaseSetEntry = BeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(set_name);
        String dbName = databaseSetEntry.getConnectionString();
        ValidateResult validResult = null;
        String resultPrefix = "The affected rows is ";
        if (pagination && "select".equalsIgnoreCase(crud_type)) {
            sql_content = SqlBuilder.pagingQuerySql(sql_content, DbUtils.getDatabaseCategory(dbName), CurrentLanguage.Java);
            sql_content = String.format(sql_content, 1, 2);
        }
        if ("select".equalsIgnoreCase(crud_type)) {
            validResult = SQLValidation.queryValidate(dbName, sql_content, sqlTypes, values);
            resultPrefix = "The result count is ";
        } else {
            validResult = SQLValidation.updateValidate(dbName, sql_content, sqlTypes, values);
        }
        if (validResult != null && validResult.isPassed()) {
            status.setInfo(resultPrefix + validResult.getAffectRows());
            status.setExplanJson(validResult.getMessage());
        } else {
            status = Status.ERROR();
            status.setInfo(validResult.getMessage());
        }
    } catch (Throwable e) {
        LoggerManager.getInstance().error(e);
        status = Status.ERROR();
        status.setInfo(e.getMessage());
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) Matcher(java.util.regex.Matcher) ValidateResult(com.ctrip.platform.dal.daogen.sql.validate.ValidateResult)

Example 2 with ValidateResult

use of com.ctrip.platform.dal.daogen.sql.validate.ValidateResult in project dal by ctripcorp.

the class GenTaskBySqlBuilderResource method validateSQL.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("sqlValidate")
public Status validateSQL(@FormParam("db_name") String set_name, @FormParam("table_name") String table_name, @FormParam("crud_type") String crud_type, @FormParam("fields") String fields, @FormParam("condition") String condition, @FormParam("sql_content") String sql_content, @FormParam("pagination") boolean pagination, @FormParam("mockValues") String mockValues) throws Exception {
    Status status = Status.OK();
    sql_content = sql_content.replaceAll("[@:]\\w+", "?");
    int[] sqlTypes = getSqlTypes(set_name, table_name, crud_type, fields, condition);
    String[] vals = mockValues.split(";");
    DatabaseSetEntry databaseSetEntry = BeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(set_name);
    String dbName = databaseSetEntry.getConnectionString();
    ValidateResult validResult = null;
    String resultPrefix = "The affected rows is ";
    try {
        if (pagination && "select".equalsIgnoreCase(crud_type)) {
            sql_content = SqlBuilder.pagingQuerySql(sql_content, DbUtils.getDatabaseCategory(dbName), CurrentLanguage.Java);
            sql_content = String.format(sql_content, 1, 2);
        }
        if ("select".equalsIgnoreCase(crud_type)) {
            validResult = SQLValidation.queryValidate(dbName, sql_content, sqlTypes, vals);
            resultPrefix = "The result count is ";
        } else {
            validResult = SQLValidation.updateValidate(dbName, sql_content, sqlTypes, vals);
        }
    } catch (Throwable e) {
        LoggerManager.getInstance().error(e);
        status = Status.ERROR();
        status.setInfo(e.getMessage());
        return status;
    }
    if (validResult != null && validResult.isPassed()) {
        status.setInfo(resultPrefix + validResult.getAffectRows());
        status.setExplanJson(validResult.getMessage());
    } else {
        status = Status.ERROR();
        status.setInfo(validResult.getMessage());
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) ValidateResult(com.ctrip.platform.dal.daogen.sql.validate.ValidateResult)

Aggregations

Status (com.ctrip.platform.dal.daogen.domain.Status)2 ValidateResult (com.ctrip.platform.dal.daogen.sql.validate.ValidateResult)2 Matcher (java.util.regex.Matcher)1