Search in sources :

Example 26 with AbstractParameterHost

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

the class JavaDataPreparerOfFreeSqlProcessor method processMethodHost.

private void processMethodHost(GenTaskByFreeSql task, String namespace, List<JavaMethodHost> methods, Map<String, JavaMethodHost> freeSqlPojoHosts, String userName) 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());
    method.setUserName(userName);
    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 27 with AbstractParameterHost

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

the class CSharpDataPreparerOfFreeSqlProcessor method buildFreeSqlPojoHost.

private CSharpFreeSqlPojoHost buildFreeSqlPojoHost(CSharpCodeGenContext codeGenCtx, GenTaskByFreeSql task) throws Exception {
    CSharpFreeSqlPojoHost freeSqlHost = new CSharpFreeSqlPojoHost();
    List<CSharpParameterHost> pHosts = new ArrayList<>();
    DatabaseCategory dbCategory = DatabaseCategory.SqlServer;
    String dbType = DbUtils.getDbType(task.getAllInOneName());
    if (dbType != null && !dbType.equalsIgnoreCase("Microsoft SQL Server")) {
        dbCategory = DatabaseCategory.MySql;
    }
    List<AbstractParameterHost> list = DbUtils.testAQuerySql(task.getAllInOneName(), task.getSql_content(), task.getParameters(), new CsharpGivenSqlResultSetExtractor(dbCategory));
    for (AbstractParameterHost _ahost : list) {
        pHosts.add((CSharpParameterHost) _ahost);
    }
    freeSqlHost.setColumns(pHosts);
    freeSqlHost.setTableName("");
    String className = task.getPojo_name();
    if (className.equalsIgnoreCase("简单类型")) {
        freeSqlHost.setClassName(task.getMethod_name().substring(0, 1).toUpperCase() + task.getMethod_name().substring(1));
    } else {
        freeSqlHost.setClassName(CommonUtils.normalizeVariable(WordUtils.capitalize(task.getPojo_name())));
    }
    freeSqlHost.setNameSpace(codeGenCtx.getNamespace());
    return freeSqlHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) DatabaseCategory(com.ctrip.platform.dal.daogen.enums.DatabaseCategory)

Example 28 with AbstractParameterHost

use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost 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)28 ArrayList (java.util.ArrayList)16 SQLException (java.sql.SQLException)13 ResultSetMetaData (java.sql.ResultSetMetaData)8 DbType (com.ctrip.platform.dal.daogen.enums.DbType)6 StoredProcedure (com.ctrip.platform.dal.daogen.domain.StoredProcedure)5 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)3 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)3 JavaParameterHost (com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)3 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)2 DatabaseCategory (com.ctrip.platform.dal.daogen.enums.DatabaseCategory)2 Matcher (java.util.regex.Matcher)2 DalHints (com.ctrip.platform.dal.dao.DalHints)1 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 Parameter (com.ctrip.platform.dal.daogen.entity.Parameter)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