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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations