-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnowflakeCommand.Top.cs
49 lines (45 loc) · 1.91 KB
/
SnowflakeCommand.Top.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
//-----------------------------------------------------------------------
// <copyright file="SnowflakeCommand.Top.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>
/// Sets a maximum amount of rows to return.
/// https://docs.snowflake.com/en/sql-reference/constructs/top_n .
/// </summary>
/// <param name="amount">The amount.</param>
/// <returns>The snowflake command.</returns>
/// <exception cref="InvalidOperationException">Command is already marked using top amount.</exception>
/// <exception cref="ArgumentOutOfRangeException">Amount must be a positive integer.</exception>
public SnowflakeCommand<T> Top(int amount)
{
if (this.Sql.Contains("SELECT DISTINCT TOP", StringComparison.Ordinal) || this.Sql.Contains("SELECT TOP", StringComparison.Ordinal))
{
throw new InvalidOperationException("Command is already marked using top amount!");
}
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be a positive integer!");
}
if (this.Sql.StartsWith("SELECT DISTINCT", StringComparison.InvariantCulture))
{
this.SqlBuilder.Replace("SELECT DISTINCT", string.Format(CultureInfo.InvariantCulture, "SELECT DISTINCT TOP {0}", amount));
}
else
{
this.SqlBuilder.Replace("SELECT", string.Format(CultureInfo.InvariantCulture, "SELECT TOP {0}", amount));
}
return this;
}
}
}