Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NewDiffgramGen.cs
Go to the documentation of this file.
5using System.Xml;
7
8namespace System.Data;
9
10internal sealed class NewDiffgramGen
11{
12 internal XmlDocument _doc;
13
14 internal DataSet _ds;
15
16 internal DataTable _dt;
17
18 internal XmlWriter _xmlw;
19
20 private bool _fBefore;
21
22 private bool _fErrors;
23
25
26 private readonly ArrayList _tables = new ArrayList();
27
28 private readonly bool _writeHierarchy;
29
31 {
32 _ds = ds;
33 _dt = null;
34 _doc = new XmlDocument();
35 for (int i = 0; i < ds.Tables.Count; i++)
36 {
37 _tables.Add(ds.Tables[i]);
38 }
40 }
41
43 {
44 _ds = null;
45 _dt = dt;
46 _doc = new XmlDocument();
47 _tables.Add(dt);
49 {
50 _writeHierarchy = true;
52 }
54 }
55
57 {
58 foreach (DataRelation childRelation in dt.ChildRelations)
59 {
60 if (!_tables.Contains(childRelation.ChildTable))
61 {
62 _tables.Add(childRelation.ChildTable);
64 }
65 }
66 }
67
68 [MemberNotNull("_rowsOrder")]
70 {
71 int num = 0;
72 for (int i = 0; i < tables.Count; i++)
73 {
74 num += ((DataTable)tables[i]).Rows.Count;
75 }
76 _rowsOrder = new Hashtable(num);
77 for (int j = 0; j < tables.Count; j++)
78 {
81 num = rows.Count;
82 for (int k = 0; k < num; k++)
83 {
84 _rowsOrder[rows[k]] = k;
85 }
86 }
87 }
88
89 private bool EmptyData()
90 {
91 for (int i = 0; i < _tables.Count; i++)
92 {
93 if (((DataTable)_tables[i]).Rows.Count > 0)
94 {
95 return false;
96 }
97 }
98 return true;
99 }
100
101 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
102 internal void Save(XmlWriter xmlw)
103 {
104 Save(xmlw, null);
105 }
106
107 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
108 internal void Save(XmlWriter xmlw, DataTable table)
109 {
111 _xmlw.WriteStartElement("diffgr", "diffgram", "urn:schemas-microsoft-com:xml-diffgram-v1");
112 _xmlw.WriteAttributeString("xmlns", "msdata", null, "urn:schemas-microsoft-com:xml-msdata");
113 if (!EmptyData())
114 {
115 if (table != null)
116 {
118 }
119 else
120 {
122 }
123 if (table == null)
124 {
125 for (int i = 0; i < _ds.Tables.Count; i++)
126 {
128 }
129 }
130 else
131 {
132 for (int j = 0; j < _tables.Count; j++)
133 {
135 }
136 }
137 if (_fBefore)
138 {
140 }
141 if (table == null)
142 {
143 for (int k = 0; k < _ds.Tables.Count; k++)
144 {
146 }
147 }
148 else
149 {
150 for (int l = 0; l < _tables.Count; l++)
151 {
153 }
154 }
155 if (_fErrors)
156 {
158 }
159 }
161 _xmlw.Flush();
162 }
163
164 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
165 private void GenerateTable(DataTable table)
166 {
167 int count = table.Rows.Count;
168 if (count > 0)
169 {
170 for (int i = 0; i < count; i++)
171 {
172 GenerateRow(table.Rows[i]);
173 }
174 }
175 }
176
177 private void GenerateTableErrors(DataTable table)
178 {
179 int count = table.Rows.Count;
180 int count2 = table.Columns.Count;
181 if (count <= 0)
182 {
183 return;
184 }
185 for (int i = 0; i < count; i++)
186 {
187 bool flag = false;
188 DataRow dataRow = table.Rows[i];
189 string prefix = ((table.Namespace.Length != 0) ? table.Prefix : string.Empty);
190 if (dataRow.HasErrors && dataRow.RowError.Length > 0)
191 {
192 if (!_fErrors)
193 {
194 _xmlw.WriteStartElement("diffgr", "errors", "urn:schemas-microsoft-com:xml-diffgram-v1");
195 _fErrors = true;
196 }
197 _xmlw.WriteStartElement(prefix, dataRow.Table.EncodedTableName, dataRow.Table.Namespace);
198 _xmlw.WriteAttributeString("diffgr", "id", "urn:schemas-microsoft-com:xml-diffgram-v1", dataRow.Table.TableName + dataRow.rowID.ToString(CultureInfo.InvariantCulture));
199 _xmlw.WriteAttributeString("diffgr", "Error", "urn:schemas-microsoft-com:xml-diffgram-v1", dataRow.RowError);
200 flag = true;
201 }
202 if (count2 <= 0)
203 {
204 continue;
205 }
206 for (int j = 0; j < count2; j++)
207 {
209 string columnError = dataRow.GetColumnError(dataColumn);
210 string prefix2 = ((dataColumn.Namespace.Length != 0) ? dataColumn.Prefix : string.Empty);
211 if (columnError == null || columnError.Length == 0)
212 {
213 continue;
214 }
215 if (!flag)
216 {
217 if (!_fErrors)
218 {
219 _xmlw.WriteStartElement("diffgr", "errors", "urn:schemas-microsoft-com:xml-diffgram-v1");
220 _fErrors = true;
221 }
222 _xmlw.WriteStartElement(prefix, dataRow.Table.EncodedTableName, dataRow.Table.Namespace);
223 _xmlw.WriteAttributeString("diffgr", "id", "urn:schemas-microsoft-com:xml-diffgram-v1", dataRow.Table.TableName + dataRow.rowID.ToString(CultureInfo.InvariantCulture));
224 flag = true;
225 }
226 _xmlw.WriteStartElement(prefix2, dataColumn.EncodedColumnName, dataColumn.Namespace);
227 _xmlw.WriteAttributeString("diffgr", "Error", "urn:schemas-microsoft-com:xml-diffgram-v1", columnError);
229 }
230 if (flag)
231 {
233 }
234 }
235 }
236
237 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
238 private void GenerateRow(DataRow row)
239 {
240 DataRowState rowState = row.RowState;
241 if (rowState == DataRowState.Unchanged || rowState == DataRowState.Added)
242 {
243 return;
244 }
245 if (!_fBefore)
246 {
247 _xmlw.WriteStartElement("diffgr", "before", "urn:schemas-microsoft-com:xml-diffgram-v1");
248 _fBefore = true;
249 }
250 DataTable table = row.Table;
251 int count = table.Columns.Count;
252 string value = table.TableName + row.rowID.ToString(CultureInfo.InvariantCulture);
253 string text = null;
254 if (rowState == DataRowState.Deleted && row.Table.NestedParentRelations.Length != 0)
255 {
257 if (nestedParentRow != null)
258 {
260 }
261 }
262 string prefix = ((table.Namespace.Length != 0) ? table.Prefix : string.Empty);
263 _xmlw.WriteStartElement(prefix, row.Table.EncodedTableName, row.Table.Namespace);
264 _xmlw.WriteAttributeString("diffgr", "id", "urn:schemas-microsoft-com:xml-diffgram-v1", value);
266 {
267 _xmlw.WriteAttributeString("diffgr", "hasErrors", "urn:schemas-microsoft-com:xml-diffgram-v1", "true");
268 }
269 if (text != null)
270 {
271 _xmlw.WriteAttributeString("diffgr", "parentId", "urn:schemas-microsoft-com:xml-diffgram-v1", text);
272 }
273 _xmlw.WriteAttributeString("msdata", "rowOrder", "urn:schemas-microsoft-com:xml-msdata", _rowsOrder[row].ToString());
274 for (int i = 0; i < count; i++)
275 {
276 if (row.Table.Columns[i].ColumnMapping == MappingType.Attribute || row.Table.Columns[i].ColumnMapping == MappingType.Hidden)
277 {
278 GenerateColumn(row, row.Table.Columns[i], DataRowVersion.Original);
279 }
280 }
281 for (int j = 0; j < count; j++)
282 {
283 if (row.Table.Columns[j].ColumnMapping == MappingType.Element || row.Table.Columns[j].ColumnMapping == MappingType.SimpleContent)
284 {
285 GenerateColumn(row, row.Table.Columns[j], DataRowVersion.Original);
286 }
287 }
289 }
290
291 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
293 {
294 string text = null;
295 text = col.GetColumnValueAsString(row, version);
296 if (text == null)
297 {
298 if (col.ColumnMapping == MappingType.SimpleContent)
299 {
300 _xmlw.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
301 }
302 return;
303 }
304 string prefix = ((col.Namespace.Length != 0) ? col.Prefix : string.Empty);
305 switch (col.ColumnMapping)
306 {
307 case MappingType.Attribute:
308 _xmlw.WriteAttributeString(prefix, col.EncodedColumnName, col.Namespace, text);
309 break;
310 case MappingType.Hidden:
311 _xmlw.WriteAttributeString("msdata", "hidden" + col.EncodedColumnName, "urn:schemas-microsoft-com:xml-msdata", text);
312 break;
313 case MappingType.SimpleContent:
315 break;
316 case MappingType.Element:
317 {
318 bool flag = true;
319 object obj = row[col, version];
320 if (!col.IsCustomType || !col.IsValueCustomTypeInstance(obj) || typeof(IXmlSerializable).IsAssignableFrom(obj.GetType()))
321 {
322 _xmlw.WriteStartElement(prefix, col.EncodedColumnName, col.Namespace);
323 flag = false;
324 }
325 Type type = obj.GetType();
326 if (!col.IsCustomType)
327 {
328 if ((type == typeof(char) || type == typeof(string)) && XmlDataTreeWriter.PreserveSpace(text))
329 {
330 _xmlw.WriteAttributeString("xml", "space", "http://www.w3.org/XML/1998/namespace", "preserve");
331 }
333 }
334 else if (obj != DBNull.Value && (!col.ImplementsINullable || !DataStorage.IsObjectSqlNull(obj)))
335 {
336 if (col.IsValueCustomTypeInstance(obj))
337 {
338 if (!flag && obj.GetType() != col.DataType)
339 {
340 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", DataStorage.GetQualifiedName(type));
341 }
342 if (!flag)
343 {
344 col.ConvertObjectToXml(obj, _xmlw, null);
345 }
346 else
347 {
348 if (obj.GetType() != col.DataType)
349 {
350 throw ExceptionBuilder.PolymorphismNotSupported(type.AssemblyQualifiedName);
351 }
352 XmlRootAttribute xmlRootAttribute = new XmlRootAttribute(col.EncodedColumnName);
354 col.ConvertObjectToXml(obj, _xmlw, xmlRootAttribute);
355 }
356 }
357 else
358 {
359 if (type == typeof(Type) || type == typeof(Guid) || type == typeof(char) || DataStorage.IsSqlType(type))
360 {
361 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", type.FullName);
362 }
363 else if (obj is Type)
364 {
365 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", "Type");
366 }
367 else
368 {
369 string value = "xs:" + XmlTreeGen.XmlDataTypeName(type);
370 _xmlw.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", value);
371 _xmlw.WriteAttributeString("xs", "xmlns", "http://www.w3.org/2001/XMLSchema", value);
372 }
374 {
375 _xmlw.WriteString(col.ConvertObjectToXml(obj));
376 }
377 else
378 {
379 col.ConvertObjectToXml(obj, _xmlw, null);
380 }
381 }
382 }
383 if (!flag)
384 {
386 }
387 break;
388 }
389 }
390 }
391
392 internal static string QualifiedName(string prefix, string name)
393 {
394 if (prefix != null)
395 {
396 return prefix + ":" + name;
397 }
398 return name;
399 }
400}
virtual int Add(object? value)
virtual bool Contains(object? item)
static readonly DBNull Value
Definition DBNull.cs:8
static string GetQualifiedName(Type type)
static bool IsObjectSqlNull(object value)
static bool IsSqlType(StorageType storageType)
DataRow GetNestedParentRow(DataRowVersion version)
Definition DataRow.cs:746
DataTableCollection Tables
Definition DataSet.cs:396
DataRowCollection Rows
Definition DataTable.cs:701
DataColumnCollection Columns
Definition DataTable.cs:327
static XmlWriter CreateWriter(XmlWriter xw)
static Exception PolymorphismNotSupported(string typeName)
void GenerateTable(DataTable table)
static string QualifiedName(string prefix, string name)
void CreateTableHierarchy(DataTable dt)
void Save(XmlWriter xmlw, DataTable table)
void DoAssignments(ArrayList tables)
void GenerateTableErrors(DataTable table)
readonly ArrayList _tables
NewDiffgramGen(DataTable dt, bool writeHierarchy)
void GenerateRow(DataRow row)
void Save(XmlWriter xmlw)
void GenerateColumn(DataRow row, DataColumn col, DataRowVersion version)
static bool PreserveSpace(object value)
void SaveDiffgramData(XmlWriter xw, Hashtable rowsOrder)
static bool RowHasErrors(DataRow row)
static string XmlDataTypeName(Type type)
static CultureInfo InvariantCulture
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
void WriteString(string? text)
void WriteAttributeString(string localName, string? ns, string? value)
Definition XmlWriter.cs:46
void WriteStartElement(string localName, string? ns)
Definition XmlWriter.cs:30