Skip to content

Commit

Permalink
Add fromBegin parameter to SelectAll method to specify whether to…
Browse files Browse the repository at this point in the history
… insert at the beginning of the SELECT clause. If true, will insert with comma if needed. (#16)
  • Loading branch information
guanghuang authored Dec 16, 2024
1 parent c06dbce commit b4360c6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ builder.SelectAll<Customer>().RawSql(", count(*) as TotalCount") // append total
- .NET 7.0+

## Version History
- 1.2.7.1
- Add `fromBegin` parameter to `SelectAll` method to specify whether to insert at the beginning of the SELECT clause. If true, will insert with comma if needed.

- 1.2.7
- Update the code to support mix order of select and from. Previously, the order of select and from must be select first and from later. Now, the order of select and from could be mixed.

Expand Down
30 changes: 22 additions & 8 deletions src/SqlBuilder/SqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,32 @@ public SqlBuilder MapTable<T>(string name)
/// Appends a SELECT clause or column to the query.
/// </summary>
/// <param name="sql">The SQL fragment to append</param>
private void AppendSelect(string sql)
/// <param name="fromBegin">Whether to insert at the beginning of the SELECT clause. If true, will insert with comma if needed</param>
private void AppendSelect(string sql, bool fromBegin = false)
{
if (hasSelect)
{
selectBuilder.Append(", ");
if (fromBegin)
{
selectBuilder.Insert(0, ", ");
}
else
{
selectBuilder.Append(", ");
}
}
else
{
hasSelect = true;
selectBuilder.Append("SELECT ");
}

selectBuilder.Append(sql).AppendLine();
if (fromBegin)
{
selectBuilder.Insert(0, sql);
}
else
{
selectBuilder.Append(sql).AppendLine();
}
}

/// <summary>
Expand Down Expand Up @@ -143,11 +156,12 @@ private string GetTablePrefix(Type type, bool alwaysCreateNew)
/// <param name="excludePropertyExpressions">Properties to exclude from the selection</param>
/// <param name="firstPropertyExpression">Property to place first in the selection</param>
/// <param name="prefix">Optional table prefix/alias</param>
/// <param name="fromBegin">Whether to insert at the beginning of the SELECT clause. If true, will insert with comma if needed</param>
/// <returns>The current SqlBuilder instance for method chaining</returns>
public SqlBuilder SelectAll<T>(Expression<Func<T, object>>[]? excludePropertyExpressions, Expression<Func<T, object>>? firstPropertyExpression, string? prefix = null)
public SqlBuilder SelectAll<T>(Expression<Func<T, object>>[]? excludePropertyExpressions, Expression<Func<T, object>>? firstPropertyExpression, string? prefix = null, bool fromBegin = false)
{
prefix ??= GetTablePrefix(typeof(T), false);
AppendSelect(Utils.GenerateSelectAllColumns(typeof(T), _nameConvention, excludePropertyExpressions, firstPropertyExpression, prefix));
AppendSelect(Utils.GenerateSelectAllColumns(typeof(T), _nameConvention, excludePropertyExpressions, firstPropertyExpression, prefix), fromBegin);
return this;
}

Expand Down Expand Up @@ -815,6 +829,6 @@ public SqlBuilder RawSql(string rawSql, bool afterFrom = true)
/// <returns>The constructed SQL query</returns>
public string Build()
{
return selectBuilder.ToString() + fromBuilder.ToString();
return "SELECT " + selectBuilder + fromBuilder;
}
}
2 changes: 1 addition & 1 deletion src/SqlBuilder/SqlBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<RepositoryUrl>https://github.com/guanghuang/SqlBuilder</RepositoryUrl>
<PackageTags>Query;Sql;SqlBuilder</PackageTags>
<Nullable>enable</Nullable>
<Version>1.2.7</Version>
<Version>1.2.7.1</Version>
<Copyright>Copyright © 2024 Kvr.SqlBuilder. All rights reserved.</Copyright>
</PropertyGroup>
<ItemGroup>
Expand Down
44 changes: 44 additions & 0 deletions test/SqlBuilder.Tests/SqlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,50 @@ public void SelectAll_WithNotMappedAndColumnAttribute_ExcludesNotMappedPropertie
sql);
}

[Fact]
public void SelectAll_WithFromBeginTrue_GeneratesCorrectSql()
{
// Arrange
Kvr.SqlBuilder.SqlBuilder.UseGlobalNameConvention(DefaultNameConvention.Create());
var builder = new Kvr.SqlBuilder.SqlBuilder();

// Act
var sql = builder
.SelectAll<Order>()
.SelectAll<Customer>(null, null, fromBegin: true)
.From<Customer>()
.Build();

// Assert
AssertSqlEqual(
"SELECT kvr1.Id as Id, kvr1.FirstName as FirstName, kvr1.LastName as LastName, kvr1.Email as Email, " +
"kvr0.OrderId as OrderId, kvr0.CustomerId as CustomerId, kvr0.Amount as Amount, kvr0.OrderDate as OrderDate " +
"FROM Customer kvr1",
sql);
}

[Fact]
public void SelectAll_WithFromBeginFalse_GeneratesCorrectSql()
{
// Arrange
Kvr.SqlBuilder.SqlBuilder.UseGlobalNameConvention(DefaultNameConvention.Create());
var builder = new Kvr.SqlBuilder.SqlBuilder();

// Act
var sql = builder
.SelectAll<Order>()
.SelectAll<Customer>(null, null, fromBegin: false)
.From<Customer>()
.Build();

// Assert
AssertSqlEqual(
"SELECT kvr0.OrderId as OrderId, kvr0.CustomerId as CustomerId, kvr0.Amount as Amount, kvr0.OrderDate as OrderDate, " +
"kvr1.Id as Id, kvr1.FirstName as FirstName, kvr1.LastName as LastName, kvr1.Email as Email " +
"FROM Customer kvr1",
sql);
}

private class EntityWithNotMapped
{
public int Id { get; set; }
Expand Down

0 comments on commit b4360c6

Please sign in to comment.