private void AddDmsRules(CProject dataProject, List <CTable> tables) { var fileName = $"DmsRules.json"; var rules = new CRules(); foreach (var table in tables) { var rList = _tableToDmsRuleConverter.Convert(table); rules.Rule.AddRange(rList); } dataProject.ProjectContent.Add(new CProjectContent { Content = rules, BuildAction = CBuildAction.Build, File = new CFile { Folder = $"", FileName = fileName } }); }
public override async Task <ConvertDDLResponse> ConvertDDL(ConvertDDLRequest request, ServerCallContext context) { var sw = Stopwatch.StartNew(); GrpcEnvironment.Logger.Info($@"Processing ConvertDDL Request"); var databaseProject = new KDataStoreProject() { KickstartCRUDStoredProcedures = false, ConvertToSnakeCase = request.ConvertToSnakeCase, DataStoreType = Utility.DataStoreTypes.Postgres, SqlTableText = request.UnconvertedTableDDL, SqlTableTypeText = request.UnconvertedTableTypeDDL, SqlStoredProcedureText = request.UnconvertedStoredProcedureDDL }; var connectionString = "Server=localhost;"; var outputRootPath = string.Empty; _dbToKSolutionConverter.BuildSqlMeta(connectionString, outputRootPath, databaseProject); var response = new ConvertDDLResponse(); ICSchemaToSchemaConverter schemaConverter; ICTableToTableConverter tableConverter; ICTableTypeToTableTypeConverter tableTypeConverter; ICStoredProcedureToStoredProcedureConverter storedProcConverter; if (request.DatabaseType == Types.DatabaseTypes.Postgres) { schemaConverter = new CSchemaToPostgresSchemaConverter(); tableConverter = new CTableToPostgresTableConverter(); tableTypeConverter = new CTableTypeToPostgresTableTypeConverter(); storedProcConverter = new CStoredProcedureToPostgresStoredProcedureConverter(); } else if (request.DatabaseType == Types.DatabaseTypes.SqlServer) { schemaConverter = new CSchemaToSqlServerSchemaConverter(); tableConverter = new CTableToSqlServerTableConverter(); tableTypeConverter = new CTableTypeToSqlServerTableTypeConverter(); storedProcConverter = new CStoredProcedureToSSqlServerStoredProcedureConverter(); } else { throw new NotImplementedException(); } var codeWriter = new CodeWriter(); var codeWriterTableType = new CodeWriter(); var codeWriterStoredProc = new CodeWriter(); using (var memoryStream = new MemoryStream( )) { using (var zipFile = new ZipArchive(memoryStream, ZipArchiveMode.Create)) { foreach (var schema in databaseProject.GetAllGeneratedSchemas()) { if (request.DatabaseType == Types.DatabaseTypes.SqlServer && schema.SchemaName.ToLower() == "dbo") { continue;// dbo already exists on Sql Server } var ddl = schemaConverter.Convert(schema); var fileName = _flywayFileNameService.GetFlywayFileName(schema); //fileWriter.WriteFile(fileName, ddl); var newEntry = zipFile.CreateEntry(Path.Combine("migrations", fileName)); using (var writer = new StreamWriter(newEntry.Open())) { writer.Write(ddl); } } foreach (var table in databaseProject.Table) { var ddl = tableConverter.Convert(table.GeneratedTable); codeWriter.WriteLine(ddl); codeWriter.WriteLine(); var fileName = _flywayFileNameService.GetFlywayFileName(table.GeneratedTable); //fileWriter.WriteFile(fileName, ddl); var newEntry = zipFile.CreateEntry(Path.Combine("migrations", fileName)); using (var writer = new StreamWriter(newEntry.Open())) { writer.Write(ddl); } } { var rules = new CRules(); foreach (var table in databaseProject.Table) { var rList = _cTableToDmsRuleConverter.Convert(table.GeneratedTable); rules.Rule.AddRange(rList); } var json = rules.ToJson(); var fileName = "DmsRules.json"; var newEntry = zipFile.CreateEntry(fileName); using (var writer = new StreamWriter(newEntry.Open())) { writer.Write(json); } response.ConvertedDmsJson = json; } foreach (var tableType in databaseProject.TableType) { var ddl = tableTypeConverter.Convert(tableType.GeneratedTableType); codeWriterTableType.WriteLine(ddl); codeWriterTableType.WriteLine(); var fileName = _flywayFileNameService.GetFlywayFileName(tableType.GeneratedTableType); //fileWriter.WriteFile(fileName, ddl); var newEntry = zipFile.CreateEntry(Path.Combine("migrations", fileName)); using (var writer = new StreamWriter(newEntry.Open())) { writer.Write(ddl); } } foreach (var storedProc in databaseProject.StoredProcedure) { var ddl = storedProcConverter.Convert(storedProc.GeneratedStoredProcedure); codeWriterStoredProc.WriteLine(ddl); codeWriter.WriteLine(); var fileName = _flywayFileNameService.GetFlywayFileName(storedProc.GeneratedStoredProcedure); //fileWriter.WriteFile(fileName, ddl); var newEntry = zipFile.CreateEntry(Path.Combine("migrations", fileName)); using (var writer = new StreamWriter(newEntry.Open())) { writer.Write(ddl); } } } response.ZipAsBase64 = Convert.ToBase64String(memoryStream.ToArray()); } response.ConvertedTableDDL = codeWriter.ToString(); response.ConvertedTableTypeDDL = codeWriterTableType.ToString(); response.ConvertedStoredProcedureDDL = codeWriterStoredProc.ToString(); sw.Stop(); GrpcEnvironment.Logger.Info($"Processed request in {sw.ElapsedMilliseconds} ms"); return(response); }