public override void Execute(AppOptionInfo opts, CodeGenRunner codeGenRunner) { var codeMapFile = GetCodeMapFile(opts); var map = MapConfig.Create(codeMapFile, true); map.Settings.AssemblyName = opts.AssemblyName; map.Settings.Namespace = opts.Namespace; map.Settings.AdditionalNamespaces = codeGenRunner.Settings.AdditionalNamespaces; map.Settings.GenerateLinkTable = codeGenRunner.Settings.GenerateLinkTable; var providerFactory = new ProviderFactory(); var importRdbms = GetRdbms(opts.ImportSqlDialect); var exportRdbms = GetRdbms(opts.ExportSqlDialect); var dbConnector = providerFactory.CreateDbConnector(importRdbms, opts.ConnectionString); if (string.IsNullOrWhiteSpace(opts.TemplateName)) { throw new GoliathDataException("Template file to use is required for generate operation. Please make sure that -in=\"Template_File_name.razt\" argument is passed in."); } var template = Path.Combine(codeGenRunner.TemplateFolder, opts.TemplateName); if (!File.Exists(template)) { throw new GoliathDataException($"template file {template} not found."); } if (string.IsNullOrWhiteSpace(opts.OutputFile)) { throw new GoliathDataException("Output file is required for generate operation. Please make sure that -out=\"YOUR_FILE.EXT\" argument is passed in."); } if (string.IsNullOrWhiteSpace(opts.Include)) { Logger.Log(LogLevel.Warning, "No table to read for enum provided. Please make sure you set the include parameter properly"); return; } var tables = opts.Include.Split(new string[] { ",", ";", "|" }, StringSplitOptions.RemoveEmptyEntries); var exporter = new DataExporterAdapter(providerFactory.CreateDialect(importRdbms), providerFactory.CreateDialect(exportRdbms), dbConnector, new TypeConverterStore()); var counter = 0; List <string> errors = new List <string>(); List <ExportModel> models = new List <ExportModel>(); foreach (var tbl in tables) { try { Logger.Log(LogLevel.Info, $"Processing entity {tbl}"); var entityMap = map.GetEntityMap($"{opts.Namespace}.{tbl}"); counter++; var data = exporter.Export(entityMap, opts.ExportIdentityColumn, opts.ExportDatabaseGeneratedColumns); //var fileName = GetFileName(entityMap.Name, counter, opts.OutputFile); if (data == null || data.DataBag.Count == 0) { Logger.Log(LogLevel.Warning, $"No data found for {tbl}"); continue; } models.Add(data); } catch (Exception ex) { errors.Add($"Error entity [{tbl}]: {ex.ToString()}"); } } if (!Directory.Exists(codeGenRunner.WorkingFolder)) { Directory.CreateDirectory(codeGenRunner.WorkingFolder); } codeGenRunner.GenerateCodeFromTemplate(models, template, codeGenRunner.WorkingFolder, opts.OutputFile, opts.ExtendedProperties); Logger.Log(LogLevel.Info, $"Enums generated file: {opts.OutputFile}"); if (errors.Count > 0) { Console.WriteLine($"\n\nEncountered {errors.Count} Errors"); Console.ForegroundColor = ConsoleColor.Red; foreach (var err in errors) { Console.WriteLine(err); } Console.ResetColor(); } }
public override void Execute(AppOptionInfo opts, CodeGenRunner codeGenRunner) { bool zipFile = false || !string.IsNullOrWhiteSpace(opts.TemplateName) && opts.TemplateName.ToUpper().EndsWith(".ZIP"); var codeMapFile = GetCodeMapFile(opts); var map = MapConfig.Create(codeMapFile, true); map.Settings.AssemblyName = opts.AssemblyName; map.Settings.Namespace = opts.Namespace; map.Settings.AdditionalNamespaces = codeGenRunner.Settings.AdditionalNamespaces; var importRdbms = GetRdbms(opts.ImportSqlDialect); var providerFactory = new ProviderFactory(); var dbConnector = providerFactory.CreateDbConnector(importRdbms, opts.ConnectionString); //var dialect = providerFactory.CreateDialect(importRdbms); Logger.Log(LogLevel.Debug, $"Importing using {importRdbms}"); if (!Directory.Exists(codeGenRunner.WorkingFolder)) { Directory.CreateDirectory(codeGenRunner.WorkingFolder); } if (zipFile) { var inputFile = Path.Combine(opts.WorkingFolder, opts.TemplateName); if (!File.Exists(inputFile)) { throw new ArgumentException($"Could not find the input file: {inputFile}"); } using (var fs = File.Open(inputFile, FileMode.Open, FileAccess.Read)) { fs.Unzip(opts.WorkingFolder); } } var files = Directory.EnumerateFiles(opts.WorkingFolder, "*.xml").ToDictionary(Path.GetFileNameWithoutExtension); using (var conn = dbConnector.CreateNewConnection() as SqlConnection) { if (conn == null) { throw new InvalidOperationException("Import is only supported on SQL Server for now."); } conn.Open(); var transaction = conn.BeginTransaction(); try { foreach (var table in map.EntityConfigs) { if (!files.TryGetValue(table.Name, out var file)) { continue; } Logger.Log(LogLevel.Debug, $"Reading: {file}"); ImportData(map, conn, transaction, opts, file); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw; } finally { transaction.Dispose(); } } }
public override void Execute(AppOptionInfo opts, CodeGenRunner codeGenRunner) { var codeMapFile = GetCodeMapFile(opts); var map = MapConfig.Create(codeMapFile, true); map.Settings.AssemblyName = opts.AssemblyName; map.Settings.Namespace = opts.Namespace; map.Settings.AdditionalNamespaces = codeGenRunner.Settings.AdditionalNamespaces; var providerFactory = new ProviderFactory(); var importRdbms = GetRdbms(opts.ImportSqlDialect); var exportRdbms = GetRdbms(opts.ExportSqlDialect); var dbConnector = providerFactory.CreateDbConnector(importRdbms, opts.ConnectionString); bool exportToxml = string.IsNullOrWhiteSpace(opts.TemplateName); if (!exportToxml && string.IsNullOrWhiteSpace(opts.TemplateName)) { throw new GoliathDataException("Template file to use is required for generate operation. Please make sure that -in=\"Template_File_name.razt\" argument is passed in."); } var template = Path.Combine(codeGenRunner.TemplateFolder, opts.TemplateName ?? ""); if (!exportToxml && !File.Exists(template)) { throw new GoliathDataException($"template file {template} not found."); } if (string.IsNullOrWhiteSpace(opts.OutputFile)) { throw new GoliathDataException("Output file is required for generate operation. Please make sure that -out=\"YOUR_FILE.EXT\" argument is passed in."); } var exporter = new DataExporterAdapter(providerFactory.CreateDialect(importRdbms), providerFactory.CreateDialect(exportRdbms), dbConnector, new TypeConverterStore()); List <string> errors = new List <string>(); List <EntityMap> mapsToImport = new List <EntityMap>(); if (!string.IsNullOrWhiteSpace(opts.Include)) { var split = opts.Include.Split(new string[] { ",", "|" }, StringSplitOptions.RemoveEmptyEntries); foreach (var s in split) { if (s.StartsWith("*")) { var q = map.EntityConfigs.Where(c => c.Name.EndsWith(s.Trim().Replace("*", string.Empty))).ToList(); if (q.Count > 0) { mapsToImport.AddRange(q); } } if (s.EndsWith("*")) { var q = map.EntityConfigs.Where(c => c.Name.StartsWith(s.Trim().Replace("*", string.Empty))).ToList(); if (q.Count > 0) { mapsToImport.AddRange(q); } } var query = map.EntityConfigs.Where(c => c.Name.Equals(s.Trim())); mapsToImport.AddRange(query); } } else { string[] split = new string[] { }; if (!string.IsNullOrWhiteSpace(opts.Excluded)) { split = opts.Excluded.Split(new string[] { ",", "|" }, StringSplitOptions.RemoveEmptyEntries); } foreach (var mapEntityConfig in map.EntityConfigs) { if (IsExcluded(mapEntityConfig.Name, split)) { continue; } mapsToImport.Add(mapEntityConfig); } } var files = new List <string>(); foreach (var entityMap in mapsToImport) { try { var data = exporter.Export(entityMap, opts.ExportIdentityColumn, opts.ExportDatabaseGeneratedColumns); if (data == null || data.DataBag.Count == 0) { continue; } if (exportToxml) { ExportToXml(data, opts, entityMap, codeGenRunner, files); } else { ExportToSql(data, opts, entityMap, codeGenRunner, template, files); } } catch (Exception ex) { errors.Add($"Error table [{entityMap.TableName}]: {ex}"); } } if (errors.Count > 0) { Logger.Log(LogLevel.Warning, $"\n\nEncountered {errors.Count} Errors"); Console.ForegroundColor = ConsoleColor.Red; foreach (var err in errors) { Logger.Log(LogLevel.Error, err); } throw new Exception($"Export completed with {errors.Count} errors."); } if (opts.Compress) { var dbName = FileHelperMethods.GetDatabaseNameFromConnectionString(opts.ConnectionString); Compress(opts, $"{dbName}_exports.zip", files); DeleteAll(files); } }