public Downloader(ServerOption serverOption, String writeToFolderPath, ILog logger) { Logger = logger; ServerOption = serverOption; WriteToFolderPath = writeToFolderPath; ReplacerFactories = new ReplacerFactories(); }
private Database GetDatabase() { Server srv = new Server(new ServerConnection() { ConnectionString = ServerOption.ToString(), }); return(srv.Databases[ServerOption.DbName]); }
private void Download(IEnumerable <IGrouping <int, string> > smoObjectsGroup) { Parallel.ForEach(smoObjectsGroup, g => { Server serv = new Server(new ServerConnection() { ConnectionString = ServerOption.ToString(), }); Scripter scripter = new Scripter { Server = serv }; scripter.Options.IncludeHeaders = true; scripter.Options.SchemaQualify = true; scripter.Options.AllowSystemObjects = false; try { foreach (var urn in g) { var urnn = new Urn(urn); var type = urnn.Type; var name = urnn.GetNameForType(type); if (!urnn.GetAttribute("Schema").ToUpper().Equals("dbo".ToUpper())) { continue; } var smoObject = serv.GetSmoObject(urnn); var scriptLines = scripter.Script(new SqlSmoObject[] { smoObject }); Logger.Log($"Forming script: '{name}'"); if (scriptLines.Count == 0) { Logger.Log($"Неизвестная ошибка формирования скрипта для: '{name}'"); continue; } scriptLines.RemoveAt(0); if (ServerOption.ReplaceFirstCreate) { var replacer = ReplacerFactories.GetReplacer(type); scriptLines = replacer.Replace(scriptLines); } var outputString = OutputSchemaObject(ServerOption.DbName, scriptLines); var extension = "sql"; var fileName = $"{name}.{type}.{extension}"; var fullFilePath = Path.Combine(WriteToFolderPath, ServerOption.ServerName, DateTime.Now.ToString("yyyyMMdd"), fileName); Logger.Log($"Write File: '{fullFilePath}'"); WriteFile(fullFilePath, outputString); } } catch (Exception e) { Logger.Log(e); } }); }