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");
}
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;
}
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;
}
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);
}
}
}
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("<", "<");
param = param.replaceAll(">", ">");
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("<", "<");
param = param.replaceAll(">", ">");
return param;
}
}
}
}
} catch (Exception e) {
}
return null;
}
Aggregations