public string PreviewSql(string cmd, Table table) { var code = string.Empty; var coreTable = LucyConvertor.ToCoreTable(table); switch (cmd) { case "create": var createtemplate = new Sql_CreateTable(coreTable); code = createtemplate.TransformText(); break; case "tvp": var tvptemplate = new Sql_CreateTvp(coreTable); code = tvptemplate.TransformText(); break; case "insert": var inserttemplate = new Sql_Sp_Insert(coreTable); code = inserttemplate.TransformText(); break; case "update": var updatetemplate = new Sql_Sp_Update(coreTable); code = updatetemplate.TransformText(); break; case "delete": var deletetemplate = new Sql_Sp_Delete(coreTable); code = deletetemplate.TransformText(); break; case "get": var gettemplate = new Sql_Sp_Get(coreTable); code = gettemplate.TransformText(); break; case "inserttvp": var inserttvptemplate = new Sql_Sp_InsertTvp(coreTable); code = inserttvptemplate.TransformText(); break; case "updatetvp": var updatetvptemplate = new Sql_Sp_UpdateTvp(coreTable); code = updatetvptemplate.TransformText(); break; case "deletetvp": var deletetvptemplate = new Sql_Sp_DeleteTvp(coreTable); code = deletetvptemplate.TransformText(); break; case "gettvp": var gettvptemplate = new Sql_Sp_GetTvp(coreTable); code = gettvptemplate.TransformText(); break; default: break; } return(code); }
static void Main(string[] args) { //var solution = PrepareData(); var solution = PrepareRealData(); var schemas = GetSchema(); var root = @"D:\TestSpace"; var solutionFolder = Path.Combine(root, solution.Name); if (!Directory.Exists(solutionFolder)) { Directory.CreateDirectory(solutionFolder); } #region Generate Files var solutionFilePath = Path.Combine(solutionFolder, string.Format("{0}.sln", solution.Name)); var solutionContent = new SolutionTemplate(solution).TransformText(); File.WriteAllText(solutionFilePath, solutionContent, Encoding.UTF8); if (solution.Projects != null && solution.Projects.Count > 0) { foreach (var project in solution.Projects) { //Project Folder var projectFolder = Path.Combine(solutionFolder, project.Name); if (!Directory.Exists(projectFolder)) { Directory.CreateDirectory(projectFolder); } //Project Properties Folder var projectPropertiesFolder = Path.Combine(projectFolder, "Properties"); if (!Directory.Exists(projectPropertiesFolder)) { Directory.CreateDirectory(projectPropertiesFolder); } var projectFilePath = string.Empty; var projectContent = string.Empty; var assemblyInfoPath = string.Empty; var assemblyContent = string.Empty; var modelFolder = string.Empty; if (project is CSharpUtilityProject) { var utilityProject = project as CSharpUtilityProject; #region Project Files //Solution.Project.Business.csproj projectFilePath = Path.Combine(projectFolder, string.Format("{0}.csproj", utilityProject.Name)); projectContent = new CSharp_Utility_Project_Template(utilityProject).TransformText(); File.WriteAllText(projectFilePath, projectContent, Encoding.UTF8); //AssemblyInfo.cs assemblyInfoPath = Path.Combine(projectPropertiesFolder, "AssemblyInfo.cs"); assemblyContent = new AssemblyInfoTemplate().TransformText(); File.WriteAllText(assemblyInfoPath, assemblyContent, Encoding.UTF8); #endregion #region SqlConnectionProvider var sqlConnectionProviderFilePath = Path.Combine(projectFolder, "SqlConnectionProvider.cs"); var sqlConnectionProviderContent = new SqlConnectionProvider(utilityProject).TransformText(); File.WriteAllText(sqlConnectionProviderFilePath, sqlConnectionProviderContent, Encoding.UTF8); #endregion } else if (project is CSharpBusinessProject) { var businessProject = project as CSharpBusinessProject; #region Project Files //Solution.Project.Business.csproj projectFilePath = Path.Combine(projectFolder, string.Format("{0}.csproj", businessProject.Name)); projectContent = new CSharp_Business_Project_Template(businessProject).TransformText(); File.WriteAllText(projectFilePath, projectContent, Encoding.UTF8); //AssemblyInfo.cs assemblyInfoPath = Path.Combine(projectPropertiesFolder, "AssemblyInfo.cs"); assemblyContent = new AssemblyInfoTemplate().TransformText(); File.WriteAllText(assemblyInfoPath, assemblyContent, Encoding.UTF8); #endregion #region Model //Model Folder modelFolder = Path.Combine(projectFolder, "Models"); if (!Directory.Exists(modelFolder)) { Directory.CreateDirectory(modelFolder); } //Models foreach (var schema in businessProject.Schemas) { foreach (var table in schema.Value.Tables) { //Model File var modelFilePath = Path.Combine(modelFolder, string.Format("{0}Model.cs", table.CsFullName)); var modelContent = new CSharp_Business_Model(businessProject, table).TransformText(); File.WriteAllText(modelFilePath, modelContent, Encoding.UTF8); } } #endregion #region Convertor //Convertor Folder var convertorFolder = Path.Combine(projectFolder, "Convertors"); if (!Directory.Exists(convertorFolder)) { Directory.CreateDirectory(convertorFolder); } //Convertor foreach (var schema in businessProject.Schemas) { foreach (var table in schema.Value.Tables) { var convertorFilePath = Path.Combine(convertorFolder, string.Format("{0}Convertor.cs", table.CsFullName)); var convertorContent = new CSharp_Business_Convertor(businessProject, table).TransformText(); File.WriteAllText(convertorFilePath, convertorContent, Encoding.UTF8); } } #endregion #region Manager //Manager Folder var managerFolder = Path.Combine(projectFolder, "Managers"); if (!Directory.Exists(managerFolder)) { Directory.CreateDirectory(managerFolder); } //Manager Partial Folder var emptyManagerFolder = Path.Combine(managerFolder, "Partial"); if (!Directory.Exists(emptyManagerFolder)) { Directory.CreateDirectory(emptyManagerFolder); } //EntityManager foreach (var schema in businessProject.Schemas) { foreach (var table in schema.Value.Tables) { //Generated Code var managerFilePath = Path.Combine(emptyManagerFolder, string.Format("{0}Manager.cs", table.CsFullName)); var managerContent = new CSharp_Business_Manager(businessProject, table).TransformText(); File.WriteAllText(managerFilePath, managerContent); //EmptyFile for user var emptyManagerFilePath = Path.Combine(managerFolder, string.Format("{0}Manager.cs", table.CsFullName)); var emptyManagerContent = new CSharp_Business_Manager_Empty(businessProject, table).TransformText(); File.WriteAllText(emptyManagerFilePath, emptyManagerContent, Encoding.UTF8); } } #endregion } else if (project is CSharpDalProject) { var dalProject = project as CSharpDalProject; #region Dal Files //Solution.Project.Dal.csproj projectFilePath = Path.Combine(projectFolder, string.Format("{0}.csproj", dalProject.Name)); projectContent = new CSharp_Dal_Project_Template(dalProject, schemas).TransformText(); File.WriteAllText(projectFilePath, projectContent, Encoding.UTF8); //AssemblyInfo.cs assemblyInfoPath = Path.Combine(projectPropertiesFolder, "AssemblyInfo.cs"); assemblyContent = new AssemblyInfoTemplate().TransformText(); File.WriteAllText(assemblyInfoPath, assemblyContent, Encoding.UTF8); foreach (var schema in schemas) { foreach (var table in schema.Value.Tables) { ////Table Folder //var tableFolder = Path.Combine(projectFolder, table.CsFullName); //if (!Directory.Exists(tableFolder)) { Directory.CreateDirectory(tableFolder); } ////Entity //var entityFilePath = Path.Combine(tableFolder, string.Format("{0}_Entity.cs", table.CsFullName)); //var entityContent = new CSharp_Dal_Entity(dalProject, table).TransformText(); //File.WriteAllText(entityFilePath, entityContent, Encoding.UTF8); ////Query Param //var queryParamFilePath = Path.Combine(tableFolder, string.Format("{0}_QueryParam.cs", table.CsFullName)); //var queryParamContent = new CSharp_Dal_QueryParam(dalProject, table).TransformText(); //File.WriteAllText(queryParamFilePath, queryParamContent, Encoding.UTF8); ////Insert //var insertFilePath = Path.Combine(tableFolder, string.Format("{0}_Insert.cs", table.CsFullName)); //var insertContent = new CSharp_Dal_Insert(dalProject, table).TransformText(); //File.WriteAllText(insertFilePath, insertContent, Encoding.UTF8); ////InsertTvp //var insertTvpFilePath = Path.Combine(tableFolder, string.Format("{0}_InsertTvp.cs", table.CsFullName)); //var insertTvpContent = new CSharp_Dal_InsertTvp(dalProject, table).TransformText(); //File.WriteAllText(insertTvpFilePath, insertTvpContent, Encoding.UTF8); ////Update //var updateFilePath = Path.Combine(tableFolder, string.Format("{0}_Update.cs", table.CsFullName)); //var updateContent = new CSharp_Dal_Update(dalProject, table).TransformText(); //File.WriteAllText(updateFilePath, updateContent, Encoding.UTF8); ////UpdateTvp //var updateTvpFilePath = Path.Combine(tableFolder, string.Format("{0}_UpdateTvp.cs", table.CsFullName)); //var updateTvpContent = new CSharp_Dal_UpdateTvp(dalProject, table).TransformText(); //File.WriteAllText(updateTvpFilePath, updateTvpContent, Encoding.UTF8); ////Delete //var deleteFilePath = Path.Combine(tableFolder, string.Format("{0}_Delete.cs", table.CsFullName)); //var deleteContent = new CSharp_Dal_Delete(dalProject, table).TransformText(); //File.WriteAllText(deleteFilePath, deleteContent, Encoding.UTF8); ////DeleteTvp //var deleteTvpFilePath = Path.Combine(tableFolder, string.Format("{0}_DeleteTvp.cs", table.CsFullName)); //var deleteTvpContent = new CSharp_Dal_DeleteTvp(dalProject, table).TransformText(); //File.WriteAllText(deleteTvpFilePath, deleteTvpContent, Encoding.UTF8); ////Get //var getFilePath = Path.Combine(tableFolder, string.Format("{0}_Get.cs", table.CsFullName)); //var getContent = new CSharp_Dal_Get(dalProject, table).TransformText(); //File.WriteAllText(getFilePath, getContent, Encoding.UTF8); ////Query //var queryFilePath = Path.Combine(tableFolder, string.Format("{0}_Query.cs", table.CsFullName)); //var queryContent = new CSharp_Dal_Query(dalProject, table).TransformText(); //File.WriteAllText(queryFilePath, queryContent, Encoding.UTF8); } } #endregion } else if (project is SqlServerProject) { var sqlProject = project as SqlServerProject; #region Sql Server Files //Project.sqlproj projectFilePath = Path.Combine(projectFolder, string.Format("{0}.sqlproj", project.Name)); projectContent = new SqlServerProjectTemplate(sqlProject).TransformText(); File.WriteAllText(projectFilePath, projectContent, Encoding.UTF8); if (sqlProject.Schemas != null && sqlProject.Schemas.Count > 0) { foreach (var schema in sqlProject.Schemas) { //Schema Folder var schemaFolder = Path.Combine(projectFolder, schema.Key); if (!Directory.Exists(schemaFolder)) { Directory.CreateDirectory(schemaFolder); } //Schema File var schemaFilePath = Path.Combine(schemaFolder, string.Format("{0}.sql", schema.Key)); var schemaContent = new Sql_CreateSchema(schema.Key).TransformText(); File.WriteAllText(schemaFilePath, schemaContent, Encoding.UTF8); //Schema Table Folder var tableFolder = Path.Combine(schemaFolder, "Table"); if (!Directory.Exists(tableFolder)) { Directory.CreateDirectory(tableFolder); } foreach (var table in schema.Value.Tables) { //Table File var tableFilePath = Path.Combine(tableFolder, string.Format("{0}.sql", table.Name)); var tableContent = new Sql_CreateTable(table).TransformText(); File.WriteAllText(tableFilePath, tableContent); } //Schema TvpFolder var tvpFolder = Path.Combine(schemaFolder, "Tvp"); if (!Directory.Exists(tvpFolder)) { Directory.CreateDirectory(tvpFolder); } foreach (var table in schema.Value.Tables) { //Tvp File var tvpFilePath = Path.Combine(tvpFolder, string.Format("Tvp_{0}.sql", table.Name)); var tvpContent = new Sql_CreateTvp(table).TransformText(); File.WriteAllText(tvpFilePath, tvpContent); } //Schema Stored Procedure Folder var spFolder = Path.Combine(schemaFolder, "StoredProcedure"); if (!Directory.Exists(spFolder)) { Directory.CreateDirectory(spFolder); } foreach (var table in schema.Value.Tables) { var spTableFolder = Path.Combine(spFolder, table.UnderlineFullName); if (!Directory.Exists(spTableFolder)) { Directory.CreateDirectory(spTableFolder); } //Insert var spInsertFilePath = Path.Combine(spTableFolder, string.Format("SP_Insert_{0}.sql", table.UnderlineFullName)); var spInsertContent = new Sql_Sp_Insert(table).TransformText(); File.WriteAllText(spInsertFilePath, spInsertContent, Encoding.UTF8); //InsertTvp var spInsertTvpFilePath = Path.Combine(spTableFolder, string.Format("SP_InsertTvp_{0}.sql", table.UnderlineFullName)); var spInsertTvpContent = new Sql_Sp_InsertTvp(table).TransformText(); File.WriteAllText(spInsertTvpFilePath, spInsertTvpContent, Encoding.UTF8); //Update var spUpdateFilePath = Path.Combine(spTableFolder, string.Format("SP_Update_{0}.sql", table.UnderlineFullName)); var spUpdateContent = new Sql_Sp_Update(table).TransformText(); File.WriteAllText(spUpdateFilePath, spUpdateContent, Encoding.UTF8); //UpdateTvp var spUpdateTvpFilePath = Path.Combine(spTableFolder, string.Format("SP_UpdateTvp_{0}.sql", table.UnderlineFullName)); var spUpdateTvpContent = new Sql_Sp_UpdateTvp(table).TransformText(); File.WriteAllText(spUpdateTvpFilePath, spUpdateTvpContent, Encoding.UTF8); //Delete var spDeleteFilePath = Path.Combine(spTableFolder, string.Format("SP_Delete_{0}.sql", table.UnderlineFullName)); var spDeleteContent = new Sql_Sp_Delete(table).TransformText(); File.WriteAllText(spDeleteFilePath, spDeleteContent, Encoding.UTF8); //DeleteTvp var spDeleteTvpFilePath = Path.Combine(spTableFolder, string.Format("SP_DeleteTvp_{0}.sql", table.UnderlineFullName)); var spDeleteTvpContent = new Sql_Sp_DeleteTvp(table).TransformText(); File.WriteAllText(spDeleteTvpFilePath, spDeleteTvpContent, Encoding.UTF8); //Get var spGetFilePath = Path.Combine(spTableFolder, string.Format("SP_Get_{0}.sql", table.UnderlineFullName)); var spGetContent = new Sql_Sp_Get(table).TransformText(); File.WriteAllText(spGetFilePath, spGetContent, Encoding.UTF8); //Query var spQueryFilePath = Path.Combine(spTableFolder, string.Format("SP_Query_{0}.sql", table.UnderlineFullName)); var spQueryContent = new Sql_Sp_Query(table).TransformText(); File.WriteAllText(spQueryFilePath, spQueryContent, Encoding.UTF8); } } } #endregion } } } #endregion }