private DataRow LoadRow(object[] values, LoadOption loadOption, Index searchIndex) {
            int recordNo;
            DataRow dataRow = null;

            if (searchIndex != null) {
                int[] primaryKeyIndex = new int[0];
                if (this.primaryKey != null) { // I do check above for PK, but in case if someone else gives me some index unrelated to PK
                    primaryKeyIndex = new int[this.primaryKey.ColumnsReference.Length];
                    for(int i = 0; i < this.primaryKey.ColumnsReference.Length; i++) {
                        primaryKeyIndex[i] = this.primaryKey.ColumnsReference[i].Ordinal;
                    }
                }

                object[] keys = new object[primaryKeyIndex.Length];
                for(int i = 0; i < primaryKeyIndex.Length; i++) {
                    keys[i] = values[primaryKeyIndex[i]];
                }

                Range result = searchIndex.FindRecords(keys);

                if (!result.IsNull) {
                    int deletedRowUpsertCount = 0;
                    for(int i = result.Min; i <= result.Max; i++) {
                        int resultRecord = searchIndex.GetRecord(i);
                        dataRow = this.recordManager[resultRecord];
                        recordNo = NewRecordFromArray(values);

                        //SQLBU DT 33648
                        // values array is being reused by DataAdapter, do not modify the values array
                        for(int count = 0; count < values.Length; count++) {
                            if (null == values[count]) {
                                columnCollection[count].Copy(resultRecord, recordNo);
                            }
                        }
                        for(int count = values.Length; count < columnCollection.Count ; count++) {
                            columnCollection[count].Copy(resultRecord, recordNo); // if there are missing values
                        }

                        if (loadOption != LoadOption.Upsert || dataRow.RowState != DataRowState.Deleted) {
                            SetDataRowWithLoadOption(dataRow , recordNo, loadOption, true);
                        }
                        else {
                            deletedRowUpsertCount++;
                        }
                    }
                    if (0 == deletedRowUpsertCount) {
                        return dataRow;
                    }
                }
            }

            recordNo = NewRecordFromArray(values);
            dataRow = NewRow(recordNo);
            // fire rowChanging event here
            DataRowAction action;
            DataRowChangeEventArgs drcevent = null;
            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    action = DataRowAction.ChangeCurrentAndOriginal;
                    break;
                case LoadOption.Upsert:
                    action = DataRowAction.Add;
                    break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }

            drcevent = RaiseRowChanging(null, dataRow, action);

            this.InsertRow (dataRow, -1, -1, false);
            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    this.SetOldRecord(dataRow,  recordNo);
                    break;
                case LoadOption.Upsert:
                    break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }
            RaiseRowChanged(drcevent, dataRow, action);

            return dataRow;
        }
Exemple #2
0
 internal DataRow FindByIndex(Index ndx, object[] key)
 {
     Range range = ndx.FindRecords(key);
     return range.IsNull ? null : _recordManager[ndx.GetRecord(range.Min)];
 }
 internal DataRow FindByIndex(Index ndx, object[] key) {
     Range range = ndx.FindRecords(key);
     if (range.IsNull) {
         return null;
     }
     return this.recordManager[ndx.GetRecord(range.Min)];
 }
        private DataRow LoadRow(object[] values, LoadOption loadOption, Index searchIndex)
        {
            DataRow dataRow = null;
            int num;
            DataRowAction changeCurrentAndOriginal;
            if (searchIndex != null)
            {
                int[] numArray = new int[0];
                if (this.primaryKey != null)
                {
                    numArray = new int[this.primaryKey.ColumnsReference.Length];
                    for (int j = 0; j < this.primaryKey.ColumnsReference.Length; j++)
                    {
                        numArray[j] = this.primaryKey.ColumnsReference[j].Ordinal;
                    }
                }
                object[] key = new object[numArray.Length];
                for (int i = 0; i < numArray.Length; i++)
                {
                    key[i] = values[numArray[i]];
                }
                System.Data.Range range = searchIndex.FindRecords(key);
                if (!range.IsNull)
                {
                    int num8 = 0;
                    for (int k = range.Min; k <= range.Max; k++)
                    {
                        int record = searchIndex.GetRecord(k);
                        dataRow = this.recordManager[record];
                        num = this.NewRecordFromArray(values);
                        for (int m = 0; m < values.Length; m++)
                        {
                            if (values[m] == null)
                            {
                                this.columnCollection[m].Copy(record, num);
                            }
                        }
                        for (int n = values.Length; n < this.columnCollection.Count; n++)
                        {
                            this.columnCollection[n].Copy(record, num);
                        }
                        if ((loadOption != LoadOption.Upsert) || (dataRow.RowState != DataRowState.Deleted))
                        {
                            this.SetDataRowWithLoadOption(dataRow, num, loadOption, true);
                        }
                        else
                        {
                            num8++;
                        }
                    }
                    if (num8 == 0)
                    {
                        return dataRow;
                    }
                }
            }
            num = this.NewRecordFromArray(values);
            dataRow = this.NewRow(num);
            DataRowChangeEventArgs args = null;
            switch (loadOption)
            {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    changeCurrentAndOriginal = DataRowAction.ChangeCurrentAndOriginal;
                    break;

                case LoadOption.Upsert:
                    changeCurrentAndOriginal = DataRowAction.Add;
                    break;

                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }
            args = this.RaiseRowChanging(null, dataRow, changeCurrentAndOriginal);
            this.InsertRow(dataRow, -1L, -1, false);
            switch (loadOption)
            {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    this.SetOldRecord(dataRow, num);
                    break;

                case LoadOption.Upsert:
                    break;

                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }
            this.RaiseRowChanged(args, dataRow, changeCurrentAndOriginal);
            return dataRow;
        }