-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
267 lines (223 loc) · 9.67 KB
/
Extensions.cs
File metadata and controls
267 lines (223 loc) · 9.67 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
//
// @(#) Extensions.cs
//
// Project: usis.Data.LocalDb
// System: Microsoft Visual Studio 2022
// Author: Udo Schäfer
//
// Copyright (c) 2018-2023 usis GmbH. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace usis.Data.LocalDb
{
// ----------------
// Extensions class
// ----------------
/// <summary>
/// Provides extension methods for the <see cref="Manager"/> class.
/// </summary>
public static partial class Extensions
{
#region EnumerateVersions method
// ------------------------
// EnumerateVersions method
// ------------------------
/// <summary>
/// Returns all SQL Server Express LocalDB versions available on the
/// computer.
/// </summary>
/// <param name="manager">The LocalDB manager object.</param>
/// <returns>An enumerator to iterate through all version informations.
/// </returns>
///<example>
///<code>
///using System;
///
///namespace usis.Data.LocalDb.Samples
///{
/// public static class Operations
/// {
/// public static void ListVersions()
/// {
/// using (var manager = Manager.Create())
/// {
/// foreach (var version in manager.EnumerateVersions())
/// {
/// string format;
/// switch (version.Version.Major)
/// {
/// case 11:
/// format = "Microsoft SQL Server 2012 ({0})";
/// break;
/// case 12:
/// format = "Microsoft SQL Server 2014 ({0})";
/// break;
/// case 13:
/// format = "Microsoft SQL Server 2016 ({0})";
/// break;
/// case 14:
/// format = "Microsoft SQL Server 2017 ({0})";
/// break;
/// default:
/// format = "{0}";
/// break;
/// }
/// Console.WriteLine(format, version.Name);
/// }
/// }
/// }
/// }
///}
/// </code>
///</example>
public static IEnumerable<VersionInfo> EnumerateVersions(this Manager manager)
{
return manager == null
? throw new ArgumentNullException(nameof(manager))
: manager.GetVersions().Select(manager.GetVersionInfo);
}
#endregion
#region EnumerateInstances methods
// --------------------------
// EnumerateInstances methods
// --------------------------
/// <summary>
/// Gets the informations about both named and default LocalDB instances
/// on the user’s workstation.
/// </summary>
/// <param name="manager">The LocalDB manager object.</param>
/// <returns>An enumerator to iterate through all instance informations.
/// </returns>
public static IEnumerable<InstanceInfo> EnumerateInstances(this Manager manager)
{
return manager == null
? throw new ArgumentNullException(nameof(manager))
: manager.GetInstances().Select(manager.GetInstanceInfo);
}
#endregion
#region CreateInstance method
// ---------------------
// CreateInstance method
// ---------------------
/// <summary>
/// Creates a new SQL Server Express LocalDB instance with the highest
/// installed version.
/// </summary>
/// <param name="manager">The LocalDB manager object.</param>
/// <param name="instanceName">The name for the LocalDB instance to
/// create.</param>
/// <exception cref="ArgumentNullException"><paramref name="manager"/>
/// is a <c>null</c> reference.</exception>
/// <exception cref="LocalDbException">SQL Server Express LocalDB is not
/// installed on the computer.</exception>
public static void CreateInstance(this Manager manager, string instanceName)
{
if (manager == null) throw new ArgumentNullException(nameof(manager));
var version = manager.GetVersions().OrderByDescending(s => s).FirstOrDefault() ?? throw new LocalDbException(Strings.NotInstalled);
manager.CreateInstance(version, instanceName);
}
#endregion
#region StopInstance method
// -------------------
// StopInstance method
// -------------------
/// <summary>
/// Stops the specified SQL Server Express LocalDB instance from
/// running.
/// </summary>
/// <param name="manager">The LocalDB manager object.</param>
/// <param name="instanceName">The name of the LocalDB instance to stop.
/// </param>
/// <remarks>
/// This function will return immediately without waiting for the
/// LocalDB instance to stop.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="manager"/>
/// is a <c>null</c> reference.</exception>
public static void StopInstance(this Manager manager, string instanceName)
{
if (manager == null) throw new ArgumentNullException(nameof(manager));
manager.StopInstance(instanceName, StopInstanceOptions.None, new TimeSpan(0));
}
/// <summary>
/// Stops the specified SQL Server Express LocalDB instance from
/// running.
/// </summary>
/// <param name="manager">The LocalDB manager object.</param>
/// <param name="instanceName">The name of the LocalDB instance to stop.
/// </param>
/// <param name="timeout">The time in seconds to wait for this operation
/// to complete. If this value is 0, this function will return
/// immediately without waiting for the LocalDB instance to stop.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="manager"/>
/// is a <c>null</c> reference.</exception>
public static void StopInstance(this Manager manager, string instanceName, int timeout)
{
if (manager == null) throw new ArgumentNullException(nameof(manager));
manager.StopInstance(instanceName, StopInstanceOptions.None, new TimeSpan(0, 0, timeout));
}
#endregion
#region internal methods
// ------------------
// GetFunction method
// ------------------
internal static T GetFunction<T>(this NativeLibraryHandle library, string procName, ref T function) where T : class
{
if (function == null)
{
var address = NativeMethods.GetProcAddress(library, procName);
if (address == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
function = Marshal.GetDelegateForFunctionPointer(address, typeof(T)) as T;
}
return function;
}
// ----------------------
// ValidateHResult method
// ----------------------
internal static bool ValidateHResult(this uint hr, Func<uint, Exception> createException, params uint[] values)
{
foreach (var ok in values)
{
if (hr == ok) return (hr & 0x80000000) == 0;
}
return (hr & 0x80000000) != 0 ? throw createException(hr) : true;
}
// ----------------
// Enumerate method
// ----------------
internal static IEnumerable<T> Enumerate<T>(this IntPtr pointer, int count, int size, Func<IntPtr, T> function)
{
for (var i = 0; i < count; i++)
{
var offset = new IntPtr(pointer.ToInt64() + (size * i));
yield return function(offset);
}
}
// ---------------
// ToString method
// ---------------
internal static string ToString(this byte[] bytes, Encoding encoding) => new([.. encoding.GetChars(bytes).TakeWhile(c => c != '\0')]);
// -----------------
// ToDateTime method
// -----------------
internal static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.FILETIME fileTime) => DateTimeFromFileTime(fileTime.dwHighDateTime, fileTime.dwLowDateTime);
#endregion
#region private methods
// ---------------------------
// DateTimeFromFileTime method
// ---------------------------
private static DateTime DateTimeFromFileTime(int high, int low)
{
if (high == 0 && low == 0) return DateTime.MinValue;
var fileTime = ((long)high << 32) + (uint)low;
return DateTime.FromFileTime(fileTime);
}
#endregion
}
}
// eof "Extensions.cs"