-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathDatabaseObject.cs
More file actions
284 lines (251 loc) · 8.58 KB
/
DatabaseObject.cs
File metadata and controls
284 lines (251 loc) · 8.58 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Text;
using System.Threading;
namespace Microsoft.Data.SqlClient.Tests.Common.Fixtures.DatabaseObjects;
/// <summary>
/// Base class for a transient database object (such as a table, type or
/// stored procedure.)
/// </summary>
/// <typeparam name="TState">
/// The type of the internal state accessible to derived types at the point of object creation
/// via the <see cref="State"/> property.
/// </typeparam>
public abstract class DatabaseObject<TState> : IDisposable
{
private readonly bool _shouldDrop;
protected SqlConnection Connection { get; }
protected TState State { get; }
public string Name { get; }
public string UnescapedName => Name.Substring(1, Name.Length - 2).Replace("]]", "]");
protected DatabaseObject(SqlConnection connection, string name, string definition, TState state, bool shouldCreate, bool shouldDrop)
{
_shouldDrop = shouldDrop;
Connection = connection;
State = state;
Name = name;
if (shouldCreate)
{
EnsureConnectionOpen();
DropObject();
CreateObject(definition);
}
}
private void EnsureConnectionOpen()
{
const int MaxWaits = 2;
int counter = MaxWaits;
if (Connection.State is System.Data.ConnectionState.Closed)
{
Connection.Open();
}
while (counter-- > 0 && Connection.State is System.Data.ConnectionState.Connecting)
{
Thread.Sleep(80);
}
}
/// <summary>
/// Generate a new GUID and return the characters from its 1st and 4th
/// parts, as shown here:
///
/// <code>
/// 7ff01cb8-88c7-11f0-b433-00155d7e531e
/// ^^^^^^^^ ^^^^
/// </code>
///
/// These 12 characters are concatenated together without any
/// separators. These 2 parts typically comprise a timestamp and clock
/// sequence, most likely to be unique for tests that generate names in
/// quick succession.
/// </summary>
private static string GetGuidParts()
{
var guid = Guid.NewGuid().ToString();
// GOTCHA: The slice operator is inclusive of the start index and
// exclusive of the end index!
return guid.Substring(0, 8) + guid.Substring(19, 4);
}
/// <summary>
/// Generate a long unique database object name, whose maximum length is
/// 96 characters, with the format:
///
/// <c>{Prefix}_{GuidParts}_{UserName}_{MachineName}</c>
///
/// The Prefix will be truncated to satisfy the overall maximum length.
///
/// The GUID Parts will be the characters from the 1st and 4th blocks
/// from a traditional string representation, as shown here:
///
/// <code>
/// 7ff01cb8-88c7-11f0-b433-00155d7e531e
/// ^^^^^^^^ ^^^^
/// </code>
///
/// These 2 parts typically comprise a timestamp and clock sequence,
/// most likely to be unique for tests that generate names in quick
/// succession. The 12 characters are concatenated together without any
/// separators.
///
/// The UserName and MachineName are obtained from the Environment,
/// and will be truncated to satisfy the maximum overall length.
/// </summary>
///
/// <param name="prefix">
/// The prefix to use when generating the unique name, truncated to at
/// most 32 characters.
///
/// This should not contain any characters that cannot be used in
/// database object names. See:
///
/// https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver17#rules-for-regular-identifiers
/// </param>
///
/// <param name="escape">
/// When true, the entire generated name will be enclosed in square
/// brackets, for example:
///
/// <c>[MyPrefix_7ff01cb811f0_test_user_ci_agent_machine_name]</c>
/// </param>
///
/// <returns>
/// A unique database object name, no more than 96 characters long.
/// </returns>
public static string GenerateLongName(string prefix, bool escape = true)
{
StringBuilder name = new(96);
if (escape)
{
name.Append('[');
}
if (prefix.Length > 32)
{
prefix = prefix.Substring(0, 32);
}
name.Append(prefix);
name.Append('_');
name.Append(GetGuidParts());
name.Append('_');
var suffix =
Environment.UserName + '_' +
Environment.MachineName;
int maxSuffixLength = 96 - name.Length;
if (escape)
{
--maxSuffixLength;
}
if (suffix.Length > maxSuffixLength)
{
suffix = suffix.Substring(0, maxSuffixLength);
}
name.Append(suffix);
if (escape)
{
name.Append(']');
}
return name.ToString();
}
/// <summary>
/// Generate a short unique database object name, whose maximum length
/// is 30 characters, with the format:
///
/// <c>{Prefix}_{GuidParts}</c>
///
/// The Prefix will be truncated to satisfy the overall maximum length.
///
/// The GUID parts will be the characters from the 1st and 4th blocks
/// from a traditional string representation, as shown here:
///
/// <code>
/// 7ff01cb8-88c7-11f0-b433-00155d7e531e
/// ^^^^^^^^ ^^^^
/// </code>
///
/// These 2 parts typically comprise a timestamp and clock sequence,
/// most likely to be unique for tests that generate names in quick
/// succession. The 12 characters are concatenated together without any
/// separators.
/// </summary>
///
/// <param name="prefix">
/// The prefix to use when generating the unique name, truncated to at
/// most 18 characters when withBracket is false, and 16 characters when
/// withBracket is true.
///
/// This should not contain any characters that cannot be used in
/// database object names. See:
///
/// https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver17#rules-for-regular-identifiers
/// </param>
///
/// <param name="escape">
/// When true, the entire generated name will be enclosed in square
/// brackets, for example:
///
/// <c>[MyPrefix_7ff01cb811f0]</c>
/// </param>
///
/// <returns>
/// A unique database object name, no more than 30 characters long.
/// </returns>
public static string GenerateShortName(string prefix, bool escape = true)
{
StringBuilder name = new(30);
if (escape)
{
name.Append('[');
}
int maxPrefixLength = escape ? 16 : 18;
if (prefix.Length > maxPrefixLength)
{
prefix = prefix.Substring(0, maxPrefixLength);
}
name.Append(prefix);
name.Append('_');
name.Append(GetGuidParts());
if (escape)
{
name.Append(']');
}
return name.ToString();
}
/// <summary>
/// Creates the object with a given definition.
/// </summary>
/// <param name="definition">Definition of the object to create.</param>
/// <remarks>
/// By the time this is called, <see cref="Connection"/> will be open.
/// </remarks>
protected abstract void CreateObject(string definition);
/// <summary>
/// Drops the object created by <see cref="CreateObject"/>.
/// </summary>
/// <remarks>
/// By the time this is called, <see cref="Connection"/> will be open.
/// Must not throw an exception if the object does not exist.
/// </remarks>
protected abstract void DropObject();
public void Dispose()
{
if (_shouldDrop)
{
EnsureConnectionOpen();
DropObject();
}
// This explicitly does not drop the wrapped SqlConnection; this is sometimes
// used in a loop to create multiple UDTs.
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Base class for a transient database object (such as a table, type or
/// stored procedure.)
/// </summary>
public abstract class DatabaseObject : DatabaseObject<object?>
{
protected DatabaseObject(SqlConnection connection, string name, string definition, bool shouldCreate, bool shouldDrop)
: base(connection, name, definition, state: null, shouldCreate, shouldDrop)
{
}
}