-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowflakeCommand.ToListAsync.cs
81 lines (68 loc) · 3.24 KB
/
SnowflakeCommand.ToListAsync.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
//-----------------------------------------------------------------------
// <copyright file="SnowflakeCommand.ToListAsync.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 a list of items for a query.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A list of items.</returns>
public Task<IList<T>> ToListAsync(CancellationToken cancellationToken = default)
{
return this.ToListAsync([], cancellationToken);
}
/// <summary>
/// Gets a list of items for a query.
/// </summary>
/// <param name="parameterList">The parameter list.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A list of items.</returns>
public async Task<IList<T>> ToListAsync(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 list = await this.ToListAsync(dbConnection, parameterList, cancellationToken).ConfigureAwait(false);
await dbConnection.CloseAsync(cancellationToken).ConfigureAwait(false);
return list;
}
if (!this._snowflakeDbConnection.IsOpen())
{
await this._snowflakeDbConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
}
return await this.ToListAsync(this._snowflakeDbConnection, parameterList, cancellationToken).ConfigureAwait(false);
}
private async Task<IList<T>> ToListAsync(SnowflakeDbConnection snowflakeDbConnection, IList<(string, DbType, object)>? parameterList = default, CancellationToken cancellationToken = default)
{
this.WriteLogInformation("Performing snowflake command to retrieve a list of entities (ToListAsync).");
var command = snowflakeDbConnection.CreateCommand();
this.WriteLogInformation(this.Sql);
command.CommandText = this.Sql;
var totalParameterList = this.ParameterList.Concat(parameterList ?? []).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 list = reader.ToList<T>();
this.WriteLogInformation($"Found {list.Count} items for the provided query.");
return list;
}
}
}