Search in sources :

Example 1 with GenTaskByFreeSql

use of com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql 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) {
                        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);
                            }
                        }
                        methods.add(method);
                    }
                    host.setMethods(methods);
                    _freeSqlHosts.add(host);
                    result.setSuccessal(true);
                } catch (Exception e) {
                    log.error(result.getTaskName() + "exception", e);
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : Progress(com.ctrip.platform.dal.daogen.entity.Progress) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql) Callable(java.util.concurrent.Callable) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult)

Example 2 with GenTaskByFreeSql

use of com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql 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 3 with GenTaskByFreeSql

use of com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql in project dal by ctripcorp.

the class CSharpDataPreparerOfFreeSqlProcessor method freeSqlGroupBy.

private Map<String, List<GenTaskByFreeSql>> freeSqlGroupBy(List<GenTaskByFreeSql> tasks) {
    Map<String, List<GenTaskByFreeSql>> groupBy = new HashMap<>();
    for (GenTaskByFreeSql task : tasks) {
        String key = String.format("%s_%s", task.getAllInOneName(), task.getClass_name().toLowerCase());
        if (groupBy.containsKey(key)) {
            groupBy.get(key).add(task);
        } else {
            groupBy.put(key, new ArrayList<GenTaskByFreeSql>());
            groupBy.get(key).add(task);
        }
    }
    return groupBy;
}
Also used : GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)

Example 4 with GenTaskByFreeSql

use of com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql in project dal by ctripcorp.

the class DaoByFreeSql method updateAndGetAllTasks.

public List<GenTaskByFreeSql> updateAndGetAllTasks(int projectId) {
    final List<GenTaskByFreeSql> tasks = new ArrayList<>();
    this.jdbcTemplate.query("SELECT id, project_id,db_name,class_name,pojo_name,method_name,crud_type," + "sql_content,parameters,`generated`,version,update_user_no,update_time,comment," + "scalarType,pojoType,pagination,sql_style,approved,approveMsg,hints" + " FROM task_sql WHERE project_id=?", new Object[] { projectId }, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            GenTaskByFreeSql task = GenTaskByFreeSql.visitRow(rs);
            task.setGenerated(true);
            if (updateTask(task) > 0) {
                tasks.add(task);
            }
        }
    });
    return tasks;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Example 5 with GenTaskByFreeSql

use of com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql in project dal by ctripcorp.

the class DaoByFreeSql method updateAndGetTasks.

public List<GenTaskByFreeSql> updateAndGetTasks(int projectId) {
    final List<GenTaskByFreeSql> tasks = new ArrayList<>();
    this.jdbcTemplate.query("SELECT id, project_id,db_name,class_name,pojo_name,method_name,crud_type," + " sql_content,parameters,`generated`,version,update_user_no,update_time," + " comment,scalarType,pojoType,pagination,sql_style,approved,approveMsg,hints" + " FROM task_sql " + " WHERE project_id=? AND `generated`=FALSE", new Object[] { projectId }, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            GenTaskByFreeSql task = GenTaskByFreeSql.visitRow(rs);
            task.setGenerated(true);
            if (updateTask(task) > 0) {
                tasks.add(task);
            }
        }
    });
    return tasks;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) GenTaskByFreeSql(com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Aggregations

GenTaskByFreeSql (com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)7 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)2 Progress (com.ctrip.platform.dal.daogen.entity.Progress)2 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)2 DaoByFreeSql (com.ctrip.platform.dal.daogen.dao.DaoByFreeSql)1 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)1 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)1 FreeSqlHost (com.ctrip.platform.dal.daogen.host.java.FreeSqlHost)1 JavaGivenSqlResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaGivenSqlResultSetExtractor)1 JavaMethodHost (com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)1 JavaParameterHost (com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)1