-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowflakeCommand.FirstOrDefaultAsync.cs
80 lines (66 loc) · 3.36 KB
/
SnowflakeCommand.FirstOrDefaultAsync.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
//-----------------------------------------------------------------------
// <copyright file="SnowflakeCommand.FirstOrDefaultAsync.cs" company="Jonas Schubert">
// Copyright (c) Jonas Schubert. All rights reserved.
// </copyright>
// <author>Jonas Schubert</author>
//-----------------------------------------------------------------------
namespace Snowflake.Data.Xt;
/// <summary>
/// The snowflake command.
/// </summary>
/// <typeparam name="T">The generic type. This is used to parse properties for the query.</typeparam>
public partial class SnowflakeCommand<T>
where T : class
{
/// <summary>
/// Gets the first item for a query or null if none is found.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The first item if found, otherwise null.</returns>
public Task<T?> FirstOrDefaultAsync(CancellationToken cancellationToken = default)
{
return this.FirstOrDefaultAsync(new List<(string, DbType, object)>() { }, cancellationToken);
}
/// <summary>
/// Gets the first item for a query or null if none is found.
/// </summary>
/// <param name="parameterList">The parameter list.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The first item if found, otherwise null.</returns>
public async Task<T?> FirstOrDefaultAsync(IList<(string, DbType, object)>? parameterList = default, CancellationToken cancellationToken = default)
{
if (this.snowflakeDbConnection is null)
{
using var dbConnection = new SnowflakeDbConnection
{
ConnectionString = EnvironmentExtensions.GetSnowflakeConnectionString(),
};
await dbConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
var item = await this.FirstOrDefaultAsync(dbConnection, parameterList, cancellationToken).ConfigureAwait(false);
await dbConnection.CloseAsync(cancellationToken).ConfigureAwait(false);
return item;
}
if (!this.snowflakeDbConnection.IsOpen())
{
await this.snowflakeDbConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
}
return await this.FirstOrDefaultAsync(this.snowflakeDbConnection, parameterList, cancellationToken).ConfigureAwait(false);
}
private async Task<T?> FirstOrDefaultAsync(SnowflakeDbConnection snowflakeDbConnection, IList<(string, DbType, object)>? parameterList = default, CancellationToken cancellationToken = default)
{
this.WriteLogInformation("Performing snowflake command to retrieve one entity (FirstOrDefaultAsync).");
var command = snowflakeDbConnection.CreateCommand();
this.WriteLogInformation(this.Sql);
command.CommandText = this.Sql;
var totalParameterList = this.ParameterList.Concat(parameterList ?? new List<(string, DbType, object)>()).ToList();
this.WriteLogInformation(string.Format(CultureInfo.InvariantCulture, "Adding {0} parameters.", totalParameterList.Count));
foreach (var parameter in totalParameterList)
{
command.AddParameter(parameter.Item1, parameter.Item2, parameter.Item3);
}
var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
var item = reader.FirstOrDefault<T>();
this.WriteLogInformation($"Found{(item is not null ? string.Empty : " no")} item for the provided query.");
return item;
}
}