-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataOperations.cs
408 lines (338 loc) · 15.3 KB
/
DataOperations.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using BaseExceptionsLibrary;
using ConfigurationHelper;
using DataTableHelpers;
using StopWatchLibrary;
namespace SqlOperations.Classes
{
public class DataOperations : BaseExceptionProperties
{
private static readonly string ConnectionString = Helper.GetConnectionString();
public delegate void OnConnect(string message);
public static event OnConnect ConnectMonitor;
public delegate void OnAfterConnect(string message);
public static event OnAfterConnect AfterConnectMonitor;
public static void GetConnectionString()
{
Debug.WriteLine(Helper.GetConnectionString());
}
public static async Task<DataTableResults> ReadProductsUsingContainer(CancellationToken ct)
{
StopWatcher.Instance.Start();
var result = new DataTableResults() { DataTable = new DataTable() };
return await Task.Run(async () =>
{
await using var cn = new SqlConnection(ConnectionString);
Debug.WriteLine(cn.ConnectionString);
await using var cmd = new SqlCommand {Connection = cn, CommandText = SelectStatement()};
try
{
ConnectMonitor?.Invoke("Before open");
await cn.OpenAsync(ct);
AfterConnectMonitor?.Invoke($"Elapsed: {StopWatcher.Instance.Elapsed}");
}
#pragma warning disable 168
catch (TaskCanceledException tce)
#pragma warning restore 168
{
/*
* For debug purposes we have a variable for TaskCanceledException
* although we can still see it w/o tce but this is easier for those
* not knowledgeable with no var for the exception
*/
result.ConnectionFailed = true;
result.ExceptionMessage = "Connection Failed";
return result;
}
catch (Exception ex)
{
result.GeneralException = ex;
return result;
}
result.DataTable.Load(await cmd.ExecuteReaderAsync(ct));
/*
* When coding is done properly, these checks are not needed but
* there are times when a developer slips so let's perform assertion
*
* Otherwise (and even better) remove the assertion and allow a run
* time exception to be thrown while in development or test mode.
*
* Note:
* First check is a language extension does the same as the other
* checks which some may like
*/
if (result.DataTable.ColumnExists("ProductID"))
{
result.DataTable.Columns["ProductID"].ColumnMapping = MappingType.Hidden;
}
if (result.DataTable.Columns.Contains("SupplierID"))
{
result.DataTable.Columns["SupplierID"].ColumnMapping = MappingType.Hidden;
}
if (result.DataTable.Columns.Contains("CategoryID"))
{
result.DataTable.Columns["CategoryID"].ColumnMapping = MappingType.Hidden;
}
return result;
}, ct);
}
/// <summary>
/// Read products into a DataTable with connection timeout where product is in a specific category
/// </summary>
/// <param name="ct"><seealso cref="CancellationToken"/></param>
/// <param name="categoryIdentifier">Existing category identifier</param>
/// <returns></returns>
public static async Task<DataTableResults> ReadProductsUsingContainerByCategory(CancellationToken ct, int categoryIdentifier)
{
StopWatcher.Instance.Start();
var result = new DataTableResults() { DataTable = new DataTable() };
return await Task.Run(async () =>
{
await using var cn = new SqlConnection(ConnectionString);
await using var cmd = new SqlCommand { Connection = cn, CommandText = SelectStatement(categoryIdentifier) };
try
{
ConnectMonitor?.Invoke("Before open");
await cn.OpenAsync(ct);
AfterConnectMonitor?.Invoke($"Elapsed: {StopWatcher.Instance.Elapsed}");
}
#pragma warning disable 168
catch (TaskCanceledException tce)
#pragma warning restore 168
{
/*
* For debug purposes we have a variable for TaskCanceledException
* although we can still see it w/o tce but this is easier for those
* not knowledgeable with no var for the exception
*/
result.ConnectionFailed = true;
result.ExceptionMessage = "Connection Failed";
return result;
}
catch (Exception ex)
{
result.GeneralException = ex;
return result;
}
result.DataTable.Load(await cmd.ExecuteReaderAsync(ct));
return result;
}, ct);
}
/// <summary>
/// Update a single product
/// </summary>
/// <param name="product">Valid product</param>
/// <returns>success or failure</returns>
public static async Task<bool> Update(Products product)
{
mHasException = false;
await using var cn = new SqlConnection(ConnectionString);
await using var cmd = new SqlCommand { Connection = cn, CommandText = SelectStatement() };
try
{
cmd.Parameters.AddWithValue("@ProductName", product.ProductName);
cmd.Parameters.AddWithValue("@SupplierID", product.SupplierId);
cmd.Parameters.AddWithValue("@CategoryID", product.CategoryId);
cmd.Parameters.AddWithValue("@QuantityPerUnit", product.QuantityPerUnit);
cmd.Parameters.AddWithValue("@UnitPrice", product.UnitPrice);
cmd.Parameters.AddWithValue("@UnitsInStock", product.UnitsInStock);
cmd.Parameters.AddWithValue("@UnitsOnOrder", product.UnitsOnOrder);
cmd.Parameters.AddWithValue("@ReorderLevel", product.ReorderLevel);
cmd.Parameters.AddWithValue("@Discontinued", product.Discontinued);
cmd.Parameters.AddWithValue("@DiscontinuedDate", product.DiscontinuedDate);
cmd.Parameters.AddWithValue("@ProductID", product.ProductId);
return cmd.ExecuteNonQuery() == 1;
}
catch (Exception e)
{
mHasException = true;
mLastException = e;
return false;
}
}
public static DataTable DatabaseTableDetails()
{
using var cn = new SqlConnection(ConnectionString);
using var cmd = new SqlCommand { Connection = cn, CommandText = DatabaseDetailsSelectStatement };
cn.Open();
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
/// <summary>
/// Example for reading data and returning via a named value tuple
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples
/// </summary>
/// <param name="ct"></param>
/// <returns></returns>
public static async Task<(bool success, DataTable dataTable, Exception exception)> ReadProductsUsingNamedValueTuple(CancellationToken ct)
{
StopWatcher.Instance.Start();
DataTable dt = new ();
return await Task.Run(async () =>
{
await using var cn = new SqlConnection(ConnectionString);
await using var cmd = new SqlCommand { Connection = cn, CommandText = SelectStatement() };
try
{
ConnectMonitor?.Invoke("Before open");
await cn.OpenAsync(ct);
AfterConnectMonitor?.Invoke($"Elapsed: {StopWatcher.Instance.Elapsed}");
dt.Load(await cmd.ExecuteReaderAsync(ct));
/*
* See comments in method above on accessing columns
*/
dt.Columns["ProductID"].ColumnMapping = MappingType.Hidden;
dt.Columns["SupplierID"].ColumnMapping = MappingType.Hidden;
dt.Columns["CategoryID"].ColumnMapping = MappingType.Hidden;
return (true, dt, null);
}
catch (Exception ex)
{
return (false, dt, ex);
}
}, ct);
}
public static async Task<Customers> SingleCustomer(int identifier)
{
Customers customer = new ();
await using var cn = new SqlConnection(ConnectionString);
await using var cmd = new SqlCommand { Connection = cn, CommandText = SelectStatement() };
cmd.CommandText =
"SELECT TOP (1) CustomerIdentifier, CompanyName, ContactId, Street, City, PostalCode, CountryIdentifier " +
"FROM dbo.Customers " +
"WHERE CustomerIdentifier = @CustomerIdentifier";
cmd.Parameters.Add("@CustomerIdentifier", SqlDbType.Int).Value = identifier;
await cn.OpenAsync();
var reader = await cmd.ExecuteReaderAsync();
if (!reader.HasRows) return customer;
reader.Read();
customer.CustomerIdentifier = reader.GetInt32(0);
customer.CompanyName = reader.GetString(1);
customer.ContactId = reader.GetInt32(2);
customer.Street = reader.GetString(3);
customer.City = reader.GetString(4);
customer.PostalCode = reader.GetString(5);
customer.CountryIdentifier = reader.GetInt32(6);
return customer;
}
/// <summary>
/// This SQL was generated in Microsoft SQL-Server Management Studio (it's free)
/// </summary>
/// <returns></returns>
private static string SelectStatement() =>
@"
SELECT
P.ProductID,
P.ProductName,
P.SupplierID,
S.CompanyName,
P.CategoryID,
C.CategoryName,
P.QuantityPerUnit,
P.UnitPrice,
P.UnitsInStock,
P.UnitsOnOrder,
P.ReorderLevel,
P.Discontinued,
P.DiscontinuedDate
FROM
Products AS P
INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID
INNER JOIN Suppliers AS S ON P.SupplierID = S.SupplierID";
private static string SelectStatement(int categoryIdentifier) =>
@$"
SELECT
P.ProductID,
P.ProductName,
P.SupplierID,
S.CompanyName,
P.CategoryID,
C.CategoryName,
P.QuantityPerUnit,
P.UnitPrice,
P.UnitsInStock,
P.UnitsOnOrder,
P.ReorderLevel,
P.Discontinued,
P.DiscontinuedDate
FROM
Products AS P
INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID
INNER JOIN Suppliers AS S ON P.SupplierID = S.SupplierID
WHERE P.CategoryID = {categoryIdentifier}";
private static string DatabaseDetailsSelectStatement =>
@"
SELECT syso.name [Table],
sysc.name [Field],
sysc.colorder [FieldOrder],
syst.name [DataType],
sysc.[length] [Length],
sysc.prec [Precision],
CASE WHEN sysc.scale IS null THEN '-' ELSE sysc.scale END [Scale],
CASE WHEN sysc.isnullable = 1 THEN 'True' ELSE 'False' END [AllowNulls],
CASE WHEN sysc.[status] = 128 THEN 'True' ELSE 'False' END [Identity],
CASE WHEN sysc.colstat = 1 THEN 'True' ELSE 'False' END [PrimaryKey],
CASE WHEN fkc.parent_object_id is NULL THEN 'False' ELSE 'True' END [ForeignKey?],
CASE WHEN fkc.parent_object_id is null THEN '-' ELSE obj.name END [RelatedTable],
CASE WHEN ep.value is NULL THEN '-' ELSE CAST(ep.value as NVARCHAR(500)) END [Description]
FROM [sys].[sysobjects] AS syso
JOIN [sys].[syscolumns] AS sysc on syso.id = sysc.id
LEFT JOIN [sys].[systypes] AS syst ON sysc.xtype = syst.xtype and syst.name != 'sysname'
LEFT JOIN [sys].[foreign_key_columns] AS fkc on syso.id = fkc.parent_object_id and sysc.colid = fkc.parent_column_id
LEFT JOIN [sys].[objects] AS obj ON fkc.referenced_object_id = obj.[object_id]
LEFT JOIN [sys].[extended_properties] AS ep ON syso.id = ep.major_id and sysc.colid = ep.minor_id and ep.name = 'MS_Description'
WHERE syso.type = 'U' AND syso.name != 'sysdiagrams'
ORDER BY [Table], FieldOrder, Field;
";
private static string UpdateStatement =>
@"
UPDATE dbo.Products
SET ProductName = @ProductName
,SupplierID = @SupplierID
,CategoryID = @CategoryID
,QuantityPerUnit = @QuantityPerUnit
,UnitPrice = @UnitPrice
,UnitsInStock = @UnitsInStock
,UnitsOnOrder = @UnitsOnOrder
,ReorderLevel = @ReorderLevel
,Discontinued = @Discontinued
,DiscontinuedDate = @DiscontinuedDate
WHERE ProductID = @ProductID";
/// <summary>
/// Used to set column header text in a DataGridView.
/// The primary key column are abbreviated as are generally not shown
/// other than for debugging purposes
/// </summary>
/// <remarks>
/// See also
/// Microsoft TechNet article: DataGridView setup header text using SQL-Server
/// https://social.technet.microsoft.com/wiki/contents/articles/52160.datagridview-setup-header-text-using-sql-server.aspx
/// </remarks>
/// <returns></returns>
public static Dictionary<string, string> GetDataGridViewColumnText()
{
return new() {
{ "ProductID","Id" },
{ "ProductName","Name" },
{ "SupplierID","Supp Id" },
{ "CompanyName","Company" },
{ "CategoryID","Cat Id" },
{ "CategoryName", "Category" },
{ "QuantityPerUnit", "Qty Per Unit" },
{ "UnitPrice", "Price" },
{ "UnitsInStock", "In stock" },
{ "UnitsOnOrder","On order" },
{ "ReorderLevel", "Reorder" },
{ "Discontinued", "Discontinued" },
{ "DiscontinuedDate","Dis date" }
};
}
}
}