Search in sources :

Example 1 with JavaParameterHost

use of com.ctrip.platform.dal.daogen.host.java.JavaParameterHost in project dal by ctripcorp.

the class JavaDataPreparerOfFreeSqlProcessor method processMethodHost.

private void processMethodHost(GenTaskByFreeSql task, String namespace, List<JavaMethodHost> methods, Map<String, JavaMethodHost> freeSqlPojoHosts) throws Exception {
    JavaMethodHost method = new JavaMethodHost();
    method.setSql(task.getSql_content());
    method.setName(task.getMethod_name());
    method.setPackageName(namespace);
    method.setScalarType(task.getScalarType());
    method.setPojoType(task.getPojoType());
    method.setPaging(task.getPagination());
    method.setCrud_type(task.getCrud_type());
    method.setComments(task.getComment());
    if (task.getPojo_name() != null && !task.getPojo_name().isEmpty())
        method.setPojoClassName(WordUtils.capitalize(task.getPojo_name() + "Pojo"));
    List<JavaParameterHost> params = new ArrayList<>();
    for (String param : StringUtils.split(task.getParameters(), ";")) {
        String[] splitedParam = StringUtils.split(param, ",");
        JavaParameterHost p = new JavaParameterHost();
        p.setName(splitedParam[0]);
        p.setSqlType(Integer.valueOf(splitedParam[1]));
        p.setJavaClass(Consts.jdbcSqlTypeToJavaClass.get(p.getSqlType()));
        p.setValidationValue(DbUtils.mockATest(p.getSqlType()));
        boolean sensitive = splitedParam.length >= 3 ? Boolean.parseBoolean(splitedParam[2]) : false;
        p.setSensitive(sensitive);
        params.add(p);
    }
    SqlBuilder.rebuildJavaInClauseSQL(task.getSql_content(), params);
    method.setParameters(params);
    method.setHints(task.getHints());
    methods.add(method);
    if (method.getPojoClassName() != null && !method.getPojoClassName().isEmpty() && !freeSqlPojoHosts.containsKey(method.getPojoClassName()) && !"update".equalsIgnoreCase(method.getCrud_type())) {
        List<JavaParameterHost> paramHosts = new ArrayList<>();
        for (AbstractParameterHost _ahost : DbUtils.testAQuerySql(task.getAllInOneName(), task.getSql_content(), task.getParameters(), new JavaGivenSqlResultSetExtractor())) {
            paramHosts.add((JavaParameterHost) _ahost);
        }
        method.setFields(paramHosts);
        freeSqlPojoHosts.put(method.getPojoClassName(), method);
    } else if ("update".equalsIgnoreCase(method.getCrud_type())) {
        DbUtils.testUpdateSql(task.getAllInOneName(), task.getSql_content(), task.getParameters());
    }
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) JavaGivenSqlResultSetExtractor(com.ctrip.platform.dal.daogen.host.java.JavaGivenSqlResultSetExtractor) JavaParameterHost(com.ctrip.platform.dal.daogen.host.java.JavaParameterHost) JavaMethodHost(com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)

Example 2 with JavaParameterHost

use of com.ctrip.platform.dal.daogen.host.java.JavaParameterHost in project dal by ctripcorp.

the class GenTaskBySqlBuilderResource method getTableColumnSqlType.

/**
 * @param set_name
 * @param table_name
 * @return <column alias, sqltype>
 */
private Map<String, Integer> getTableColumnSqlType(String set_name, String table_name) throws Exception {
    DatabaseSetEntry databaseSetEntry = BeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(set_name);
    String dbName = databaseSetEntry.getConnectionString();
    DatabaseCategory dbCategory = DbUtils.getDatabaseCategory(dbName);
    List<AbstractParameterHost> paramsHost = DbUtils.getAllColumnNames(dbName, table_name, new JavaColumnNameResultSetExtractor(dbName, table_name, dbCategory));
    Map<String, Integer> map = new HashMap<>();
    if (paramsHost != null) {
        for (int i = 0; i < paramsHost.size(); i++) {
            JavaParameterHost paramHost = (JavaParameterHost) paramsHost.get(i);
            map.put(paramHost.getAlias().toLowerCase(), paramHost.getSqlType());
        }
    }
    return map;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) DatabaseCategory(com.ctrip.platform.dal.daogen.enums.DatabaseCategory) JavaColumnNameResultSetExtractor(com.ctrip.platform.dal.daogen.host.java.JavaColumnNameResultSetExtractor) HashMap(java.util.HashMap) JavaParameterHost(com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)

Example 3 with JavaParameterHost

use of com.ctrip.platform.dal.daogen.host.java.JavaParameterHost in project dal by ctripcorp.

the class AbstractCSharpDataPreparer method buildSelectMethodHosts.

private List<CSharpMethodHost> buildSelectMethodHosts(List<CSharpParameterHost> allColumns, List<GenTaskBySqlBuilder> currentTableBuilders) throws Exception {
    List<CSharpMethodHost> methods = new ArrayList<>();
    for (GenTaskBySqlBuilder builder : currentTableBuilders) {
        if (!builder.getCrud_type().equals("select")) {
            continue;
        }
        CSharpMethodHost method = new CSharpMethodHost();
        method.setCrud_type(builder.getCrud_type());
        method.setName(builder.getMethod_name());
        String sql = builder.getSql_content();
        int index = 0;
        if (builder.getPagination()) {
            sql = SqlBuilder.pagingQuerySql(sql, getDatabaseCategory(builder.getAllInOneName()), CurrentLanguage.CSharp);
            index += 2;
        }
        Matcher m = CSharpCodeGenContext.inRegxPattern.matcher(builder.getSql_content());
        while (m.find()) {
            sql = sql.replace(m.group(1), String.format("({%d}) ", index));
            index++;
        }
        method.setSql(sql);
        method.setScalarType(builder.getScalarType());
        method.setPaging(builder.getPagination());
        List<AbstractParameterHost> paramAbstractHosts = DbUtils.getSelectFieldHosts(builder.getAllInOneName(), builder.getSql_content(), new JavaSelectFieldResultSetExtractor());
        List<JavaParameterHost> paramHosts = new ArrayList<>();
        for (AbstractParameterHost phost : paramAbstractHosts) {
            paramHosts.add((JavaParameterHost) phost);
        }
        method.setFields(paramHosts);
        List<CSharpParameterHost> whereParams = buildMethodParameterHost4SqlConditin(builder, allColumns);
        method.setParameters(buildSqlParamName(whereParams, method.getSql()));
        String orderBy = builder.getOrderby();
        if (orderBy != null && !orderBy.trim().isEmpty() && orderBy.indexOf("-1,") != 0) {
            String[] str = orderBy.split(",");
            String odyExp = "p => p." + str[0] + ", ";
            odyExp = "asc".equalsIgnoreCase(str[1]) ? odyExp + "true" : odyExp + "false";
            method.setOrderByExp(odyExp);
        }
        methods.add(method);
    }
    return methods;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) Matcher(java.util.regex.Matcher) JavaSelectFieldResultSetExtractor(com.ctrip.platform.dal.daogen.host.java.JavaSelectFieldResultSetExtractor) JavaParameterHost(com.ctrip.platform.dal.daogen.host.java.JavaParameterHost) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)

Aggregations

AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)3 JavaParameterHost (com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)3 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)1 DatabaseCategory (com.ctrip.platform.dal.daogen.enums.DatabaseCategory)1 JavaColumnNameResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaColumnNameResultSetExtractor)1 JavaGivenSqlResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaGivenSqlResultSetExtractor)1 JavaMethodHost (com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)1 JavaSelectFieldResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaSelectFieldResultSetExtractor)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1