Exemple #1
0
        /// <summary>
        /// Create schema nodes and move tables, functions and stored procedures under its schema node
        /// </summary>
        /// <param name="node">Table node to reorganize</param>
        /// <param name="nodeTag">Tag of new node</param>
        /// <returns>The count of schema nodes.</returns>
        public int ReorganizeNodes(TreeNode node, string nodeTag)
        {
            debug_message("ReorganizeNodes");

            if (node.Nodes.Count <= 1)
            {
                return(0);
            }

            //debug_message(DateTime.Now.ToString("ss.fff"));

            node.TreeView.BeginUpdate();

            //can't move nodes while iterating forward over them
            //create list of nodes to move then perform the update

            var schemas = new Dictionary <String, List <TreeNode> >();

            foreach (TreeNode childNode in node.Nodes)
            {
                //skip schema node folders but make sure they are in schemas list
                if (childNode.Tag != null && childNode.Tag.ToString() == nodeTag)
                {
                    if (!schemas.ContainsKey(childNode.Name))
                    {
                        schemas.Add(childNode.Name, new List <TreeNode>());
                    }

                    continue;
                }

                var schema = GetNodeSchema(childNode);

                if (string.IsNullOrEmpty(schema))
                {
                    continue;
                }

                //create schema node
                if (!node.Nodes.ContainsKey(schema))
                {
                    TreeNode schemaNode;
                    if (Options.CloneParentNode)
                    {
                        schemaNode = new SchemaFolderTreeNode(node);
                        node.Nodes.Add(schemaNode);
                    }
                    else
                    {
                        schemaNode = node.Nodes.Add(schema);
                    }

                    schemaNode.Name = schema;
                    schemaNode.Text = schema;
                    schemaNode.Tag  = nodeTag;

                    if (Options.AppendDot)
                    {
                        schemaNode.Text += ".";
                    }

                    if (Options.UseObjectIcon)
                    {
                        schemaNode.ImageIndex         = childNode.ImageIndex;
                        schemaNode.SelectedImageIndex = childNode.ImageIndex;
                    }
                    else
                    {
                        schemaNode.ImageIndex         = node.ImageIndex;
                        schemaNode.SelectedImageIndex = node.ImageIndex;
                    }
                }

                //add node to schema list
                List <TreeNode> schemaNodeList;
                if (!schemas.TryGetValue(schema, out schemaNodeList))
                {
                    schemaNodeList = new List <TreeNode>();
                    schemas.Add(schema, schemaNodeList);
                }
                schemaNodeList.Add(childNode);
            }

            //debug_message(DateTime.Now.ToString("ss.fff"));

            //move nodes to schema node
            foreach (string schema in schemas.Keys)
            {
                var schemaNode = node.Nodes[schema];
                foreach (TreeNode childNode in schemas[schema])
                {
                    node.Nodes.Remove(childNode);
                    schemaNode.Nodes.Add(childNode);
                }
            }

            node.TreeView.EndUpdate();

            //debug_message(DateTime.Now.ToString("ss.fff"));

            return(schemas.Count);
        }
Exemple #2
0
        //private String GetNodeSchema(TreeNode node)
        //{
        //    var ni = GetNodeInformation(node);
        //    if (ni != null)
        //    {
        //        // parse ni.Context = Server[@Name='NR-DEV\SQL2008R2EXPRESS']/Database[@Name='tempdb']/Table[@Name='test.''escape''[value]' and @Schema='dbo']
        //        // or compare ni.Name vs ni.InvariantName = ObjectName vs SchemaName.ObjectName

        //        //var match = NodeSchemaRegex.Match(ni.Context);
        //        //if (match.Success)
        //        //    return match.Groups[1].Value;

        //        if (ni.InvariantName.EndsWith("." + ni.Name))
        //            return ni.InvariantName.Replace("." + ni.Name, String.Empty);
        //    }
        //    return null;
        //}

        /// <summary>
        /// Create schema nodes and move tables, functions and stored procedures under its schema node
        /// </summary>
        /// <param name="node">Table node to reorganize</param>
        /// <param name="nodeTag">Tag of new node</param>
        /// <returns>The count of schema nodes.</returns>
        public int ReorganizeNodes(TreeNode node, string nodeTag)
        {
            debug_message("ReorganizeNodes");

            if (node.Nodes.Count <= 1)
            {
                return(0);
            }

            node.TreeView.BeginUpdate();

            var schemas    = new Dictionary <String, SchemaFolderTreeNode>();
            var childNodes = new List <TreeNode>();

            foreach (TreeNode childNode in node.Nodes)
            {
                if (childNode.Text.Contains(".") && childNode.Tag == null)
                {
                    SchemaFolderTreeNode schemaNode = null;

                    var parts = GetParts(childNode.Text, Options.Separators);

                    if (parts == null)
                    {
                        continue;
                    }

                    var part = parts.GetLastPart();

                    if (!schemas.ContainsKey($"{part.FullName}"))
                    {
                        var parentNode = node;

                        while (parts != null)
                        {
                            if (!schemas.ContainsKey(parts.FullName))
                            {
                                schemaNode = new SchemaFolderTreeNode(node)
                                {
                                    Name = parts.Name,
                                    Text = parts.Name,
                                    Tag  = nodeTag
                                };

                                if (Options.AppendDot && !schemaNode.Text.EndsWith("."))
                                {
                                    schemaNode.Text += ".";
                                }

                                if (Options.UseObjectIcon)
                                {
                                    schemaNode.ImageIndex         = childNode.ImageIndex;
                                    schemaNode.SelectedImageIndex = childNode.ImageIndex;
                                }
                                else
                                {
                                    schemaNode.ImageIndex         = node.ImageIndex;
                                    schemaNode.SelectedImageIndex = node.ImageIndex;
                                }

                                parentNode.Nodes.Add(schemaNode);
                                schemas.Add(parts.FullName, schemaNode);
                            }
                            else
                            {
                                schemaNode = schemas[parts.FullName];
                            }

                            if (parts.Sibling == null)
                            {
                                break;
                            }

                            parentNode = schemaNode;
                            parts      = parts.Sibling;
                        }
                    }
                    else
                    {
                        schemaNode = schemas[part.FullName];
                    }

                    childNode.Text = childNode.Text.Substring(part.Location);
                    childNode.Tag  = schemaNode;

                    childNodes.Add(childNode);
                }
            }

            foreach (TreeNode childNode in childNodes)
            {
                node.Nodes.Remove(childNode);
                (childNode.Tag as SchemaFolderTreeNode).Nodes.Add(childNode);
            }

            node.TreeView.EndUpdate();

            return(node.GetNodeCount(true));
        }
        public int ReorganizeDatabaseNodes(TreeNode node)
        {
            debug_message("ReorganizeDatabaseNodes");

            if (node.Nodes.Count <= 1)
            {
                return(0);
            }

            TreeNode folderNode = null;

            //create node
            string nodeTag   = "OfflineDatabasesFolder";
            string folderKey = "OfflineDatabases";

            if (!node.Nodes.ContainsKey(folderKey))
            {
                int index = 0;
                foreach (TreeNode n in node.Nodes)
                {
                    string urnPath = GetNodeUrnPath(n);
                    if (urnPath == "Server/Database")
                    {
                        index = node.Nodes.IndexOf(n);
                        break;
                    }
                }

                if (Options.CloneParentNode)
                {
                    folderNode = new SchemaFolderTreeNode(node);
                    node.Nodes.Insert(index, folderNode);
                }
                else
                {
                    folderNode = node.Nodes.Insert(index, folderKey);
                }

                folderNode.Name = folderKey;
                folderNode.Text = "Offline Databases";
                folderNode.Tag  = nodeTag;

                folderNode.ImageIndex         = node.ImageIndex;
                folderNode.SelectedImageIndex = node.ImageIndex;
            }
            else
            {
                folderNode = node.Nodes[folderKey];
            }

            //debug_message(DateTime.Now.ToString("ss.fff"));

            node.TreeView.BeginUpdate();

            //can't move nodes while iterating forward over them
            //create list of nodes to move then perform the update
            var nodes = new List <TreeNode>();

            foreach (TreeNode childNode in node.Nodes)
            {
                string urnPath = GetNodeUrnPath(childNode);
                if (urnPath != "Server/Database")
                {
                    continue;
                }

                if (!childNode.Text.EndsWith("(Offline)"))
                {
                    continue;
                }

                //add node to list
                nodes.Add(childNode);
            }

            //debug_message(DateTime.Now.ToString("ss.fff"));

            //move nodes to folder
            foreach (TreeNode childNode in nodes)
            {
                node.Nodes.Remove(childNode);
                folderNode.Nodes.Add(childNode);
            }

            node.TreeView.EndUpdate();

            //debug_message(DateTime.Now.ToString("ss.fff"));

            return(nodes.Count);
        }
        /// <summary>
        /// Create schema nodes and move tables, functions and stored procedures under its schema node
        /// </summary>
        /// <param name="node">Table node to reorganize</param>
        /// <param name="nodeTag">Tag of new node</param>
        /// <returns>The count of schema nodes.</returns>
        public int ReorganizeNodes(TreeNode node, string nodeTag)
        {
            if (node.Nodes.Count <= 1)
            {
                return(0);
            }

            node.TreeView.BeginUpdate();

            //can't move nodes while iterating forward over them
            //create list of nodes to move then perform the update

            var schemas          = new Dictionary <String, List <TreeNode> >();
            var initialNodeCount = node.Nodes.Count;

            foreach (TreeNode childNode in node.Nodes)
            {
                //skip schema node folders but make sure they are in schemas list
                if (childNode.Tag != null && childNode.Tag.ToString() == nodeTag)
                {
                    if (!schemas.ContainsKey(childNode.Name))
                    {
                        schemas.Add(childNode.Name, new List <TreeNode>());
                    }

                    continue;
                }

                var schema = GetNodeSchema(childNode);

                if (string.IsNullOrEmpty(schema))
                {
                    continue;
                }

                //create schema node
                if (!node.Nodes.ContainsKey(schema))
                {
                    int insertIndex = initialNodeCount;
                    for (; insertIndex < node.Nodes.Count; insertIndex++)
                    {
                        if (String.CompareOrdinal(node.Nodes[insertIndex].Name, schema) > 0)
                        {
                            break;
                        }
                    }

                    TreeNode schemaNode;
                    if (Options.CloneParentNode)
                    {
                        schemaNode = new SchemaFolderTreeNode(node);
                        node.Nodes.Insert(insertIndex, schemaNode);
                    }
                    else
                    {
                        schemaNode = node.Nodes.Insert(insertIndex, schema);
                    }

                    schemaNode.Name = schema;
                    schemaNode.Text = schema;
                    schemaNode.Tag  = nodeTag;

                    if (Options.AppendDot)
                    {
                        schemaNode.Text += ".";
                    }

                    if (Options.UseObjectIcon)
                    {
                        schemaNode.ImageIndex         = childNode.ImageIndex;
                        schemaNode.SelectedImageIndex = childNode.ImageIndex;
                    }
                    else
                    {
                        schemaNode.ImageIndex         = node.ImageIndex;
                        schemaNode.SelectedImageIndex = node.ImageIndex;
                    }
                }

                //add node to schema list
                List <TreeNode> schemaNodeList;
                if (!schemas.TryGetValue(schema, out schemaNodeList))
                {
                    schemaNodeList = new List <TreeNode>();
                    schemas.Add(schema, schemaNodeList);
                }
                schemaNodeList.Add(childNode);
            }

            //move nodes to schema node
            foreach (string schema in schemas.Keys)
            {
                var schemaNode         = node.Nodes[schema];
                var orderedSchemaNodes = schemas[schema].OrderBy(_ => _.Name);
                foreach (TreeNode childNode in orderedSchemaNodes)
                {
                    node.Nodes.Remove(childNode);
                    if (Options.RenameNode)
                    {
                        // Note: Node is renamed back to orginal after expanding.
                        RenameNode(childNode);
                    }
                    schemaNode.Nodes.Add(childNode);
                }
            }

            node.TreeView.EndUpdate();

            return(schemas.Count);
        }