| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065 |
- //
- // System.Data.DataTable.cs
- //
- // Author:
- // Franklin Wise <[email protected]>
- // Christopher Podurgiel ([email protected])
- // Daniel Morgan <[email protected]>
- // Rodrigo Moya <[email protected]>
- // Tim Coleman ([email protected])
- // Ville Palo <[email protected]>
- // Sureshkumar T <[email protected]>
- // Konstantin Triger <[email protected]>
- //
- // (C) Chris Podurgiel
- // (C) Ximian, Inc 2002
- // Copyright (C) Tim Coleman, 2002-2003
- // Copyright (C) Daniel Morgan, 2002-2003
- //
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Data.Common;
- using System.Collections;
- using System.ComponentModel;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Xml;
- using System.Xml.Schema;
- using System.Text.RegularExpressions;
- using Mono.Data.SqlExpressions;
- namespace System.Data {
- //[Designer]
- [ToolboxItem (false)]
- [DefaultEvent ("RowChanging")]
- [DefaultProperty ("TableName")]
- [DesignTimeVisible (false)]
- [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataTableEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
- [Serializable]
- public class DataTable : MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable
- {
- #region Fields
- internal DataSet dataSet;
-
- private bool _caseSensitive;
- private DataColumnCollection _columnCollection;
- private ConstraintCollection _constraintCollection;
- // never access it. Use DefaultView.
- private DataView _defaultView = null;
- private string _displayExpression;
- private PropertyCollection _extendedProperties;
- private bool _hasErrors;
- private CultureInfo _locale;
- private int _minimumCapacity;
- private string _nameSpace;
- private DataRelationCollection _childRelations;
- private DataRelationCollection _parentRelations;
- private string _prefix;
- private UniqueConstraint _primaryKeyConstraint;
- private DataRowCollection _rows;
- private ISite _site;
- private string _tableName;
- private bool _containsListCollection;
- private string _encodedTableName;
- internal bool _duringDataLoad;
- internal bool _nullConstraintViolationDuringDataLoad;
- private bool dataSetPrevEnforceConstraints;
- private bool dataTablePrevEnforceConstraints;
- private bool enforceConstraints = true;
- private DataRowBuilder _rowBuilder;
- private ArrayList _indexes;
- private RecordCache _recordCache;
- private int _defaultValuesRowIndex = -1;
- protected internal bool initInProgress;
- // If CaseSensitive property is changed once it does not anymore follow owner DataSet's
- // CaseSensitive property. So when you lost you virginity it's gone for ever
- private bool _virginCaseSensitive = true;
-
- private PropertyDescriptorCollection _propertyDescriptorsCache;
- static DataColumn[] _emptyColumnArray = new DataColumn[0];
- // Regex to parse the Sort string.
- static Regex SortRegex = new Regex ( @"^((\[(?<ColName>.+)\])|(?<ColName>\S+))([ ]+(?<Order>ASC|DESC))?$",
- RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
-
- DataColumn[] _latestPrimaryKeyCols;
- #endregion //Fields
-
- /// <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);
- }
- /// <summary>
- /// Intitalizes a new instance of the DataTable class with the specified table name.
- /// </summary>
- public DataTable (string tableName) : this ()
- {
- _tableName = tableName;
- }
- /// <summary>
- /// Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.
- /// </summary>
- [MonoTODO]
- protected DataTable (SerializationInfo info, StreamingContext context)
- : this ()
- {
- string schema = info.GetString ("XmlSchema");
- string data = info.GetString ("XmlDiffGram");
-
- DataSet ds = new DataSet ();
- ds.ReadXmlSchema (new StringReader (schema));
- ds.Tables [0].CopyProperties (this);
- ds = new DataSet ();
- ds.Tables.Add (this);
- ds.ReadXml (new StringReader (data), XmlReadMode.DiffGram);
- ds.Tables.Remove (this);
- /* keeping for a while. With the change above, we shouldn't have to consider
- * DataTable mode in schema inference/read.
- XmlSchemaMapper mapper = new XmlSchemaMapper (this);
- XmlTextReader xtr = new XmlTextReader(new StringReader (schema));
- mapper.Read (xtr);
-
- XmlDiffLoader loader = new XmlDiffLoader (this);
- xtr = new XmlTextReader(new StringReader (data));
- loader.Load (xtr);
- */
- }
- #if NET_2_0
- public DataTable (string tableName, string tbNamespace)
- : this (tableName)
- {
- _nameSpace = tbNamespace;
- }
- #endif
- /// <summary>
- /// Indicates whether string comparisons within the table are case-sensitive.
- /// </summary>
- #if !NET_2_0
- [DataSysDescription ("Indicates whether comparing strings within the table is case sensitive.")]
- #endif
- public bool CaseSensitive {
- get {
- if (_virginCaseSensitive && dataSet != null)
- return dataSet.CaseSensitive;
- else
- return _caseSensitive;
- }
- set {
- if (_childRelations.Count > 0 || _parentRelations.Count > 0) {
- throw new ArgumentException ("Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.");
- }
- _virginCaseSensitive = false;
- _caseSensitive = value;
- ResetCaseSensitiveIndexes();
- }
- }
-
- internal ArrayList Indexes{
- get { return _indexes; }
- }
- internal void ChangedDataColumn (DataRow dr, DataColumn dc, object pv)
- {
- DataColumnChangeEventArgs e = new DataColumnChangeEventArgs (dr, dc, pv);
- OnColumnChanged(e);
- }
- internal void ChangingDataColumn (DataRow dr, DataColumn dc, object pv)
- {
- DataColumnChangeEventArgs e = new DataColumnChangeEventArgs (dr, dc, pv);
- OnColumnChanging (e);
- }
- internal void DeletedDataRow (DataRow dr, DataRowAction action)
- {
- DataRowChangeEventArgs e = new DataRowChangeEventArgs (dr, action);
- OnRowDeleted (e);
- }
- internal void DeletingDataRow (DataRow dr, DataRowAction action)
- {
- DataRowChangeEventArgs e = new DataRowChangeEventArgs (dr, action);
- OnRowDeleting(e);
- }
- internal void ChangedDataRow (DataRow dr, DataRowAction action)
- {
- DataRowChangeEventArgs e = new DataRowChangeEventArgs (dr, action);
- OnRowChanged (e);
- }
- internal void ChangingDataRow (DataRow dr, DataRowAction action)
- {
- DataRowChangeEventArgs e = new DataRowChangeEventArgs (dr, action);
- OnRowChanging (e);
- }
- /// <summary>
- /// Gets the collection of child relations for this DataTable.
- /// </summary>
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("Returns the child relations for this table.")]
- #endif
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public DataRelationCollection ChildRelations {
- get {
- return _childRelations;
- }
- }
- /// <summary>
- /// Gets the collection of columns that belong to this table.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("The collection that holds the columns for this table.")]
- #endif
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public DataColumnCollection Columns {
- get { return _columnCollection; }
- }
- /// <summary>
- /// Gets the collection of constraints maintained by this table.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("The collection that holds the constraints for this table.")]
- #endif
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public ConstraintCollection Constraints {
- get { return _constraintCollection; }
- }
- /// <summary>
- /// Gets the DataSet that this table belongs to.
- /// </summary>
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("Indicates the DataSet to which this table belongs.")]
- #endif
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public DataSet DataSet {
- get { return dataSet; }
- }
-
- /// <summary>
- /// Gets a customized view of the table which may
- /// include a filtered view, or a cursor position.
- /// </summary>
- [MonoTODO]
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("This is the default DataView for the table.")]
- #endif
- public DataView DefaultView {
- get {
- if (_defaultView == null) {
- lock(this){
- if (_defaultView == null){
- if (dataSet != null)
- _defaultView = dataSet.DefaultViewManager.CreateDataView(this);
- else
- _defaultView = new DataView(this);
- }
- }
- }
- return _defaultView;
- }
- }
-
- /// <summary>
- /// Gets or sets the expression that will return
- /// a value used to represent this table in the user interface.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("The expression used to compute the data-bound value of this row.")]
- #endif
- [DefaultValue ("")]
- public string DisplayExpression {
- get { return _displayExpression == null ? "" : _displayExpression; }
- set { _displayExpression = value; }
- }
- /// <summary>
- /// Gets the collection of customized user information.
- /// </summary>
- [Browsable (false)]
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("The collection that holds custom user information.")]
- #endif
- public PropertyCollection ExtendedProperties {
- get { return _extendedProperties; }
- }
- /// <summary>
- /// Gets a value indicating whether there are errors in
- /// any of the_rows in any of the tables of the DataSet to
- /// which the table belongs.
- /// </summary>
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("Returns whether the table has errors.")]
- #endif
- public bool HasErrors {
- get {
- // we can not use the _hasError flag because we do not know when to turn it off!
- for (int i = 0; i < _rows.Count; i++)
- {
- if (_rows[i].HasErrors)
- return true;
- }
- return false;
- }
- }
- /// <summary>
- /// Gets or sets the locale information used to
- /// compare strings within the table.
- /// </summary>
- #if !NET_2_0
- [DataSysDescription ("Indicates a locale under which to compare strings within the table.")]
- #endif
- public CultureInfo Locale {
- get {
- // if the locale is null, we check for the DataSet locale
- // and if the DataSet is null we return the current culture.
- // this way if DataSet locale is changed, only if there is no locale for
- // the DataTable it influece the Locale get;
- if (_locale != null)
- return _locale;
- if (DataSet != null)
- return DataSet.Locale;
- return CultureInfo.CurrentCulture;
- }
- set {
- if (_childRelations.Count > 0 || _parentRelations.Count > 0) {
- throw new ArgumentException ("Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.");
- }
- if (_locale == null || !_locale.Equals(value))
- _locale = value;
- }
- }
- /// <summary>
- /// Gets or sets the initial starting size for this table.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Indicates an initial starting size for this table.")]
- #endif
- [DefaultValue (50)]
- public int MinimumCapacity {
- get { return _minimumCapacity; }
- set { _minimumCapacity = value; }
- }
- /// <summary>
- /// Gets or sets the namespace for the XML represenation
- /// of the data stored in the DataTable.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Indicates the XML uri namespace for the elements contained in this table.")]
- #endif
- public string Namespace {
- get
- {
- if (_nameSpace != null)
- {
- return _nameSpace;
- }
- if (DataSet != null)
- {
- return DataSet.Namespace;
- }
- return String.Empty;
- }
- set { _nameSpace = value; }
- }
- /// <summary>
- /// Gets the collection of parent relations for
- /// this DataTable.
- /// </summary>
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("Returns the parent relations for this table.")]
- #endif
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public DataRelationCollection ParentRelations {
- get {
- return _parentRelations;
- }
- }
- /// <summary>
- /// Gets or sets the namespace for the XML represenation
- /// of the data stored in the DataTable.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Indicates the Prefix of the namespace used for this table in XML representation.")]
- #endif
- [DefaultValue ("")]
- public string Prefix {
- get { return _prefix == null ? "" : _prefix; }
- set {
- // Prefix cannot contain any special characters other than '_' and ':'
- for (int i = 0; i < value.Length; i++) {
- if (!(Char.IsLetterOrDigit (value [i])) && (value [i] != '_') && (value [i] != ':'))
- throw new DataException ("Prefix '" + value + "' is not valid, because it contains special characters.");
- }
- _prefix = value;
- }
- }
- /// <summary>
- /// Gets or sets an array of columns that function as
- /// primary keys for the data table.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Indicates the column(s) that represent the primary key for this table.")]
- #endif
- [EditorAttribute ("Microsoft.VSDesigner.Data.Design.PrimaryKeyEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
- [TypeConverterAttribute ("System.Data.PrimaryKeyTypeConverter, " + Consts.AssemblySystem_Data)]
- public DataColumn[] PrimaryKey {
- get {
- if (_primaryKeyConstraint == null) {
- return new DataColumn[] {};
- }
- return _primaryKeyConstraint.Columns;
- }
- set {
- if (value == null || value.Length == 0) {
- if (_primaryKeyConstraint != null) {
- Constraints.Remove(_primaryKeyConstraint);
- _primaryKeyConstraint = null;
- }
- return;
- }
-
- if (InitInProgress) {
- _latestPrimaryKeyCols = value;
- return;
- }
- // first check if value is the same as current PK.
- if (_primaryKeyConstraint!= null && DataColumn.AreColumnSetsTheSame(value, _primaryKeyConstraint.Columns))
- return;
- //Does constraint exist for these columns
- UniqueConstraint uc = UniqueConstraint.GetUniqueConstraintForColumnSet(this.Constraints, (DataColumn[]) value);
- //if constraint doesn't exist for columns
- //create new unique primary key constraint
- if (null == uc) {
- foreach (DataColumn Col in (DataColumn[]) value) {
- if (Col.Table == null)
- break;
- if (Columns.IndexOf (Col) < 0)
- throw new ArgumentException ("PrimaryKey columns do not belong to this table.");
- }
- // create constraint with primary key indication set to false
- // to avoid recursion
- uc = new UniqueConstraint( (DataColumn[]) value, false);
- Constraints.Add (uc);
- }
- //Remove the existing primary key
- if (_primaryKeyConstraint != null) {
- Constraints.Remove(_primaryKeyConstraint);
- _primaryKeyConstraint = null;
- }
-
- //set the constraint as the new primary key
- UniqueConstraint.SetAsPrimaryKey(this.Constraints, uc);
- _primaryKeyConstraint = uc;
- for (int j=0; j < uc.Columns.Length; ++j)
- uc.Columns[j].AllowDBNull = false;
- }
- }
- internal UniqueConstraint PrimaryKeyConstraint {
- get{
- return _primaryKeyConstraint;
- }
- }
- /// <summary>
- /// Gets the collection of_rows that belong to this table.
- /// </summary>
- [Browsable (false)]
- #if !NET_2_0
- [DataSysDescription ("Indicates the collection that holds the rows of data for this table.")]
- #endif
- public DataRowCollection Rows {
- get { return _rows; }
- }
- /// <summary>
- /// Gets or sets an System.ComponentModel.ISite
- /// for the DataTable.
- /// </summary>
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public override ISite Site {
- get { return _site; }
- set { _site = value; }
- }
- /// <summary>
- /// Gets or sets the name of the the DataTable.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Indicates the name used to look up this table in the Tables collection of a DataSet.")]
- #endif
- [DefaultValue ("")]
- [RefreshProperties (RefreshProperties.All)]
- public string TableName {
- get { return _tableName == null ? "" : _tableName; }
- set { _tableName = value; }
- }
-
- bool IListSource.ContainsListCollection {
- get {
- // the collection is a DataView
- return false;
- }
- }
-
- internal RecordCache RecordCache {
- get {
- return _recordCache;
- }
- }
-
- private DataRowBuilder RowBuilder
- {
- get
- {
- // initiate only one row builder.
- if (_rowBuilder == null)
- _rowBuilder = new DataRowBuilder (this, -1, 0);
- else
- // new row get id -1.
- _rowBuilder._rowId = -1;
- return _rowBuilder;
- }
- }
-
- internal bool EnforceConstraints {
- get { return enforceConstraints; }
- set {
- if (value != enforceConstraints) {
- if (value) {
- // reset indexes since they may be outdated
- ResetIndexes();
- bool violatesConstraints = false;
- //FIXME: use index for AllowDBNull
- for (int i = 0; i < Columns.Count; i++) {
- DataColumn column = Columns[i];
- if (!column.AllowDBNull) {
- for (int j = 0; j < Rows.Count; j++){
- if (Rows[j].IsNull(column)) {
- violatesConstraints = true;
- Rows[j].RowError = String.Format("Column '{0}' does not allow DBNull.Value.", column.ColumnName);
- }
- }
- }
- }
- if (violatesConstraints)
- Constraint.ThrowConstraintException();
- // assert all constraints
- foreach (Constraint constraint in Constraints) {
- constraint.AssertConstraint();
- }
- }
- enforceConstraints = value;
- }
- }
- }
- internal bool RowsExist(DataColumn[] columns, DataColumn[] relatedColumns,DataRow row)
- {
- int curIndex = row.IndexFromVersion(DataRowVersion.Default);
- int tmpRecord = RecordCache.NewRecord();
- try {
- for (int i = 0; i < relatedColumns.Length; i++) {
- // according to MSDN: the DataType value for both columns must be identical.
- columns[i].DataContainer.CopyValue(relatedColumns[i].DataContainer, curIndex, tmpRecord);
- }
- return RowsExist(columns, tmpRecord);
- }
- finally {
- RecordCache.DisposeRecord(tmpRecord);
- }
- }
- bool RowsExist(DataColumn[] columns, int index)
- {
- bool rowsExist = false;
- Index indx = this.FindIndex(columns);
- if (indx != null) { // lookup for a row in index
- rowsExist = (indx.Find(index) != -1);
- }
- else {
- // we have to perform full-table scan
- // check that there is a parent for this row.
- foreach (DataRow thisRow in this.Rows) {
- if (thisRow.RowState != DataRowState.Deleted) {
- bool match = true;
- // check if the values in the columns are equal
- int thisIndex = thisRow.IndexFromVersion(DataRowVersion.Current);
- foreach (DataColumn column in columns) {
- if (column.DataContainer.CompareValues(thisIndex, index) != 0) {
- match = false;
- break;
- }
- }
- if (match) {// there is a row with columns values equals to those supplied.
- rowsExist = true;
- break;
- }
- }
- }
- }
- return rowsExist;
- }
- /// <summary>
- /// Commits all the changes made to this table since the
- /// last time AcceptChanges was called.
- /// </summary>
- public void AcceptChanges ()
- {
- //FIXME: Do we need to validate anything here or
- //try to catch any errors to deal with them?
-
- // we do not use foreach because if one of the rows is in Delete state
- // it will be romeved from Rows and we get an exception.
- DataRow myRow;
- for (int i = 0; i < Rows.Count; )
- {
- myRow = Rows[i];
- myRow.AcceptChanges();
- // if the row state is Detached it meens that it was removed from row list (Rows)
- // so we should not increase 'i'.
- if (myRow.RowState != DataRowState.Detached)
- i++;
- }
- }
- /// <summary>
- /// Begins the initialization of a DataTable that is used
- /// on a form or used by another component. The initialization
- /// occurs at runtime.
- /// </summary>
- public virtual void BeginInit ()
- {
- InitInProgress = true;
- }
- /// <summary>
- /// Turns off notifications, index maintenance, and
- /// constraints while loading data.
- /// </summary>
- [MonoTODO]
- public void BeginLoadData ()
- {
- if (!this._duringDataLoad)
- {
- //duringDataLoad is important to EndLoadData and
- //for not throwing unexpected exceptions.
- this._duringDataLoad = true;
- this._nullConstraintViolationDuringDataLoad = false;
-
- if (this.dataSet != null)
- {
- //Saving old Enforce constraints state for later
- //use in the EndLoadData.
- this.dataSetPrevEnforceConstraints = this.dataSet.EnforceConstraints;
- this.dataSet.EnforceConstraints = false;
- }
- else {
- //if table does not belong to any data set use EnforceConstraints of the table
- this.EnforceConstraints = false;
- }
- }
- return;
- }
- /// <summary>
- /// Clears the DataTable of all data.
- /// </summary>
- public void Clear () {
- // Foriegn key constraints are checked in _rows.Clear method
- _rows.Clear ();
- foreach(Index index in Indexes)
- index.Reset();
- #if NET_2_0
- OnTableCleared (new DataTableClearEventArgs (this));
- #endif // NET_2_0
- }
- /// <summary>
- /// Clones the structure of the DataTable, including
- /// all DataTable schemas and constraints.
- /// </summary>
- public virtual DataTable Clone ()
- {
- // Use Activator so we can use non-public constructors.
- DataTable Copy = (DataTable) Activator.CreateInstance(GetType(), true);
- CopyProperties (Copy);
- return Copy;
- }
- /// <summary>
- /// Computes the given expression on the current_rows that
- /// pass the filter criteria.
- /// </summary>
- [MonoTODO]
- public object Compute (string expression, string filter)
- {
- // expression is an aggregate function
- // filter is an expression used to limit rows
- DataRow[] rows = Select(filter);
-
- if (rows == null || rows.Length == 0)
- return DBNull.Value;
-
- Parser parser = new Parser (rows);
- IExpression expr = parser.Compile (expression);
- object obj = expr.Eval (rows[0]);
-
- return obj;
- }
- /// <summary>
- /// Copies both the structure and data for this DataTable.
- /// </summary>
- public DataTable Copy ()
- {
- DataTable copy = Clone();
- copy._duringDataLoad = true;
- foreach (DataRow row in Rows) {
- DataRow newRow = copy.NewNotInitializedRow();
- copy.Rows.AddInternal(newRow);
- CopyRow(row,newRow);
- }
- copy._duringDataLoad = false;
-
- // rebuild copy indexes after loading all rows
- copy.ResetIndexes();
- return copy;
- }
- internal void CopyRow(DataRow fromRow,DataRow toRow)
- {
- if (fromRow.HasErrors) {
- fromRow.CopyErrors(toRow);
- }
- if (fromRow.HasVersion(DataRowVersion.Original)) {
- toRow.Original = toRow.Table.RecordCache.CopyRecord(this,fromRow.Original,-1);
- }
- if (fromRow.HasVersion(DataRowVersion.Current)) {
- if (fromRow.Original != fromRow.Current) {
- toRow.Current = toRow.Table.RecordCache.CopyRecord(this,fromRow.Current,-1);
- }
- else {
- toRow.Current = toRow.Original;
- }
- }
- }
- private void CopyProperties (DataTable Copy)
- {
- Copy.CaseSensitive = CaseSensitive;
- Copy._virginCaseSensitive = _virginCaseSensitive;
- // Copy.ChildRelations
- // Copy.Constraints
- // Copy.Container
- // Copy.DefaultView
- // Copy.DesignMode
- Copy.DisplayExpression = DisplayExpression;
- if(ExtendedProperties.Count > 0) {
- // Cannot copy extended properties directly as the property does not have a set accessor
- Array tgtArray = Array.CreateInstance( typeof (object), ExtendedProperties.Count);
- ExtendedProperties.Keys.CopyTo (tgtArray, 0);
- for (int i=0; i < ExtendedProperties.Count; i++)
- Copy.ExtendedProperties.Add (tgtArray.GetValue (i), ExtendedProperties[tgtArray.GetValue (i)]);
- }
- Copy.Locale = Locale;
- Copy.MinimumCapacity = MinimumCapacity;
- Copy.Namespace = Namespace;
- // Copy.ParentRelations
- Copy.Prefix = Prefix;
- Copy.Site = Site;
- Copy.TableName = TableName;
- bool isEmpty = Copy.Columns.Count == 0;
- // Copy columns
- foreach (DataColumn column in Columns) {
- // When cloning a table, the columns may be added in the default constructor.
- if (isEmpty || !Copy.Columns.Contains(column.ColumnName)) {
- Copy.Columns.Add (column.Clone());
- }
- }
- CopyConstraints(Copy);
- // add primary key to the copy
- if (PrimaryKey.Length > 0) {
- DataColumn[] pColumns = new DataColumn[PrimaryKey.Length];
- for (int i = 0; i < pColumns.Length; i++)
- pColumns[i] = Copy.Columns[PrimaryKey[i].ColumnName];
- Copy.PrimaryKey = pColumns;
- }
- }
- private void CopyConstraints(DataTable copy)
- {
- UniqueConstraint origUc;
- UniqueConstraint copyUc;
- for (int i = 0; i < this.Constraints.Count; i++)
- {
- if (this.Constraints[i] is UniqueConstraint)
- {
- // typed ds can already contain the constraints
- if (copy.Constraints.Contains (this.Constraints [i].ConstraintName))
- continue;
-
- origUc = (UniqueConstraint)this.Constraints[i];
- DataColumn[] columns = new DataColumn[origUc.Columns.Length];
- for (int j = 0; j < columns.Length; j++)
- columns[j] = copy.Columns[origUc.Columns[j].ColumnName];
-
- copyUc = new UniqueConstraint(origUc.ConstraintName, columns, origUc.IsPrimaryKey);
- copy.Constraints.Add(copyUc);
- }
- }
- }
- /// <summary>
- /// Ends the initialization of a DataTable that is used
- /// on a form or used by another component. The
- /// initialization occurs at runtime.
- /// </summary>
- [MonoTODO]
- public virtual void EndInit ()
- {
- InitInProgress = false;
- FinishInit ();
- }
- internal bool InitInProgress {
- get { return initInProgress; }
- set { initInProgress = value; }
- }
- internal void FinishInit ()
- {
- UniqueConstraint oldPK = _primaryKeyConstraint;
-
- // Columns shud be added 'before' the constraints
- Columns.PostAddRange ();
- // Add the constraints
- _constraintCollection.PostAddRange ();
-
- // ms.net behavior : If a PrimaryKey (UniqueConstraint) is added thru AddRange,
- // then it takes precedence over an direct assignment of PrimaryKey
- if (_primaryKeyConstraint == oldPK)
- PrimaryKey = _latestPrimaryKeyCols;
- }
- /// <summary>
- /// Turns on notifications, index maintenance, and
- /// constraints after loading data.
- /// </summary>
- public void EndLoadData()
- {
- if (this._duringDataLoad) {
- if(this._nullConstraintViolationDuringDataLoad) {
- this._nullConstraintViolationDuringDataLoad = false;
- throw new ConstraintException ("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
- }
-
- if (this.dataSet !=null) {
- //Getting back to previous EnforceConstraint state
- this.dataSet.InternalEnforceConstraints(this.dataSetPrevEnforceConstraints,true);
- }
- else {
- //Getting back to the table's previous EnforceConstraint state
- this.EnforceConstraints = true;
- }
- //Returning from loading mode, raising exceptions as usual
- this._duringDataLoad = false;
- }
- }
- /// <summary>
- /// Gets a copy of the DataTable that contains all
- /// changes made to it since it was loaded or
- /// AcceptChanges was last called.
- /// </summary>
- public DataTable GetChanges()
- {
- return GetChanges(DataRowState.Added | DataRowState.Deleted | DataRowState.Modified);
- }
- /// <summary>
- /// Gets a copy of the DataTable containing all
- /// changes made to it since it was last loaded, or
- /// since AcceptChanges was called, filtered by DataRowState.
- /// </summary>
- public DataTable GetChanges(DataRowState rowStates)
- {
- DataTable copyTable = null;
- IEnumerator rowEnumerator = Rows.GetEnumerator();
- while (rowEnumerator.MoveNext()) {
- DataRow row = (DataRow)rowEnumerator.Current;
- // The spec says relationship constraints may cause Unchanged parent rows to be included but
- // MS .NET 1.1 does not include Unchanged rows even if their child rows are changed.
- if (row.IsRowChanged(rowStates)) {
- if (copyTable == null)
- copyTable = Clone();
- DataRow newRow = copyTable.NewNotInitializedRow();
- row.CopyValuesToRow(newRow);
- copyTable.Rows.AddInternal (newRow);
- }
- }
-
- return copyTable;
- }
- #if NET_2_0
- [MonoTODO]
- public DataTableReader GetDataReader ()
- {
- throw new NotImplementedException ();
- }
- #endif
- /// <summary>
- /// Gets an array of DataRow objects that contain errors.
- /// </summary>
- public DataRow[] GetErrors ()
- {
- ArrayList errors = new ArrayList();
- for (int i = 0; i < _rows.Count; i++)
- {
- if (_rows[i].HasErrors)
- errors.Add(_rows[i]);
- }
-
- DataRow[] ret = NewRowArray(errors.Count);
- errors.CopyTo(ret, 0);
- return ret;
- }
-
- /// <summary>
- /// This member is only meant to support Mono's infrastructure
- /// </summary>
- protected virtual DataTable CreateInstance ()
- {
- return Activator.CreateInstance (this.GetType (), true) as DataTable;
- }
- /// <summary>
- /// This member is only meant to support Mono's infrastructure
- /// </summary>
- protected virtual Type GetRowType ()
- {
- return typeof (DataRow);
- }
- /// <summary>
- /// This member is only meant to support Mono's infrastructure
- ///
- /// Used for Data Binding between System.Web.UI. controls
- /// like a DataGrid
- /// or
- /// System.Windows.Forms controls like a DataGrid
- /// </summary>
- IList IListSource.GetList ()
- {
- IList list = (IList) DefaultView;
- return list;
- }
-
- /// <summary>
- /// Copies a DataRow into a DataTable, preserving any
- /// property settings, as well as original and current values.
- /// </summary>
- public void ImportRow (DataRow row)
- {
- if (row.RowState == DataRowState.Detached)
- return;
- DataRow newRow = NewNotInitializedRow();
- int original = -1;
- if (row.HasVersion(DataRowVersion.Original)) {
- original = row.IndexFromVersion(DataRowVersion.Original);
- newRow.Original = RecordCache.NewRecord();
- RecordCache.CopyRecord(row.Table,original,newRow.Original);
- }
- if (row.HasVersion(DataRowVersion.Current)) {
- int current = row.IndexFromVersion(DataRowVersion.Current);
- if (current == original)
- newRow.Current = newRow.Original;
- else {
- newRow.Current = RecordCache.NewRecord();
- RecordCache.CopyRecord(row.Table,current,newRow.Current);
- }
- }
-
- //Import the row only if RowState is not detached
- //Validation for Deleted Rows happens during Accept/RejectChanges
- if (row.RowState != DataRowState.Deleted)
- newRow.Validate();
- else
- AddRowToIndexes (newRow);
- Rows.AddInternal(newRow);
- if (row.HasErrors) {
- row.CopyErrors(newRow);
- }
- }
- internal int DefaultValuesRowIndex
- {
- get {
- return _defaultValuesRowIndex;
- }
- }
- /// <summary>
- /// This member is only meant to support Mono's infrastructure
- /// </summary>
- void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
- {
- DataSet dset;
- if (dataSet != null)
- dset = dataSet;
- else {
- dset = new DataSet ("tmpDataSet");
- dset.Tables.Add (this);
- }
-
- StringWriter sw = new StringWriter ();
- XmlTextWriter tw = new XmlTextWriter (sw);
- tw.Formatting = Formatting.Indented;
- dset.WriteIndividualTableContent (tw, this, XmlWriteMode.DiffGram);
- tw.Close ();
-
- StringWriter sw2 = new StringWriter ();
- DataTableCollection tables = new DataTableCollection (dset);
- tables.Add (this);
- XmlSchemaWriter.WriteXmlSchema (dset, new XmlTextWriter (sw2), tables, null);
- sw2.Close ();
-
- info.AddValue ("XmlSchema", sw2.ToString(), typeof(string));
- info.AddValue ("XmlDiffGram", sw.ToString(), typeof(string));
- }
- #if NET_2_0
- /// <summary>
- /// Loads the table with the values from the reader
- /// </summary>
- public void Load (IDataReader reader)
- {
- Load (reader, LoadOption.PreserveChanges);
- }
- /// <summary>
- /// Loads the table with the values from the reader and the pattern
- /// of the changes to the existing rows or the new rows are based on
- /// the LoadOption passed.
- /// </summary>
- public void Load (IDataReader reader, LoadOption loadOption)
- {
- bool prevEnforceConstr = this.EnforceConstraints;
- try {
- this.EnforceConstraints = false;
- int [] mapping = DbDataAdapter.BuildSchema (reader, this, SchemaType.Mapped,
- MissingSchemaAction.AddWithKey,
- MissingMappingAction.Passthrough,
- new DataTableMappingCollection ());
- DbDataAdapter.FillFromReader (this,
- reader,
- 0, // start from
- 0, // all records
- mapping,
- loadOption);
- } finally {
- this.EnforceConstraints = prevEnforceConstr;
- }
- }
-
- #endif
- /// <summary>
- /// Finds and updates a specific row. If no matching row
- /// is found, a new row is created using the given values.
- /// </summary>
- public DataRow LoadDataRow (object[] values, bool fAcceptChanges)
- {
- DataRow row = null;
- if (PrimaryKey.Length == 0) {
- row = Rows.Add (values);
- if (fAcceptChanges)
- row.AcceptChanges ();
- }
- else {
- int newRecord = CreateRecord(values);
- int existingRecord = _primaryKeyConstraint.Index.Find(newRecord);
- if (existingRecord < 0) {
- row = NewRowFromBuilder (RowBuilder);
- row.Proposed = newRecord;
- Rows.AddInternal(row);
- }
- else {
- row = RecordCache[existingRecord];
- row.BeginEdit();
- row.ImportRecord(newRecord);
- row.EndEdit();
-
- }
-
- if (fAcceptChanges)
- row.AcceptChanges ();
- }
-
- return row;
- }
- internal DataRow LoadDataRow(IDataRecord record, int[] mapping, int length, bool fAcceptChanges)
- {
- DataRow row = null;
- int tmpRecord = this.RecordCache.NewRecord();
- try {
- RecordCache.ReadIDataRecord(tmpRecord,record,mapping,length);
- if (PrimaryKey.Length != 0) {
- bool hasPrimaryValues = true;
- foreach(DataColumn col in PrimaryKey) {
- if(!(col.Ordinal < mapping.Length)) {
- hasPrimaryValues = false;
- break;
- }
- }
-
- if (hasPrimaryValues) {
- // find the row in the table.
- row = Rows.Find(tmpRecord);
- }
- }
-
- bool shouldUpdateIndex = false;
- if (row == null) {
- row = NewNotInitializedRow();
- row.ImportRecord(tmpRecord);
- Rows.AddInternal (row);
- shouldUpdateIndex = true;
- }
- else {
- // Proposed = tmpRecord
- row.ImportRecord(tmpRecord);
- }
-
- if (fAcceptChanges) {
- row.AcceptChanges();
- }
-
- if (shouldUpdateIndex && !fAcceptChanges) {
- AddRowToIndexes(row);
- }
- }
- catch(Exception e) {
- this.RecordCache.DisposeRecord(tmpRecord);
- throw e;
- }
- return row;
- }
- #if NET_2_0
- /// <summary>
- /// Loads the given values into an existing row if matches or creates
- /// the new row popluated with the values.
- /// </summary>
- /// <remarks>
- /// This method searches for the values using primary keys and it first
- /// searches using the original values of the rows and if not found, it
- /// searches using the current version of the row.
- /// </remarks>
- public DataRow LoadDataRow (object [] values, LoadOption loadOption)
- {
- DataRow row = null;
-
- // Find Data DataRow
- if (this.PrimaryKey.Length > 0) {
- object [] keys = new object [PrimaryKey.Length];
- for (int i=0; i < PrimaryKey.Length; i++)
- keys [i] = values [PrimaryKey [i].Ordinal];
- row = Rows.Find(keys, DataViewRowState.OriginalRows);
- if (row == null)
- row = Rows.Find (keys);
- }
-
- // If not found, add new row
- if (row == null
- || (row.RowState == DataRowState.Deleted
- && loadOption == LoadOption.Upsert)) {
- row = NewNotInitializedRow ();
- row.ImportRecord (CreateRecord(values));
- row.Validate(); // this adds to index ;-)
-
- if (loadOption == LoadOption.OverwriteChanges ||
- loadOption == LoadOption.PreserveChanges) {
- Rows.AddInternal(row);
- ChangingDataRow (row, DataRowAction.ChangeCurrentAndOriginal);
- row.Original = row.Current;
- ChangedDataRow (row, DataRowAction.ChangeCurrentAndOriginal);
- } else
- Rows.AddInternal(row);
- return row;
- }
- if (row.RowState == DataRowState.Deleted
- && loadOption == LoadOption.OverwriteChanges)
- row.RejectChanges ();
- row.Load (values, loadOption);
- return row;
- }
- [MonoTODO]
- public void Merge (DataTable table)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public void Merge (DataTable table, bool preserveChanges)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public void Merge (DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction)
- {
- throw new NotImplementedException ();
- }
- #endif
- /// <summary>
- /// Creates a new DataRow with the same schema as the table.
- /// </summary>
- public DataRow NewRow ()
- {
- EnsureDefaultValueRowIndex();
- DataRow newRow = NewRowFromBuilder (RowBuilder);
- newRow.Proposed = CreateRecord(null);
- return newRow;
- }
- internal int CreateRecord(object[] values) {
- int valCount = values != null ? values.Length : 0;
- if (valCount > Columns.Count)
- throw new ArgumentException("Input array is longer than the number of columns in this table.");
- int index = RecordCache.NewRecord();
- try {
-
- for (int i = 0; i < valCount; i++) {
- object value = values[i];
- if (value == null)
- Columns[i].SetDefaultValue(index);
- else
- Columns[i][index] = values[i];
- }
- for(int i = valCount; i < Columns.Count; i++) {
- Columns[i].SetDefaultValue(index);
- }
- return index;
- }
- catch {
- RecordCache.DisposeRecord(index);
- throw;
- }
- }
- private void EnsureDefaultValueRowIndex()
- {
- // initialize default values row for the first time
- if ( _defaultValuesRowIndex == -1 ) {
- _defaultValuesRowIndex = RecordCache.NewRecord();
- foreach(DataColumn column in Columns) {
- column.DataContainer[_defaultValuesRowIndex] = column.DefaultValue;
- }
- }
- }
- #if NET_2_0
- internal int CompareRecords(int x, int y) {
- for (int col = 0; col < Columns.Count; col++) {
- int res = Columns[col].DataContainer.CompareValues (x, y);
- if (res != 0)
- return res;
- }
- return 0;
- }
- #endif
- /// <summary>
- /// This member supports the .NET Framework infrastructure
- /// and is not intended to be used directly from your code.
- /// </summary>
- protected internal DataRow[] NewRowArray (int size)
- {
- return (DataRow[]) Array.CreateInstance (GetRowType (), size);
- }
- /// <summary>
- /// Creates a new row from an existing row.
- /// </summary>
- protected virtual DataRow NewRowFromBuilder (DataRowBuilder builder)
- {
- return new DataRow (builder);
- }
-
- internal DataRow NewNotInitializedRow()
- {
- EnsureDefaultValueRowIndex();
- return NewRowFromBuilder (RowBuilder);
- }
- #if NET_2_0
- [MonoTODO]
- XmlReadMode ReadXml (Stream stream)
- {
- throw new NotImplementedException ();
- }
- public void ReadXmlSchema (Stream stream)
- {
- ReadXmlSchema (new XmlTextReader (stream));
- }
- public void ReadXmlSchema (TextReader reader)
- {
- ReadXmlSchema (new XmlTextReader (reader));
- }
- public void ReadXmlSchema (string fileName)
- {
- XmlTextReader reader = null;
- try {
- reader = new XmlTextReader (fileName);
- ReadXmlSchema (reader);
- } finally {
- if (reader != null)
- reader.Close ();
- }
- }
- public void ReadXmlSchema (XmlReader reader)
- {
- DataSet ds = new DataSet ();
- new XmlSchemaDataImporter (ds, reader).Process ();
- DataTable target = null;
- if (TableName == String.Empty) {
- if (ds.Tables.Count > 0)
- target = ds.Tables [0];
- }
- else {
- target = ds.Tables [TableName];
- if (target == null)
- throw new ArgumentException (String.Format ("DataTable '{0}' does not match to any DataTable in source.", TableName));
- }
- if (target != null)
- target.CopyProperties (this);
- }
- #endif
- /// <summary>
- /// Rolls back all changes that have been made to the
- /// table since it was loaded, or the last time AcceptChanges
- /// was called.
- /// </summary>
- public void RejectChanges ()
- {
- for (int i = _rows.Count - 1; i >= 0; i--) {
- DataRow row = _rows [i];
- if (row.RowState != DataRowState.Unchanged)
- _rows [i].RejectChanges ();
- }
- }
- /// <summary>
- /// Resets the DataTable to its original state.
- /// </summary>
- public virtual void Reset ()
- {
- Clear();
- while (ParentRelations.Count > 0)
- {
- if (dataSet.Relations.Contains(ParentRelations[ParentRelations.Count - 1].RelationName))
- dataSet.Relations.Remove(ParentRelations[ParentRelations.Count - 1]);
- }
- while (ChildRelations.Count > 0)
- {
- if (dataSet.Relations.Contains(ChildRelations[ChildRelations.Count - 1].RelationName))
- dataSet.Relations.Remove(ChildRelations[ChildRelations.Count - 1]);
- }
- Constraints.Clear();
- Columns.Clear();
- }
- /// <summary>
- /// Gets an array of all DataRow objects.
- /// </summary>
- public DataRow[] Select ()
- {
- return Select(String.Empty, String.Empty, DataViewRowState.CurrentRows);
- }
- /// <summary>
- /// Gets an array of all DataRow objects that match
- /// the filter criteria in order of primary key (or
- /// lacking one, order of addition.)
- /// </summary>
- public DataRow[] Select (string filterExpression)
- {
- return Select(filterExpression, String.Empty, DataViewRowState.CurrentRows);
- }
- /// <summary>
- /// Gets an array of all DataRow objects that
- /// match the filter criteria, in the the
- /// specified sort order.
- /// </summary>
- public DataRow[] Select (string filterExpression, string sort)
- {
- return Select(filterExpression, sort, DataViewRowState.CurrentRows);
- }
- /// <summary>
- /// Gets an array of all DataRow objects that match
- /// the filter in the order of the sort, that match
- /// the specified state.
- /// </summary>
- [MonoTODO]
- public DataRow[] Select(string filterExpression, string sort, DataViewRowState recordStates)
- {
- if (filterExpression == null)
- filterExpression = String.Empty;
- DataColumn[] columns = _emptyColumnArray;
- ListSortDirection[] sorts = null;
- if (sort != null && !sort.Equals(String.Empty))
- columns = ParseSortString (this, sort, out sorts, false);
- IExpression filter = null;
- if (filterExpression != String.Empty) {
- Parser parser = new Parser ();
- filter = parser.Compile (filterExpression);
- }
- Index index = GetIndex(columns, sorts, recordStates, filter, false);
- int[] records = index.GetAll();
- DataRow[] dataRows = NewRowArray(index.Size);
- for (int i = 0; i < dataRows.Length; i++)
- dataRows[i] = RecordCache[records[i]];
- return dataRows;
- }
-
- private void AddIndex (Index index)
- {
- if (_indexes == null) {
- _indexes = new ArrayList();
- }
- _indexes.Add (index);
- }
- /// <summary>
- /// Returns index corresponding to columns,sort,row state filter and unique values given.
- /// If such an index not exists, creates a new one.
- /// </summary>
- /// <param name="columns">Columns set of the index to look for.</param>
- /// <param name="sort">Columns sort order of the index to look for.</param>
- /// <param name="rowState">Rpw state filter of the index to look for.</param>
- /// <param name="unique">Uniqueness of the index to look for.</param>
- /// <param name="strict">Indicates whenever the index found should correspond in its uniquness to the value of unique parameter specified.</param>
- /// <param name="reset">Indicates whenever the already existing index should be forced to reset.</param>
- /// <returns></returns>
- internal Index GetIndex(DataColumn[] columns, ListSortDirection[] sort, DataViewRowState rowState, IExpression filter, bool reset)
- {
- Index index = FindIndex(columns,sort,rowState,filter);
- if (index == null ) {
- index = new Index(new Key(this,columns,sort,rowState,filter));
- AddIndex(index);
- }
- else if (reset) {
- // reset existing index only if asked for this
- index.Reset();
- }
- return index;
- }
- internal Index FindIndex(DataColumn[] columns)
- {
- return FindIndex(columns,null,DataViewRowState.None, null);
- }
- internal Index FindIndex(DataColumn[] columns, ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
- {
- if (Indexes != null) {
- foreach (Index index in Indexes) {
- if (index.Key.Equals(columns,sort,rowState, filter)) {
- return index;
- }
- }
- }
- return null;
- }
- internal void ResetIndexes()
- {
- foreach(Index index in Indexes) {
- index.Reset();
- }
- }
- internal void ResetCaseSensitiveIndexes()
- {
- foreach(Index index in Indexes) {
- bool containsStringcolumns = false;
- foreach(DataColumn column in index.Key.Columns) {
- if (column.DataType == typeof(string)) {
- containsStringcolumns = true;
- break;
- }
- }
- if (!containsStringcolumns && index.Key.HasFilter)
- foreach (DataColumn column in Columns)
- if ((column.DataType == DbTypes.TypeOfString) && (index.Key.DependsOn (column))) {
- containsStringcolumns = true;
- break;
- }
- if (containsStringcolumns) {
- index.Reset();
- }
- }
- }
- internal void DropIndex(Index index)
- {
- if (index != null && index.RefCount == 0) {
- _indexes.Remove(index);
- }
- }
- internal void DropReferencedIndexes (DataColumn column)
- {
- if (_indexes != null)
- for (int i = _indexes.Count - 1; i >= 0; i--) {
- Index indx = (Index)_indexes [i];
- if (indx.Key.DependsOn (column))
- _indexes.Remove (indx);
- }
- }
- internal void AddRowToIndexes (DataRow row) {
- if (_indexes != null) {
- foreach (Index indx in _indexes) {
- indx.Add (row);
- }
- }
- }
- internal void DeleteRowFromIndexes (DataRow row)
- {
- if (_indexes != null) {
- foreach (Index indx in _indexes) {
- indx.Delete (row);
- }
- }
- }
- /// <summary>
- /// Gets the TableName and DisplayExpression, if
- /// there is one as a concatenated string.
- /// </summary>
- public override string ToString()
- {
- //LAMESPEC: spec says concat the two. impl puts a
- //plus sign infront of DisplayExpression
- string retVal = TableName;
- if(DisplayExpression != null && DisplayExpression != "")
- retVal += " + " + DisplayExpression;
- return retVal;
- }
- #if NET_2_0
- private XmlWriterSettings GetWriterSettings ()
- {
- XmlWriterSettings s = new XmlWriterSettings ();
- s.Indent = true;
- return s;
- }
- public void WriteXml (Stream stream)
- {
- WriteXml (stream, XmlWriteMode.IgnoreSchema);
- }
- public void WriteXml (TextWriter writer)
- {
- WriteXml (writer, XmlWriteMode.IgnoreSchema);
- }
- public void WriteXml (XmlWriter writer)
- {
- WriteXml (writer, XmlWriteMode.IgnoreSchema);
- }
- public void WriteXml (string fileName)
- {
- WriteXml (fileName, XmlWriteMode.IgnoreSchema);
- }
- public void WriteXml (Stream stream, XmlWriteMode mode)
- {
- WriteXml (XmlWriter.Create (stream, GetWriterSettings ()), mode);
- }
- public void WriteXml (TextWriter writer, XmlWriteMode mode)
- {
- WriteXml (XmlWriter.Create (writer, GetWriterSettings ()), mode);
- }
- [MonoTODO]
- public void WriteXml (XmlWriter writer, XmlWriteMode mode)
- {
- throw new NotImplementedException ();
- }
- public void WriteXml (string fileName, XmlWriteMode mode)
- {
- XmlWriter xw = null;
- try {
- xw = XmlWriter.Create (fileName, GetWriterSettings ());
- WriteXml (xw, mode);
- } finally {
- if (xw != null)
- xw.Close ();
- }
- }
- public void WriteXmlSchema (Stream stream)
- {
- WriteXmlSchema (XmlWriter.Create (stream, GetWriterSettings ()));
- }
- public void WriteXmlSchema (TextWriter writer)
- {
- WriteXmlSchema (XmlWriter.Create (writer, GetWriterSettings ()));
- }
- public void WriteXmlSchema (XmlWriter writer)
- {
- DataSet ds = DataSet;
- DataSet tmp = null;
- try {
- if (ds == null) {
- tmp = ds = new DataSet ();
- ds.Tables.Add (this);
- }
- DataTableCollection col = new DataTableCollection (ds);
- col.Add (this);
- XmlSchemaWriter.WriteXmlSchema (ds, writer, col, null);
- } finally {
- if (tmp != null)
- ds.Tables.Remove (this);
- }
- }
- public void WriteXmlSchema (string fileName)
- {
- XmlWriter xw = null;
- try {
- xw = XmlWriter.Create (fileName, GetWriterSettings ());
- WriteXmlSchema (xw);
- } finally {
- if (xw != null)
- xw.Close ();
- }
- }
- #endif
-
- #region Events
-
- /// <summary>
- /// Raises the ColumnChanged event.
- /// </summary>
- protected virtual void OnColumnChanged (DataColumnChangeEventArgs e) {
- if (null != ColumnChanged) {
- ColumnChanged (this, e);
- }
- }
- internal void RaiseOnColumnChanged (DataColumnChangeEventArgs e) {
- OnColumnChanged(e);
- }
- #if NET_2_0
- /// <summary>
- /// Raises TableCleared Event and delegates to subscribers
- /// </summary>
- protected virtual void OnTableCleared (DataTableClearEventArgs e) {
- if (TableCleared != null)
- TableCleared (this, e);
- }
- #endif // NET_2_0
- /// <summary>
- /// Raises the ColumnChanging event.
- /// </summary>
- protected virtual void OnColumnChanging (DataColumnChangeEventArgs e) {
- if (null != ColumnChanging) {
- ColumnChanging (this, e);
- }
- }
- internal void RaiseOnColumnChanging (DataColumnChangeEventArgs e) {
- OnColumnChanging(e);
- }
- /// <summary>
- /// Raises the PropertyChanging event.
- /// </summary>
- [MonoTODO]
- protected internal virtual void OnPropertyChanging (PropertyChangedEventArgs pcevent) {
- // if (null != PropertyChanging)
- // {
- // PropertyChanging (this, e);
- // }
- }
- /// <summary>
- /// Notifies the DataTable that a DataColumn is being removed.
- /// </summary>
- protected internal virtual void OnRemoveColumn (DataColumn column) {
- DropReferencedIndexes (column);
- }
- /// <summary>
- /// Raises the RowChanged event.
- /// </summary>
- protected virtual void OnRowChanged (DataRowChangeEventArgs e) {
- if (null != RowChanged) {
- RowChanged(this, e);
- }
- }
- /// <summary>
- /// Raises the RowChanging event.
- /// </summary>
- protected virtual void OnRowChanging (DataRowChangeEventArgs e) {
- if (null != RowChanging) {
- RowChanging(this, e);
- }
- }
- /// <summary>
- /// Raises the RowDeleted event.
- /// </summary>
- protected virtual void OnRowDeleted (DataRowChangeEventArgs e) {
- if (null != RowDeleted) {
- RowDeleted(this, e);
- }
- }
- /// <summary>
- /// Raises the RowDeleting event.
- /// </summary>
- protected virtual void OnRowDeleting (DataRowChangeEventArgs e) {
- if (null != RowDeleting) {
- RowDeleting(this, e);
- }
- }
- /// <summary>
- /// Occurs when after a value has been changed for
- /// the specified DataColumn in a DataRow.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs when a value has been changed for this column.")]
- #endif
- public event DataColumnChangeEventHandler ColumnChanged;
- /// <summary>
- /// Occurs when a value is being changed for the specified
- /// DataColumn in a DataRow.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs when a value has been submitted for this column. The user can modify the proposed value and should throw an exception to cancel the edit.")]
- #endif
- public event DataColumnChangeEventHandler ColumnChanging;
- /// <summary>
- /// Occurs after a DataRow has been changed successfully.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs after a row in the table has been successfully edited.")]
- #endif
- public event DataRowChangeEventHandler RowChanged;
- /// <summary>
- /// Occurs when a DataRow is changing.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs when the row is being changed so that the event handler can modify or cancel the change. The user can modify values in the row and should throw an exception to cancel the edit.")]
- #endif
- public event DataRowChangeEventHandler RowChanging;
- /// <summary>
- /// Occurs after a row in the table has been deleted.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs after a row in the table has been successfully deleted.")]
- #endif
- public event DataRowChangeEventHandler RowDeleted;
- /// <summary>
- /// Occurs before a row in the table is about to be deleted.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs when a row in the table marked for deletion. Throw an exception to cancel the deletion.")]
- #endif
- public event DataRowChangeEventHandler RowDeleting;
- #if NET_2_0
- /// <summary>
- /// Occurs after the Clear method is called on the datatable.
- /// </summary>
- [DataCategory ("Data")]
- #if !NET_2_0
- [DataSysDescription ("Occurs when the rows in a table is cleared . Throw an exception to cancel the deletion.")]
- #endif
- public event DataTableClearEventHandler TableCleared;
- #endif // NET_2_0
- #endregion // Events
- /// <summary>
- /// Removes all UniqueConstraints
- /// </summary>
- private void RemoveUniqueConstraints ()
- {
- foreach (Constraint Cons in Constraints) {
-
- if (Cons is UniqueConstraint) {
- Constraints.Remove (Cons);
- break;
- }
- }
-
- UniqueConstraint.SetAsPrimaryKey(this.Constraints, null);
- }
- internal static DataColumn[] ParseSortString (DataTable table, string sort, out ListSortDirection[] sortDirections, bool rejectNoResult)
- {
- DataColumn[] sortColumns = _emptyColumnArray;
- sortDirections = null;
-
- ArrayList columns = null;
- ArrayList sorts = null;
-
- if (sort != null && !sort.Equals ("")) {
- columns = new ArrayList ();
- sorts = new ArrayList();
- string[] columnExpression = sort.Trim ().Split (new char[1] {','});
-
- for (int c = 0; c < columnExpression.Length; c++) {
- string rawColumnName = columnExpression[c].Trim ();
- Match match = SortRegex.Match (rawColumnName);
- CaptureCollection cc = match.Groups["ColName"].Captures ;
- if (cc.Count == 0)
- throw new IndexOutOfRangeException ("Could not find column: " + rawColumnName);
- string columnName = cc[0].Value;
- DataColumn dc = table.Columns[columnName];
- if (dc == null){
- try {
- dc = table.Columns[Int32.Parse (columnName)];
- } catch (FormatException) {
- throw new IndexOutOfRangeException("Cannot find column " + columnName);
- }
- }
- columns.Add (dc);
- cc = match.Groups["Order"].Captures;
- if (cc.Count == 0 || String.Compare (cc[0].Value, "ASC", true) == 0)
- sorts.Add(ListSortDirection.Ascending);
- else
- sorts.Add (ListSortDirection.Descending);
- }
- sortColumns = (DataColumn[]) columns.ToArray (typeof (DataColumn));
- sortDirections = new ListSortDirection[sorts.Count];
- for (int i = 0; i < sortDirections.Length; i++)
- sortDirections[i] = (ListSortDirection)sorts[i];
- }
- if (rejectNoResult) {
- if (sortColumns == null)
- throw new SystemException ("sort expression result is null");
- if (sortColumns.Length == 0)
- throw new SystemException("sort expression result is 0");
- }
- return sortColumns;
- }
- private void UpdatePropertyDescriptorsCache()
- {
- PropertyDescriptor[] descriptors = new PropertyDescriptor[Columns.Count + ChildRelations.Count];
- int index = 0;
- foreach(DataColumn col in Columns) {
- descriptors[index++] = new DataColumnPropertyDescriptor(col);
- }
- foreach(DataRelation rel in ChildRelations) {
- descriptors[index++] = new DataRelationPropertyDescriptor(rel);
- }
- _propertyDescriptorsCache = new PropertyDescriptorCollection(descriptors);
- }
- internal PropertyDescriptorCollection GetPropertyDescriptorCollection()
- {
- if (_propertyDescriptorsCache == null) {
- UpdatePropertyDescriptorsCache();
- }
- return _propertyDescriptorsCache;
- }
- internal void ResetPropertyDescriptorsCache() {
- _propertyDescriptorsCache = null;
- }
- }
- }
|