private TreeNodeCollection AddTreeNodes <T>(TreeNode node, DatabaseObjectType types, DatabaseObjectType type, List <T> dbObjects, bool createFolderNode = true, bool createFakeNode = false) where T : DatabaseObject { TreeNode targetNode = node; if (types.HasFlag(type)) { if (createFolderNode) { targetNode = node.AddDbObjectFolderNode(dbObjects); } else { targetNode = node.AddDbObjectNodes(dbObjects); } } if (createFakeNode && targetNode != null) { foreach (TreeNode child in targetNode.Nodes) { child.Nodes.Add(DbObjectsTreeHelper.CreateFakeNode()); } } return(node.Nodes); }
private async Task AddTableObjectNodes(TreeNode treeNode, Table table, DatabaseObjectType databaseObjectType) { string nodeName = treeNode.Name; string database = this.GetDatabaseNode(treeNode).Name; DbInterpreter dbInterpreter = this.GetDbInterpreter(database, false); dbInterpreter.Subscribe(this); SchemaInfo schemaInfo = await dbInterpreter.GetSchemaInfoAsync(new SchemaInfoFilter() { Strict = true, DatabaseObjectType = databaseObjectType, TableNames = new string[] { table.Name } }); this.ClearNodes(treeNode); #region Columns if (nodeName == nameof(DbObjectTreeFolderType.Columns)) { foreach (TableColumn column in schemaInfo.TableColumns) { string text = this.GetColumnText(dbInterpreter, table, column); bool isPrimaryKey = schemaInfo.TablePrimaryKeys.Any(item => item.Columns.Any(t => t.ColumnName == column.Name)); bool isForeignKey = schemaInfo.TableForeignKeys.Any(item => item.Columns.Any(t => t.ColumnName == column.Name)); string imageKeyName = isPrimaryKey ? nameof(TablePrimaryKey) : (isForeignKey ? nameof(TableForeignKey) : nameof(TableColumn)); TreeNode node = DbObjectsTreeHelper.CreateTreeNode(column.Name, text, imageKeyName); node.Tag = column; treeNode.Nodes.Add(node); } } #endregion if (nodeName == nameof(DbObjectTreeFolderType.Triggers)) { treeNode.AddDbObjectNodes(schemaInfo.TableTriggers); } #region Indexes if (nodeName == nameof(DbObjectTreeFolderType.Indexes) && schemaInfo.TableIndexes.Any()) { foreach (TableIndex index in schemaInfo.TableIndexes) { bool isUnique = index.IsUnique; string strColumns = string.Join(",", index.Columns.OrderBy(item => item.Order).Select(item => item.ColumnName)); string content = isUnique ? $"(Unique, {strColumns})" : $"({strColumns})"; string text = $"{index.Name}{content}"; string imageKeyName = nameof(TableIndex); TreeNode node = DbObjectsTreeHelper.CreateTreeNode(index.Name, text, imageKeyName); node.Tag = index; treeNode.Nodes.Add(node); } } #endregion if (nodeName == nameof(DbObjectTreeFolderType.Keys)) { foreach (TablePrimaryKey key in schemaInfo.TablePrimaryKeys) { TreeNode node = DbObjectsTreeHelper.CreateTreeNode(key); treeNode.Nodes.Add(node); } foreach (TableForeignKey key in schemaInfo.TableForeignKeys) { TreeNode node = DbObjectsTreeHelper.CreateTreeNode(key); treeNode.Nodes.Add(node); } } #region Constraints if (nodeName == nameof(DbObjectTreeFolderType.Constraints) && schemaInfo.TableConstraints.Any()) { foreach (TableConstraint constraint in schemaInfo.TableConstraints) { TreeNode node = DbObjectsTreeHelper.CreateTreeNode(constraint); treeNode.Nodes.Add(node); } } #endregion this.Feedback(""); }