Search in sources :

Example 1 with CSharpCodeGenContext

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

the class GenTaskResource method buildAutoTaskMethodParam.

private String buildAutoTaskMethodParam(GenTaskBySqlBuilder 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<JavaTableHost> tableHosts = ctx.getTableHosts();
            JavaTableHost tableHost = null;
            while ((tableHost = tableHosts.poll()) != null) {
                List<JavaMethodHost> methods = tableHost.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<CSharpTableHost> tableHosts = ctx.getTableViewHosts();
            CSharpTableHost tableHost = null;
            while ((tableHost = tableHosts.poll()) != null) {
                List<CSharpMethodHost> methods = tableHost.getExtraMethods();
                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) JavaTableHost(com.ctrip.platform.dal.daogen.host.java.JavaTableHost) JavaDalGenerator(com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator) JavaMethodHost(com.ctrip.platform.dal.daogen.host.java.JavaMethodHost) EmailException(org.apache.commons.mail.EmailException) CSharpTableHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpTableHost) CSharpDalGenerator(com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator) CSharpMethodHost(com.ctrip.platform.dal.daogen.host.csharp.CSharpMethodHost) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 2 with CSharpCodeGenContext

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

the class CSharpCodeGeneratorOfSpProcessor method process.

@Override
public void process(CodeGenContext context) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) context;
    int projectId = ctx.getProjectId();
    Progress progress = ctx.getProgress();
    final File dir = new File(String.format("%s/%s/cs", ctx.getGeneratePath(), projectId));
    List<Callable<ExecuteResult>> spCallables = generateSpDao(ctx, dir);
    TaskUtils.invokeBatch(log, spCallables);
    ProgressResource.addDoneFiles(progress, ctx.getSpHosts().size());
}
Also used : Progress(com.ctrip.platform.dal.daogen.entity.Progress) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) File(java.io.File) Callable(java.util.concurrent.Callable)

Example 3 with CSharpCodeGenContext

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

the class CSharpCodeGeneratorOfTableProcessor method process.

@Override
public void process(CodeGenContext context) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) context;
    int projectId = ctx.getProjectId();
    Progress progress = ctx.getProgress();
    final File dir = new File(String.format("%s/%s/cs", ctx.getGeneratePath(), projectId));
    List<Callable<ExecuteResult>> tableCallables = generateTableDao(ctx, dir);
    TaskUtils.invokeBatch(log, tableCallables);
    ProgressResource.addDoneFiles(progress, ctx.getTableViewHosts().size());
}
Also used : Progress(com.ctrip.platform.dal.daogen.entity.Progress) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) File(java.io.File) Callable(java.util.concurrent.Callable)

Example 4 with CSharpCodeGenContext

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

the class CSharpCodeGeneratorOfTableProcessor method generateTableDao.

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

            @Override
            public ExecuteResult call() {
                //progress.setOtherMessage("正在生成 " + host.getClassName());
                ExecuteResult result = new ExecuteResult("Generate Table[" + host.getTableName() + "] 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/dao/standard/DAO.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), ctx.isNewPojo() ? "templates/csharp/PojoNew.cs.tpl" : "templates/csharp/Pojo.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/IDao/I%sDao.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/dao/standard/IDAO.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sTest.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/test/DAOTest.cs.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sUnitTest.cs", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/csharp/test/DAOUnitTest.cs.tpl");
                    result.setSuccessal(true);
                } catch (Exception e) {
                    log.error(result.getTaskName() + "exception", 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 5 with CSharpCodeGenContext

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

the class AbstractCSharpDataPreparer method buildTableHost.

protected CSharpTableHost buildTableHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, String table, DatabaseCategory dbCategory, List<StoredProcedure> allSpNames) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    if (!DbUtils.tableExists(tableViewSp.getAllInOneName(), table)) {
        throw new Exception(String.format("表 %s 不存在,请编辑DAO再生成", table));
    }
    // 主键及所有列
    List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), table, new CsharpColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), table, dbCategory));
    List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), table);
    List<CSharpParameterHost> allColumns = new ArrayList<>();
    for (AbstractParameterHost h : allColumnsAbstract) {
        allColumns.add((CSharpParameterHost) h);
    }
    List<CSharpParameterHost> primaryKeys = new ArrayList<>();
    for (CSharpParameterHost h : allColumns) {
        if (primaryKeyNames.contains(h.getName())) {
            h.setPrimary(true);
            primaryKeys.add(h);
        }
    }
    Queue<GenTaskBySqlBuilder> _sqlBuilders = ctx.getSqlBuilders();
    List<GenTaskBySqlBuilder> currentTableBuilders = filterExtraMethods(_sqlBuilders, tableViewSp.getAllInOneName(), table);
    List<CSharpMethodHost> methods = buildSqlBuilderMethodHost(allColumns, currentTableBuilders);
    CSharpTableHost tableHost = new CSharpTableHost();
    tableHost.setExtraMethods(methods);
    tableHost.setNameSpace(ctx.getNamespace());
    tableHost.setDatabaseCategory(dbCategory);
    tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
    tableHost.setTableName(table);
    tableHost.setClassName(CommonUtils.normalizeVariable(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), table)));
    tableHost.setTable(true);
    tableHost.setSpa(tableViewSp.isCud_by_sp());
    // SP方式增删改
    if (tableHost.isSpa()) {
        tableHost.setSpaInsert(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "i"));
        tableHost.setSpaUpdate(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "u"));
        tableHost.setSpaDelete(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "d"));
    }
    tableHost.setPrimaryKeys(primaryKeys);
    tableHost.setColumns(allColumns);
    tableHost.setHasPagination(tableViewSp.isPagination());
    StoredProcedure expectSptI = new StoredProcedure();
    expectSptI.setName(String.format("spT_%s_i", table));
    StoredProcedure expectSptU = new StoredProcedure();
    expectSptU.setName(String.format("spT_%s_u", table));
    StoredProcedure expectSptD = new StoredProcedure();
    expectSptD.setName(String.format("spT_%s_d", table));
    tableHost.setHasSptI(allSpNames.contains(expectSptI));
    tableHost.setHasSptU(allSpNames.contains(expectSptU));
    tableHost.setHasSptD(allSpNames.contains(expectSptD));
    tableHost.setHasSpt(tableHost.isHasSptI() || tableHost.isHasSptU() || tableHost.isHasSptD());
    tableHost.setApi_list(tableViewSp.getApi_list());
    return tableHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure)

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 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 CSharpFreeSqlHost (com.ctrip.platform.dal.daogen.host.csharp.CSharpFreeSqlHost)2