/// <summary> /// Launch apply bulk changes /// </summary> /// <returns></returns> public int ApplyBulkChanges(DmView dmChanges, ScopeInfo fromScope, List <SyncConflict> conflicts) { DbCommand bulkCommand = null; if (this.applyType == DmRowState.Added) { bulkCommand = this.GetCommand(DbObjectType.BulkInsertProcName); } else if (this.applyType == DmRowState.Modified) { bulkCommand = this.GetCommand(DbObjectType.BulkUpdateProcName); } else if (this.applyType == DmRowState.Deleted) { bulkCommand = this.GetCommand(DbObjectType.BulkDeleteProcName); } else { throw new Exception("DmRowState not valid during ApplyBulkChanges operation"); } if (Transaction != null && Transaction.Connection != null) { bulkCommand.Transaction = Transaction; } DmTable batchDmTable = dmChanges.Table.Clone(); DmTable failedDmtable = new DmTable { Culture = CultureInfo.InvariantCulture }; // Create the schema for failed rows (just add the Primary keys) this.AddSchemaForFailedRowsTable(batchDmTable, failedDmtable); int batchCount = 0; int rowCount = 0; foreach (var dmRow in dmChanges) { // Cancel the delete state to be able to get the row, more simplier if (applyType == DmRowState.Deleted) { dmRow.RejectChanges(); } // Load the datarow DmRow dataRow = batchDmTable.LoadDataRow(dmRow.ItemArray, false); // Apply the delete // is it mandatory ? if (applyType == DmRowState.Deleted) { dmRow.Delete(); } batchCount++; rowCount++; if (batchCount != 500 && rowCount != dmChanges.Count) { continue; } // Since the update and create timestamp come from remote, change name for the bulk operations batchDmTable.Columns["update_timestamp"].ColumnName = "update_timestamp"; batchDmTable.Columns["create_timestamp"].ColumnName = "create_timestamp"; // execute the batch, through the provider ExecuteBatchCommand(bulkCommand, batchDmTable, failedDmtable, fromScope); // Clear the batch batchDmTable.Clear(); // Recreate a Clone // TODO : Evaluate if it's necessary batchDmTable = dmChanges.Table.Clone(); batchCount = 0; } // Update table progress //tableProgress.ChangesApplied = dmChanges.Count - failedDmtable.Rows.Count; if (failedDmtable.Rows.Count == 0) { return(dmChanges.Count); } // Check all conflicts raised var failedFilter = new Predicate <DmRow>(row => { if (row.RowState == DmRowState.Deleted) { return(failedDmtable.FindByKey(row.GetKeyValues(DmRowVersion.Original)) != null); } else { return(failedDmtable.FindByKey(row.GetKeyValues()) != null); } }); // New View var dmFailedRows = new DmView(dmChanges, failedFilter); // Generate a conflict and add it foreach (var dmFailedRow in dmFailedRows) { conflicts.Add(GetConflict(dmFailedRow)); } int failedRows = dmFailedRows.Count; // Dispose the failed view dmFailedRows.Dispose(); // return applied rows - failed rows (generating a conflict) return(dmChanges.Count - failedRows); }
public void DmTable_LoadDataRow() { // Trying with a key GUID (not auto incremented) var tbl2 = new DmTable(); var id = new DmColumn <Guid>("ServiceTicketID"); tbl2.Columns.Add(id); var key = new DmKey(new DmColumn[] { id }); tbl2.PrimaryKey = key; tbl2.Columns.Add(new DmColumn <string>("Title")); tbl2.Columns.Add(new DmColumn <string>("Description")); tbl2.Columns.Add(new DmColumn <int>("StatusValue")); tbl2.Columns.Add(new DmColumn <int>("EscalationLevel")); tbl2.Columns.Add(new DmColumn <DateTime>("Opened")); tbl2.Columns.Add(new DmColumn <DateTime>("Closed")); tbl2.Columns.Add(new DmColumn <int>("CustomerID")); var st = tbl2.NewRow(); st["ServiceTicketID"] = Guid.NewGuid(); st["Title"] = "Titre AER"; st["Description"] = "Description 2"; st["EscalationLevel"] = 1; st["StatusValue"] = 2; st["Opened"] = DateTime.Now; st["Closed"] = null; st["CustomerID"] = 1; tbl2.Rows.Add(st); var array = new object[] { Guid.NewGuid(), "Titre 2", "Desc", 1, 2, DateTime.Now, null, 3 }; tbl2.LoadDataRow(array, true); Assert.Equal(2, tbl2.Rows.Count); // Trying with a key Int auto incremented var tbl3 = new DmTable(); var id3 = new DmColumn <Int32>("ServiceTicketID"); id3.IsAutoIncrement = true; tbl3.Columns.Add(id3); var key3 = new DmKey(new DmColumn[] { id3 }); tbl3.PrimaryKey = key3; tbl3.Columns.Add(new DmColumn <string>("Title")); tbl3.Columns.Add(new DmColumn <string>("Description")); tbl3.Columns.Add(new DmColumn <int>("StatusValue")); tbl3.Columns.Add(new DmColumn <int>("EscalationLevel")); tbl3.Columns.Add(new DmColumn <DateTime>("Opened")); tbl3.Columns.Add(new DmColumn <DateTime>("Closed")); tbl3.Columns.Add(new DmColumn <int>("CustomerID")); var st3 = tbl3.NewRow(); st3["Title"] = "Titre AER"; st3["Description"] = "Description 2"; st3["EscalationLevel"] = 1; st3["StatusValue"] = 2; st3["Opened"] = DateTime.Now; st3["Closed"] = null; st3["CustomerID"] = 1; tbl3.Rows.Add(st3); var array3 = new object[] { "Titre 2", "Desc", 1, 2, DateTime.Now, null, 3 }; tbl3.LoadDataRow(array3, true); Assert.Equal(1, tbl3.Rows[0][0]); Assert.Equal(2, tbl3.Rows[1][0]); }