1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
//------------------------------------------------------------------------------
// <copyright file="Merger.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
/// <devdoc>
/// Merge Utilities.
/// </devdoc>
internal sealed class Merger {
private DataSet dataSet = null;
private DataTable dataTable = null;
private bool preserveChanges;
private MissingSchemaAction missingSchemaAction;
private bool isStandAlonetable = false;
private bool _IgnoreNSforTableLookup = false; // Everett Behavior : SQL BU DT 370850
internal Merger(DataSet dataSet, bool preserveChanges, MissingSchemaAction missingSchemaAction) {
this.dataSet = dataSet;
this.preserveChanges = preserveChanges;
// map AddWithKey -> Add
if (missingSchemaAction == MissingSchemaAction.AddWithKey)
this.missingSchemaAction = MissingSchemaAction.Add;
else
this.missingSchemaAction = missingSchemaAction;
}
internal Merger(DataTable dataTable, bool preserveChanges, MissingSchemaAction missingSchemaAction) {
isStandAlonetable = true;
this.dataTable = dataTable;
this.preserveChanges = preserveChanges;
// map AddWithKey -> Add
if (missingSchemaAction == MissingSchemaAction.AddWithKey)
this.missingSchemaAction = MissingSchemaAction.Add;
else
this.missingSchemaAction = missingSchemaAction;
}
internal void MergeDataSet(DataSet source) {
if (source == dataSet) return; //somebody is doing an 'automerge'
bool fEnforce = dataSet.EnforceConstraints;
dataSet.EnforceConstraints = false;
_IgnoreNSforTableLookup = (dataSet.namespaceURI != source.namespaceURI); // if two DataSets have different
// Namespaces, ignore NS for table lookups as we wont be able to find the right tables which inherits its NS
List<DataColumn> existingColumns = null;// need to cache existing columns
if (MissingSchemaAction.Add == missingSchemaAction) {
existingColumns = new List<DataColumn>(); // need to cache existing columns
foreach(DataTable dt in dataSet.Tables) {
foreach(DataColumn dc in dt.Columns) {
existingColumns.Add(dc);
}
}
}
for (int i = 0; i < source.Tables.Count; i++) {
MergeTableData(source.Tables[i]); // since column expression might have dependency on relation, we do not set
//column expression at this point. We need to set it after adding relations
}
if (MissingSchemaAction.Ignore != missingSchemaAction) {
// Add all independent constraints
MergeConstraints(source);
// Add all relationships
for (int i = 0; i < source.Relations.Count; i++) {
MergeRelation(source.Relations[i]);
}
}
// WebData 88234
if (MissingSchemaAction.Add == missingSchemaAction) { // for which other options we should add expressions also?
foreach (DataTable sourceTable in source.Tables) {
DataTable targetTable;
if (_IgnoreNSforTableLookup) {
targetTable = dataSet.Tables[sourceTable.TableName];
}
else {
targetTable = dataSet.Tables[sourceTable.TableName, sourceTable.Namespace];// we know that target table wont be null since MissingSchemaAction is Add , we have already added it!
}
foreach(DataColumn dc in sourceTable.Columns) { // Should we overwrite the previous expression column? No, refer to spec, if it is new column we need to add the schema
if (dc.Computed) {
DataColumn targetColumn = targetTable.Columns[dc.ColumnName];
if (!existingColumns.Contains(targetColumn)) {
targetColumn.Expression = dc.Expression;
}
}
}
}
}
MergeExtendedProperties(source.ExtendedProperties, dataSet.ExtendedProperties);
foreach(DataTable dt in dataSet.Tables)
dt.EvaluateExpressions();
dataSet.EnforceConstraints = fEnforce;
}
internal void MergeTable(DataTable src) {
bool fEnforce = false;
if (!isStandAlonetable) {
if (src.DataSet == dataSet) return; //somebody is doing an 'automerge'
fEnforce = dataSet.EnforceConstraints;
dataSet.EnforceConstraints = false;
}
else {
if (src == dataTable) return; //somebody is doing an 'automerge'
dataTable.SuspendEnforceConstraints = true;
}
if (this.dataSet != null) { // this is ds.Merge
// if source does not have a DS, or if NS of both DS does not match, ignore the NS
if (src.DataSet == null || src.DataSet.namespaceURI != this.dataSet.namespaceURI) {
_IgnoreNSforTableLookup = true;
}
}
else { // this is dt.Merge
if (this.dataTable.DataSet == null || src.DataSet == null ||
src.DataSet.namespaceURI != this.dataTable.DataSet.namespaceURI) {
_IgnoreNSforTableLookup = true;
}
}
MergeTableData(src);
DataTable dt = dataTable;
if (dt == null && dataSet != null) {
if (_IgnoreNSforTableLookup) {
dt = dataSet.Tables[src.TableName];
}
else {
dt = dataSet.Tables[src.TableName, src.Namespace];
}
}
if (dt != null) {
dt.EvaluateExpressions();
}
if (!isStandAlonetable) {
dataSet.EnforceConstraints = fEnforce;
}
else {
dataTable.SuspendEnforceConstraints = false;
try {
if (dataTable.EnforceConstraints) {
dataTable.EnableConstraints();
}
}
catch(ConstraintException) {
if (dataTable.DataSet != null) {
dataTable.DataSet.EnforceConstraints = false;
}
throw;
}
}
}
private void MergeTable(DataTable src, DataTable dst) {
int rowsCount = src.Rows.Count;
bool wasEmpty = dst.Rows.Count == 0;
if(0 < rowsCount) {
Index ndxSearch = null;
DataKey key = default(DataKey);
dst.SuspendIndexEvents();
try {
if(! wasEmpty && dst.primaryKey != null) {
key = GetSrcKey(src, dst);
if (key.HasValue)
ndxSearch = dst.primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows | DataViewRowState.Added );
}
// SQLBU 414992: Serious performance issue when calling Merge
// this improves performance by iterating over the rows instead of computing their position
foreach(DataRow sourceRow in src.Rows) {
DataRow targetRow = null;
if(ndxSearch != null) {
targetRow = dst.FindMergeTarget(sourceRow, key, ndxSearch);
}
dst.MergeRow(sourceRow, targetRow, preserveChanges, ndxSearch);
}
}
finally {
dst.RestoreIndexEvents(true);
}
}
MergeExtendedProperties(src.ExtendedProperties, dst.ExtendedProperties);
}
internal void MergeRows(DataRow[] rows) {
DataTable src = null;
DataTable dst = null;
DataKey key = default(DataKey);
Index ndxSearch = null;
bool fEnforce = dataSet.EnforceConstraints;
dataSet.EnforceConstraints = false;
for (int i = 0; i < rows.Length; i++) {
DataRow row = rows[i];
if (row == null) {
throw ExceptionBuilder.ArgumentNull("rows[" + i + "]");
}
if (row.Table == null) {
throw ExceptionBuilder.ArgumentNull("rows[" + i + "].Table");
}
//somebody is doing an 'automerge'
if (row.Table.DataSet == dataSet)
continue;
if (src != row.Table) { // row.Table changed from prev. row.
src = row.Table;
dst = MergeSchema(row.Table);
if (dst == null) {
Debug.Assert(MissingSchemaAction.Ignore == missingSchemaAction, "MergeSchema failed");
dataSet.EnforceConstraints = fEnforce;
return;
}
if(dst.primaryKey != null) {
key = GetSrcKey(src, dst);
}
if (key.HasValue) {
// Getting our own copy instead. ndxSearch = dst.primaryKey.Key.GetSortIndex();
// IMO, Better would be to reuse index
// ndxSearch = dst.primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows | DataViewRowState.Added );
if (null != ndxSearch) {
ndxSearch.RemoveRef();
ndxSearch = null;
}
ndxSearch = new Index(dst, dst.primaryKey.Key.GetIndexDesc(), DataViewRowState.OriginalRows | DataViewRowState.Added, (IFilter)null);
ndxSearch.AddRef(); // need to addref twice, otherwise it will be collected
ndxSearch.AddRef(); // in past first adref was done in const
}
}
if (row.newRecord == -1 && row.oldRecord == -1)
continue;
DataRow targetRow = null;
if(0 < dst.Rows.Count && ndxSearch != null) {
targetRow = dst.FindMergeTarget(row, key, ndxSearch);
}
targetRow = dst.MergeRow(row, targetRow, preserveChanges, ndxSearch);
if (targetRow.Table.dependentColumns != null && targetRow.Table.dependentColumns.Count > 0)
targetRow.Table.EvaluateExpressions(targetRow, DataRowAction.Change, null);
}
if (null != ndxSearch) {
ndxSearch.RemoveRef();
ndxSearch = null;
}
dataSet.EnforceConstraints = fEnforce;
}
private DataTable MergeSchema(DataTable table) {
DataTable targetTable = null;
if (!isStandAlonetable) {
if (dataSet.Tables.Contains(table.TableName, true))
if (_IgnoreNSforTableLookup) {
targetTable = dataSet.Tables[table.TableName];
}
else {
targetTable = dataSet.Tables[table.TableName, table.Namespace];
}
}
else {
targetTable = dataTable;
}
if (targetTable == null) { // in case of standalone table, we make sure that targetTable is not null, so if this check passes, it will be when it is called via detaset
if (MissingSchemaAction.Add == missingSchemaAction) {
targetTable = table.Clone(table.DataSet); // if we are here mainly we are called from DataSet.Merge at this point we don't set
//expression columns, since it might have refer to other columns via relation, so it wont find the table and we get exception;
// do it after adding relations.
dataSet.Tables.Add(targetTable);
}
else if (MissingSchemaAction.Error == missingSchemaAction) {
throw ExceptionBuilder.MergeMissingDefinition(table.TableName);
}
}
else {
if (MissingSchemaAction.Ignore != missingSchemaAction) {
// Do the columns
int oldCount = targetTable.Columns.Count;
for (int i = 0; i < table.Columns.Count; i++) {
DataColumn src = table.Columns[i];
DataColumn dest = (targetTable.Columns.Contains(src.ColumnName, true)) ? targetTable.Columns[src.ColumnName] : null;
if (dest == null) {
if (MissingSchemaAction.Add == missingSchemaAction) {
dest = src.Clone();
targetTable.Columns.Add(dest);
}
else {
if (!isStandAlonetable)
dataSet.RaiseMergeFailed(targetTable, Res.GetString(Res.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName), missingSchemaAction);
else
throw ExceptionBuilder.MergeFailed(Res.GetString(Res.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName));
}
}
else {
if (dest.DataType != src.DataType ||
((dest.DataType == typeof(DateTime)) && (dest.DateTimeMode != src.DateTimeMode) && ((dest.DateTimeMode & src.DateTimeMode) != DataSetDateTime.Unspecified))) {
if (!isStandAlonetable)
dataSet.RaiseMergeFailed(targetTable, Res.GetString(Res.DataMerge_DataTypeMismatch, src.ColumnName), MissingSchemaAction.Error);
else
throw ExceptionBuilder.MergeFailed(Res.GetString(Res.DataMerge_DataTypeMismatch, src.ColumnName));
}
//
MergeExtendedProperties(src.ExtendedProperties, dest.ExtendedProperties);
}
}
// Set DataExpression
if (isStandAlonetable) {
for (int i = oldCount; i < targetTable.Columns.Count; i++) {
targetTable.Columns[i].Expression = table.Columns[targetTable.Columns[i].ColumnName].Expression;
}
}
// check the PrimaryKey
DataColumn[] targetPKey = targetTable.PrimaryKey;
DataColumn[] tablePKey = table.PrimaryKey;
if (targetPKey.Length != tablePKey.Length) {
// special case when the target table does not have the PrimaryKey
if (targetPKey.Length == 0) {
DataColumn[] key = new DataColumn[tablePKey.Length];
for (int i = 0; i < tablePKey.Length; i++) {
key[i] = targetTable.Columns[tablePKey[i].ColumnName];
}
targetTable.PrimaryKey = key;
}
else if (tablePKey.Length != 0) {
dataSet.RaiseMergeFailed(targetTable, Res.GetString(Res.DataMerge_PrimaryKeyMismatch), missingSchemaAction);
}
}
else {
for (int i = 0; i < targetPKey.Length; i++) {
if (String.Compare(targetPKey[i].ColumnName, tablePKey[i].ColumnName, false, targetTable.Locale) != 0) {
dataSet.RaiseMergeFailed(table,
Res.GetString(Res.DataMerge_PrimaryKeyColumnsMismatch, targetPKey[i].ColumnName, tablePKey[i].ColumnName),
missingSchemaAction
);
}
}
}
}
MergeExtendedProperties(table.ExtendedProperties, targetTable.ExtendedProperties);
}
return targetTable;
}
private void MergeTableData(DataTable src) {
DataTable dest = MergeSchema(src);
if (dest == null) return;
dest.MergingData = true;
try {
MergeTable(src, dest);
}
finally {
dest.MergingData = false;
}
}
private void MergeConstraints(DataSet source) {
for (int i = 0; i < source.Tables.Count; i ++) {
MergeConstraints(source.Tables[i]);
}
}
private void MergeConstraints(DataTable table) {
// Merge constraints
for (int i = 0; i < table.Constraints.Count; i++) {
Constraint src = table.Constraints[i];
Constraint dest = src.Clone(dataSet, _IgnoreNSforTableLookup);
if (dest == null) {
dataSet.RaiseMergeFailed(table,
Res.GetString(Res.DataMerge_MissingConstraint, src.GetType().FullName, src.ConstraintName),
missingSchemaAction
);
}
else {
Constraint cons = dest.Table.Constraints.FindConstraint(dest);
if (cons == null) {
if (MissingSchemaAction.Add == missingSchemaAction) {
try {
// try to keep the original name
dest.Table.Constraints.Add(dest);
}
catch (DuplicateNameException) {
// if fail, assume default name
dest.ConstraintName = "";
dest.Table.Constraints.Add(dest);
}
}
else if (MissingSchemaAction.Error == missingSchemaAction) {
dataSet.RaiseMergeFailed(table,
Res.GetString(Res.DataMerge_MissingConstraint, src.GetType().FullName, src.ConstraintName),
missingSchemaAction
);
}
}
else {
MergeExtendedProperties(src.ExtendedProperties, cons.ExtendedProperties);
}
}
}
}
private void MergeRelation(DataRelation relation) {
Debug.Assert(MissingSchemaAction.Error == missingSchemaAction ||
MissingSchemaAction.Add == missingSchemaAction,
"Unexpected value of MissingSchemaAction parameter : " + ((Enum) missingSchemaAction).ToString());
DataRelation destRelation = null;
// try to find given relation in this dataSet
int iDest = dataSet.Relations.InternalIndexOf(relation.RelationName);
if (iDest >= 0) {
// check the columns and Relation properties..
destRelation = dataSet.Relations[iDest];
if (relation.ParentKey.ColumnsReference.Length != destRelation.ParentKey.ColumnsReference.Length) {
dataSet.RaiseMergeFailed(null,
Res.GetString(Res.DataMerge_MissingDefinition, relation.RelationName),
missingSchemaAction
);
}
for (int i = 0; i < relation.ParentKey.ColumnsReference.Length; i++) {
DataColumn dest = destRelation.ParentKey.ColumnsReference[i];
DataColumn src = relation.ParentKey.ColumnsReference[i];
if (0 != string.Compare(dest.ColumnName, src.ColumnName, false, dest.Table.Locale)) {
dataSet.RaiseMergeFailed(null,
Res.GetString(Res.DataMerge_ReltionKeyColumnsMismatch, relation.RelationName),
missingSchemaAction
);
}
dest = destRelation.ChildKey.ColumnsReference[i];
src = relation.ChildKey.ColumnsReference[i];
if (0 != string.Compare(dest.ColumnName, src.ColumnName, false, dest.Table.Locale)) {
dataSet.RaiseMergeFailed(null,
Res.GetString(Res.DataMerge_ReltionKeyColumnsMismatch, relation.RelationName),
missingSchemaAction
);
}
}
}
else {
if (MissingSchemaAction.Add == missingSchemaAction) {
// create identical realtion in the current dataset
DataTable parent;
if (_IgnoreNSforTableLookup){
parent = dataSet.Tables[relation.ParentTable.TableName];
}
else {
parent = dataSet.Tables[relation.ParentTable.TableName, relation.ParentTable.Namespace];
}
DataTable child;
if (_IgnoreNSforTableLookup) {
child = dataSet.Tables[relation.ChildTable.TableName];
}
else {
child = dataSet.Tables[relation.ChildTable.TableName,relation.ChildTable.Namespace];
}
DataColumn[] parentColumns = new DataColumn[relation.ParentKey.ColumnsReference.Length];
DataColumn[] childColumns = new DataColumn[relation.ParentKey.ColumnsReference.Length];
for (int i = 0; i < relation.ParentKey.ColumnsReference.Length; i++) {
parentColumns[i] = parent.Columns[relation.ParentKey.ColumnsReference[i].ColumnName];
childColumns[i] = child.Columns[relation.ChildKey.ColumnsReference[i].ColumnName];
}
try {
destRelation = new DataRelation(relation.RelationName, parentColumns, childColumns, relation.createConstraints);
destRelation.Nested = relation.Nested;
dataSet.Relations.Add(destRelation);
}
catch (Exception e) {
//
if (!Common.ADP.IsCatchableExceptionType(e)) {
throw;
}
ExceptionBuilder.TraceExceptionForCapture(e);
dataSet.RaiseMergeFailed(null, e.Message, missingSchemaAction);
}
}
else {
Debug.Assert(MissingSchemaAction.Error == missingSchemaAction, "Unexpected value of MissingSchemaAction parameter : " + ((Enum) missingSchemaAction).ToString());
throw ExceptionBuilder.MergeMissingDefinition(relation.RelationName);
}
}
MergeExtendedProperties(relation.ExtendedProperties, destRelation.ExtendedProperties);
return;
}
private void MergeExtendedProperties(PropertyCollection src, PropertyCollection dst) {
if (MissingSchemaAction.Ignore == missingSchemaAction) {
return;
}
IDictionaryEnumerator srcDE = src.GetEnumerator();
while (srcDE.MoveNext()) {
if (!preserveChanges || dst[srcDE.Key] == null)
dst[srcDE.Key] = srcDE.Value;
}
}
private DataKey GetSrcKey(DataTable src, DataTable dst) {
if (src.primaryKey != null)
return src.primaryKey.Key;
DataKey key = default(DataKey);
if (dst.primaryKey != null) {
DataColumn[] dstColumns = dst.primaryKey.Key.ColumnsReference;
DataColumn[] srcColumns = new DataColumn[dstColumns.Length];
for (int j = 0; j < dstColumns.Length; j++) {
srcColumns[j] = src.Columns[dstColumns[j].ColumnName];
}
key = new DataKey(srcColumns, false); // DataKey will take ownership of srcColumns
}
return key;
}
}
}
|