-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowflakeCommand.Having.cs
47 lines (41 loc) · 1.9 KB
/
SnowflakeCommand.Having.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
//-----------------------------------------------------------------------
// <copyright file="SnowflakeCommand.Having.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>
/// Adds a having value. Requires a group by clause.
/// https://docs.snowflake.com/en/sql-reference/constructs/having .
/// </summary>
/// <param name="having">The having value.</param>
/// <returns>The snowflake command.</returns>
/// <exception cref="InvalidOperationException">Command already has a having clause.</exception>
/// <exception cref="InvalidOperationException">Group By is missing and has to be called before Having.</exception>
/// <exception cref="ArgumentNullException">Value for having predicate may not be empty.</exception>
public SnowflakeCommand<T> Having(string having)
{
if (this.Sql.Contains("HAVING", StringComparison.Ordinal))
{
throw new InvalidOperationException("Command already has a having clause!");
}
if (!this.Sql.Contains("GROUP BY", StringComparison.Ordinal))
{
throw new InvalidOperationException("Group By is missing and has to be called before Having!");
}
if (string.IsNullOrWhiteSpace(having))
{
throw new ArgumentNullException(nameof(having), "Value for having predicate may not be empty!");
}
this.SqlBuilder.Append($" {(having.Trim().StartsWith("HAVING", ignoreCase: true, CultureInfo.InvariantCulture) ? having.Trim() : $"HAVING {having.Trim()}")}");
return this;
}
}