Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 84 additions & 28 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,28 +1,84 @@
root = true

[*]
charset = utf-8
end_of_line = lf
# Note: the trim_trailing_whitespace option is br0ken in visualstudio, it
# simply does not follow the EditorConfig specification. Therefor you are
# strongly encouraged to not rely on this setting alone, but please install
# the following extension too: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.TrailingWhitespaceVisualizer
#
# References:
# https://developercommunity.visualstudio.com/t/EditorConfig:-trim_trailing_whitespace-d/1240174?space=8&q=trim_trailing_whitespace
# https://developercommunity.visualstudio.com/t/editorconfig-trim_trailing_whitespace-on/134457?space=8&q=trim_trailing_whitespace
# https://developercommunity.visualstudio.com/t/trim_trailing_whitespace-in-editorconfi/1351034?space=8&q=trim_trailing_whitespace
# https://developercommunity.visualstudio.com/t/BUG:-editorconfig-trim_trailing_whitespa/953937?space=8&q=trim_trailing_whitespace
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab
indent_size = 2


[*.cs] # To match existing style
indent_style = space
indent_size = 4


[*.yml]
indent_style = space
# top-most EditorConfig file
root = true

###########################################################
# Core EditorConfig Settings - Apply to ALL files
###########################################################

[*]
# Indentation and spacing
indent_style = space
indent_size = 4
tab_width = 4

# Newline preferences
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

###########################################################
# .NET / C# Code Style Rules
###########################################################

[*.cs]

#### C# Language Style Rules (csharp_style_) ####

# Use "var" when the type is obvious or when a tuple/anonymous type
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_when_type_is_obvious = true:suggestion
csharp_style_var_elsewhere = false:suggestion

# Use file-scoped namespaces (if targeting .NET 6+)
csharp_style_namespace_declarations = file_scoped:warning

# Use pattern matching for 'is' and 'switch'
csharp_style_pattern_matching_over_is_with_not_null = true:suggestion
csharp_style_pattern_matching_over_as = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion

# Use explicit type for arrays
csharp_style_explicit_array_type = true:suggestion

# Use simplified accessors (e.g., private set)
csharp_style_expression_bodied_accessors = false:suggestion
csharp_style_expression_bodied_properties = false:suggestion
csharp_style_expression_bodied_methods = false:suggestion

# Prefer to use `_` or `this.` for member access
csharp_prefer_static_local_function = true:suggestion
csharp_style_qualification_for_field = false:suggestion
csharp_style_qualification_for_property = false:suggestion
csharp_style_qualification_for_method = false:suggestion
csharp_style_qualification_for_event = false:suggestion

# Prefer throwing an exception over returning null (if applicable)
csharp_style_throw_expression = true:suggestion

#### .NET Language Style Rules (dotnet_style_) ####

# Use 'using' declarations instead of using statements
dotnet_style_prefer_dispose_pattern = true:suggestion

# Prefer collection expressions for arrays and lists (if targeting C# 12+)
dotnet_style_prefer_collection_expression = true:suggestion

# Prefer to use C# built-in type names (e.g., 'int' instead of 'Int32')
dotnet_style_predefined_type_names = true:suggestion

#### Formatting Rules (csharp_space_, csharp_indent_) ####

# Control spacing around binary operators (e.g., `a = b` vs `a=b`)
csharp_space_around_binary_operators = true:suggestion

# Control space within parentheses (e.g., `( a )` vs `(a)`)
csharp_space_within_parentheses = false:suggestion

# Control new line for braces (e.g., `if () {` vs `if () \n {`)
csharp_new_line_before_open_brace = all:suggestion
csharp_new_line_before_members_in_classes = true:suggestion
csharp_new_line_before_members_in_structs = true:suggestion

# Ensure using directives are sorted alphabetically
csharp_using_directive_placement = inside_namespace:suggestion
174 changes: 82 additions & 92 deletions QueryBuilder.Tests/AggregateTests.cs
Original file line number Diff line number Diff line change
@@ -1,92 +1,82 @@
using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using Xunit;

namespace SqlKata.Tests
{
public class AggregateTests : TestSupport
{
[Fact]
public void Count()
{
var query = new Query("A").AsCount();

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM [A]", c[EngineCodes.SqlServer]);
Assert.Equal("SELECT COUNT(*) AS `count` FROM `A`", c[EngineCodes.MySql]);
Assert.Equal("SELECT COUNT(*) AS \"count\" FROM \"A\"", c[EngineCodes.PostgreSql]);
Assert.Equal("SELECT COUNT(*) AS \"COUNT\" FROM \"A\"", c[EngineCodes.Firebird]);
}

[Fact]
public void CountMultipleColumns()
{
var query = new Query("A").AsCount(new[] { "ColumnA", "ColumnB" });

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT 1 FROM [A] WHERE [ColumnA] IS NOT NULL AND [ColumnB] IS NOT NULL) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void DistinctCount()
{
var query = new Query("A").Distinct().AsCount();

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT DISTINCT * FROM [A]) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void DistinctCountMultipleColumns()
{
var query = new Query("A").Distinct().AsCount(new[] { "ColumnA", "ColumnB" });

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT DISTINCT [ColumnA], [ColumnB] FROM [A]) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Average()
{
var query = new Query("A").AsAverage("TTL");

var c = Compile(query);

Assert.Equal("SELECT AVG([TTL]) AS [avg] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Sum()
{
var query = new Query("A").AsSum("PacketsDropped");

var c = Compile(query);

Assert.Equal("SELECT SUM([PacketsDropped]) AS [sum] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Max()
{
var query = new Query("A").AsMax("LatencyMs");

var c = Compile(query);

Assert.Equal("SELECT MAX([LatencyMs]) AS [max] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Min()
{
var query = new Query("A").AsMin("LatencyMs");

var c = Compile(query);

Assert.Equal("SELECT MIN([LatencyMs]) AS [min] FROM [A]", c[EngineCodes.SqlServer]);
}
}
}
namespace SqlKata.Tests;

using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using Xunit;

public class AggregateTests : TestSupport {
[Fact]
public void Count() {
var query = new Query("A").AsCount();

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM [A]", c[EngineCodes.SqlServer]);
Assert.Equal("SELECT COUNT(*) AS `count` FROM `A`", c[EngineCodes.MySql]);
Assert.Equal("SELECT COUNT(*) AS \"count\" FROM \"A\"", c[EngineCodes.PostgreSql]);
Assert.Equal("SELECT COUNT(*) AS \"COUNT\" FROM \"A\"", c[EngineCodes.Firebird]);
}

[Fact]
public void CountMultipleColumns() {
var query = new Query("A").AsCount(new[] { "ColumnA", "ColumnB" });

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT 1 FROM [A] WHERE [ColumnA] IS NOT NULL AND [ColumnB] IS NOT NULL) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void DistinctCount() {
var query = new Query("A").Distinct().AsCount();

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT DISTINCT * FROM [A]) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void DistinctCountMultipleColumns() {
var query = new Query("A").Distinct().AsCount(new[] { "ColumnA", "ColumnB" });

var c = Compile(query);

Assert.Equal("SELECT COUNT(*) AS [count] FROM (SELECT DISTINCT [ColumnA], [ColumnB] FROM [A]) AS [countQuery]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Average() {
var query = new Query("A").AsAverage("TTL");

var c = Compile(query);

Assert.Equal("SELECT AVG([TTL]) AS [avg] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Sum() {
var query = new Query("A").AsSum("PacketsDropped");

var c = Compile(query);

Assert.Equal("SELECT SUM([PacketsDropped]) AS [sum] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Max() {
var query = new Query("A").AsMax("LatencyMs");

var c = Compile(query);

Assert.Equal("SELECT MAX([LatencyMs]) AS [max] FROM [A]", c[EngineCodes.SqlServer]);
}

[Fact]
public void Min() {
var query = new Query("A").AsMin("LatencyMs");

var c = Compile(query);

Assert.Equal("SELECT MIN([LatencyMs]) AS [min] FROM [A]", c[EngineCodes.SqlServer]);
}
}
Loading