Search in sources :

Example 6 with GenTaskBySqlBuilder

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

the class JavaDataPreparerOfTableViewSpProcessor method prepareTableViewSp.

private List<Callable<ExecuteResult>> prepareTableViewSp(CodeGenContext context) throws Exception {
    final JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    int projectId = ctx.getProjectId();
    boolean regenerate = ctx.isRegenerate();
    final Progress progress = ctx.getProgress();
    List<GenTaskByTableViewSp> tableViewSpTasks;
    List<GenTaskBySqlBuilder> sqlBuilderTasks;
    if (regenerate) {
        tableViewSpTasks = daoByTableViewSp.updateAndGetAllTasks(projectId);
        sqlBuilderTasks = daoBySqlBuilder.updateAndGetAllTasks(projectId);
        prepareDbFromTableViewSp(ctx, tableViewSpTasks, sqlBuilderTasks);
    } else {
        tableViewSpTasks = daoByTableViewSp.updateAndGetTasks(projectId);
        sqlBuilderTasks = daoBySqlBuilder.updateAndGetTasks(projectId);
        prepareDbFromTableViewSp(ctx, daoByTableViewSp.getTasksByProjectId(projectId), daoBySqlBuilder.getTasksByProjectId(projectId));
    }
    if (!ctx.isIgnoreApproveStatus() && tableViewSpTasks != null && tableViewSpTasks.size() > 0) {
        Iterator<GenTaskByTableViewSp> ite = tableViewSpTasks.iterator();
        while (ite.hasNext()) {
            int approved = ite.next().getApproved();
            if (approved != 2 && approved != 0) {
                ite.remove();
            }
        }
    }
    if (!ctx.isIgnoreApproveStatus() && sqlBuilderTasks != null && sqlBuilderTasks.size() > 0) {
        Iterator<GenTaskBySqlBuilder> ite = sqlBuilderTasks.iterator();
        while (ite.hasNext()) {
            int approved = ite.next().getApproved();
            if (approved != 2 && approved != 0) {
                ite.remove();
            }
        }
    }
    Queue<GenTaskBySqlBuilder> sqlBuilders = ctx.getSqlBuilders();
    for (GenTaskBySqlBuilder sqlBuilder : sqlBuilderTasks) {
        sqlBuilders.add(sqlBuilder);
    }
    final Queue<JavaTableHost> tableHosts = ctx.getTableHosts();
    final Queue<ViewHost> viewHosts = ctx.getViewHosts();
    final Queue<SpHost> spHosts = ctx.getSpHosts();
    final Map<String, SpDbHost> spHostMaps = ctx.getSpHostMaps();
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    for (final GenTaskByTableViewSp tableViewSp : tableViewSpTasks) {
        final String[] viewNames = StringUtils.split(tableViewSp.getView_names(), ",");
        final String[] tableNames = StringUtils.split(tableViewSp.getTable_names(), ",");
        final String[] spNames = StringUtils.split(tableViewSp.getSp_names(), ",");
        final DatabaseCategory dbCategory;
        String dbType = DbUtils.getDbType(tableViewSp.getAllInOneName());
        if (null != dbType && !dbType.equalsIgnoreCase("Microsoft SQL Server")) {
            dbCategory = DatabaseCategory.MySql;
        } else {
            dbCategory = DatabaseCategory.SqlServer;
        }
        try {
            results.addAll(prepareTable(ctx, progress, tableHosts, tableViewSp, tableNames, dbCategory));
            results.addAll(prepareView(ctx, progress, viewHosts, tableViewSp, viewNames, dbCategory));
            results.addAll(prepareSp(ctx, progress, spHosts, spHostMaps, tableViewSp, spNames));
        } catch (Throwable e) {
            throw e;
        }
    }
    return results;
}
Also used : GenTaskByTableViewSp(com.ctrip.platform.dal.daogen.entity.GenTaskByTableViewSp) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder) Callable(java.util.concurrent.Callable) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext) Progress(com.ctrip.platform.dal.daogen.entity.Progress) DatabaseCategory(com.ctrip.platform.dal.daogen.enums.DatabaseCategory)

Example 7 with GenTaskBySqlBuilder

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

the class DaoBySqlBuilder method updateTask.

public int updateTask(GenTaskBySqlBuilder task) throws SQLException {
    {
        FreeSelectSqlBuilder<GenTaskBySqlBuilder> builder = new FreeSelectSqlBuilder<>(dbCategory);
        builder.setTemplate("SELECT 1 FROM task_auto WHERE id != ? AND project_id=? AND db_name=? AND table_name=? AND method_name=? LIMIT 1");
        StatementParameters parameters = new StatementParameters();
        int i = 1;
        parameters.set(i++, "id", Types.INTEGER, task.getId());
        parameters.set(i++, "project_id", Types.INTEGER, task.getProject_id());
        parameters.set(i++, "db_name", Types.VARCHAR, task.getDatabaseSetName());
        parameters.set(i++, "table_name", Types.VARCHAR, task.getTable_name());
        parameters.set(i++, "method_name", Types.VARCHAR, task.getMethod_name());
        builder.mapWith(genTaskBySqlBuilderRowMapper).requireFirst().nullable();
        DalHints hints = DalHints.createIfAbsent(null).allowPartial();
        GenTaskBySqlBuilder entity = queryDao.query(builder, parameters, hints);
        if (entity != null)
            return 0;
    }
    FreeUpdateSqlBuilder builder = new FreeUpdateSqlBuilder(dbCategory);
    StringBuilder sb = new StringBuilder();
    sb.append("UPDATE task_auto SET project_id=?,db_name=?, table_name=?, class_name=?,method_name=?,");
    sb.append("sql_style=?,crud_type=?,fields=?,where_condition=?,sql_content=?,`generated`=?,");
    sb.append("version=version+1,update_user_no=?,update_time=?,comment=?,scalarType=?,");
    sb.append("pagination=?,orderby=?,approved=?,approveMsg=?,hints=? ");
    sb.append("WHERE id=? AND version = ?");
    builder.setTemplate(sb.toString());
    StatementParameters parameters = new StatementParameters();
    int i = 1;
    parameters.set(i++, "project_id", Types.INTEGER, task.getProject_id());
    parameters.set(i++, "db_name", Types.VARCHAR, task.getDatabaseSetName());
    parameters.set(i++, "table_name", Types.VARCHAR, task.getTable_name());
    parameters.set(i++, "class_name", Types.VARCHAR, task.getClass_name());
    parameters.set(i++, "method_name", Types.VARCHAR, task.getMethod_name());
    parameters.set(i++, "sql_style", Types.VARCHAR, task.getSql_style());
    parameters.set(i++, "crud_type", Types.VARCHAR, task.getCrud_type());
    parameters.set(i++, "fields", Types.LONGVARCHAR, task.getFields());
    parameters.set(i++, "where_condition", Types.LONGVARCHAR, task.getCondition());
    parameters.set(i++, "sql_content", Types.LONGVARCHAR, task.getSql_content());
    parameters.set(i++, "generated", Types.BIT, task.getGenerated());
    parameters.set(i++, "update_user_no", Types.VARCHAR, task.getUpdate_user_no());
    parameters.set(i++, "update_time", Types.TIMESTAMP, task.getUpdate_time());
    parameters.set(i++, "comment", Types.LONGVARCHAR, task.getComment());
    parameters.set(i++, "scalarType", Types.VARCHAR, task.getScalarType());
    parameters.set(i++, "pagination", Types.BIT, task.getPagination());
    parameters.set(i++, "orderby", Types.VARCHAR, task.getOrderby());
    parameters.set(i++, "approved", Types.INTEGER, task.getApproved());
    parameters.set(i++, "approveMsg", Types.LONGVARCHAR, task.getApproveMsg());
    parameters.set(i++, "hints", Types.VARCHAR, task.getHints());
    // parameters.set(i++, "length", Types.TINYINT, task.getLength());
    parameters.set(i++, "id", Types.INTEGER, task.getId());
    parameters.set(i++, "version", Types.INTEGER, task.getVersion());
    DalHints hints = DalHints.createIfAbsent(null);
    return queryDao.update(builder, parameters, hints);
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) DalHints(com.ctrip.platform.dal.dao.DalHints) FreeUpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeUpdateSqlBuilder) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)

Example 8 with GenTaskBySqlBuilder

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

the class DaoBySqlBuilder method updateAndGetTasks.

public List<GenTaskBySqlBuilder> updateAndGetTasks(int projectId) throws SQLException {
    FreeSelectSqlBuilder<List<GenTaskBySqlBuilder>> builder = new FreeSelectSqlBuilder<>(dbCategory);
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT  id, project_id, db_name,table_name,class_name,method_name,sql_style,crud_type,fields,where_condition,sql_content,`generated`,version,update_user_no,update_time,comment,scalarType,pagination,orderby,approved,approveMsg,hints ");
    sb.append("FROM task_auto WHERE project_id=? AND `generated`=FALSE");
    builder.setTemplate(sb.toString());
    StatementParameters parameters = new StatementParameters();
    int i = 1;
    parameters.set(i++, "project_id", Types.INTEGER, projectId);
    builder.mapWith(genTaskBySqlBuilderRowMapper);
    DalHints hints = DalHints.createIfAbsent(null).allowPartial();
    List<GenTaskBySqlBuilder> list = queryDao.query(builder, parameters, hints);
    List<GenTaskBySqlBuilder> result = new ArrayList<>();
    if (list == null || list.size() == 0)
        return result;
    processList(list);
    for (GenTaskBySqlBuilder entity : list) {
        entity.setGenerated(true);
        if (updateTask(entity) > 0) {
            result.add(entity);
        }
    }
    return result;
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)

Example 9 with GenTaskBySqlBuilder

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

the class AbstractCSharpDataPreparer method buildUpdateMethodHosts.

private List<CSharpMethodHost> buildUpdateMethodHosts(List<CSharpParameterHost> allColumns, List<GenTaskBySqlBuilder> currentTableBuilders) throws Exception {
    List<CSharpMethodHost> methods = new ArrayList<>();
    for (GenTaskBySqlBuilder builder : currentTableBuilders) {
        if (!builder.getCrud_type().equals("update")) {
            continue;
        }
        CSharpMethodHost method = new CSharpMethodHost();
        method.setCrud_type(builder.getCrud_type());
        method.setName(builder.getMethod_name());
        method.setSql(builder.getSql_content());
        method.setScalarType(builder.getScalarType());
        method.setPaging(builder.getPagination());
        List<CSharpParameterHost> parameters = new ArrayList<>();
        String[] fields = StringUtils.split(builder.getFields(), ",");
        for (String field : fields) {
            for (CSharpParameterHost pHost : allColumns) {
                if (pHost.getName().equals(field)) {
                    parameters.add(pHost);
                    break;
                }
            }
        }
        List<CSharpParameterHost> whereParams = buildMethodParameterHost4SqlConditin(builder, allColumns);
        Pattern pt = Pattern.compile("where.*", Pattern.CASE_INSENSITIVE);
        Matcher mt = pt.matcher(method.getSql());
        if (mt.find())
            parameters.addAll(buildSqlParamName(whereParams, mt.group()));
        else
            parameters.addAll(whereParams);
        method.setParameters(parameters);
        methods.add(method);
    }
    return methods;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)

Example 10 with GenTaskBySqlBuilder

use of com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder 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.getCud_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.getPagination());
    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) SQLException(java.sql.SQLException) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure)

Aggregations

GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)18 DalHints (com.ctrip.platform.dal.dao.DalHints)5 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)4 Progress (com.ctrip.platform.dal.daogen.entity.Progress)4 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)4 Callable (java.util.concurrent.Callable)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 GenTaskByTableViewSp (com.ctrip.platform.dal.daogen.entity.GenTaskByTableViewSp)3 ArrayList (java.util.ArrayList)3 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)2 DatabaseCategory (com.ctrip.platform.dal.daogen.enums.DatabaseCategory)2 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)2 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 FreeUpdateSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeUpdateSqlBuilder)1 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)1 StoredProcedure (com.ctrip.platform.dal.daogen.domain.StoredProcedure)1 CSharpTableHost (com.ctrip.platform.dal.daogen.host.csharp.CSharpTableHost)1 JavaParameterHost (com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)1