/
SQLGuid.cs
329 lines (272 loc) · 10.6 KB
/
SQLGuid.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace System.Data.SqlTypes
{
/// <summary>
/// Represents a globally unique identifier to be stored in
/// or retrieved from a database.
/// </summary>
[Serializable]
[XmlSchemaProvider("GetXsdType")]
[System.Runtime.CompilerServices.TypeForwardedFrom("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public struct SqlGuid : INullable, IComparable, ISerializable, IXmlSerializable, IEquatable<SqlGuid>
{
private const int SizeOfGuid = 16; // sizeof(Guid)
// NOTE: If any instance fields change, update SqlTypeWorkarounds type in System.Data.SqlClient.
private Guid? _value; // the SqlGuid is null if _value is null
// constructor
public SqlGuid(byte[] value)
{
if (value == null || value.Length != SizeOfGuid)
throw new ArgumentException(SQLResource.InvalidArraySizeMessage);
_value = new Guid(value);
}
public SqlGuid(string s)
{
_value = new Guid(s);
}
public SqlGuid(Guid g)
{
_value = g;
}
public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
: this(new Guid(a, b, c, d, e, f, g, h, i, j, k))
{
}
// Maintains backwards-compatible binary serialization for the `private byte[] m_value` field
// see src/libraries/System.Runtime.Serialization.Formatters/tests/BinaryFormatterTestData.cs
// for test data
private SqlGuid(SerializationInfo info, StreamingContext context)
{
byte[]? value = (byte[]?)info.GetValue("m_value", typeof(byte[]));
if (value is null)
_value = null;
else
_value = new Guid(value);
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("m_value", ToByteArray(), typeof(byte[]));
}
// INullable
public bool IsNull => _value is null;
// property: Value
public Guid Value => _value ?? throw new SqlNullValueException();
// Implicit conversion from Guid to SqlGuid
public static implicit operator SqlGuid(Guid x)
{
return new SqlGuid(x);
}
// Explicit conversion from SqlGuid to Guid. Throw exception if x is Null.
public static explicit operator Guid(SqlGuid x)
{
return x.Value;
}
public byte[]? ToByteArray()
{
if (_value is null)
return null;
return _value.GetValueOrDefault().ToByteArray();
}
public override string ToString()
{
if (_value is null)
return SQLResource.NullString;
return _value.GetValueOrDefault().ToString();
}
public static SqlGuid Parse(string s)
{
if (s == SQLResource.NullString)
return SqlGuid.Null;
else
return new SqlGuid(s);
}
// Comparison operators
private static EComparison Compare(SqlGuid x, SqlGuid y)
{
// Comparison orders.
ReadOnlySpan<byte> rgiGuidOrder = [10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3];
Debug.Assert(!x.IsNull);
Debug.Assert(!y.IsNull);
// Swap to the correct order to be compared
Span<byte> xBytes = stackalloc byte[SizeOfGuid];
bool xWrote = x._value.GetValueOrDefault().TryWriteBytes(xBytes);
Debug.Assert(xWrote);
Span<byte> yBytes = stackalloc byte[SizeOfGuid];
bool yWrote = y._value.GetValueOrDefault().TryWriteBytes(yBytes);
Debug.Assert(yWrote);
for (int i = 0; i < SizeOfGuid; i++)
{
byte b1 = xBytes[rgiGuidOrder[i]];
byte b2 = yBytes[rgiGuidOrder[i]];
if (b1 != b2)
{
return (b1 < b2) ? EComparison.LT : EComparison.GT;
}
}
return EComparison.EQ;
}
// Implicit conversions
// Explicit conversions
// Explicit conversion from SqlString to SqlGuid
public static explicit operator SqlGuid(SqlString x)
{
return x.IsNull ? Null : new SqlGuid(x.Value);
}
// Explicit conversion from SqlBinary to SqlGuid
public static explicit operator SqlGuid(SqlBinary x)
{
return x.IsNull ? Null : new SqlGuid(x.Value);
}
// Overloading comparison operators
public static SqlBoolean operator ==(SqlGuid x, SqlGuid y)
{
return (x.IsNull || y.IsNull) ? SqlBoolean.Null :
new SqlBoolean(x._value.GetValueOrDefault() == y._value.GetValueOrDefault());
}
public static SqlBoolean operator !=(SqlGuid x, SqlGuid y)
{
return !(x == y);
}
public static SqlBoolean operator <(SqlGuid x, SqlGuid y)
{
return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT);
}
public static SqlBoolean operator >(SqlGuid x, SqlGuid y)
{
return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.GT);
}
public static SqlBoolean operator <=(SqlGuid x, SqlGuid y)
{
if (x.IsNull || y.IsNull)
return SqlBoolean.Null;
EComparison cmp = Compare(x, y);
return new SqlBoolean(cmp == EComparison.LT || cmp == EComparison.EQ);
}
public static SqlBoolean operator >=(SqlGuid x, SqlGuid y)
{
if (x.IsNull || y.IsNull)
return SqlBoolean.Null;
EComparison cmp = Compare(x, y);
return new SqlBoolean(cmp == EComparison.GT || cmp == EComparison.EQ);
}
//--------------------------------------------------
// Alternative methods for overloaded operators
//--------------------------------------------------
// Alternative method for operator ==
public static SqlBoolean Equals(SqlGuid x, SqlGuid y)
{
return (x == y);
}
// Alternative method for operator !=
public static SqlBoolean NotEquals(SqlGuid x, SqlGuid y)
{
return (x != y);
}
// Alternative method for operator <
public static SqlBoolean LessThan(SqlGuid x, SqlGuid y)
{
return (x < y);
}
// Alternative method for operator >
public static SqlBoolean GreaterThan(SqlGuid x, SqlGuid y)
{
return (x > y);
}
// Alternative method for operator <=
public static SqlBoolean LessThanOrEqual(SqlGuid x, SqlGuid y)
{
return (x <= y);
}
// Alternative method for operator >=
public static SqlBoolean GreaterThanOrEqual(SqlGuid x, SqlGuid y)
{
return (x >= y);
}
// Alternative method for conversions.
public SqlString ToSqlString()
{
return (SqlString)this;
}
public SqlBinary ToSqlBinary()
{
return (SqlBinary)this;
}
// IComparable
// Compares this object to another object, returning an integer that
// indicates the relationship.
// Returns a value less than zero if this < object, zero if this = object,
// or a value greater than zero if this > object.
// null is considered to be less than any instance.
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlGuid i)
{
return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlGuid));
}
public int CompareTo(SqlGuid value)
{
// If both Null, consider them equal.
// Otherwise, Null is less than anything.
if (IsNull)
return value.IsNull ? 0 : -1;
else if (value.IsNull)
return 1;
if (this < value) return -1;
if (this > value) return 1;
return 0;
}
// Compares this instance with a specified object
public override bool Equals([NotNullWhen(true)] object? value) =>
value is SqlGuid other && Equals(other);
/// <summary>Indicates whether the current instance is equal to another instance of the same type.</summary>
/// <param name="other">An instance to compare with this instance.</param>
/// <returns>true if the current instance is equal to the other instance; otherwise, false.</returns>
public bool Equals(SqlGuid other) => _value == other._value;
// For hashing purpose
public override int GetHashCode() => _value.GetHashCode();
XmlSchema? IXmlSerializable.GetSchema() { return null; }
void IXmlSerializable.ReadXml(XmlReader reader)
{
string? isNull = reader.GetAttribute("nil", XmlSchema.InstanceNamespace);
if (isNull != null && XmlConvert.ToBoolean(isNull))
{
// Read the next value.
reader.ReadElementString();
_value = null;
}
else
{
_value = new Guid(reader.ReadElementString());
}
}
void IXmlSerializable.WriteXml(XmlWriter writer)
{
if (_value is null)
{
writer.WriteAttributeString("xsi", "nil", XmlSchema.InstanceNamespace, "true");
}
else
{
writer.WriteString(XmlConvert.ToString(_value.GetValueOrDefault()));
}
}
public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet)
{
return new XmlQualifiedName("string", XmlSchema.Namespace);
}
public static readonly SqlGuid Null;
} // SqlGuid
} // namespace System.Data.SqlTypes