/// <summary> /// Create a clone of the current DmTable /// </summary> public DmTable Clone() { DmTable clone = new DmTable(); // Set All properties clone.TableName = tableName; clone.Schema = schema; clone.Culture = culture; clone.CaseSensitive = caseSensitive; clone.OriginalProvider = OriginalProvider; clone.SyncDirection = SyncDirection; clone.TrackingTablesPrefix = TrackingTablesPrefix; clone.TrackingTablesSuffix = TrackingTablesSuffix; clone.StoredProceduresPrefix = StoredProceduresPrefix; clone.StoredProceduresSuffix = StoredProceduresSuffix; clone.TriggersPrefix = TriggersPrefix; clone.TriggersSuffix = TriggersSuffix; // add all columns var clmns = this.Columns; for (int i = 0; i < clmns.Count; i++) { clone.Columns.Add(clmns[i].Clone()); } // Create PrimaryKey DmColumn[] pkey = PrimaryKey.Columns; if (pkey != null && pkey.Length > 0) { DmColumn[] key = new DmColumn[pkey.Length]; for (int i = 0; i < pkey.Length; i++) { key[i] = clone.Columns[pkey[i].Ordinal]; } clone.PrimaryKey = new DmKey(key); } return(clone); }
/// <summary> /// Copy a record, whatever the state (Added, Modified, Deleted etc...) /// </summary> internal void CopyRecords(DmTable src, int srcRecord, int newRecord) { // Parcours de toutes les colonnes de destination for (int i = 0; i < this.Columns.Count; ++i) { // colonne de destination DmColumn dstColumn = this.Columns[i]; // colonne à copier DmColumn srcColumn = src.Columns.FirstOrDefault(c => this.IsEqual(c.ColumnName, dstColumn.ColumnName)); if (srcColumn == null) { dstColumn.Init(newRecord); continue; } if (dstColumn.IsValueType) { dstColumn[newRecord] = srcColumn[srcRecord]; continue; } if (dstColumn.DataType == typeof(byte[])) { byte[] srcArray = (byte[])srcColumn[srcRecord]; byte[] destArray = (byte[])dstColumn[newRecord]; Buffer.BlockCopy(srcArray, 0, destArray, 0, srcArray.Length); continue; } if (dstColumn.DataType == typeof(char[])) { char[] srcArray = (char[])srcColumn[srcRecord]; char[] destArray = (char[])dstColumn[newRecord]; Buffer.BlockCopy(srcArray, 0, destArray, 0, srcArray.Length); continue; } dstColumn[newRecord] = srcColumn[srcRecord]; } }
/// <summary> /// Gets or sets the data stored in the column /// </summary> public object this[DmColumn column] { get { CheckColumn(column); int record = GetRecordId(); return(column[record]); } set { CheckColumn(column); if (RowId != -1 && column.ReadOnly) { throw new Exception($"ReadOnly {column.ColumnName}"); } if (value == null && !column.AllowDBNull) { throw new Exception($"Cannot set null to this {column}"); } // we are going to change a value, so try to enter begin edit bool immediate = BeginEditInternal(); try { int record = GetProposedRecordId(); column[record] = value; } catch (Exception) { throw; } if (immediate) { EndEdit(); } } }
DmKey GetSrcKey(DmTable src, DmTable dst) { if (src.PrimaryKey != null) { return(src.PrimaryKey); } DmKey key = default(DmKey); if (dst.PrimaryKey != null) { DmColumn[] dstColumns = dst.PrimaryKey.Columns; DmColumn[] srcColumns = new DmColumn[dstColumns.Length]; for (int j = 0; j < dstColumns.Length; j++) { srcColumns[j] = src.Columns[dstColumns[j].ColumnName]; } key = new DmKey(srcColumns); // DmKey will take ownership of srcColumns } return(key); }
/// <summary> /// Fill the DmTable from a DbDataReader connected to any kind of database /// </summary> public void Fill(DbDataReader reader) { var readerFieldCount = reader.FieldCount; if (readerFieldCount == 0) { return; } if (this.Columns.Count == 0) { for (int i = 0; i < reader.FieldCount; i++) { var columnName = reader.GetName(i); var columnType = reader.GetFieldType(i); DmColumn column = DmColumn.CreateColumn(columnName, columnType); this.Columns.Add(column); } } // Count - 2 because we can have a autoinc columns if (readerFieldCount < this.columns.Count - 2) { return; } if (reader.HasRows) { while (reader.Read()) { object[] readerDataValues = new object[reader.FieldCount]; reader.GetValues(readerDataValues); var dataRow = this.LoadDataRow(readerDataValues, true); } } }
DmTable MergeSchema(DmTable table) { if (dataTable == null) { dataTable = table.Clone(); if (dataTable.DmSet != null) { dataTable.DmSet.Tables.Add(dataTable); } } else { // Do the columns int oldCount = dataTable.Columns.Count; for (int i = 0; i < table.Columns.Count; i++) { DmColumn src = table.Columns[i]; DmColumn dest = (dataTable.Columns.Contains(src.ColumnName, true)) ? dataTable.Columns[src.ColumnName] : null; // If columns doesn't exist, create it if (dest == null) { dest = src.Clone(); dataTable.Columns.Add(dest); } } // check the PrimaryKey DmColumn[] targetPKey = dataTable.PrimaryKey.Columns; DmColumn[] tablePKey = table.PrimaryKey.Columns; if (targetPKey.Length != tablePKey.Length) { if (tablePKey.Length != 0) { throw new Exception("Merge Failed, keys are differents"); } // special case when the target table does not have the PrimaryKey if (targetPKey.Length == 0) { DmColumn[] key = new DmColumn[tablePKey.Length]; for (int i = 0; i < tablePKey.Length; i++) { key[i] = dataTable.Columns[tablePKey[i].ColumnName]; } dataTable.PrimaryKey = new DmKey(key); } } else { // Check the keys are same for (int i = 0; i < targetPKey.Length; i++) { if (!table.IsEqual(targetPKey[i].ColumnName, tablePKey[i].ColumnName)) { throw new Exception("Merge Failed, keys are differents"); } } } } return(dataTable); }
public DmKey(DmColumn column) { this.columns = new[] { column }; }