-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnowflakeCommand.SingleOrDefault.cs
More file actions
65 lines (55 loc) · 2.23 KB
/
SnowflakeCommand.SingleOrDefault.cs
File metadata and controls
65 lines (55 loc) · 2.23 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
//-----------------------------------------------------------------------
// <copyright file="SnowflakeCommand.SingleOrDefault.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 single item for a query or null if none is found.
/// </summary>
/// <param name="parameterList">The parameter list.</param>
/// <returns>The single item if found, otherwise null.</returns>
/// <exception cref="InvalidOperationException">Result has more then one element.</exception>
public T? SingleOrDefault(IList<(string, DbType, object)>? parameterList = default)
{
if (this._snowflakeDbConnection is null)
{
using var dbConnection = new SnowflakeDbConnection
{
ConnectionString = EnvironmentExtensions.GetSnowflakeConnectionString(),
};
dbConnection.Open();
var item = this.SingleOrDefault(dbConnection, parameterList);
dbConnection.Close();
return item;
}
if (!this._snowflakeDbConnection.IsOpen())
{
this._snowflakeDbConnection.Open();
}
return this.SingleOrDefault(this._snowflakeDbConnection, parameterList);
}
private T? SingleOrDefault(SnowflakeDbConnection snowflakeDbConnection, IList<(string, DbType, object)>? parameterList = default)
{
var command = snowflakeDbConnection.CreateCommand();
command.CommandText = this.Sql;
var totalParameterList = this.ParameterList.Concat(parameterList ?? []).ToList();
foreach (var parameter in totalParameterList)
{
command.AddParameter(parameter.Item1, parameter.Item2, parameter.Item3);
}
var reader = command.ExecuteReader();
var item = reader.ToList<T>().SingleOrDefault();
return item;
}
}
}