Search in sources :

Example 16 with CSharpCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext in project dal by ctripcorp.

the class CSharpCodeGeneratorOfOthersProcessor method generateCommonCode.

/**
 * 生成C#的公共部分,如Dal.config,Program.cs以及DALFactory.cs
 */
private void generateCommonCode(CodeGenContext codeGenCtx) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    final int id = ctx.getProjectId();
    final VelocityContext context = GenUtils.buildDefaultVelocityContext();
    final File csMavenLikeDir = new File(String.format("%s/%s/cs", ctx.getGeneratePath(), id));
    context.put("host", ctx.getDalConfigHost());
    context.put("dbs", ctx.getDbHosts().values());
    context.put("namespace", ctx.getNamespace());
    context.put("freeSqlHosts", ctx.getFreeDaos());
    context.put("tableHosts", ctx.getTableDaos());
    context.put("spHosts", ctx.getSpDaos());
    GenUtils.mergeVelocityContext(context, String.format("%s/Config/Dal.config.tpl", csMavenLikeDir.getAbsolutePath()), "templates/csharp/Dal.config.cs.tpl");
    GenUtils.mergeVelocityContext(context, String.format("%s/DalFactory.cs", csMavenLikeDir.getAbsolutePath()), "templates/csharp/DalFactory.cs.tpl");
}
Also used : CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File)

Example 17 with CSharpCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext in project dal by ctripcorp.

the class CSharpCodeGeneratorOfSpProcessor method generateSpDao.

private List<Callable<ExecuteResult>> generateSpDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
    final CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    final Progress progress = ctx.getProgress();
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    Queue<CSharpTableHost> _spHosts = ctx.getSpHosts();
    for (final CSharpTableHost host : _spHosts) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                // progress.setOtherMessage("正在生成 " + host.getClassName());
                ExecuteResult result = new ExecuteResult("Generate SP[" + host.getClassName() + "] Dao");
                progress.setOtherMessage(result.getTaskName());
                try {
                    VelocityContext context = GenUtils.buildDefaultVelocityContext();
                    context.put("host", host);
                    GenUtils.mergeVelocityContext(context, String.format("%s/Dao/%sDao.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/DAOBySp.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/PojoBySp.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sTest.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/test/SpTest.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sUnitTest.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/test/SpUnitTest.cs.tpl");
                    result.setSuccessal(true);
                } catch (Throwable e) {
                    throw e;
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : CSharpTableHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpTableHost) Progress(com.ctrip.platform.dal.daogen.entity.Progress) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) Callable(java.util.concurrent.Callable)

Example 18 with CSharpCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext in project dal by ctripcorp.

the class CSharpDataPreparerOfFreeSqlProcessor method prepareFreeSql.

private List<Callable<ExecuteResult>> prepareFreeSql(CodeGenContext codeGenCtx) throws Exception {
    final CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    int projectId = ctx.getProjectId();
    boolean regenerate = ctx.isRegenerate();
    final Progress progress = ctx.getProgress();
    final String namespace = ctx.getNamespace();
    List<GenTaskByFreeSql> freeSqlTasks;
    if (regenerate) {
        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,且ClassName不区分大小写
    final Map<String, List<GenTaskByFreeSql>> groupBy = freeSqlGroupBy(freeSqlTasks);
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    final Map<String, CSharpFreeSqlPojoHost> _freeSqlPojoHosts = ctx.getFreeSqlPojoHosts();
    final Queue<CSharpFreeSqlHost> _freeSqlHosts = ctx.getFreeSqlHosts();
    // 随后,以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());
                try {
                    List<GenTaskByFreeSql> currentTasks = entry.getValue();
                    if (currentTasks.size() < 1)
                        return result;
                    CSharpFreeSqlHost host = new CSharpFreeSqlHost();
                    host.setDbSetName(currentTasks.get(0).getDatabaseSetName());
                    host.setClassName(CommonUtils.normalizeVariable(WordUtils.capitalize(currentTasks.get(0).getClass_name())));
                    host.setNameSpace(namespace);
                    host.setDatabaseCategory(getDatabaseCategory(currentTasks.get(0).getAllInOneName()));
                    List<CSharpMethodHost> methods = new ArrayList<>();
                    // 每个Method可能就有一个Pojo
                    for (GenTaskByFreeSql task : currentTasks) {
                        try {
                            CSharpMethodHost method = buildFreeSqlMethodHost(ctx, task);
                            if (!_freeSqlPojoHosts.containsKey(task.getPojo_name()) && method.getPojoName() != null && !method.getPojoName().isEmpty() && (!method.isFirstOrSingle() || !method.isSampleType()) && !"update".equalsIgnoreCase(task.getCrud_type())) {
                                CSharpFreeSqlPojoHost freeSqlPojoHost = buildFreeSqlPojoHost(ctx, task);
                                if (null != freeSqlPojoHost) {
                                    _freeSqlPojoHosts.put(task.getPojo_name(), freeSqlPojoHost);
                                }
                            } else if ("update".equalsIgnoreCase(task.getCrud_type())) {
                                DbUtils.testUpdateSql(task.getAllInOneName(), task.getSql_content(), task.getParameters());
                            }
                            methods.add(method);
                        } catch (Throwable e) {
                            progress.setOtherMessage(e.getMessage());
                            throw new Exception(String.format("Task Id[%s]:%s\r\n", task.getId(), e.getMessage()), e);
                        }
                    }
                    host.setMethods(methods);
                    _freeSqlHosts.add(host);
                    result.setSuccessal(true);
                } catch (Throwable e) {
                    throw e;
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql) Callable(java.util.concurrent.Callable) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) Progress(com.ctrip.platform.dal.daogen.entity.Progress) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) SQLException(java.sql.SQLException)

Example 19 with CSharpCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext in project dal by ctripcorp.

the class CSharpDataPreparerOfFreeSqlProcessor method prepareDbFromFreeSql.

private void prepareDbFromFreeSql(CodeGenContext codeGenCtx, List<GenTaskByFreeSql> freeSqls) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    Map<String, DatabaseHost> _dbHosts = ctx.getDbHosts();
    Set<String> _freeDaos = ctx.getFreeDaos();
    for (GenTaskByFreeSql task : freeSqls) {
        addDatabaseSet(ctx, task.getDatabaseSetName());
        _freeDaos.add(WordUtils.capitalize(task.getClass_name()));
        if (!_dbHosts.containsKey(task.getAllInOneName())) {
            String provider = "sqlProvider";
            String dbType = DbUtils.getDbType(task.getAllInOneName());
            if (null != dbType && !dbType.equalsIgnoreCase("Microsoft SQL Server")) {
                provider = "mySqlProvider";
            }
            DatabaseHost host = new DatabaseHost();
            host.setAllInOneName(task.getAllInOneName());
            host.setProviderType(provider);
            host.setDatasetName(task.getDatabaseSetName());
            _dbHosts.put(task.getAllInOneName(), host);
        }
    }
}
Also used : CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)

Example 20 with CSharpCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext 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) SQLException(java.sql.SQLException) 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)

Aggregations

CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)20 Callable (java.util.concurrent.Callable)9 Progress (com.ctrip.platform.dal.daogen.entity.Progress)8 SQLException (java.sql.SQLException)6 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)5 File (java.io.File)5 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)4 CSharpTableHost (com.ctrip.platform.dal.daogen.host.csharp.CSharpTableHost)4 VelocityContext (org.apache.velocity.VelocityContext)4 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)3 ArrayList (java.util.ArrayList)3 CodeGenContext (com.ctrip.platform.dal.daogen.CodeGenContext)2 DalGenerator (com.ctrip.platform.dal.daogen.DalGenerator)2 StoredProcedure (com.ctrip.platform.dal.daogen.domain.StoredProcedure)2 GenTaskByFreeSql (com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)2 GenTaskByTableViewSp (com.ctrip.platform.dal.daogen.entity.GenTaskByTableViewSp)2 CSharpDalGenerator (com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator)2 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)2 JavaDalGenerator (com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator)2 DalConfigHost (com.ctrip.platform.dal.daogen.host.DalConfigHost)2