/// <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); }
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 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 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 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 }
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; }
internal override void RemoveFromConstraintCollectionCleanup( ConstraintCollection collection) { _parentUniqueConstraint.ChildConstraint = null; #if NOT_PFX Index = null; #endif }
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(); } }
internal override void RemoveFromConstraintCollectionCleanup( ConstraintCollection collection) { if (Columns.Length == 1) { Columns [0].Unique = false; } _belongsToCollection = false; Index = null; }
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();)
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; }
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(); }
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); }
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); }
/// <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; }
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; }
internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
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; }
//call once before removing a constraint to a collection //can throw an exception to prevent the removal internal abstract void RemoveFromConstraintCollectionCleanup(ConstraintCollection collection);
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; }
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(); }
internal abstract bool CanRemoveFromCollection (ConstraintCollection col, bool shouldThrow);
//call once before removing a constraint to a collection //can throw an exception to prevent the removal internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
//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);
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; }
internal override void RemoveFromConstraintCollectionCleanup( ConstraintCollection collection) { _belongsToCollection = false; Index index = Index; Index = null; }
internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
//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; }
internal abstract bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow);
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; }
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; }
/// <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; } }
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); }
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 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; }
internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) { return true; }
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; }
internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) { return(true); }
internal override void RemoveFromConstraintCollectionCleanup( ConstraintCollection collection) { _parentUniqueConstraint.ChildConstraint = null; Index = null; }