Search in sources :

Example 16 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfFreeSqlProcessor method process.

public void process(CodeGenContext context) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    int projectId = ctx.getProjectId();
    File dir = new File(String.format("%s/%s/java", ctx.getGeneratePath(), projectId));
    List<Callable<ExecuteResult>> freeCallables = generateFreeSqlDao(ctx, dir);
    TaskUtils.invokeBatch(log, freeCallables);
}
Also used : File(java.io.File) Callable(java.util.concurrent.Callable) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 17 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class GenTaskResource method buildFreesqlTaskMethodParam.

private String buildFreesqlTaskMethodParam(GenTaskByFreeSql task) {
    try {
        DalGenerator generator = null;
        CodeGenContext context = null;
        Progress progress = new Progress();
        if ("java".equalsIgnoreCase(task.getSql_style())) {
            generator = new JavaDalGenerator();
            context = generator.createContext(task.getProject_id(), true, progress, true, true);
            generator.prepareData(context);
            JavaCodeGenContext ctx = (JavaCodeGenContext) context;
            Queue<FreeSqlHost> freeSqlHosts = ctx.getFreeSqlHosts();
            FreeSqlHost freeSqlHost = null;
            while ((freeSqlHost = freeSqlHosts.poll()) != null) {
                List<JavaMethodHost> methods = freeSqlHost.getMethods();
                for (JavaMethodHost method : methods) {
                    if (task.getMethod_name().equalsIgnoreCase(method.getName())) {
                        String param = method.getParameterDeclaration();
                        param = param.replaceAll("<", "&lt;");
                        param = param.replaceAll(">", "&gt;");
                        return param;
                    }
                }
            }
        } else {
            generator = new CSharpDalGenerator();
            context = generator.createContext(task.getProject_id(), true, progress, true, true);
            generator.prepareData(context);
            CSharpCodeGenContext ctx = (CSharpCodeGenContext) context;
            Queue<CSharpFreeSqlHost> freeSqlHosts = ctx.getFreeSqlHosts();
            CSharpFreeSqlHost freeSqlHost = null;
            while ((freeSqlHost = freeSqlHosts.poll()) != null) {
                List<CSharpMethodHost> methods = freeSqlHost.getMethods();
                for (CSharpMethodHost method : methods) {
                    if (task.getMethod_name().equalsIgnoreCase(method.getName())) {
                        String param = method.getParameterDeclaration();
                        param = param.replaceAll("<", "&lt;");
                        param = param.replaceAll(">", "&gt;");
                        return param;
                    }
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}
Also used : CSharpDalGenerator(com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator) JavaDalGenerator(com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator) DalGenerator(com.ctrip.platform.dal.daogen.DalGenerator) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) CodeGenContext(com.ctrip.platform.dal.daogen.CodeGenContext) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext) FreeSqlHost(com.ctrip.platform.dal.daogen.host.java.FreeSqlHost) CSharpFreeSqlHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpFreeSqlHost) JavaDalGenerator(com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator) JavaMethodHost(com.ctrip.platform.dal.daogen.host.java.JavaMethodHost) EmailException(org.apache.commons.mail.EmailException) CSharpDalGenerator(com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator) CSharpFreeSqlHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpFreeSqlHost) CSharpMethodHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpMethodHost) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 18 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaDataPreparerOfFreeSqlProcessor method prepareFreeSql.

private List<Callable<ExecuteResult>> prepareFreeSql(CodeGenContext codeGenCtx) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    int projectId = ctx.getProjectId();
    final Progress progress = ctx.getProgress();
    final String namespace = ctx.getNamespace();
    final Map<String, JavaMethodHost> _freeSqlPojoHosts = ctx.get_freeSqlPojoHosts();
    final Queue<FreeSqlHost> _freeSqlHosts = ctx.getFreeSqlHosts();
    DaoByFreeSql daoByFreeSql = SpringBeanGetter.getDaoByFreeSql();
    List<GenTaskByFreeSql> freeSqlTasks;
    if (ctx.isRegenerate()) {
        freeSqlTasks = daoByFreeSql.updateAndGetAllTasks(projectId);
        prepareDbFromFreeSql(ctx, freeSqlTasks);
    } else {
        freeSqlTasks = daoByFreeSql.updateAndGetTasks(projectId);
        prepareDbFromFreeSql(ctx, daoByFreeSql.getTasksByProjectId(projectId));
    }
    if (!ctx.isIgnoreApproveStatus() && freeSqlTasks != null && freeSqlTasks.size() > 0) {
        Iterator<GenTaskByFreeSql> ite = freeSqlTasks.iterator();
        while (ite.hasNext()) {
            int approved = ite.next().getApproved();
            if (approved != 2 && approved != 0) {
                ite.remove();
            }
        }
    }
    // 按照DbName以及ClassName做一次GroupBy(相同DbName的GenTaskByFreeSql作为一组),且ClassName不区分大小写
    final Map<String, List<GenTaskByFreeSql>> groupBy = freeSqlGroupBy(freeSqlTasks);
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    // 以DbName以及ClassName为维度,为每个维度生成一个DAO类
    for (final Map.Entry<String, List<GenTaskByFreeSql>> entry : groupBy.entrySet()) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Build  Free SQL[" + entry.getKey() + "] Host");
                progress.setOtherMessage(result.getTaskName());
                List<GenTaskByFreeSql> currentTasks = entry.getValue();
                if (currentTasks.size() < 1)
                    return result;
                FreeSqlHost host = new FreeSqlHost();
                host.setDbSetName(currentTasks.get(0).getDatabaseSetName());
                host.setClassName(currentTasks.get(0).getClass_name());
                host.setPackageName(namespace);
                host.setDatabaseCategory(getDatabaseCategory(currentTasks.get(0).getAllInOneName()));
                List<JavaMethodHost> methods = new ArrayList<>();
                // 每个Method可能就有一个Pojo
                for (GenTaskByFreeSql task : currentTasks) {
                    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.isPagination());
                    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);
                    }
                }
                host.setMethods(methods);
                _freeSqlHosts.add(host);
                result.setSuccessal(true);
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : JavaGivenSqlResultSetExtractor(com.ctrip.platform.dal.daogen.host.java.JavaGivenSqlResultSetExtractor) JavaParameterHost(com.ctrip.platform.dal.daogen.host.java.JavaParameterHost) FreeSqlHost(com.ctrip.platform.dal.daogen.host.java.FreeSqlHost) GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql) JavaMethodHost(com.ctrip.platform.dal.daogen.host.java.JavaMethodHost) Callable(java.util.concurrent.Callable) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext) AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) Progress(com.ctrip.platform.dal.daogen.entity.Progress) DaoByFreeSql(com.ctrip.platform.dal.daogen.dao.DaoByFreeSql)

Example 19 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaDataPreparerOfTableViewSpProcessor method buildViewHost.

private ViewHost buildViewHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, DatabaseCategory dbCategory, String viewName) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    if (!DbUtils.viewExists(tableViewSp.getAllInOneName(), viewName)) {
        log.error(String.format("The view[%s] doesn't exist, pls check", viewName));
        return null;
    }
    ViewHost vhost = new ViewHost();
    String className = viewName.replace("_", "");
    className = getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), className);
    vhost.setPackageName(ctx.getNamespace());
    vhost.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
    vhost.setDbSetName(tableViewSp.getDatabaseSetName());
    vhost.setPojoClassName(className);
    vhost.setViewName(viewName);
    List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), viewName);
    List<AbstractParameterHost> params = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), viewName, new JavaColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), viewName, dbCategory));
    List<JavaParameterHost> realParams = new ArrayList<JavaParameterHost>();
    if (null == params) {
        throw new Exception(String.format("The column names of view[%s, %s] is null", tableViewSp.getAllInOneName(), viewName));
    }
    for (AbstractParameterHost p : params) {
        JavaParameterHost jHost = (JavaParameterHost) p;
        if (primaryKeyNames.contains(jHost.getName())) {
            jHost.setPrimary(true);
        }
        realParams.add(jHost);
    }
    vhost.setFields(realParams);
    return vhost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 20 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaDataPreparerOfTableViewSpProcessor method buildSpHost.

private SpHost buildSpHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, String spName) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    String schema = "dbo";
    String realSpName = spName;
    if (spName.contains(".")) {
        String[] splitSp = StringUtils.split(spName, '.');
        schema = splitSp[0];
        realSpName = splitSp[1];
    }
    StoredProcedure currentSp = new StoredProcedure();
    currentSp.setSchema(schema);
    currentSp.setName(realSpName);
    if (!DbUtils.spExists(tableViewSp.getAllInOneName(), currentSp)) {
        throw new Exception(String.format("The store procedure[%s, %s] doesn't exist, pls check", tableViewSp.getAllInOneName(), currentSp.getName()));
    }
    SpHost spHost = new SpHost();
    String className = realSpName.replace("_", "");
    className = getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), className);
    spHost.setPackageName(ctx.getNamespace());
    spHost.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
    spHost.setDbName(tableViewSp.getDatabaseSetName());
    spHost.setPojoClassName(className);
    spHost.setSpName(spName);
    List<AbstractParameterHost> params = DbUtils.getSpParams(tableViewSp.getAllInOneName(), currentSp, new JavaSpParamResultSetExtractor(tableViewSp.getAllInOneName(), currentSp.getName()));
    List<JavaParameterHost> realParams = new ArrayList<>();
    String callParams = "";
    if (null == params) {
        throw new Exception(String.format("The sp[%s, %s] parameters is null", tableViewSp.getAllInOneName(), currentSp.getName()));
    }
    for (AbstractParameterHost p : params) {
        callParams += "?,";
        realParams.add((JavaParameterHost) p);
    }
    spHost.setCallParameters(StringUtils.removeEnd(callParams, ","));
    spHost.setFields(realParams);
    return spHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Aggregations

JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)22 Callable (java.util.concurrent.Callable)11 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 File (java.io.File)7 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)6 VelocityContext (org.apache.velocity.VelocityContext)6 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)4 JavaMethodHost (com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)4 ArrayList (java.util.ArrayList)4 FreeSqlHost (com.ctrip.platform.dal.daogen.host.java.FreeSqlHost)3 JavaTableHost (com.ctrip.platform.dal.daogen.host.java.JavaTableHost)3 CodeGenContext (com.ctrip.platform.dal.daogen.CodeGenContext)2 DalGenerator (com.ctrip.platform.dal.daogen.DalGenerator)2 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)2 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)2 CSharpDalGenerator (com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator)2 JavaDalGenerator (com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator)2 DalConfigHost (com.ctrip.platform.dal.daogen.host.DalConfigHost)2 CSharpMethodHost (com.ctrip.platform.dal.daogen.host.csharp.CSharpMethodHost)2 EmailException (org.apache.commons.mail.EmailException)2