-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathDbDataAdapter.cs
More file actions
1832 lines (1674 loc) · 81.1 KB
/
Copy pathDbDataAdapter.cs
File metadata and controls
1832 lines (1674 loc) · 81.1 KB
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Collections.Generic;
using System.Data.ProviderBase;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Data.Common
{
public abstract class DbDataAdapter : DataAdapter, IDbDataAdapter, ICloneable
{
public const string DefaultSourceTableName = "Table";
internal static readonly object s_parameterValueNonNullValue = 0;
internal static readonly object s_parameterValueNullValue = 1;
private IDbCommand? _deleteCommand, _insertCommand, _selectCommand, _updateCommand;
private CommandBehavior _fillCommandBehavior;
private struct BatchCommandInfo
{
internal int _commandIdentifier; // whatever AddToBatch returns, so we can reference the command later in GetBatchedParameter
internal int _parameterCount; // number of parameters on the command, so we know how many to loop over when processing output parameters
internal DataRow _row; // the row that the command is intended to update
internal StatementType _statementType; // the statement type of the command, needed for accept changes
internal UpdateRowSource _updatedRowSource; // the UpdatedRowSource value from the command, to know whether we need to look for output parameters or not
internal int? _recordsAffected;
internal Exception? _errors;
}
protected DbDataAdapter() : base()
{
}
protected DbDataAdapter(DbDataAdapter adapter) : base(adapter)
{
CloneFrom(adapter);
}
private IDbDataAdapter _IDbDataAdapter
{
get
{
return this;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
public DbCommand? DeleteCommand
{
get
{
return (DbCommand?)(_IDbDataAdapter.DeleteCommand);
}
set
{
_IDbDataAdapter.DeleteCommand = value;
}
}
IDbCommand? IDbDataAdapter.DeleteCommand
{
get
{
return _deleteCommand;
}
set
{
_deleteCommand = value;
}
}
protected internal CommandBehavior FillCommandBehavior
{
get
{
return (_fillCommandBehavior | CommandBehavior.SequentialAccess);
}
set
{
_fillCommandBehavior = (value | CommandBehavior.SequentialAccess);
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
public DbCommand? InsertCommand
{
get
{
return (DbCommand?)(_IDbDataAdapter.InsertCommand);
}
set
{
_IDbDataAdapter.InsertCommand = value;
}
}
IDbCommand? IDbDataAdapter.InsertCommand
{
get
{
return _insertCommand;
}
set
{
_insertCommand = value;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
public DbCommand? SelectCommand
{
get
{
return (DbCommand?)(_IDbDataAdapter.SelectCommand);
}
set
{
_IDbDataAdapter.SelectCommand = value;
}
}
IDbCommand? IDbDataAdapter.SelectCommand
{
get
{
return _selectCommand;
}
set
{
_selectCommand = value;
}
}
[DefaultValue(1)]
public virtual int UpdateBatchSize
{
get
{
return 1;
}
set
{
if (1 != value)
{
throw ADP.NotSupported();
}
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
public DbCommand? UpdateCommand
{
get
{
return (DbCommand?)(_IDbDataAdapter.UpdateCommand);
}
set
{
_IDbDataAdapter.UpdateCommand = value;
}
}
IDbCommand? IDbDataAdapter.UpdateCommand
{
get
{
return _updateCommand;
}
set
{
_updateCommand = value;
}
}
private System.Data.MissingMappingAction UpdateMappingAction
{
get
{
if (System.Data.MissingMappingAction.Passthrough == MissingMappingAction)
{
return System.Data.MissingMappingAction.Passthrough;
}
return System.Data.MissingMappingAction.Error;
}
}
private System.Data.MissingSchemaAction UpdateSchemaAction
{
get
{
System.Data.MissingSchemaAction action = MissingSchemaAction;
if ((System.Data.MissingSchemaAction.Add == action) || (System.Data.MissingSchemaAction.AddWithKey == action))
{
return System.Data.MissingSchemaAction.Ignore;
}
return System.Data.MissingSchemaAction.Error;
}
}
protected virtual int AddToBatch(IDbCommand command)
{
// Called to add a single command to the batch of commands that need
// to be executed as a batch, when batch updates are requested. It
// must return an identifier that can be used to identify the command
// to GetBatchedParameter later.
throw ADP.NotSupported();
}
protected virtual void ClearBatch()
{
// Called when batch updates are requested to clear out the contents
// of the batch, whether or not it's been executed.
throw ADP.NotSupported();
}
object ICloneable.Clone()
{
#pragma warning disable 618 // ignore obsolete warning about CloneInternals
DbDataAdapter clone = (DbDataAdapter)CloneInternals();
#pragma warning restore 618
clone.CloneFrom(this);
return clone;
}
private void CloneFrom(DbDataAdapter from)
{
IDbDataAdapter pfrom = from._IDbDataAdapter;
_IDbDataAdapter.SelectCommand = CloneCommand(pfrom.SelectCommand);
_IDbDataAdapter.InsertCommand = CloneCommand(pfrom.InsertCommand);
_IDbDataAdapter.UpdateCommand = CloneCommand(pfrom.UpdateCommand);
_IDbDataAdapter.DeleteCommand = CloneCommand(pfrom.DeleteCommand);
}
private static IDbCommand? CloneCommand(IDbCommand? command)
{
return (IDbCommand?)((command is ICloneable) ? ((ICloneable)command).Clone() : null);
}
protected virtual RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand? command, StatementType statementType, DataTableMapping tableMapping)
{
return new RowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
}
protected virtual RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand? command, StatementType statementType, DataTableMapping tableMapping)
{
return new RowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// release mananged objects
IDbDataAdapter pthis = this; // must cast to interface to obtain correct value
pthis.SelectCommand = null;
pthis.InsertCommand = null;
pthis.UpdateCommand = null;
pthis.DeleteCommand = null;
}
// release unmanaged objects
base.Dispose(disposing); // notify base classes
}
protected virtual int ExecuteBatch()
{
// Called to execute the batched update command, returns the number
// of rows affected, just as ExecuteNonQuery would.
throw ADP.NotSupported();
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public DataTable? FillSchema(DataTable dataTable, SchemaType schemaType)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.FillSchema|API> {0}, dataTable, schemaType={1}", ObjectID, schemaType);
try
{
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return FillSchema(dataTable, schemaType, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public override DataTable[] FillSchema(DataSet dataSet, SchemaType schemaType)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.FillSchema|API> {0}, dataSet, schemaType={1}", ObjectID, schemaType);
try
{
IDbCommand? command = _IDbDataAdapter.SelectCommand;
if (DesignMode && ((null == command) || (null == command.Connection) || string.IsNullOrEmpty(command.CommandText)))
{
return Array.Empty<DataTable>(); // design-time support
}
CommandBehavior cmdBehavior = FillCommandBehavior;
return FillSchema(dataSet, schemaType, command!, DbDataAdapter.DefaultSourceTableName, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public DataTable[] FillSchema(DataSet dataSet, SchemaType schemaType, string srcTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.FillSchema|API> {0}, dataSet, schemaType={1}, srcTable={2}", ObjectID, (int)schemaType, srcTable);
try
{
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return FillSchema(dataSet, schemaType, selectCmd!, srcTable, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from command) schema table types cannot be statically analyzed.")]
protected virtual DataTable[] FillSchema(DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.FillSchema|API> {0}, dataSet, schemaType, command, srcTable, behavior={1}", ObjectID, behavior);
try
{
if (null == dataSet)
{
throw ADP.ArgumentNull(nameof(dataSet));
}
if ((SchemaType.Source != schemaType) && (SchemaType.Mapped != schemaType))
{
throw ADP.InvalidSchemaType(schemaType);
}
if (string.IsNullOrEmpty(srcTable))
{
throw ADP.FillSchemaRequiresSourceTableName(nameof(srcTable));
}
if (null == command)
{
throw ADP.MissingSelectCommand(ADP.FillSchema);
}
// Never returns null if dataSet is non-null
return (DataTable[])FillSchemaInternal(dataSet, null, schemaType, command, srcTable, behavior)!;
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from command) schema table types cannot be statically analyzed.")]
protected virtual DataTable? FillSchema(DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.FillSchema|API> {0}, dataTable, schemaType, command, behavior={1}", ObjectID, behavior);
try
{
if (null == dataTable)
{
throw ADP.ArgumentNull(nameof(dataTable));
}
if ((SchemaType.Source != schemaType) && (SchemaType.Mapped != schemaType))
{
throw ADP.InvalidSchemaType(schemaType);
}
if (null == command)
{
throw ADP.MissingSelectCommand(ADP.FillSchema);
}
string srcTableName = dataTable.TableName;
int index = IndexOfDataSetTable(srcTableName);
if (-1 != index)
{
srcTableName = TableMappings[index].SourceTable;
}
return (DataTable?)FillSchemaInternal(null, dataTable, schemaType, command, srcTableName, behavior | CommandBehavior.SingleResult);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from command) schema table types cannot be statically analyzed.")]
private object? FillSchemaInternal(DataSet? dataset, DataTable? datatable, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
{
object? dataTables = null;
bool restoreNullConnection = (null == command.Connection);
try
{
IDbConnection activeConnection = DbDataAdapter.GetConnection3(command, ADP.FillSchema);
ConnectionState originalState = ConnectionState.Open;
try
{
QuietOpen(activeConnection, out originalState);
using (IDataReader dataReader = command.ExecuteReader(behavior | CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo))
{
if (null != datatable)
{ // delegate to next set of protected FillSchema methods
dataTables = FillSchema(datatable, schemaType, dataReader);
}
else
{
dataTables = FillSchema(dataset!, schemaType, srcTable, dataReader);
}
}
}
finally
{
QuietClose(activeConnection, originalState);
}
}
finally
{
if (restoreNullConnection)
{
command.Transaction = null;
command.Connection = null;
}
}
return dataTables;
}
public override int Fill(DataSet dataSet)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataSet", ObjectID);
try
{
// delegate to Fill4
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return Fill(dataSet, 0, 0, DbDataAdapter.DefaultSourceTableName, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
public int Fill(DataSet dataSet, string srcTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataSet, srcTable='{1}'", ObjectID, srcTable);
try
{
// delegate to Fill4
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return Fill(dataSet, 0, 0, srcTable, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
public int Fill(DataSet dataSet, int startRecord, int maxRecords, string srcTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataSet, startRecord={1}, maxRecords={2}, srcTable='{3}'", ObjectID, startRecord, maxRecords, srcTable);
try
{
// delegate to Fill4
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return Fill(dataSet, startRecord, maxRecords, srcTable, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
protected virtual int Fill(DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataSet, startRecord, maxRecords, srcTable, command, behavior={1}", ObjectID, behavior);
try
{
if (null == dataSet)
{
throw ADP.FillRequires(nameof(dataSet));
}
if (startRecord < 0)
{
throw ADP.InvalidStartRecord(nameof(startRecord), startRecord);
}
if (maxRecords < 0)
{
throw ADP.InvalidMaxRecords(nameof(maxRecords), maxRecords);
}
if (string.IsNullOrEmpty(srcTable))
{
throw ADP.FillRequiresSourceTableName(nameof(srcTable));
}
if (null == command)
{
throw ADP.MissingSelectCommand(ADP.Fill);
}
return FillInternal(dataSet, null, startRecord, maxRecords, srcTable, command, behavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
public int Fill(DataTable dataTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataTable", ObjectID);
try
{
// delegate to Fill8
DataTable[] dataTables = new DataTable[1] { dataTable };
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return Fill(dataTables, 0, 0, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
public int Fill(int startRecord, int maxRecords, params DataTable[] dataTables)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, startRecord={1}, maxRecords={2}, dataTable[]", ObjectID, startRecord, maxRecords);
try
{
// delegate to Fill8
IDbCommand? selectCmd = _IDbDataAdapter.SelectCommand;
CommandBehavior cmdBehavior = FillCommandBehavior;
return Fill(dataTables, startRecord, maxRecords, selectCmd!, cmdBehavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
protected virtual int Fill(DataTable dataTable, IDbCommand command, CommandBehavior behavior)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataTable, command, behavior={1}", ObjectID, behavior);
try
{
// delegate to Fill8
DataTable[] dataTables = new DataTable[1] { dataTable };
return Fill(dataTables, 0, 0, command, behavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
protected virtual int Fill(DataTable[] dataTables, int startRecord, int maxRecords, IDbCommand command, CommandBehavior behavior)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Fill|API> {0}, dataTables[], startRecord, maxRecords, command, behavior={1}", ObjectID, behavior);
try
{
if ((null == dataTables) || (0 == dataTables.Length) || (null == dataTables[0]))
{
throw ADP.FillRequires("dataTable");
}
if (startRecord < 0)
{
throw ADP.InvalidStartRecord(nameof(startRecord), startRecord);
}
if (maxRecords < 0)
{
throw ADP.InvalidMaxRecords(nameof(maxRecords), maxRecords);
}
if ((1 < dataTables.Length) && ((0 != startRecord) || (0 != maxRecords)))
{
throw ADP.OnlyOneTableForStartRecordOrMaxRecords();
}
if (null == command)
{
throw ADP.MissingSelectCommand(ADP.Fill);
}
if (1 == dataTables.Length)
{
behavior |= CommandBehavior.SingleResult;
}
return FillInternal(null, dataTables, startRecord, maxRecords, null, command, behavior);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
private int FillInternal(DataSet? dataset, DataTable[]? datatables, int startRecord, int maxRecords, string? srcTable, IDbCommand command, CommandBehavior behavior)
{
int rowsAddedToDataSet = 0;
bool restoreNullConnection = (null == command.Connection);
try
{
IDbConnection activeConnection = DbDataAdapter.GetConnection3(command, ADP.Fill);
ConnectionState originalState = ConnectionState.Open;
// the default is MissingSchemaAction.Add, the user must explicitly
// set MisingSchemaAction.AddWithKey to get key information back in the dataset
if (Data.MissingSchemaAction.AddWithKey == MissingSchemaAction)
{
behavior |= CommandBehavior.KeyInfo;
}
try
{
QuietOpen(activeConnection, out originalState);
behavior |= CommandBehavior.SequentialAccess;
IDataReader? dataReader = null;
try
{
dataReader = command.ExecuteReader(behavior);
if (null != datatables)
{ // delegate to next set of protected Fill methods
rowsAddedToDataSet = Fill(datatables, dataReader, startRecord, maxRecords);
}
else
{
rowsAddedToDataSet = Fill(dataset!, srcTable!, dataReader, startRecord, maxRecords);
}
}
finally
{
dataReader?.Dispose();
}
}
finally
{
QuietClose(activeConnection, originalState);
}
}
finally
{
if (restoreNullConnection)
{
command.Transaction = null;
command.Connection = null;
}
}
return rowsAddedToDataSet;
}
protected virtual IDataParameter GetBatchedParameter(int commandIdentifier, int parameterIndex)
{
// Called to retrieve a parameter from a specific bached command, the
// first argument is the value that was returned by AddToBatch when it
// was called for the command.
throw ADP.NotSupported();
}
protected virtual bool GetBatchedRecordsAffected(int commandIdentifier, out int recordsAffected, out Exception? error)
{
// Called to retrieve the records affected from a specific batched command,
// first argument is the value that was returned by AddToBatch when it
// was called for the command.
// default implementation always returns 1, derived classes override for otherwise
// otherwise DbConcurrencyException will only be thrown if sum of all records in batch is 0
// return 0 to cause Update to throw DbConcurrencyException
recordsAffected = 1;
error = null;
return true;
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public override IDataParameter[] GetFillParameters()
{
IDataParameter[]? value = null;
IDbCommand? select = _IDbDataAdapter.SelectCommand;
if (null != select)
{
IDataParameterCollection parameters = select.Parameters;
if (null != parameters)
{
value = new IDataParameter[parameters.Count];
parameters.CopyTo(value, 0);
}
}
if (null == value)
{
value = Array.Empty<IDataParameter>();
}
return value;
}
internal DataTableMapping GetTableMapping(DataTable dataTable)
{
DataTableMapping? tableMapping = null;
int index = IndexOfDataSetTable(dataTable.TableName);
if (-1 != index)
{
tableMapping = TableMappings[index];
}
if (null == tableMapping)
{
if (System.Data.MissingMappingAction.Error == MissingMappingAction)
{
throw ADP.MissingTableMappingDestination(dataTable.TableName);
}
tableMapping = new DataTableMapping(dataTable.TableName, dataTable.TableName);
}
return tableMapping;
}
protected virtual void InitializeBatching()
{
// Called when batch updates are requested to prepare for processing
// of a batch of commands.
throw ADP.NotSupported();
}
protected virtual void OnRowUpdated(RowUpdatedEventArgs value)
{
}
protected virtual void OnRowUpdating(RowUpdatingEventArgs value)
{
}
private void ParameterInput(IDataParameterCollection parameters, StatementType typeIndex, DataRow row, DataTableMapping mappings)
{
Data.MissingMappingAction missingMapping = UpdateMappingAction;
Data.MissingSchemaAction missingSchema = UpdateSchemaAction;
foreach (IDataParameter parameter in parameters)
{
if ((null != parameter) && (0 != (ParameterDirection.Input & parameter.Direction)))
{
string columnName = parameter.SourceColumn;
if (!string.IsNullOrEmpty(columnName))
{
DataColumn? dataColumn = mappings.GetDataColumn(columnName, null, row.Table, missingMapping, missingSchema);
if (null != dataColumn)
{
DataRowVersion version = DbDataAdapter.GetParameterSourceVersion(typeIndex, parameter);
parameter.Value = row[dataColumn, version];
}
else
{
parameter.Value = null;
}
DbParameter? dbparameter = (parameter as DbParameter);
if ((null != dbparameter) && dbparameter.SourceColumnNullMapping)
{
Debug.Assert(DbType.Int32 == parameter.DbType, "unexpected DbType");
parameter.Value = ADP.IsNull(parameter.Value) ? s_parameterValueNullValue : s_parameterValueNonNullValue;
}
}
}
}
}
private static void ParameterOutput(IDataParameter parameter, DataRow row, DataTableMapping mappings, MissingMappingAction missingMapping, MissingSchemaAction missingSchema)
{
if (0 != (ParameterDirection.Output & parameter.Direction))
{
object? value = parameter.Value;
if (null != value)
{
// null means default, meaning we leave the current DataRow value alone
string columnName = parameter.SourceColumn;
if (!string.IsNullOrEmpty(columnName))
{
DataColumn? dataColumn = mappings.GetDataColumn(columnName, null, row.Table, missingMapping, missingSchema);
if (null != dataColumn)
{
if (dataColumn.ReadOnly)
{
try
{
dataColumn.ReadOnly = false;
row[dataColumn] = value;
}
finally
{
dataColumn.ReadOnly = true;
}
}
else
{
row[dataColumn] = value;
}
}
}
}
}
}
private void ParameterOutput(IDataParameterCollection parameters, DataRow row, DataTableMapping mappings)
{
Data.MissingMappingAction missingMapping = UpdateMappingAction;
Data.MissingSchemaAction missingSchema = UpdateSchemaAction;
foreach (IDataParameter parameter in parameters)
{
if (null != parameter)
{
ParameterOutput(parameter, row, mappings, missingMapping, missingSchema);
}
}
}
protected virtual void TerminateBatching()
{
// Called when batch updates are requested to cleanup after a batch
// update has been completed.
throw ADP.NotSupported();
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public override int Update(DataSet dataSet)
{
return Update(dataSet, DbDataAdapter.DefaultSourceTableName);
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public int Update(DataRow[] dataRows)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Update|API> {0}, dataRows[]", ObjectID);
try
{
int rowsAffected = 0;
if (null == dataRows)
{
throw ADP.ArgumentNull(nameof(dataRows));
}
else if (0 != dataRows.Length)
{
DataTable? dataTable = null;
for (int i = 0; i < dataRows.Length; ++i)
{
if ((null != dataRows[i]) && (dataTable != dataRows[i].Table))
{
if (null != dataTable)
{
throw ADP.UpdateMismatchRowTable(i);
}
dataTable = dataRows[i].Table;
}
}
if (null != dataTable)
{
DataTableMapping tableMapping = GetTableMapping(dataTable);
rowsAffected = Update(dataRows, tableMapping);
}
}
return rowsAffected;
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public int Update(DataTable dataTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Update|API> {0}, dataTable", ObjectID);
try
{
if (null == dataTable)
{
throw ADP.UpdateRequiresDataTable(nameof(dataTable));
}
DataTableMapping? tableMapping = null;
int index = IndexOfDataSetTable(dataTable.TableName);
if (-1 != index)
{
tableMapping = TableMappings[index];
}
if (null == tableMapping)
{
if (System.Data.MissingMappingAction.Error == MissingMappingAction)
{
throw ADP.MissingTableMappingDestination(dataTable.TableName);
}
tableMapping = new DataTableMapping(DbDataAdapter.DefaultSourceTableName, dataTable.TableName);
}
return UpdateFromDataTable(dataTable, tableMapping);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
public int Update(DataSet dataSet, string srcTable)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Update|API> {0}, dataSet, srcTable='{1}'", ObjectID, srcTable);
try
{
if (null == dataSet)
{
throw ADP.UpdateRequiresNonNullDataSet(nameof(dataSet));
}
if (string.IsNullOrEmpty(srcTable))
{
throw ADP.UpdateRequiresSourceTableName(nameof(srcTable));
}
int rowsAffected = 0;
DataTableMapping? tableMapping = GetTableMappingBySchemaAction(srcTable, srcTable, UpdateMappingAction);
Debug.Assert(null != tableMapping, "null TableMapping when MissingMappingAction.Error");
// the ad-hoc scenario of no dataTable just returns
// ad-hoc scenario is defined as MissingSchemaAction.Add or MissingSchemaAction.Ignore
System.Data.MissingSchemaAction schemaAction = UpdateSchemaAction;
DataTable? dataTable = tableMapping.GetDataTableBySchemaAction(dataSet, schemaAction);
if (null != dataTable)
{
rowsAffected = UpdateFromDataTable(dataTable, tableMapping);
}
else if (!HasTableMappings() || (-1 == TableMappings.IndexOf(tableMapping)))
{
//throw error since the user didn't explicitly map this tableName to Ignore.
throw ADP.UpdateRequiresSourceTable(srcTable);
}
return rowsAffected;
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
[RequiresUnreferencedCode("IDataReader's (built from adapter commands) schema table types cannot be statically analyzed.")]
protected virtual int Update(DataRow[] dataRows, DataTableMapping tableMapping)
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<comm.DbDataAdapter.Update|API> {0}, dataRows[], tableMapping", ObjectID);
try
{
Debug.Assert((null != dataRows) && (0 < dataRows.Length), "Update: bad dataRows");
Debug.Assert(null != tableMapping, "Update: bad DataTableMapping");
// If records were affected, increment row count by one - that is number of rows affected in dataset.
int cumulativeDataRowsAffected = 0;
IDbConnection?[] connections = new IDbConnection[5]; // one for each statementtype
ConnectionState[] connectionStates = new ConnectionState[5]; // closed by default (== 0)
bool useSelectConnectionState = false;
IDbCommand? tmpcmd = _IDbDataAdapter.SelectCommand;
if (null != tmpcmd)
{
connections[0] = tmpcmd.Connection;
if (null != connections[0])
{
connectionStates[0] = connections[0]!.State;
useSelectConnectionState = true;
}
}
int maxBatchCommands = Math.Min(UpdateBatchSize, dataRows.Length);
if (maxBatchCommands < 1)
{ // batch size of zero indicates one batch, no matter how large...
maxBatchCommands = dataRows.Length;
}
BatchCommandInfo[] batchCommands = new BatchCommandInfo[maxBatchCommands];
DataRow[] rowBatch = new DataRow[maxBatchCommands];
int commandCount = 0;
// the outer try/finally is for closing any connections we may have opened
try
{