Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Data / System / Data / UniqueConstraint.cs / 1305376 / UniqueConstraint.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //[....] //[....] //----------------------------------------------------------------------------- namespace System.Data { using System; using System.Diagnostics; using System.ComponentModel; ////// [ DefaultProperty("ConstraintName"), Editor("Microsoft.VSDesigner.Data.Design.UniqueConstraintEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing), ] public class UniqueConstraint : Constraint { private DataKey key; private Index _constraintIndex; internal bool bPrimaryKey = false; // Design time serialization internal string constraintName = null; internal string[] columnNames = null; ////// Represents a restriction on a set of columns in which all values must be unique. /// ////// public UniqueConstraint(String name, DataColumn column) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and /// . /// public UniqueConstraint(DataColumn column) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; Create(null, columns); } ///Initializes a new instance of the ///with the specified . /// public UniqueConstraint(String name, DataColumn[] columns) { Create(name, columns); } ///Initializes a new instance of the ///with the specified name and array /// of objects. /// public UniqueConstraint(DataColumn[] columns) { Create(null, columns); } // Construct design time object ////// Initializes a new instance of the ///with the given array of /// objects. /// /// [Browsable(false)] public UniqueConstraint(String name, string[] columnNames, bool isPrimaryKey) { this.constraintName = name; this.columnNames = columnNames; this.bPrimaryKey = isPrimaryKey; } ///[To be supplied.] ////// public UniqueConstraint(String name, DataColumn column, bool isPrimaryKey) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; this.bPrimaryKey = isPrimaryKey; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and /// . /// public UniqueConstraint(DataColumn column, bool isPrimaryKey) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; this.bPrimaryKey = isPrimaryKey; Create(null, columns); } ///Initializes a new instance of the ///with the specified . /// public UniqueConstraint(String name, DataColumn[] columns, bool isPrimaryKey) { this.bPrimaryKey = isPrimaryKey; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and array /// of objects. /// public UniqueConstraint(DataColumn[] columns, bool isPrimaryKey) { this.bPrimaryKey = isPrimaryKey; Create(null, columns); } // design time serialization only internal string[] ColumnNames { get { return key.GetColumnNames(); } } internal Index ConstraintIndex { get { Debug.Assert(null != _constraintIndex, "null UniqueConstraint index"); Debug.Assert(_constraintIndex == key.GetSortIndex(), "different unique index"); return _constraintIndex; } } internal void ConstraintIndexClear() { if (null != _constraintIndex) { _constraintIndex.RemoveRef(); _constraintIndex = null; } } internal void ConstraintIndexInitialize() { //Debug.Assert(null == _constraintIndex, "non-null UniqueConstraint index"); if (null == _constraintIndex) { _constraintIndex = key.GetSortIndex(); _constraintIndex.AddRef(); } Debug.Assert(_constraintIndex == key.GetSortIndex(), "unexpected sort index"); } internal override void CheckState() { NonVirtualCheckState(); } private void NonVirtualCheckState() { key.CheckState(); } internal override void CheckCanAddToCollection(ConstraintCollection constraints) { } internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) { if (this.Equals(constraints.Table.primaryKey)) { Debug.Assert(constraints.Table.primaryKey == this, "If the primary key and this are 'Equal', they should also be '=='"); if (!fThrowException) return false; else throw ExceptionBuilder.RemovePrimaryKey(constraints.Table); } for (ParentForeignKeyConstraintEnumerator cs = new ParentForeignKeyConstraintEnumerator(Table.DataSet, Table); cs.GetNext();) { ForeignKeyConstraint constraint = cs.GetForeignKeyConstraint(); if (!key.ColumnsEqual(constraint.ParentKey)) continue; if (!fThrowException) return false; else throw ExceptionBuilder.NeededForForeignKeyConstraint(this, constraint); } return true; } internal override bool CanEnableConstraint() { if (Table.EnforceConstraints) return ConstraintIndex.CheckUnique(); return true; } internal override bool IsConstraintViolated() { bool result = false; Index index = ConstraintIndex; if (index.HasDuplicates) { // object[] uniqueKeys = index.GetUniqueKeyValues(); for (int i = 0; i < uniqueKeys.Length; i++) { Range r = index.FindRecords((object[])uniqueKeys[i]); if (1 < r.Count) { DataRow[] rows = index.GetRows(r); string error = ExceptionBuilder.UniqueConstraintViolationText(key.ColumnsReference, (object[])uniqueKeys[i]); for (int j = 0; j < rows.Length; j++) { // rows[j].RowError = error; foreach(DataColumn dataColumn in key.ColumnsReference){ rows[j].SetColumnError(dataColumn, error); } } // SQLBU 20011224: set_RowError for all DataRow with a unique constraint violation result = true; } } } return result; } internal override void CheckConstraint(DataRow row, DataRowAction action) { if (Table.EnforceConstraints && (action == DataRowAction.Add || action == DataRowAction.Change || (action == DataRowAction.Rollback && row.tempRecord != -1))) { if (row.HaveValuesChanged(ColumnsReference)) { if (ConstraintIndex.IsKeyRecordInIndex(row.GetDefaultRecord())) { object[] values = row.GetColumnValues(ColumnsReference); throw ExceptionBuilder.ConstraintViolation(ColumnsReference, values); } } } } internal override bool ContainsColumn(DataColumn column) { return key.ContainsColumn(column); } internal override Constraint Clone(DataSet destination) { return Clone(destination, false); } internal override Constraint Clone(DataSet destination, bool ignorNSforTableLookup) { int iDest; if (ignorNSforTableLookup) { iDest = destination.Tables.IndexOf(Table.TableName); } else { iDest = destination.Tables.IndexOf(Table.TableName, Table.Namespace, false);// pass false for last param to be backward compatable } if (iDest < 0) return null; DataTable table = destination.Tables[iDest]; int keys = ColumnsReference.Length; DataColumn[] columns = new DataColumn[keys]; for (int i = 0; i < keys; i++) { DataColumn src = ColumnsReference[i]; iDest = table.Columns.IndexOf(src.ColumnName); if (iDest < 0) return null; columns[i] = table.Columns[iDest]; } UniqueConstraint clone = new UniqueConstraint(ConstraintName, columns); // ...Extended Properties foreach(Object key in this.ExtendedProperties.Keys) { clone.ExtendedProperties[key]=this.ExtendedProperties[key]; } return clone; } internal UniqueConstraint Clone(DataTable table) { int keys = ColumnsReference.Length; DataColumn[] columns = new DataColumn[keys]; for (int i = 0; i < keys; i++) { DataColumn src = ColumnsReference[i]; int iDest = table.Columns.IndexOf(src.ColumnName); if (iDest < 0) return null; columns[i] = table.Columns[iDest]; } UniqueConstraint clone = new UniqueConstraint(ConstraintName, columns); // ...Extended Properties foreach(Object key in this.ExtendedProperties.Keys) { clone.ExtendedProperties[key]=this.ExtendedProperties[key]; } return clone; } ////// Initializes a new instance of the ///with the given array of /// objects. /// /// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.KeyConstraintColumnsDescr), ReadOnly(true) ] public virtual DataColumn[] Columns { get { return key.ToArray(); } } internal DataColumn[] ColumnsReference { get { return key.ColumnsReference; } } ///Gets the array of columns that this constraint affects. ////// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.KeyConstraintIsPrimaryKeyDescr) ] public bool IsPrimaryKey { get { if (Table == null) { return false; } return(this == Table.primaryKey); } } private void Create(String constraintName, DataColumn[] columns) { for (int i = 0; i < columns.Length; i++) { if (columns[i].Computed) { throw ExceptionBuilder.ExpressionInConstraint(columns[i]); } } this.key = new DataKey(columns, true); ConstraintName = constraintName; NonVirtualCheckState(); } ///Gets /// a value indicating whether or not the constraint is on a primary key. ////// public override bool Equals(object key2) { if (!(key2 is UniqueConstraint)) return false; return Key.ColumnsEqual(((UniqueConstraint)key2).Key); } public override Int32 GetHashCode() { return base.GetHashCode(); } internal override bool InCollection { set { base.InCollection = value; if (key.ColumnsReference.Length == 1) { key.ColumnsReference[0].InternalUnique(value); } } } internal DataKey Key { get { return key; } } ///Compares this constraint to a second to /// determine if both are identical. ////// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.ConstraintTableDescr), ReadOnly(true) ] public override DataTable Table { get { if (key.HasValue) { return key.Table; } return null; } } // misc } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //Gets the table to which this constraint belongs. ///// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //[....] //[....] //----------------------------------------------------------------------------- namespace System.Data { using System; using System.Diagnostics; using System.ComponentModel; ////// [ DefaultProperty("ConstraintName"), Editor("Microsoft.VSDesigner.Data.Design.UniqueConstraintEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing), ] public class UniqueConstraint : Constraint { private DataKey key; private Index _constraintIndex; internal bool bPrimaryKey = false; // Design time serialization internal string constraintName = null; internal string[] columnNames = null; ////// Represents a restriction on a set of columns in which all values must be unique. /// ////// public UniqueConstraint(String name, DataColumn column) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and /// . /// public UniqueConstraint(DataColumn column) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; Create(null, columns); } ///Initializes a new instance of the ///with the specified . /// public UniqueConstraint(String name, DataColumn[] columns) { Create(name, columns); } ///Initializes a new instance of the ///with the specified name and array /// of objects. /// public UniqueConstraint(DataColumn[] columns) { Create(null, columns); } // Construct design time object ////// Initializes a new instance of the ///with the given array of /// objects. /// /// [Browsable(false)] public UniqueConstraint(String name, string[] columnNames, bool isPrimaryKey) { this.constraintName = name; this.columnNames = columnNames; this.bPrimaryKey = isPrimaryKey; } ///[To be supplied.] ////// public UniqueConstraint(String name, DataColumn column, bool isPrimaryKey) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; this.bPrimaryKey = isPrimaryKey; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and /// . /// public UniqueConstraint(DataColumn column, bool isPrimaryKey) { DataColumn[] columns = new DataColumn[1]; columns[0] = column; this.bPrimaryKey = isPrimaryKey; Create(null, columns); } ///Initializes a new instance of the ///with the specified . /// public UniqueConstraint(String name, DataColumn[] columns, bool isPrimaryKey) { this.bPrimaryKey = isPrimaryKey; Create(name, columns); } ///Initializes a new instance of the ///with the specified name and array /// of objects. /// public UniqueConstraint(DataColumn[] columns, bool isPrimaryKey) { this.bPrimaryKey = isPrimaryKey; Create(null, columns); } // design time serialization only internal string[] ColumnNames { get { return key.GetColumnNames(); } } internal Index ConstraintIndex { get { Debug.Assert(null != _constraintIndex, "null UniqueConstraint index"); Debug.Assert(_constraintIndex == key.GetSortIndex(), "different unique index"); return _constraintIndex; } } internal void ConstraintIndexClear() { if (null != _constraintIndex) { _constraintIndex.RemoveRef(); _constraintIndex = null; } } internal void ConstraintIndexInitialize() { //Debug.Assert(null == _constraintIndex, "non-null UniqueConstraint index"); if (null == _constraintIndex) { _constraintIndex = key.GetSortIndex(); _constraintIndex.AddRef(); } Debug.Assert(_constraintIndex == key.GetSortIndex(), "unexpected sort index"); } internal override void CheckState() { NonVirtualCheckState(); } private void NonVirtualCheckState() { key.CheckState(); } internal override void CheckCanAddToCollection(ConstraintCollection constraints) { } internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) { if (this.Equals(constraints.Table.primaryKey)) { Debug.Assert(constraints.Table.primaryKey == this, "If the primary key and this are 'Equal', they should also be '=='"); if (!fThrowException) return false; else throw ExceptionBuilder.RemovePrimaryKey(constraints.Table); } for (ParentForeignKeyConstraintEnumerator cs = new ParentForeignKeyConstraintEnumerator(Table.DataSet, Table); cs.GetNext();) { ForeignKeyConstraint constraint = cs.GetForeignKeyConstraint(); if (!key.ColumnsEqual(constraint.ParentKey)) continue; if (!fThrowException) return false; else throw ExceptionBuilder.NeededForForeignKeyConstraint(this, constraint); } return true; } internal override bool CanEnableConstraint() { if (Table.EnforceConstraints) return ConstraintIndex.CheckUnique(); return true; } internal override bool IsConstraintViolated() { bool result = false; Index index = ConstraintIndex; if (index.HasDuplicates) { // object[] uniqueKeys = index.GetUniqueKeyValues(); for (int i = 0; i < uniqueKeys.Length; i++) { Range r = index.FindRecords((object[])uniqueKeys[i]); if (1 < r.Count) { DataRow[] rows = index.GetRows(r); string error = ExceptionBuilder.UniqueConstraintViolationText(key.ColumnsReference, (object[])uniqueKeys[i]); for (int j = 0; j < rows.Length; j++) { // rows[j].RowError = error; foreach(DataColumn dataColumn in key.ColumnsReference){ rows[j].SetColumnError(dataColumn, error); } } // SQLBU 20011224: set_RowError for all DataRow with a unique constraint violation result = true; } } } return result; } internal override void CheckConstraint(DataRow row, DataRowAction action) { if (Table.EnforceConstraints && (action == DataRowAction.Add || action == DataRowAction.Change || (action == DataRowAction.Rollback && row.tempRecord != -1))) { if (row.HaveValuesChanged(ColumnsReference)) { if (ConstraintIndex.IsKeyRecordInIndex(row.GetDefaultRecord())) { object[] values = row.GetColumnValues(ColumnsReference); throw ExceptionBuilder.ConstraintViolation(ColumnsReference, values); } } } } internal override bool ContainsColumn(DataColumn column) { return key.ContainsColumn(column); } internal override Constraint Clone(DataSet destination) { return Clone(destination, false); } internal override Constraint Clone(DataSet destination, bool ignorNSforTableLookup) { int iDest; if (ignorNSforTableLookup) { iDest = destination.Tables.IndexOf(Table.TableName); } else { iDest = destination.Tables.IndexOf(Table.TableName, Table.Namespace, false);// pass false for last param to be backward compatable } if (iDest < 0) return null; DataTable table = destination.Tables[iDest]; int keys = ColumnsReference.Length; DataColumn[] columns = new DataColumn[keys]; for (int i = 0; i < keys; i++) { DataColumn src = ColumnsReference[i]; iDest = table.Columns.IndexOf(src.ColumnName); if (iDest < 0) return null; columns[i] = table.Columns[iDest]; } UniqueConstraint clone = new UniqueConstraint(ConstraintName, columns); // ...Extended Properties foreach(Object key in this.ExtendedProperties.Keys) { clone.ExtendedProperties[key]=this.ExtendedProperties[key]; } return clone; } internal UniqueConstraint Clone(DataTable table) { int keys = ColumnsReference.Length; DataColumn[] columns = new DataColumn[keys]; for (int i = 0; i < keys; i++) { DataColumn src = ColumnsReference[i]; int iDest = table.Columns.IndexOf(src.ColumnName); if (iDest < 0) return null; columns[i] = table.Columns[iDest]; } UniqueConstraint clone = new UniqueConstraint(ConstraintName, columns); // ...Extended Properties foreach(Object key in this.ExtendedProperties.Keys) { clone.ExtendedProperties[key]=this.ExtendedProperties[key]; } return clone; } ////// Initializes a new instance of the ///with the given array of /// objects. /// /// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.KeyConstraintColumnsDescr), ReadOnly(true) ] public virtual DataColumn[] Columns { get { return key.ToArray(); } } internal DataColumn[] ColumnsReference { get { return key.ColumnsReference; } } ///Gets the array of columns that this constraint affects. ////// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.KeyConstraintIsPrimaryKeyDescr) ] public bool IsPrimaryKey { get { if (Table == null) { return false; } return(this == Table.primaryKey); } } private void Create(String constraintName, DataColumn[] columns) { for (int i = 0; i < columns.Length; i++) { if (columns[i].Computed) { throw ExceptionBuilder.ExpressionInConstraint(columns[i]); } } this.key = new DataKey(columns, true); ConstraintName = constraintName; NonVirtualCheckState(); } ///Gets /// a value indicating whether or not the constraint is on a primary key. ////// public override bool Equals(object key2) { if (!(key2 is UniqueConstraint)) return false; return Key.ColumnsEqual(((UniqueConstraint)key2).Key); } public override Int32 GetHashCode() { return base.GetHashCode(); } internal override bool InCollection { set { base.InCollection = value; if (key.ColumnsReference.Length == 1) { key.ColumnsReference[0].InternalUnique(value); } } } internal DataKey Key { get { return key; } } ///Compares this constraint to a second to /// determine if both are identical. ////// [ ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.ConstraintTableDescr), ReadOnly(true) ] public override DataTable Table { get { if (key.HasValue) { return key.Table; } return null; } } // misc } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.Gets the table to which this constraint belongs. ///
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DropSource.cs
- ToolStripItem.cs
- IRCollection.cs
- TimeoutTimer.cs
- TextBox.cs
- ObjectConverter.cs
- PerformanceCountersElement.cs
- MarkupExtensionParser.cs
- OutputCacheProfileCollection.cs
- SqlSupersetValidator.cs
- PolygonHotSpot.cs
- BuildResult.cs
- ContentElement.cs
- Bidi.cs
- CommonGetThemePartSize.cs
- PersistenceTask.cs
- PeerNameRecord.cs
- SystemUdpStatistics.cs
- InlineCollection.cs
- GlyphCache.cs
- mediapermission.cs
- CacheChildrenQuery.cs
- ViewLoader.cs
- ProtectedConfigurationSection.cs
- GetCertificateRequest.cs
- AppendHelper.cs
- BaseDataListActionList.cs
- TrailingSpaceComparer.cs
- XmlTypeAttribute.cs
- SqlEnums.cs
- SerializationInfo.cs
- DesignerVerb.cs
- SmtpClient.cs
- OutOfMemoryException.cs
- VirtualDirectoryMappingCollection.cs
- RepeatInfo.cs
- SafeProcessHandle.cs
- MarginsConverter.cs
- ReferenceEqualityComparer.cs
- NeutralResourcesLanguageAttribute.cs
- AccessibleObject.cs
- _NegoState.cs
- DataGridViewAdvancedBorderStyle.cs
- Solver.cs
- Boolean.cs
- WpfWebRequestHelper.cs
- LinkAreaEditor.cs
- SafeSystemMetrics.cs
- ImplicitInputBrush.cs
- AttachInfo.cs
- PrinterResolution.cs
- SvcMapFileLoader.cs
- IntSecurity.cs
- StrokeNode.cs
- HwndAppCommandInputProvider.cs
- BindingWorker.cs
- ClientEventManager.cs
- HttpRequest.cs
- QuerySettings.cs
- TimeSpanMinutesConverter.cs
- AuthenticationSection.cs
- FormsAuthenticationEventArgs.cs
- RenderContext.cs
- TempFiles.cs
- XmlWellformedWriter.cs
- XPathQueryGenerator.cs
- MetadataArtifactLoaderCompositeFile.cs
- XslVisitor.cs
- LayoutTable.cs
- ListViewSortEventArgs.cs
- CompilerCollection.cs
- Constraint.cs
- RenamedEventArgs.cs
- ScriptBehaviorDescriptor.cs
- XmlSchemaInferenceException.cs
- Timer.cs
- SerializationUtility.cs
- SyndicationItemFormatter.cs
- SimpleTextLine.cs
- SqlDataSourceFilteringEventArgs.cs
- SplitterCancelEvent.cs
- SyndicationSerializer.cs
- Domain.cs
- SelectedGridItemChangedEvent.cs
- SqlRewriteScalarSubqueries.cs
- Membership.cs
- AsymmetricKeyExchangeFormatter.cs
- StatusBarItem.cs
- BuildProvider.cs
- Vector3DKeyFrameCollection.cs
- RemotingService.cs
- RegistrySecurity.cs
- SqlConnectionHelper.cs
- CommonObjectSecurity.cs
- IISMapPath.cs
- ReverseQueryOperator.cs
- TaiwanCalendar.cs
- TextTreeTextBlock.cs
- EncryptedData.cs
- HttpModuleAction.cs