private List <Urn> GetGenericUrns(SmoCollectionBase collections) { var urns = new List <Urn>(); foreach (SqlSmoObject smo in collections) { //skip all system objects if (smo is Schema && ((smo as Schema).IsSystemObject)) { continue; } if (smo is Table && ((smo as Table).IsSystemObject)) { continue; } if (smo is View && ((smo as View).IsSystemObject)) { continue; } if (smo is StoredProcedure && ((smo as StoredProcedure).IsSystemObject)) { continue; } if (smo is UserDefinedFunction && ((smo as UserDefinedFunction).IsSystemObject)) { continue; } TraceService.Debug($"Listed urn for scripting: {smo.Urn}"); urns.Add(smo.Urn); } return(urns); }
private void ShowDetails(TreeNode node) { SqlSmoObject smoObject = null; SmoCollectionBase smoCollection = null; this.ListView.Items.Clear(); if (node == null) { return; } switch (node.Tag.GetType().Name) { case "DatabaseCollection": case "TableCollection": case "ViewCollection": case "StoredProcedureCollection": case "ColumnCollection": case "SqlAssemblyCollection": // Load the items of a collection, if not already loaded LoadTreeViewItems(node); // Update the TreeView smoCollection = (SmoCollectionBase)node.Tag; UpdateListViewWithCollection(smoCollection); break; case "Server": smoObject = ((Server)node.Tag).Information; UpdateListView(smoObject, true, "Information"); smoObject = ((Server)node.Tag).Settings; UpdateListView(smoObject, false, "Settings"); break; case "Database": case "Table": case "View": case "StoredProcedure": case "Column": case "SqlAssembly": smoObject = (SqlSmoObject)node.Tag; UpdateListView(smoObject, true, null); break; default: throw new Exception(Properties.Resources.UnrecognizedType + node.Tag.GetType().ToString()); } }
// Lists a collection in the listview if the container node is selected private void UpdateListViewWithCollection(SmoCollectionBase smoCollection) { this.ListView.Columns.Clear(); this.ListView.Groups.Clear(); ColumnHeader colHeader = new ColumnHeader(); colHeader.Text = Properties.Resources.ObjectName; this.ListView.Columns.Add(colHeader); colHeader = new ColumnHeader(); if (smoCollection is ColumnCollection) { colHeader.Text = Properties.Resources.DataType; } else { colHeader.Text = Properties.Resources.DateCreated; } this.ListView.Columns.Add(colHeader); foreach (SqlSmoObject smoObject in smoCollection) { if (smoObject.Properties.Contains("IsSystemObject") == true && (bool)smoObject.Properties["IsSystemObject"].Value == true) { continue; } ListViewItem lvi = new ListViewItem(); lvi.Text = smoObject.ToString(); lvi.Name = smoObject.ToString(); if (smoObject is Column) { lvi.SubItems.Add(smoObject.Properties["DataType"].Value.ToString()); } else { lvi.SubItems.Add(smoObject.Properties["CreateDate"].Value.ToString()); } lvi.Tag = smoObject; this.ListView.Items.Add(lvi); } this.ListView.Sorting = System.Windows.Forms.SortOrder.Ascending; this.ListView.Sort(); this.ListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); this.ListView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); }
static void DumpObjects <T>(SmoCollectionBase objects, string prompt, ScriptingOptions options) where T : NamedSmoObject, IScriptable { var isSystemObject = typeof(T).GetProperties().FirstOrDefault(x => x.Name == "IsSystemObject"); foreach (T sec in objects) { if (isSystemObject != null && (bool)isSystemObject.GetValue(sec)) { continue; } StringCollection sc = sec.Script(options); Console.WriteLine(prompt + sec.Name); } }
void AppendType <T>(StringBuilder stringBuilder, ScriptingOptions options, SmoCollectionBase items, Func <T, bool> isSystem) where T : NamedSmoObject, IScriptable { var filtered = items.Cast <T>() .Where(x => !isSystem(x) && settings.IncludeItem(x.Name)) .ToList(); if (!filtered.Any()) { return; } stringBuilder.AppendLine($"-- {typeof(T).Name}s"); foreach (var item in filtered) { stringBuilder.AppendLine(); var lines = item.Script(options) .Cast <string>() .Where(x => !IsSet(x)) .ToList(); if (lines.Count == 1) { stringBuilder.AppendLine(lines[0].Trim()); } else { for (var index = 0; index < lines.Count; index++) { var line = lines[index]; if (index == 0) { stringBuilder.AppendLine(line.TrimStart()); continue; } if (index == lines.Count - 1) { stringBuilder.AppendLine(line.TrimEnd()); continue; } stringBuilder.AppendLine(line); } } } stringBuilder.AppendLine(); stringBuilder.AppendLine(); }
private void Output(SmoCollectionBase coll, TextWriter tw, Scripter scrp, String header = null) { LinkedList <string> tableAlterings = new LinkedList <string>(); // When dropping tables, table alterings (drop constraints) must be output first, // so we place the main table operations (drop) in a temporary writer (tmpWriter), // output the table alterings (drop constraint) to the main writer and then output // the temporary writer to the main writer. TextWriter tmpWriter = scrp.Options.ScriptDrops ? new StringWriter() : tw; String tmpHeader = scrp.Options.ScriptDrops ? null : header; foreach (NamedSmoObject o in coll) { if (!(o is DatabaseRole) || (!((DatabaseRole)o).IsFixedRole && o.Name != "public")) // Don't output fixed database roles neither the "public" database role { Output(o, tmpWriter, scrp, tableAlterings, ref tmpHeader); } } if (!scrp.Options.ScriptDrops) { header = tmpHeader; } foreach (string s in tableAlterings) { if (header != null) { tw.WriteLine(header); header = null; } tw.WriteLine(s); } if (scrp.Options.ScriptDrops) { if (tmpWriter.ToString().Length > 0 && header != null) { tw.WriteLine(header); } tw.Write(tmpWriter); } }
/// <summary> /// Constructor which accepts a <see cref="SmoCollectionBase"/> containing the objects /// to wrap /// </summary> /// <param name="collection"><see cref="SmoCollectionBase"/> or null if none were set</param> public SmoCollectionWrapper(SmoCollectionBase collection) { this.collection = collection; }
/// <summary> /// Saves all objects in database as *.sql scripts in subdirectories /// organized by object type. /// </summary> /// <param name="basePath">Base directory to save scripts to.</param> /// <param name="label">Label of subfolder.</param> /// <param name="collection">Collection of SMOs to save.</param> /// <param name="extendedSave">Optional custom save delegate (if not null, callee is completely responsible for saving.)</param> protected void SaveObjects(String basePath, String label, SmoCollectionBase collection, SaveAllowedDelegate saveAllowed) { String folder = Path.Combine(basePath, label); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); foreach (SqlSmoObject obj in collection) { SmoWrapper wrapper = new SmoWrapper(obj); if (wrapper.IsSystemObject) continue; if (saveAllowed == null || (saveAllowed != null && saveAllowed(wrapper))) { using (ScriptWriter scriptWriter = new ScriptWriter( new StreamWriter( Path.Combine(folder, String.Format("{0}.sql", StringUtils.NormalizeFilename( wrapper.QualifiedName)))), _options)) { scriptWriter.WriteDefinition(wrapper); } } } }
/// <summary> /// Saves all objects in database as *.sql scripts in subdirectories /// organized by object type. /// </summary> /// <param name="basePath">Base directory to save scripts to.</param> /// <param name="label">Label of subfolder.</param> /// <param name="collection">Collection of SMOs to save.</param> protected void SaveObjects(String basePath, String label, SmoCollectionBase collection) { SaveObjects(basePath, label, collection, null); }