Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DbCommandBuilder.cs
Go to the documentation of this file.
4using System.Text;
6
7namespace System.Data.Common;
8
9public abstract class DbCommandBuilder : Component
10{
11 private sealed class ParameterNames
12 {
13 private string _originalPrefix;
14
15 private string _isNullPrefix;
16
17 private readonly Regex _parameterNameParser;
18
20
21 private readonly string[] _baseParameterNames;
22
23 private readonly string[] _originalParameterNames;
24
25 private readonly string[] _nullParameterNames;
26
27 private readonly bool[] _isMutatedName;
28
29 private readonly int _count;
30
32
33 private readonly int _adjustedParameterNameMaxLength;
34
35 internal ParameterNames(DbCommandBuilder dbCommandBuilder, DbSchemaRow[] schemaRows)
36 {
37 _dbCommandBuilder = dbCommandBuilder;
38 _baseParameterNames = new string[schemaRows.Length];
39 _originalParameterNames = new string[schemaRows.Length];
40 _nullParameterNames = new string[schemaRows.Length];
41 _isMutatedName = new bool[schemaRows.Length];
42 _count = schemaRows.Length;
46 for (int i = 0; i < schemaRows.Length; i++)
47 {
48 DbSchemaRow dbSchemaRow = schemaRows[i];
49 if (dbSchemaRow == null)
50 {
51 continue;
52 }
53 bool flag = false;
54 string text = dbSchemaRow.ColumnName;
55 if ((_originalPrefix == null || !text.StartsWith(_originalPrefix, StringComparison.OrdinalIgnoreCase)) && (_isNullPrefix == null || !text.StartsWith(_isNullPrefix, StringComparison.OrdinalIgnoreCase)))
56 {
57 if (text.Contains(' '))
58 {
59 text = text.Replace(' ', '_');
60 flag = true;
61 }
63 {
65 _isMutatedName[i] = flag;
66 }
67 }
68 }
70 for (int j = 0; j < schemaRows.Length; j++)
71 {
72 if (_baseParameterNames[j] != null)
73 {
74 if (_originalPrefix != null)
75 {
77 }
78 if (_isNullPrefix != null && schemaRows[j].AllowDBNull)
79 {
81 }
82 }
83 }
85 GenerateMissingNames(schemaRows);
86 }
87
89 {
90 if (_parameterNameParser.IsMatch("IsNull_"))
91 {
92 _isNullPrefix = "IsNull_";
93 }
94 else if (_parameterNameParser.IsMatch("isnull"))
95 {
96 _isNullPrefix = "isnull";
97 }
98 else if (_parameterNameParser.IsMatch("ISNULL"))
99 {
100 _isNullPrefix = "ISNULL";
101 }
102 else
103 {
104 _isNullPrefix = null;
105 }
106 if (_parameterNameParser.IsMatch("Original_"))
107 {
108 _originalPrefix = "Original_";
109 }
110 else if (_parameterNameParser.IsMatch("original"))
111 {
112 _originalPrefix = "original";
113 }
114 else if (_parameterNameParser.IsMatch("ORIGINAL"))
115 {
116 _originalPrefix = "ORIGINAL";
117 }
118 else
119 {
120 _originalPrefix = null;
121 }
122 }
123
125 {
126 for (int i = 0; i < _baseParameterNames.Length; i++)
127 {
128 string text = _baseParameterNames[i];
129 if (text != null)
130 {
132 }
133 string text2 = _originalParameterNames[i];
134 if (text2 != null)
135 {
137 }
138 string text3 = _nullParameterNames[i];
139 if (text3 != null)
140 {
142 }
143 }
144 }
145
147 {
148 for (int i = 0; i < _count - 1; i++)
149 {
150 string text = _baseParameterNames[i];
151 if (text == null)
152 {
153 continue;
154 }
155 for (int j = i + 1; j < _count; j++)
156 {
158 {
159 int num = (_isMutatedName[j] ? j : i);
160 _baseParameterNames[num] = null;
161 }
162 }
163 }
164 }
165
166 internal void GenerateMissingNames(DbSchemaRow[] schemaRows)
167 {
168 for (int i = 0; i < _baseParameterNames.Length; i++)
169 {
170 string text = _baseParameterNames[i];
171 if (text == null)
172 {
175 DbSchemaRow dbSchemaRow = schemaRows[i];
176 if (dbSchemaRow != null && dbSchemaRow.AllowDBNull)
177 {
179 }
180 }
181 }
182 }
183
185 {
186 int num = Math.Max((_isNullPrefix != null) ? _isNullPrefix.Length : 0, (_originalPrefix != null) ? _originalPrefix.Length : 0) + _dbCommandBuilder.GetParameterName("").Length;
187 return _dbCommandBuilder.ParameterNameMaxLength - num;
188 }
189
191 {
192 bool flag;
193 string parameterName;
194 do
195 {
196 flag = false;
199 for (int i = 0; i < _baseParameterNames.Length; i++)
200 {
202 {
203 flag = true;
204 break;
205 }
206 }
207 }
208 while (flag);
209 return parameterName;
210 }
211
212 internal string GetBaseParameterName(int index)
213 {
215 }
216
217 internal string GetOriginalParameterName(int index)
218 {
220 }
221
222 internal string GetNullParameterName(int index)
223 {
225 }
226 }
227
229
231
233
235
237
238 private ConflictOption _conflictDetection = ConflictOption.CompareAllSearchableValues;
239
240 private bool _setAllValues;
241
243
245
247
248 private string[] _sourceColumnNames;
249
251
252 private string _quotedBaseTableName;
253
255
256 private string _catalogSeparator = ".";
257
258 private string _schemaSeparator = ".";
259
260 private string _quotePrefix = string.Empty;
261
262 private string _quoteSuffix = string.Empty;
263
264 private string _parameterNamePattern;
265
267
269
270 [DefaultValue(ConflictOption.CompareAllSearchableValues)]
272 {
273 get
274 {
275 return _conflictDetection;
276 }
277 set
278 {
279 if ((uint)(value - 1) <= 2u)
280 {
282 return;
283 }
285 }
286 }
287
288 [DefaultValue(CatalogLocation.Start)]
290 {
291 get
292 {
293 return _catalogLocation;
294 }
295 set
296 {
297 if (_dbSchemaTable != null)
298 {
299 throw ADP.NoQuoteChange();
300 }
301 if ((uint)(value - 1) <= 1u)
302 {
304 return;
305 }
307 }
308 }
309
310 [DefaultValue(".")]
311 public virtual string CatalogSeparator
312 {
313 get
314 {
315 string catalogSeparator = _catalogSeparator;
316 if (catalogSeparator == null || 0 >= catalogSeparator.Length)
317 {
318 return ".";
319 }
320 return catalogSeparator;
321 }
322 [param: AllowNull]
323 set
324 {
325 if (_dbSchemaTable != null)
326 {
327 throw ADP.NoQuoteChange();
328 }
330 }
331 }
332
333 [Browsable(false)]
336 {
337 get
338 {
339 return _dataAdapter;
340 }
341 set
342 {
343 if (_dataAdapter != value)
344 {
346 if (_dataAdapter != null)
347 {
349 _dataAdapter = null;
350 }
351 if (value != null)
352 {
355 }
356 }
357 }
358 }
359
361
363
365
366 [DefaultValue("")]
367 public virtual string QuotePrefix
368 {
369 get
370 {
371 return _quotePrefix ?? string.Empty;
372 }
373 [param: AllowNull]
374 set
375 {
376 if (_dbSchemaTable != null)
377 {
378 throw ADP.NoQuoteChange();
379 }
381 }
382 }
383
384 [DefaultValue("")]
385 public virtual string QuoteSuffix
386 {
387 get
388 {
389 string quoteSuffix = _quoteSuffix;
390 if (quoteSuffix == null)
391 {
392 return string.Empty;
393 }
394 return quoteSuffix;
395 }
396 [param: AllowNull]
397 set
398 {
399 if (_dbSchemaTable != null)
400 {
401 throw ADP.NoQuoteChange();
402 }
404 }
405 }
406
407 [DefaultValue(".")]
408 public virtual string SchemaSeparator
409 {
410 get
411 {
412 string schemaSeparator = _schemaSeparator;
413 if (schemaSeparator == null || 0 >= schemaSeparator.Length)
414 {
415 return ".";
416 }
417 return schemaSeparator;
418 }
419 [param: AllowNull]
420 set
421 {
422 if (_dbSchemaTable != null)
423 {
424 throw ADP.NoQuoteChange();
425 }
427 }
428 }
429
430 [DefaultValue(false)]
431 public bool SetAllValues
432 {
433 get
434 {
435 return _setAllValues;
436 }
437 set
438 {
440 }
441 }
442
444 {
445 get
446 {
447 return _insertCommand;
448 }
449 set
450 {
452 }
453 }
454
456 {
457 get
458 {
459 return _updateCommand;
460 }
461 set
462 {
464 }
465 }
466
468 {
469 get
470 {
471 return _deleteCommand;
472 }
473 set
474 {
476 }
477 }
478
479 private void BuildCache(bool closeConnection, DataRow dataRow, bool useColumnsForParameterNames)
480 {
481 if (_dbSchemaTable != null && (!useColumnsForParameterNames || _parameterNames != null))
482 {
483 return;
484 }
485 DataTable dataTable = null;
486 DbCommand selectCommand = GetSelectCommand();
487 DbConnection connection = selectCommand.Connection;
488 if (connection == null)
489 {
491 }
492 try
493 {
494 if ((ConnectionState.Open & connection.State) == 0)
495 {
496 connection.Open();
497 }
498 else
499 {
500 closeConnection = false;
501 }
502 if (useColumnsForParameterNames)
503 {
505 if (schema.Rows.Count == 1)
506 {
510 _parameterNameMaxLength = ((obj is int) ? ((int)obj) : 0);
512 {
513 useColumnsForParameterNames = false;
514 }
515 }
516 else
517 {
518 useColumnsForParameterNames = false;
519 }
520 }
521 dataTable = GetSchemaTable(selectCommand);
522 }
523 finally
524 {
525 if (closeConnection)
526 {
527 connection.Close();
528 }
529 }
530 if (dataTable == null)
531 {
533 }
534 BuildInformation(dataTable);
535 _dbSchemaTable = dataTable;
536 DbSchemaRow[] dbSchemaRows = _dbSchemaRows;
537 string[] array = new string[dbSchemaRows.Length];
538 for (int i = 0; i < dbSchemaRows.Length; i++)
539 {
540 DbSchemaRow dbSchemaRow = dbSchemaRows[i];
541 if (dbSchemaRow != null)
542 {
543 array[i] = dbSchemaRow.ColumnName;
544 }
545 }
547 if (useColumnsForParameterNames)
548 {
549 _parameterNames = new ParameterNames(this, dbSchemaRows);
550 }
552 }
553
554 protected virtual DataTable? GetSchemaTable(DbCommand sourceCommand)
555 {
556 using IDataReader dataReader = sourceCommand.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
557 return dataReader.GetSchemaTable();
558 }
559
560 private void BuildInformation(DataTable schemaTable)
561 {
562 DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(schemaTable, returnProviderSpecificTypes: false);
563 if (sortedSchemaRows == null || sortedSchemaRows.Length == 0)
564 {
566 }
567 string text = string.Empty;
568 string text2 = string.Empty;
569 string text3 = string.Empty;
570 string text4 = null;
571 for (int i = 0; i < sortedSchemaRows.Length; i++)
572 {
573 DbSchemaRow dbSchemaRow = sortedSchemaRows[i];
574 string baseTableName = dbSchemaRow.BaseTableName;
575 if (baseTableName == null || baseTableName.Length == 0)
576 {
577 sortedSchemaRows[i] = null;
578 continue;
579 }
580 string text5 = dbSchemaRow.BaseServerName;
581 string text6 = dbSchemaRow.BaseCatalogName;
582 string text7 = dbSchemaRow.BaseSchemaName;
583 if (text5 == null)
584 {
585 text5 = string.Empty;
586 }
587 if (text6 == null)
588 {
589 text6 = string.Empty;
590 }
591 if (text7 == null)
592 {
593 text7 = string.Empty;
594 }
595 if (text4 == null)
596 {
597 text = text5;
598 text2 = text6;
599 text3 = text7;
600 text4 = baseTableName;
601 }
602 else if (ADP.SrcCompare(text4, baseTableName) != 0 || ADP.SrcCompare(text3, text7) != 0 || ADP.SrcCompare(text2, text6) != 0 || ADP.SrcCompare(text, text5) != 0)
603 {
605 }
606 }
607 if (text.Length == 0)
608 {
609 text = null;
610 }
611 if (text2.Length == 0)
612 {
613 text = null;
614 text2 = null;
615 }
616 if (text3.Length == 0)
617 {
618 text = null;
619 text2 = null;
620 text3 = null;
621 }
622 if (text4 == null || text4.Length == 0)
623 {
625 }
626 CatalogLocation catalogLocation = CatalogLocation;
627 string catalogSeparator = CatalogSeparator;
628 string schemaSeparator = SchemaSeparator;
629 string quotePrefix = QuotePrefix;
630 string quoteSuffix = QuoteSuffix;
631 if (!string.IsNullOrEmpty(quotePrefix) && -1 != text4.IndexOf(quotePrefix, StringComparison.Ordinal))
632 {
633 throw ADP.DynamicSQLNestedQuote(text4, quotePrefix);
634 }
635 if (!string.IsNullOrEmpty(quoteSuffix) && -1 != text4.IndexOf(quoteSuffix, StringComparison.Ordinal))
636 {
637 throw ADP.DynamicSQLNestedQuote(text4, quoteSuffix);
638 }
639 StringBuilder stringBuilder = new StringBuilder();
640 if (CatalogLocation.Start == catalogLocation)
641 {
642 if (text != null)
643 {
644 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text));
645 stringBuilder.Append(catalogSeparator);
646 }
647 if (text2 != null)
648 {
649 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text2));
650 stringBuilder.Append(catalogSeparator);
651 }
652 }
653 if (text3 != null)
654 {
655 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text3));
656 stringBuilder.Append(schemaSeparator);
657 }
658 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text4));
659 if (CatalogLocation.End == catalogLocation)
660 {
661 if (text != null)
662 {
663 stringBuilder.Append(catalogSeparator);
664 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text));
665 }
666 if (text2 != null)
667 {
668 stringBuilder.Append(catalogSeparator);
669 stringBuilder.Append(ADP.BuildQuotedString(quotePrefix, quoteSuffix, text2));
670 }
671 }
672 _quotedBaseTableName = stringBuilder.ToString();
673 _hasPartialPrimaryKey = false;
674 DbSchemaRow[] array = sortedSchemaRows;
675 foreach (DbSchemaRow dbSchemaRow2 in array)
676 {
677 if (dbSchemaRow2 != null && (dbSchemaRow2.IsKey || dbSchemaRow2.IsUnique) && !dbSchemaRow2.IsLong && !dbSchemaRow2.IsRowVersion && dbSchemaRow2.IsHidden)
678 {
680 break;
681 }
682 }
683 _dbSchemaRows = sortedSchemaRows;
684 }
685
687 {
689 StringBuilder stringBuilder = new StringBuilder();
690 int parameterCount = 0;
691 stringBuilder.Append("DELETE FROM ");
692 stringBuilder.Append(QuotedBaseTableName);
693 parameterCount = BuildWhereClause(mappings, dataRow, stringBuilder, dbCommand, parameterCount, isUpdate: false);
694 dbCommand.CommandText = stringBuilder.ToString();
695 RemoveExtraParameters(dbCommand, parameterCount);
696 DeleteCommand = dbCommand;
697 return dbCommand;
698 }
699
701 {
703 StringBuilder stringBuilder = new StringBuilder();
704 int num = 0;
705 string value = " (";
706 stringBuilder.Append("INSERT INTO ");
707 stringBuilder.Append(QuotedBaseTableName);
708 DbSchemaRow[] dbSchemaRows = _dbSchemaRows;
709 string[] array = new string[dbSchemaRows.Length];
710 for (int i = 0; i < dbSchemaRows.Length; i++)
711 {
712 DbSchemaRow dbSchemaRow = dbSchemaRows[i];
713 if (dbSchemaRow == null || dbSchemaRow.BaseColumnName.Length == 0 || !IncludeInInsertValues(dbSchemaRow))
714 {
715 continue;
716 }
717 object obj = null;
718 string text = _sourceColumnNames[i];
719 if (mappings != null && dataRow != null)
720 {
721 DataColumn dataColumn = GetDataColumn(text, mappings, dataRow);
722 if (dataColumn == null || (dbSchemaRow.IsReadOnly && dataColumn.ReadOnly))
723 {
724 continue;
725 }
726 obj = GetColumnValue(dataRow, dataColumn, DataRowVersion.Current);
727 if (!dbSchemaRow.AllowDBNull && (obj == null || Convert.IsDBNull(obj)))
728 {
729 continue;
730 }
731 }
732 stringBuilder.Append(value);
733 value = ", ";
734 stringBuilder.Append(QuotedColumn(dbSchemaRow.BaseColumnName));
735 array[num] = CreateParameterForValue(dbCommand, GetBaseParameterName(i), text, DataRowVersion.Current, num, obj, dbSchemaRow, StatementType.Insert, whereClause: false);
736 num++;
737 }
738 if (num == 0)
739 {
740 stringBuilder.Append(" DEFAULT VALUES");
741 }
742 else
743 {
744 stringBuilder.Append(')');
745 stringBuilder.Append(" VALUES ");
746 stringBuilder.Append('(');
747 stringBuilder.Append(array[0]);
748 for (int j = 1; j < num; j++)
749 {
750 stringBuilder.Append(", ");
751 stringBuilder.Append(array[j]);
752 }
753 stringBuilder.Append(')');
754 }
755 dbCommand.CommandText = stringBuilder.ToString();
756 RemoveExtraParameters(dbCommand, num);
757 InsertCommand = dbCommand;
758 return dbCommand;
759 }
760
762 {
764 StringBuilder stringBuilder = new StringBuilder();
765 string value = " SET ";
766 int num = 0;
767 stringBuilder.Append("UPDATE ");
768 stringBuilder.Append(QuotedBaseTableName);
769 DbSchemaRow[] dbSchemaRows = _dbSchemaRows;
770 for (int i = 0; i < dbSchemaRows.Length; i++)
771 {
772 DbSchemaRow dbSchemaRow = dbSchemaRows[i];
773 if (dbSchemaRow == null || dbSchemaRow.BaseColumnName.Length == 0 || !IncludeInUpdateSet(dbSchemaRow))
774 {
775 continue;
776 }
777 object obj = null;
778 string text = _sourceColumnNames[i];
779 if (mappings != null && dataRow != null)
780 {
781 DataColumn dataColumn = GetDataColumn(text, mappings, dataRow);
782 if (dataColumn == null || (dbSchemaRow.IsReadOnly && dataColumn.ReadOnly))
783 {
784 continue;
785 }
786 obj = GetColumnValue(dataRow, dataColumn, DataRowVersion.Current);
787 if (!SetAllValues)
788 {
789 object columnValue = GetColumnValue(dataRow, dataColumn, DataRowVersion.Original);
790 if (columnValue == obj || (columnValue != null && columnValue.Equals(obj)))
791 {
792 continue;
793 }
794 }
795 }
796 stringBuilder.Append(value);
797 value = ", ";
798 stringBuilder.Append(QuotedColumn(dbSchemaRow.BaseColumnName));
799 stringBuilder.Append(" = ");
800 stringBuilder.Append(CreateParameterForValue(dbCommand, GetBaseParameterName(i), text, DataRowVersion.Current, num, obj, dbSchemaRow, StatementType.Update, whereClause: false));
801 num++;
802 }
803 bool flag = num == 0;
804 num = BuildWhereClause(mappings, dataRow, stringBuilder, dbCommand, num, isUpdate: true);
805 dbCommand.CommandText = stringBuilder.ToString();
806 RemoveExtraParameters(dbCommand, num);
807 UpdateCommand = dbCommand;
808 if (!flag)
809 {
810 return dbCommand;
811 }
812 return null;
813 }
814
815 private int BuildWhereClause(DataTableMapping mappings, DataRow dataRow, StringBuilder builder, DbCommand command, int parameterCount, bool isUpdate)
816 {
817 string value = string.Empty;
818 int num = 0;
819 builder.Append(" WHERE ");
820 builder.Append('(');
821 DbSchemaRow[] dbSchemaRows = _dbSchemaRows;
822 for (int i = 0; i < dbSchemaRows.Length; i++)
823 {
824 DbSchemaRow dbSchemaRow = dbSchemaRows[i];
825 if (dbSchemaRow != null && dbSchemaRow.BaseColumnName.Length != 0 && IncludeInWhereClause(dbSchemaRow, isUpdate))
826 {
827 builder.Append(value);
828 value = " AND ";
829 object value2 = null;
830 string text = _sourceColumnNames[i];
831 string value3 = QuotedColumn(dbSchemaRow.BaseColumnName);
832 if (mappings != null && dataRow != null)
833 {
834 value2 = GetColumnValue(dataRow, text, mappings, DataRowVersion.Original);
835 }
836 if (!dbSchemaRow.AllowDBNull)
837 {
838 builder.Append('(');
839 builder.Append(value3);
840 builder.Append(" = ");
841 builder.Append(CreateParameterForValue(command, GetOriginalParameterName(i), text, DataRowVersion.Original, parameterCount, value2, dbSchemaRow, isUpdate ? StatementType.Update : StatementType.Delete, whereClause: true));
842 parameterCount++;
843 builder.Append(')');
844 }
845 else
846 {
847 builder.Append('(');
848 builder.Append('(');
849 builder.Append(CreateParameterForNullTest(command, GetNullParameterName(i), text, DataRowVersion.Original, parameterCount, value2, dbSchemaRow, isUpdate ? StatementType.Update : StatementType.Delete, whereClause: true));
850 parameterCount++;
851 builder.Append(" = 1");
852 builder.Append(" AND ");
853 builder.Append(value3);
854 builder.Append(" IS NULL");
855 builder.Append(')');
856 builder.Append(" OR ");
857 builder.Append('(');
858 builder.Append(value3);
859 builder.Append(" = ");
860 builder.Append(CreateParameterForValue(command, GetOriginalParameterName(i), text, DataRowVersion.Original, parameterCount, value2, dbSchemaRow, isUpdate ? StatementType.Update : StatementType.Delete, whereClause: true));
861 parameterCount++;
862 builder.Append(')');
863 builder.Append(')');
864 }
865 if (IncrementWhereCount(dbSchemaRow))
866 {
867 num++;
868 }
869 }
870 }
871 builder.Append(')');
872 if (num == 0)
873 {
874 if (isUpdate)
875 {
876 if (ConflictOption.CompareRowVersion == ConflictOption)
877 {
879 }
881 }
882 if (ConflictOption.CompareRowVersion == ConflictOption)
883 {
885 }
887 }
888 return parameterCount;
889 }
890
891 private string CreateParameterForNullTest(DbCommand command, string parameterName, string sourceColumn, DataRowVersion version, int parameterCount, object value, DbSchemaRow row, StatementType statementType, bool whereClause)
892 {
893 DbParameter nextParameter = GetNextParameter(command, parameterCount);
894 if (parameterName == null)
895 {
896 nextParameter.ParameterName = GetParameterName(1 + parameterCount);
897 }
898 else
899 {
900 nextParameter.ParameterName = parameterName;
901 }
902 nextParameter.Direction = ParameterDirection.Input;
903 nextParameter.SourceColumn = sourceColumn;
904 nextParameter.SourceVersion = version;
905 nextParameter.SourceColumnNullMapping = true;
906 nextParameter.Value = value;
907 nextParameter.Size = 0;
908 ApplyParameterInfo(nextParameter, row.DataRow, statementType, whereClause);
909 nextParameter.DbType = DbType.Int32;
910 nextParameter.Value = (ADP.IsNull(value) ? DbDataAdapter.s_parameterValueNullValue : DbDataAdapter.s_parameterValueNonNullValue);
911 if (!command.Parameters.Contains(nextParameter))
912 {
913 command.Parameters.Add(nextParameter);
914 }
915 if (parameterName == null)
916 {
917 return GetParameterPlaceholder(1 + parameterCount);
918 }
919 return string.Format(CultureInfo.InvariantCulture, _parameterMarkerFormat, parameterName);
920 }
921
922 private string CreateParameterForValue(DbCommand command, string parameterName, string sourceColumn, DataRowVersion version, int parameterCount, object value, DbSchemaRow row, StatementType statementType, bool whereClause)
923 {
924 DbParameter nextParameter = GetNextParameter(command, parameterCount);
925 if (parameterName == null)
926 {
927 nextParameter.ParameterName = GetParameterName(1 + parameterCount);
928 }
929 else
930 {
931 nextParameter.ParameterName = parameterName;
932 }
933 nextParameter.Direction = ParameterDirection.Input;
934 nextParameter.SourceColumn = sourceColumn;
935 nextParameter.SourceVersion = version;
936 nextParameter.SourceColumnNullMapping = false;
937 nextParameter.Value = value;
938 nextParameter.Size = 0;
939 ApplyParameterInfo(nextParameter, row.DataRow, statementType, whereClause);
940 if (!command.Parameters.Contains(nextParameter))
941 {
942 command.Parameters.Add(nextParameter);
943 }
944 if (parameterName == null)
945 {
946 return GetParameterPlaceholder(1 + parameterCount);
947 }
948 return string.Format(CultureInfo.InvariantCulture, _parameterMarkerFormat, parameterName);
949 }
950
951 protected override void Dispose(bool disposing)
952 {
953 if (disposing)
954 {
955 DataAdapter = null;
956 }
957 base.Dispose(disposing);
958 }
959
961 {
962 DataTableMapping result = null;
963 if (dataRow != null)
964 {
965 DataTable table = dataRow.Table;
966 if (table != null)
967 {
968 DbDataAdapter dataAdapter = DataAdapter;
969 if (dataAdapter != null)
970 {
971 result = dataAdapter.GetTableMapping(table);
972 }
973 else
974 {
975 string tableName = table.TableName;
976 result = new DataTableMapping(tableName, tableName);
977 }
978 }
979 }
980 return result;
981 }
982
983 private string GetBaseParameterName(int index)
984 {
985 if (_parameterNames != null)
986 {
988 }
989 return null;
990 }
991
992 private string GetOriginalParameterName(int index)
993 {
994 if (_parameterNames != null)
995 {
997 }
998 return null;
999 }
1000
1001 private string GetNullParameterName(int index)
1002 {
1003 if (_parameterNames != null)
1004 {
1006 }
1007 return null;
1008 }
1009
1011 {
1012 DbCommand dbCommand = null;
1013 DbDataAdapter dataAdapter = DataAdapter;
1014 if (dataAdapter != null)
1015 {
1017 {
1019 }
1020 dbCommand = dataAdapter.SelectCommand;
1021 }
1022 if (dbCommand == null)
1023 {
1024 throw ADP.MissingSourceCommand();
1025 }
1026 return dbCommand;
1027 }
1028
1030 {
1031 return GetInsertCommand(null, useColumnsForParameterNames: false);
1032 }
1033
1034 public DbCommand GetInsertCommand(bool useColumnsForParameterNames)
1035 {
1036 return GetInsertCommand(null, useColumnsForParameterNames);
1037 }
1038
1039 internal DbCommand GetInsertCommand(DataRow dataRow, bool useColumnsForParameterNames)
1040 {
1041 BuildCache(closeConnection: true, dataRow, useColumnsForParameterNames);
1042 BuildInsertCommand(GetTableMapping(dataRow), dataRow);
1043 return InsertCommand;
1044 }
1045
1047 {
1048 return GetUpdateCommand(null, useColumnsForParameterNames: false);
1049 }
1050
1051 public DbCommand GetUpdateCommand(bool useColumnsForParameterNames)
1052 {
1053 return GetUpdateCommand(null, useColumnsForParameterNames);
1054 }
1055
1056 internal DbCommand GetUpdateCommand(DataRow dataRow, bool useColumnsForParameterNames)
1057 {
1058 BuildCache(closeConnection: true, dataRow, useColumnsForParameterNames);
1059 BuildUpdateCommand(GetTableMapping(dataRow), dataRow);
1060 return UpdateCommand;
1061 }
1062
1064 {
1065 return GetDeleteCommand(null, useColumnsForParameterNames: false);
1066 }
1067
1068 public DbCommand GetDeleteCommand(bool useColumnsForParameterNames)
1069 {
1070 return GetDeleteCommand(null, useColumnsForParameterNames);
1071 }
1072
1073 internal DbCommand GetDeleteCommand(DataRow dataRow, bool useColumnsForParameterNames)
1074 {
1075 BuildCache(closeConnection: true, dataRow, useColumnsForParameterNames);
1076 BuildDeleteCommand(GetTableMapping(dataRow), dataRow);
1077 return DeleteCommand;
1078 }
1079
1080 private object GetColumnValue(DataRow row, string columnName, DataTableMapping mappings, DataRowVersion version)
1081 {
1082 return GetColumnValue(row, GetDataColumn(columnName, mappings, row), version);
1083 }
1084
1085 [return: NotNullIfNotNull("column")]
1086 private object GetColumnValue(DataRow row, DataColumn column, DataRowVersion version)
1087 {
1088 object result = null;
1089 if (column != null)
1090 {
1091 result = row[column, version];
1092 }
1093 return result;
1094 }
1095
1096 private DataColumn GetDataColumn(string columnName, DataTableMapping tablemapping, DataRow row)
1097 {
1098 DataColumn result = null;
1099 if (!string.IsNullOrEmpty(columnName))
1100 {
1101 result = tablemapping.GetDataColumn(columnName, null, row.Table, _missingMappingAction, MissingSchemaAction.Error);
1102 }
1103 return result;
1104 }
1105
1106 private static DbParameter GetNextParameter(DbCommand command, int pcount)
1107 {
1108 if (pcount < command.Parameters.Count)
1109 {
1110 return command.Parameters[pcount];
1111 }
1112 return command.CreateParameter();
1113 }
1114
1116 {
1117 if (!row.IsAutoIncrement && !row.IsHidden && !row.IsExpression && !row.IsRowVersion)
1118 {
1119 return !row.IsReadOnly;
1120 }
1121 return false;
1122 }
1123
1125 {
1126 if (!row.IsAutoIncrement && !row.IsRowVersion && !row.IsHidden)
1127 {
1128 return !row.IsReadOnly;
1129 }
1130 return false;
1131 }
1132
1133 private bool IncludeInWhereClause(DbSchemaRow row, bool isUpdate)
1134 {
1135 bool flag = IncrementWhereCount(row);
1136 if (flag && row.IsHidden)
1137 {
1138 if (ConflictOption.CompareRowVersion == ConflictOption)
1139 {
1141 }
1143 }
1144 if (!flag && ConflictOption.CompareAllSearchableValues == ConflictOption)
1145 {
1146 flag = !row.IsLong && !row.IsRowVersion && !row.IsHidden;
1147 }
1148 return flag;
1149 }
1150
1152 {
1153 ConflictOption conflictOption = ConflictOption;
1154 switch (conflictOption)
1155 {
1156 case ConflictOption.CompareAllSearchableValues:
1157 case ConflictOption.OverwriteChanges:
1158 if ((row.IsKey || row.IsUnique) && !row.IsLong)
1159 {
1160 return !row.IsRowVersion;
1161 }
1162 return false;
1163 case ConflictOption.CompareRowVersion:
1164 if (((row.IsKey || row.IsUnique) && !_hasPartialPrimaryKey) || row.IsRowVersion)
1165 {
1166 return !row.IsLong;
1167 }
1168 return false;
1169 default:
1170 throw ADP.InvalidConflictOptions(conflictOption);
1171 }
1172 }
1173
1174 protected virtual DbCommand InitializeCommand(DbCommand? command)
1175 {
1176 if (command == null)
1177 {
1178 DbCommand selectCommand = GetSelectCommand();
1179 command = selectCommand.Connection.CreateCommand();
1180 command.CommandTimeout = selectCommand.CommandTimeout;
1181 command.Transaction = selectCommand.Transaction;
1182 }
1183 command.CommandType = CommandType.Text;
1184 command.UpdatedRowSource = UpdateRowSource.None;
1185 return command;
1186 }
1187
1188 private string QuotedColumn(string column)
1189 {
1191 }
1192
1193 public virtual string QuoteIdentifier(string unquotedIdentifier)
1194 {
1195 throw ADP.NotSupported();
1196 }
1197
1198 public virtual void RefreshSchema()
1199 {
1200 _dbSchemaTable = null;
1201 _dbSchemaRows = null;
1202 _sourceColumnNames = null;
1203 _quotedBaseTableName = null;
1204 DbDataAdapter dataAdapter = DataAdapter;
1205 if (dataAdapter != null)
1206 {
1207 if (InsertCommand == dataAdapter.InsertCommand)
1208 {
1209 dataAdapter.InsertCommand = null;
1210 }
1211 if (UpdateCommand == dataAdapter.UpdateCommand)
1212 {
1213 dataAdapter.UpdateCommand = null;
1214 }
1215 if (DeleteCommand == dataAdapter.DeleteCommand)
1216 {
1217 dataAdapter.DeleteCommand = null;
1218 }
1219 }
1220 DbCommand insertCommand;
1221 if ((insertCommand = InsertCommand) != null)
1222 {
1223 insertCommand.Dispose();
1224 }
1225 if ((insertCommand = UpdateCommand) != null)
1226 {
1227 insertCommand.Dispose();
1228 }
1229 if ((insertCommand = DeleteCommand) != null)
1230 {
1231 insertCommand.Dispose();
1232 }
1233 InsertCommand = null;
1234 UpdateCommand = null;
1235 DeleteCommand = null;
1236 }
1237
1238 private static void RemoveExtraParameters(DbCommand command, int usedParameterCount)
1239 {
1240 for (int num = command.Parameters.Count - 1; num >= usedParameterCount; num--)
1241 {
1242 command.Parameters.RemoveAt(num);
1243 }
1244 }
1245
1246 protected void RowUpdatingHandler(RowUpdatingEventArgs rowUpdatingEvent)
1247 {
1248 if (rowUpdatingEvent == null)
1249 {
1250 throw ADP.ArgumentNull("rowUpdatingEvent");
1251 }
1252 try
1253 {
1254 if (rowUpdatingEvent.Status != 0)
1255 {
1256 return;
1257 }
1258 StatementType statementType = rowUpdatingEvent.StatementType;
1259 DbCommand dbCommand = (DbCommand)rowUpdatingEvent.Command;
1260 if (dbCommand != null)
1261 {
1262 switch (statementType)
1263 {
1264 case StatementType.Select:
1265 return;
1266 case StatementType.Insert:
1267 dbCommand = InsertCommand;
1268 break;
1269 case StatementType.Update:
1270 dbCommand = UpdateCommand;
1271 break;
1272 case StatementType.Delete:
1273 dbCommand = DeleteCommand;
1274 break;
1275 default:
1276 throw ADP.InvalidStatementType(statementType);
1277 }
1278 if (dbCommand != rowUpdatingEvent.Command)
1279 {
1280 dbCommand = (DbCommand)rowUpdatingEvent.Command;
1281 if (dbCommand != null && dbCommand.Connection == null)
1282 {
1283 DbCommand dbCommand2 = DataAdapter?.SelectCommand;
1284 if (dbCommand2 != null)
1285 {
1286 dbCommand.Connection = dbCommand2.Connection;
1287 }
1288 }
1289 }
1290 else
1291 {
1292 dbCommand = null;
1293 }
1294 }
1295 if (dbCommand == null)
1296 {
1297 RowUpdatingHandlerBuilder(rowUpdatingEvent);
1298 }
1299 }
1300 catch (Exception ex) when (ADP.IsCatchableExceptionType(ex))
1301 {
1303 rowUpdatingEvent.Status = UpdateStatus.ErrorsOccurred;
1304 rowUpdatingEvent.Errors = ex;
1305 }
1306 }
1307
1309 {
1310 DataRow row = rowUpdatingEvent.Row;
1311 BuildCache(closeConnection: false, row, useColumnsForParameterNames: false);
1312 DbCommand dbCommand = rowUpdatingEvent.StatementType switch
1313 {
1314 StatementType.Insert => BuildInsertCommand(rowUpdatingEvent.TableMapping, row),
1315 StatementType.Update => BuildUpdateCommand(rowUpdatingEvent.TableMapping, row),
1316 StatementType.Delete => BuildDeleteCommand(rowUpdatingEvent.TableMapping, row),
1317 _ => throw ADP.InvalidStatementType(rowUpdatingEvent.StatementType),
1318 };
1319 if (dbCommand == null)
1320 {
1321 row?.AcceptChanges();
1322 rowUpdatingEvent.Status = UpdateStatus.SkipCurrentRow;
1323 }
1324 rowUpdatingEvent.Command = dbCommand;
1325 }
1326
1327 public virtual string UnquoteIdentifier(string quotedIdentifier)
1328 {
1329 throw ADP.NotSupported();
1330 }
1331
1332 protected abstract void ApplyParameterInfo(DbParameter parameter, DataRow row, StatementType statementType, bool whereClause);
1333
1334 protected abstract string GetParameterName(int parameterOrdinal);
1335
1336 protected abstract string GetParameterName(string parameterName);
1337
1338 protected abstract string GetParameterPlaceholder(int parameterOrdinal);
1339
1340 protected abstract void SetRowUpdatingHandler(DbDataAdapter adapter);
1341}
static bool IsDBNull([NotNullWhen(true)] object? value)
Definition Convert.cs:215
static InvalidOperationException DynamicSQLNoKeyInfoUpdate()
Definition ADP.cs:496
static string BuildQuotedString(string quotePrefix, string quoteSuffix, string unQuotedString)
Definition ADP.cs:880
static ArgumentNullException ArgumentNull(string parameter)
Definition ADP.cs:699
static int SrcCompare(string strA, string strB)
Definition ADP.cs:651
static InvalidOperationException MissingSourceCommand()
Definition ADP.cs:521
static bool CompareInsensitiveInvariant(string strvalue, string strconst)
Definition ADP.cs:947
static ArgumentOutOfRangeException InvalidStatementType(StatementType value)
Definition ADP.cs:149
static InvalidOperationException DynamicSQLNoTableInfo()
Definition ADP.cs:486
static InvalidOperationException DynamicSQLNoKeyInfoRowVersionUpdate()
Definition ADP.cs:506
static ArgumentOutOfRangeException InvalidCatalogLocation(CatalogLocation value)
Definition ADP.cs:109
static void TraceExceptionForCapture(Exception e)
Definition ADP.cs:74
static InvalidOperationException NoQuoteChange()
Definition ADP.cs:516
static InvalidOperationException MissingSourceCommandConnection()
Definition ADP.cs:526
static NotSupportedException NotSupported()
Definition ADP.cs:753
static InvalidOperationException DynamicSQLNoKeyInfoRowVersionDelete()
Definition ADP.cs:501
static void BuildSchemaTableInfoTableNames(string[] columnNameArray)
Definition ADP.cs:592
static InvalidOperationException DynamicSQLNestedQuote(string name, string quote)
Definition ADP.cs:511
static bool IsCatchableExceptionType(Exception e)
Definition ADP.cs:790
static InvalidOperationException DynamicSQLJoinUnsupported()
Definition ADP.cs:481
static bool IsNull(object value)
Definition ADP.cs:957
static InvalidOperationException DynamicSQLNoKeyInfoDelete()
Definition ADP.cs:491
static ArgumentOutOfRangeException InvalidConflictOptions(ConflictOption value)
Definition ADP.cs:114
MissingMappingAction MissingMappingAction
override void Dispose(bool disposing)
DataColumn? GetDataColumn(string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
ParameterNames(DbCommandBuilder dbCommandBuilder, DbSchemaRow[] schemaRows)
object GetColumnValue(DataRow row, string columnName, DataTableMapping mappings, DataRowVersion version)
string GetParameterPlaceholder(int parameterOrdinal)
DbCommand GetUpdateCommand(bool useColumnsForParameterNames)
static void RemoveExtraParameters(DbCommand command, int usedParameterCount)
override void Dispose(bool disposing)
DbCommand GetDeleteCommand(DataRow dataRow, bool useColumnsForParameterNames)
DataColumn GetDataColumn(string columnName, DataTableMapping tablemapping, DataRow row)
string CreateParameterForValue(DbCommand command, string parameterName, string sourceColumn, DataRowVersion version, int parameterCount, object value, DbSchemaRow row, StatementType statementType, bool whereClause)
void ApplyParameterInfo(DbParameter parameter, DataRow row, StatementType statementType, bool whereClause)
static DbParameter GetNextParameter(DbCommand command, int pcount)
DbCommand BuildUpdateCommand(DataTableMapping mappings, DataRow dataRow)
virtual DbCommand InitializeCommand(DbCommand? command)
void BuildCache(bool closeConnection, DataRow dataRow, bool useColumnsForParameterNames)
virtual CatalogLocation CatalogLocation
string GetParameterName(int parameterOrdinal)
int BuildWhereClause(DataTableMapping mappings, DataRow dataRow, StringBuilder builder, DbCommand command, int parameterCount, bool isUpdate)
void SetRowUpdatingHandler(DbDataAdapter adapter)
DbCommand BuildInsertCommand(DataTableMapping mappings, DataRow dataRow)
string CreateParameterForNullTest(DbCommand command, string parameterName, string sourceColumn, DataRowVersion version, int parameterCount, object value, DbSchemaRow row, StatementType statementType, bool whereClause)
DbCommand GetDeleteCommand(bool useColumnsForParameterNames)
void BuildInformation(DataTable schemaTable)
object GetColumnValue(DataRow row, DataColumn column, DataRowVersion version)
virtual string UnquoteIdentifier(string quotedIdentifier)
bool IncludeInWhereClause(DbSchemaRow row, bool isUpdate)
DataTableMapping GetTableMapping(DataRow dataRow)
void RowUpdatingHandlerBuilder(RowUpdatingEventArgs rowUpdatingEvent)
DbCommand BuildDeleteCommand(DataTableMapping mappings, DataRow dataRow)
DbCommand GetUpdateCommand(DataRow dataRow, bool useColumnsForParameterNames)
bool IncludeInInsertValues(DbSchemaRow row)
DbCommand GetInsertCommand(bool useColumnsForParameterNames)
DbCommand GetInsertCommand(DataRow dataRow, bool useColumnsForParameterNames)
string GetParameterName(string parameterName)
void RowUpdatingHandler(RowUpdatingEventArgs rowUpdatingEvent)
virtual ? DataTable GetSchemaTable(DbCommand sourceCommand)
virtual string QuoteIdentifier(string unquotedIdentifier)
DbParameterCollection Parameters
Definition DbCommand.cs:65
DbTransaction? Transaction
Definition DbCommand.cs:73
DbParameter CreateParameter()
Definition DbCommand.cs:112
DbDataReader ExecuteReader()
Definition DbCommand.cs:128
virtual DataTable GetSchema()
static readonly object s_parameterValueNonNullValue
DataTableMapping GetTableMapping(DataTable dataTable)
virtual ? DataTable GetSchemaTable()
static DbSchemaRow[] GetSortedSchemaRows(DataTable dataTable, bool returnProviderSpecificTypes)
DataTable Table
Definition DataRow.cs:169
DataRowCollection Rows
Definition DataTable.cs:701
static CultureInfo InvariantCulture
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static bool IsMatch(string input, string pattern)
Definition Regex.cs:406
override string ToString()
StringBuilder Append(char value, int repeatCount)