Inheritance: InternalDataCollectionBase
Exemplo n.º 1
1
        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Data.DataTable'/> class with no arguments.</para>
        /// </devdoc>
        public DataTable() {
            GC.SuppressFinalize(this);
            Bid.Trace("<ds.DataTable.DataTable|API> %d#\n", ObjectID);
            nextRowID = 1;
            recordManager = new RecordManager(this);

            _culture = CultureInfo.CurrentCulture;
            this.columnCollection = new DataColumnCollection(this);
            this.constraintCollection = new ConstraintCollection(this);
            this.rowCollection = new DataRowCollection(this);
            this.indexes = new List<Index>();

            rowBuilder = new DataRowBuilder(this, -1);
        }
        internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException)
        {
            if (this.Equals(constraints.Table.primaryKey))
            {
                if (fThrowException)
                {
                    throw ExceptionBuilder.RemovePrimaryKey(constraints.Table);
                }
                return(false);
            }
            ParentForeignKeyConstraintEnumerator enumerator = new ParentForeignKeyConstraintEnumerator(this.Table.DataSet, this.Table);

            while (enumerator.GetNext())
            {
                ForeignKeyConstraint foreignKeyConstraint = enumerator.GetForeignKeyConstraint();
                if (this.key.ColumnsEqual(foreignKeyConstraint.ParentKey))
                {
                    if (fThrowException)
                    {
                        throw ExceptionBuilder.NeededForForeignKeyConstraint(this, foreignKeyConstraint);
                    }
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        internal static UniqueConstraint GetUniqueConstraintForColumnSet(ConstraintCollection collection,
                                                                         DataColumn[] columns)
        {
            if (null == collection)
            {
                throw new ArgumentNullException("Collection can't be null.");
            }
            if (null == columns)
            {
                return(null);
            }

            foreach (Constraint constraint in collection)
            {
                if (constraint is UniqueConstraint)
                {
                    UniqueConstraint uc = constraint as UniqueConstraint;
                    if (DataColumn.AreColumnSetsTheSame(uc.Columns, columns))
                    {
                        return(uc);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        internal static UniqueConstraint GetPrimaryKeyConstraint(ConstraintCollection collection)
        {
            if (null == collection)
            {
                throw new ArgumentNullException("Collection can't be null.");
            }

            UniqueConstraint uc;
            IEnumerator      enumer = collection.GetEnumerator();

            while (enumer.MoveNext())
            {
                uc = enumer.Current as UniqueConstraint;
                if (null == uc)
                {
                    continue;
                }

                if (uc.IsPrimaryKey)
                {
                    return(uc);
                }
            }

            //if we got here there was no pk
            return(null);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow)
        {
            if (IsPrimaryKey)
            {
                if (shouldThrow)
                {
                    throw new ArgumentException("Cannot remove unique constraint since it's the primary key of a table.");
                }

                return(false);
            }

            if (Table.DataSet == null)
            {
                return(true);
            }

            if (ChildConstraint != null)
            {
                if (!shouldThrow)
                {
                    return(false);
                }
                throw new ArgumentException(String.Format(
                                                "Cannot remove unique constraint '{0}'." +
                                                "Remove foreign key constraint '{1}' first.",
                                                ConstraintName, ChildConstraint.ConstraintName));
            }
            return(true);
        }
        //Checks to see if a related unique constraint exists
        //if it doesn't then a unique constraint is created.
        //if a unique constraint can't be created an exception will be thrown
        private void _ensureUniqueConstraintExists(ConstraintCollection collection,
                                                   DataColumn [] parentColumns)
        {
            //not null
            if (null == parentColumns)
            {
                throw new ArgumentNullException(
                          "ParentColumns can't be null");
            }

            UniqueConstraint uc = null;

            //see if unique constraint already exists
            //if not create unique constraint
            if (parentColumns[0] != null)
            {
                uc = UniqueConstraint.GetUniqueConstraintForColumnSet(parentColumns[0].Table.Constraints, parentColumns);
            }

            if (null == uc)
            {
                uc = new UniqueConstraint(parentColumns, false); //could throw
                parentColumns [0].Table.Constraints.Add(uc);
            }

            //keep reference
            _parentUniqueConstraint = uc;
            _parentUniqueConstraint.ChildConstraint = this;
        }
        internal override void AddToConstraintCollectionSetup(
            ConstraintCollection collection)
        {
            if (collection.Table != Table)
            {
                throw new InvalidConstraintException("This constraint cannot be added since ForeignKey doesn't belong to table " + RelatedTable.TableName + ".");
            }

            //run Ctor rules again
            _validateColumns(_parentColumns, _childColumns);

            //we must have a unique constraint on the parent
            _ensureUniqueConstraintExists(collection, _parentColumns);

            if ((Table.DataSet != null && Table.DataSet.EnforceConstraints) ||
                (Table.DataSet == null && Table.EnforceConstraints))
            {
                if (IsConstraintViolated())
                {
                    throw new ArgumentException("This constraint cannot be enabled as not all values have corresponding parent values.");
                }
            }
            //FIXME : if this fails and we created a unique constraint
            //we should probably roll it back
            // and remove index form Table
        }
Exemplo n.º 9
0
        internal static void SetAsPrimaryKey(ConstraintCollection collection, UniqueConstraint newPrimaryKey)
        {
            //not null
            if (null == collection)
            {
                throw new ArgumentNullException("ConstraintCollection can't be null.");
            }

            //make sure newPrimaryKey belongs to the collection parm unless it is null
            if (collection.IndexOf(newPrimaryKey) < 0 && (null != newPrimaryKey))
            {
                throw new ArgumentException("newPrimaryKey must belong to collection.");
            }

            //Get existing pk
            UniqueConstraint uc = GetPrimaryKeyConstraint(collection);

            //clear existing
            if (null != uc)
            {
                uc._isPrimaryKey = false;
            }

            //set new key
            if (null != newPrimaryKey)
            {
                newPrimaryKey._isPrimaryKey = true;
            }
        }
 internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException)
 {
     if (this.Equals(constraints.Table.primaryKey))
     {
         if (fThrowException)
         {
             throw ExceptionBuilder.RemovePrimaryKey(constraints.Table);
         }
         return false;
     }
     ParentForeignKeyConstraintEnumerator enumerator = new ParentForeignKeyConstraintEnumerator(this.Table.DataSet, this.Table);
     while (enumerator.GetNext())
     {
         ForeignKeyConstraint foreignKeyConstraint = enumerator.GetForeignKeyConstraint();
         if (this.key.ColumnsEqual(foreignKeyConstraint.ParentKey))
         {
             if (fThrowException)
             {
                 throw ExceptionBuilder.NeededForForeignKeyConstraint(this, foreignKeyConstraint);
             }
             return false;
         }
     }
     return true;
 }
Exemplo n.º 11
0
        internal override void RemoveFromConstraintCollectionCleanup(
            ConstraintCollection collection)
        {
            _parentUniqueConstraint.ChildConstraint = null;
#if NOT_PFX
            Index = null;
#endif
        }
Exemplo n.º 12
0
 internal override void CheckCanAddToCollection(ConstraintCollection constraints)
 {
     if (Table != constraints.Table)
     {
         throw ExceptionBuilder.ConstraintAddFailed(constraints.Table);
     }
     if (Table.Locale.LCID != RelatedTable.Locale.LCID || Table.CaseSensitive != RelatedTable.CaseSensitive)
     {
         throw ExceptionBuilder.CaseLocaleMismatch();
     }
 }
Exemplo n.º 13
0
        internal override void RemoveFromConstraintCollectionCleanup(
            ConstraintCollection collection)
        {
            if (Columns.Length == 1)
            {
                Columns [0].Unique = false;
            }

            _belongsToCollection = false;
            Index = null;
        }
Exemplo n.º 14
0
 internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException)
 {
     if (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();)
Exemplo n.º 15
0
        internal override void AddToConstraintCollectionSetup(
            ConstraintCollection collection)
        {
            for (int i = 0; i < Columns.Length; i++)
            {
                if (Columns[i].Table != collection.Table)
                {
                    throw new ArgumentException("These columns don't point to this table.");
                }
            }
            //run Ctor rules again
            _validateColumns(_dataColumns);

            //make sure a unique constraint doesn't already exists for these columns
            UniqueConstraint uc = UniqueConstraint.GetUniqueConstraintForColumnSet(collection, this.Columns);

            if (null != uc)
            {
                throw new ArgumentException("Unique constraint already exists for these" +
                                            " columns. Existing ConstraintName is " + uc.ConstraintName);
            }

            //Allow only one primary key
            if (this.IsPrimaryKey)
            {
                uc = GetPrimaryKeyConstraint(collection);
                if (null != uc)
                {
                    uc._isPrimaryKey = false;
                }
            }

            // if constraint is based on one column only
            // this column becomes unique
            if (_dataColumns.Length == 1)
            {
                _dataColumns[0].SetUnique();
            }

            if (IsConstraintViolated())
            {
                throw new ArgumentException("These columns don't currently have unique values.");
            }

            _belongsToCollection = true;
        }
Exemplo n.º 16
0
        public virtual void Reset()
        {
            // first we remove all ForeignKeyConstraints (if we will not do that
            // we will get an exception when clearing the tables).
            for (int i = 0; i < Tables.Count; i++)
            {
                ConstraintCollection cc = Tables[i].Constraints;
                for (int j = 0; j < cc.Count; j++)
                {
                    if (cc[j] is ForeignKeyConstraint)
                    {
                        cc.Remove(cc[j]);
                    }
                }
            }

            Clear();
            Relations.Clear();
            Tables.Clear();
        }
Exemplo n.º 17
0
        private ForeignKeyConstraint FindForeignKey(ConstraintCollection cl)
        {
            ForeignKeyConstraint fkc = null;

            foreach (Constraint o in cl)
            {
                if (!(o is ForeignKeyConstraint))
                {
                    continue;
                }
                fkc = (ForeignKeyConstraint)o;
                /* Check ChildColumns & ParentColumns */
                if (CompareDataColumns(ChildColumns, fkc.Columns) &&
                    CompareDataColumns(ParentColumns, fkc.RelatedColumns))
                {
                    return(fkc);
                }
            }
            return(null);
        }
Exemplo n.º 18
0
        private UniqueConstraint FindUniqueConstraint(ConstraintCollection cl)
        {
            UniqueConstraint uc = null;

            // find if the unique constraint already exists in the parent table.
            foreach (Constraint o in cl)
            {
                if (!(o is UniqueConstraint))
                {
                    continue;
                }
                uc = (UniqueConstraint)o;
                //Check in ParentColumns
                if (CompareDataColumns(ParentColumns, uc.Columns))
                {
                    return(uc);
                }
            }
            return(null);
        }
Exemplo n.º 19
0
		/// <summary>
		/// Initializes a new instance of the DataTable class with no arguments.
		/// </summary>
		public DataTable ()
		{
			dataSet = null;
			_columnCollection = new DataColumnCollection(this);
			_constraintCollection = new ConstraintCollection(this);
			_extendedProperties = new PropertyCollection();
			_tableName = "";
			_nameSpace = null;
			_caseSensitive = false;	 	//default value
			_displayExpression = null;
			_primaryKeyConstraint = null;
			_site = null;
			_rows = new DataRowCollection (this);
			_indexes = new ArrayList();
			_recordCache = new RecordCache(this);

			//LAMESPEC: spec says 25 impl does 50
			_minimumCapacity = 50;

			_childRelations = new DataRelationCollection.DataTableRelationCollection (this);
			_parentRelations = new DataRelationCollection.DataTableRelationCollection (this);
		}
        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;
        }
Exemplo n.º 21
0
		private UniqueConstraint FindUniqueConstraint (ConstraintCollection cl)
		{
			UniqueConstraint uc = null;
			// find if the unique constraint already exists in the parent table.
			foreach (Constraint o in cl){
				if (! (o is UniqueConstraint))
					continue;
				uc = (UniqueConstraint) o;
				//Check in ParentColumns
				if (CompareDataColumns (ParentColumns, uc.Columns))
					return uc;
			}
			return null;
		}
Exemplo n.º 22
0
 internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
Exemplo n.º 23
0
		internal override void RemoveFromConstraintCollectionCleanup( 
				ConstraintCollection collection)
		{
			if (Columns.Length == 1)
				Columns [0].Unique = false;

			_belongsToCollection = false;
			Index = null;
		}
		internal override void AddToConstraintCollectionSetup(
				ConstraintCollection collection)
		{
			for (int i = 0; i < Columns.Length; i++)
				if (Columns[i].Table != collection.Table)
					throw new ArgumentException("These columns don't point to this table.");
			//run Ctor rules again
			_validateColumns(_dataColumns);
			
			//make sure a unique constraint doesn't already exists for these columns
			UniqueConstraint uc = UniqueConstraint.GetUniqueConstraintForColumnSet(collection, this.Columns);	
			if (null != uc) throw new ArgumentException("Unique constraint already exists for these" +
					" columns. Existing ConstraintName is " + uc.ConstraintName);

			//Allow only one primary key
			if (this.IsPrimaryKey) {
				uc = GetPrimaryKeyConstraint(collection);
				if (null != uc) uc._isPrimaryKey = false;
			}

			// if constraint is based on one column only
			// this column becomes unique
			if (_dataColumns.Length == 1) {
				_dataColumns[0].SetUnique();
			}
					
			//FIXME: ConstraintCollection calls AssertContraint() again rigth after calling
			//this method, so that it is executed twice. Need to investigate which
			// call to remove as that migth affect other parts of the classes.
			//AssertConstraint();
			if (IsConstraintViolated())
				throw new ArgumentException("These columns don't currently have unique values.");

			_belongsToCollection = true;
		}
Exemplo n.º 25
0
 //call once before removing a constraint to a collection
 //can throw an exception to prevent the removal
 internal abstract void RemoveFromConstraintCollectionCleanup(ConstraintCollection collection);
Exemplo n.º 26
0
		internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow){
			if (IsPrimaryKey) {
				if (shouldThrow)
					throw new ArgumentException("Cannot remove unique constraint since it's the primary key of a table.");

				return false;
			}
			
			if (Table.DataSet == null)
				return true;

			if (ChildConstraint != null) {
				if (!shouldThrow)
					return false;
				throw new ArgumentException (String.Format (
								"Cannot remove unique constraint '{0}'." +
								"Remove foreign key constraint '{1}' first.",
								ConstraintName,ChildConstraint.ConstraintName));
			}
			return true;
		}
Exemplo n.º 27
0
 internal override void CheckCanAddToCollection(ConstraintCollection constraints) {
     if (Table != constraints.Table)
         throw ExceptionBuilder.ConstraintAddFailed(constraints.Table);
     if (Table.Locale.LCID != RelatedTable.Locale.LCID || Table.CaseSensitive != RelatedTable.CaseSensitive)
         throw ExceptionBuilder.CaseLocaleMismatch();
 }
Exemplo n.º 28
0
		internal abstract bool CanRemoveFromCollection (ConstraintCollection col, bool shouldThrow);
Exemplo n.º 29
0
		//call once before removing a constraint to a collection
		//can throw an exception to prevent the removal
		internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
Exemplo n.º 30
0
		//call once before adding a constraint to a collection
		//will throw an exception to prevent the add if a rule is broken
		internal abstract void AddToConstraintCollectionSetup (ConstraintCollection collection);
Exemplo n.º 31
0
		internal override void AddToConstraintCollectionSetup(
				ConstraintCollection collection)
		{
			for (int i = 0; i < Columns.Length; i++)
				if (Columns[i].Table != collection.Table)
					throw new ArgumentException("These columns don't point to this table.");
			//run Ctor rules again
			_validateColumns(_dataColumns);
			
			//make sure a unique constraint doesn't already exists for these columns
			UniqueConstraint uc = UniqueConstraint.GetUniqueConstraintForColumnSet(collection, this.Columns);	
			if (null != uc) throw new ArgumentException("Unique constraint already exists for these" +
					" columns. Existing ConstraintName is " + uc.ConstraintName);

			//Allow only one primary key
			if (this.IsPrimaryKey) {
				uc = GetPrimaryKeyConstraint(collection);
				if (null != uc) uc._isPrimaryKey = false;
			}

			// if constraint is based on one column only
			// this column becomes unique
			if (_dataColumns.Length == 1) {
				_dataColumns[0].SetUnique();
			}
					
			if (IsConstraintViolated())
				throw new ArgumentException("These columns don't currently have unique values.");

			_belongsToCollection = true;
		}
Exemplo n.º 32
0
 internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
		internal override void RemoveFromConstraintCollectionCleanup( 
				ConstraintCollection collection)
		{
			_belongsToCollection = false;
			Index index = Index;
			Index = null;
		}
Exemplo n.º 34
0
 internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
Exemplo n.º 35
0
 //call once before adding a constraint to a collection
 //will throw an exception to prevent the add if a rule is broken
 internal abstract void AddToConstraintCollectionSetup(ConstraintCollection collection);
		//Checks to see if a related unique constraint exists
		//if it doesn't then a unique constraint is created.
		//if a unique constraint can't be created an exception will be thrown
		private void _ensureUniqueConstraintExists(ConstraintCollection collection,
				DataColumn [] parentColumns)
		{
			//not null
			if (null == parentColumns) throw new ArgumentNullException(
					"ParentColumns can't be null");

			UniqueConstraint uc = null;
			
			//see if unique constraint already exists
			//if not create unique constraint
			if(parentColumns[0] != null)
				uc = UniqueConstraint.GetUniqueConstraintForColumnSet(parentColumns[0].Table.Constraints, parentColumns);

			if (null == uc)	{
				uc = new UniqueConstraint(parentColumns, false); //could throw
				parentColumns [0].Table.Constraints.Add (uc);
			}

			//keep reference
			_parentUniqueConstraint = uc;
			_parentUniqueConstraint.ChildConstraint = this;
		}
Exemplo n.º 37
0
 internal abstract bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow);
Exemplo n.º 38
0
    private static Dictionary<string, ForeignKeyConstraint> GetForeignKeyConstraints(
      ConstraintCollection constraints )
    {
      Dictionary<string, ForeignKeyConstraint> foreignKeyConstraints =
        new Dictionary<string, ForeignKeyConstraint>();

      // Detect every ForeignKeyConstraints
      foreach( Constraint constraint in constraints )
      {
        ForeignKeyConstraint foreignKeyConstraint = constraint as ForeignKeyConstraint;

        // Not a ForeignKeyConstraint
        if( foreignKeyConstraint == null )
          continue;

        // We only support auto-detection when the ForeignKey is composed of 
        // a single column
        if( ( foreignKeyConstraint.Columns != null )
          && ( foreignKeyConstraint.Columns.Length == 1 ) )
        {
          foreignKeyConstraints.Add( foreignKeyConstraint.Columns[ 0 ].ColumnName, foreignKeyConstraint );
        }
      }

      return foreignKeyConstraints;
    }
Exemplo n.º 39
0
 internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
Exemplo n.º 40
0
		private ForeignKeyConstraint FindForeignKey (ConstraintCollection cl)
		{
			ForeignKeyConstraint fkc = null;
			foreach (Constraint o in cl) {
				if (! (o is ForeignKeyConstraint))
					continue;
				fkc = (ForeignKeyConstraint) o;
				/* Check ChildColumns & ParentColumns */
				if (CompareDataColumns (ChildColumns, fkc.Columns) &&
				    CompareDataColumns (ParentColumns, fkc.RelatedColumns))
					return fkc;
			}
			return null;
		}
 internal override void CheckCanAddToCollection(ConstraintCollection constraints) {
 }
		internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow){
			return true;
		}
Exemplo n.º 43
0
 /// <summary>
 /// 打印 ConstraintCollection .
 /// </summary>
 /// <param name="sb">输出缓冲区.</param>
 /// <param name="indent">缩进.</param>
 /// <param name="obj">对象.</param>
 public static void PrintConstraintCollection(StringBuilder sb, int indent, ConstraintCollection obj)
 {
     int indentnext = indent + 1;
     String indentstr = GetIndentStr(indent);
     sb.AppendLine(string.Format("{0}# <{1}>", indentstr, obj.GetType().FullName));
     sb.AppendLine(string.Format("{0}# Count:\t{1}", indentstr, obj.Count));
     int i = 0;
     foreach (Constraint p in obj) {
         sb.AppendLine(string.Format("{0}[{1}]:\t{2}", indentstr, i, p));
         PrintConstraint(sb, indentnext, p);
         ++i;
     }
 }
Exemplo n.º 44
0
		internal static UniqueConstraint GetPrimaryKeyConstraint(ConstraintCollection collection)
		{
			if (null == collection) throw new ArgumentNullException("Collection can't be null.");

			UniqueConstraint uc;
			IEnumerator enumer = collection.GetEnumerator();
			while (enumer.MoveNext())
			{
				uc = enumer.Current as UniqueConstraint;
				if (null == uc) continue;
				
				if (uc.IsPrimaryKey) return uc;	
			}

			//if we got here there was no pk
			return null;
			
		}
 internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow)
 {
     return(true);
 }
Exemplo n.º 46
0
		internal static void SetAsPrimaryKey(ConstraintCollection collection, UniqueConstraint newPrimaryKey)
		{
			//not null
			if (null == collection) throw new ArgumentNullException("ConstraintCollection can't be null.");
			
			//make sure newPrimaryKey belongs to the collection parm unless it is null
			if (  collection.IndexOf(newPrimaryKey) < 0 && (null != newPrimaryKey) ) 
				throw new ArgumentException("newPrimaryKey must belong to collection.");
			
			//Get existing pk
			UniqueConstraint uc = GetPrimaryKeyConstraint(collection);
			
			//clear existing
			if (null != uc) uc._isPrimaryKey = false;

			//set new key
			if (null != newPrimaryKey) newPrimaryKey._isPrimaryKey = true;
			
			
		}
Exemplo n.º 47
0
 internal override void CheckCanAddToCollection(ConstraintCollection constraints)
 {
 }
		internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow){
			if (Equals(col.Table.PrimaryKey)){
				if (shouldThrow)
					throw new ArgumentException("Cannot remove unique constraint since it's the primary key of a table.");

				return false;
			}

			if (Table.DataSet != null){
				foreach (DataTable table in Table.DataSet.Tables){
					foreach (Constraint constraint in table.Constraints){
						if (constraint is ForeignKeyConstraint)
							if (((ForeignKeyConstraint)constraint).RelatedTable == Table){
								if (shouldThrow)
									throw new ArgumentException(
										String.Format("Cannot remove unique constraint '{0}'. Remove foreign key constraint '{1}' first.",
										ConstraintName, constraint.ConstraintName)
										);
								return false;
							}
					}
				}
			}

			return true;
		}
Exemplo n.º 49
0
 internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) {
     return true;
 }
Exemplo n.º 50
0
		internal static UniqueConstraint GetUniqueConstraintForColumnSet(ConstraintCollection collection,
				DataColumn[] columns)
		{
			if (null == collection) throw new ArgumentNullException("Collection can't be null.");
			if (null == columns ) return null;
			
			foreach(Constraint constraint in collection) {
				if (constraint is UniqueConstraint) {
					UniqueConstraint uc = constraint as UniqueConstraint;
					if ( DataColumn.AreColumnSetsTheSame(uc.Columns, columns) ) {
						return uc;
					}
				}
			}
			return null;
		}
		internal override void AddToConstraintCollectionSetup(
				ConstraintCollection collection)
		{			
			if (collection.Table != Table)
				throw new InvalidConstraintException("This constraint cannot be added since ForeignKey doesn't belong to table " + RelatedTable.TableName + ".");

			//run Ctor rules again
			_validateColumns(_parentColumns, _childColumns);
			
			//we must have a unique constraint on the parent
			_ensureUniqueConstraintExists(collection, _parentColumns);
			
			if ( (Table.DataSet != null && Table.DataSet.EnforceConstraints)
			     || (Table.DataSet == null && Table.EnforceConstraints)) {
				if (IsConstraintViolated())
					throw new ArgumentException("This constraint cannot be enabled as not all values have corresponding parent values.");
			}
			//FIXME : if this fails and we created a unique constraint
			//we should probably roll it back
			// and remove index form Table			
		}
		internal override void RemoveFromConstraintCollectionCleanup( 
				ConstraintCollection collection)
		{
			Index = null;
		}
Exemplo n.º 53
0
 internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException)
 {
     return(true);
 }
		internal override void RemoveFromConstraintCollectionCleanup( 
				ConstraintCollection collection)
		{
			_parentUniqueConstraint.ChildConstraint = null;
			Index = null;
		}