Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / System / data / design / TableAdapterManagerHelper.cs / 3 / TableAdapterManagerHelper.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All Rights Reserved. // Information Contained Herein is Proprietary and Confidential. // //----------------------------------------------------------------------------- namespace System.Data.Design { using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Diagnostics; using System.ComponentModel; internal class TableAdapterManagerHelper { ////// Find out the self referenced relation /// Rule: if there is multiple self-ref relations, /// and if there is relations with FK, they and only they will be returned /// otherewise, return all self-ref relations /// /// Example: table has RELFK1,RELFK2,REL3 --> return RELFK1,RELFK2 /// Example: table has REL3, REL4 --> return REL3, REL4 /// Example: table has FK1,FK2 --> return null /// /// the dataTable ///the selfRef relations for this table. internal static DataRelation[] GetSelfRefRelations(DataTable dataTable) { Debug.Assert(dataTable != null); ListselfRefs = new List (); List selfRefWithFKs = new List (); foreach (DataRelation relation in dataTable.ParentRelations) { if (relation.ChildTable == relation.ParentTable){ selfRefs.Add(relation); if (relation.ChildKeyConstraint != null) { selfRefWithFKs.Add(relation); } } } if (selfRefWithFKs.Count > 0) { return selfRefWithFKs.ToArray(); } return selfRefs.ToArray(); } /// /// Find out the hierarchical update order based on the first FKs then the relations. /// Example, customer(parent of order), order(parent of orderdetail), orderdetail /// --> out put will be Customer, Order, OrderDetail /// Self-referece is not considered in the order /// Circle referece will stop the searching once detected /// /// the dataset ///DataTable array with parents first internal static DataTable[] GetUpdateOrder(DataSet ds) { // Find out the tables that get involved, then build a tree HierarchicalObject[] orders = new HierarchicalObject[ds.Tables.Count]; for (int i = 0; i < ds.Tables.Count; i++) { DataTable t = ds.Tables[i]; HierarchicalObject ho = new HierarchicalObject(t); orders[i] = ho; } // First, build up the parent tree for (int i = 0; i < orders.Length; i++) { DataTable t = orders[i].TheObject as DataTable; // build HU parent relation tree based on FK foreach (Constraint c in t.Constraints) { ForeignKeyConstraint fc = c as ForeignKeyConstraint; // We do not care if the rule is turned on or not // We do not consider self referenced FK if (fc != null && !Object.ReferenceEquals(fc.RelatedTable, t)) { int index = ds.Tables.IndexOf(fc.RelatedTable); Debug.Assert(index >= 0); orders[i].AddUniqueParent(orders[index]); } } // build HU parent relation tree based on relation foreach (DataRelation relation in t.ParentRelations) { if (!object.ReferenceEquals(relation.ParentTable, t)) { int index = ds.Tables.IndexOf(relation.ParentTable); Debug.Assert(index >= 0); orders[i].AddUniqueParent(orders[index]); } } } // Work out the priorities for (int i = 0; i < orders.Length; i++) { HierarchicalObject ho = orders[i]; if (ho.HasParent) { ho.CheckParents(); } } // Get the result with sorted order DataTable[] dataTables = new DataTable[orders.Length]; System.Array.Sort(orders); for (int i = 0; i < orders.Length; i++) { HierarchicalObject ho = orders[i]; dataTables[i] = (DataTable)ho.TheObject; } return dataTables; } /// /// The object with a list of parents /// internal class HierarchicalObject : IComparable{ internal int Height = 0; // the hierarchical priority internal Object TheObject; private List parents; internal List Parents { get { if (parents == null) { parents = new List (); } return parents; } } internal bool HasParent { get { return parents != null && parents.Count > 0; } } internal HierarchicalObject(Object theObject) { this.TheObject = theObject; } /// /// Add the parent if it is not exist in the parent list /// /// internal void AddUniqueParent(HierarchicalObject parent) { if (!Parents.Contains(parent)) { Parents.Add(parent); } } ////// Check to see if it has parent or not in a loop and update its parent's value /// internal void CheckParents() { if (HasParent) { Stackpath = new Stack (); Stack work = new Stack (); work.Push(this); path.Push(this); this.CheckParents(work, path); } } /// /// Check to see if it has parent or not in a loop and update its parent's value /// internal void CheckParents(Stackwork, Stack path) { if (!HasParent || (!object.ReferenceEquals(this, path.Peek()) && path.Contains(this))) { // Stop if there is no parent or it is in a loop // path.Peek() is always this, so we need to exclude it. // Debug.Assert(work.Count > 0 && path.Count > 0 && object.ReferenceEquals(path.Peek(), this)); HierarchicalObject topPath = path.Pop(); HierarchicalObject topWork = work.Pop(); while (work.Count > 0 && path.Count > 0 && object.ReferenceEquals(topPath, topWork)) { topPath = path.Pop(); topWork = work.Pop(); } if (topWork != topPath) { path.Push(topWork); topWork.CheckParents(work, path); } return; } else if (this.HasParent) { // has parent HierarchicalObject first = null; // find out all parents that is not in a loop and has lower priority then this // increase their priority and push them to the work stack, we need to walk up to the tree // one by one to update the grandparent's priority for (int i = Parents.Count - 1; i >= 0; i--) { HierarchicalObject current = Parents[i]; if (!path.Contains(current) && current.Height <= this.Height) { current.Height = this.Height + 1; Debug.Assert(current.Height < 1000); if (current.Height > 1000) { return; } work.Push(current); first = current; } } // Now we walk up the first parent if (first != null) { path.Push(first); first.CheckParents(work, path); } } } int IComparable .CompareTo(HierarchicalObject other) { return other.Height - this.Height; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XamlWriter.cs
- CompilationPass2Task.cs
- ConstructorNeedsTagAttribute.cs
- GridViewSelectEventArgs.cs
- ThreadAttributes.cs
- ToolStripScrollButton.cs
- State.cs
- SubtreeProcessor.cs
- VBCodeProvider.cs
- ParentQuery.cs
- ExpressionConverter.cs
- DataGridViewCellStyleContentChangedEventArgs.cs
- BatchServiceHost.cs
- SapiGrammar.cs
- _Semaphore.cs
- XmlDictionaryReaderQuotasElement.cs
- ProtocolsConfigurationHandler.cs
- MethodSet.cs
- SurrogateSelector.cs
- MarginsConverter.cs
- EqualityComparer.cs
- XmlFormatWriterGenerator.cs
- OneToOneMappingSerializer.cs
- CharacterShapingProperties.cs
- GetRecipientListRequest.cs
- OrderedEnumerableRowCollection.cs
- RepeaterItemCollection.cs
- BlurBitmapEffect.cs
- DocumentOrderQuery.cs
- WindowsProgressbar.cs
- ZipPackage.cs
- SchemaMapping.cs
- COM2IPerPropertyBrowsingHandler.cs
- EntityClientCacheEntry.cs
- NotifyIcon.cs
- DependencyPropertyKey.cs
- ReflectionTypeLoadException.cs
- InvokeMethodActivityDesigner.cs
- OdbcConnectionHandle.cs
- SmtpClient.cs
- RuntimeEnvironment.cs
- MruCache.cs
- Double.cs
- SingleStorage.cs
- InstanceKeyNotReadyException.cs
- TransformConverter.cs
- DesignerAutoFormatCollection.cs
- SoapException.cs
- TypeDescriptionProviderAttribute.cs
- SyntaxCheck.cs
- PageThemeBuildProvider.cs
- ExpressionConverter.cs
- FormsAuthenticationCredentials.cs
- DesignerImageAdapter.cs
- FormViewUpdatedEventArgs.cs
- AsyncCompletedEventArgs.cs
- DataGridPagerStyle.cs
- ProbeRequestResponseAsyncResult.cs
- wmiprovider.cs
- BStrWrapper.cs
- UniqueConstraint.cs
- XmlAttributeAttribute.cs
- SoapElementAttribute.cs
- reliableinputsessionchannel.cs
- HeaderedItemsControl.cs
- LifetimeServices.cs
- UriTemplateDispatchFormatter.cs
- DocumentOrderQuery.cs
- ProfileSettings.cs
- CacheMemory.cs
- NotificationContext.cs
- BufferedWebEventProvider.cs
- HttpWriter.cs
- SimpleHandlerFactory.cs
- UIEndRequest.cs
- RectValueSerializer.cs
- GridViewRow.cs
- DataGridViewHeaderCell.cs
- GroupBox.cs
- Int32Rect.cs
- QueryCursorEventArgs.cs
- BooleanSwitch.cs
- ContainerFilterService.cs
- WebAdminConfigurationHelper.cs
- ComboBoxDesigner.cs
- GPRECT.cs
- PersonalizationProviderHelper.cs
- XamlStyleSerializer.cs
- MetadataItemCollectionFactory.cs
- followingsibling.cs
- RadioButtonAutomationPeer.cs
- ColorConvertedBitmap.cs
- PropertyGrid.cs
- Quack.cs
- LinqDataSourceSelectEventArgs.cs
- SQLDecimal.cs
- InkCanvasSelectionAdorner.cs
- FixedSOMLineRanges.cs
- DataGridViewSelectedRowCollection.cs
- XmlSchemaComplexType.cs