Skip to content

Commit

Permalink
Merge pull request #536 from mk3008/535-review-function-name-for-addi…
Browse files Browse the repository at this point in the history
…ng-parameters

Marked AddParameter as obsolete and encouraged the use of Parameter m…
  • Loading branch information
mk3008 authored Sep 6, 2024
2 parents 8abc018 + e9a68b5 commit 3a91af1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions demo/DynamicCTE/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ GROUP BY

// Add date filter condition
var saleDate = ":sale_date";
sq.AddParameter(saleDate, summaryMonth)
sq.Parameter(saleDate, summaryMonth)
.BetweenInclusiveStart("sale_date", saleDate, $"{saleDate}::timestamp + '1 month'");

// Convert the entire query to a CTE
sq = sq.ToCTEQuery("final", "f");
sq = sq.ToCteQuery("final", "f");

// Add sorting conditions
sq.RemoveSelect("sort_number")
Expand Down
2 changes: 1 addition & 1 deletion demo/DynamicFiltering/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ product as p
var sq = SelectQuery.Parse(sql)
.GreaterThanOrEqualIfNotNullOrEmpty("price", minPrice)
.LessThanOrEqualIfNotNullOrEmpty("price", maxPrice)
.AddParameter(pname, category)
.Parameter(pname, category)
.EqualIfNotNullOrEmpty("category", pname)
.EqualIfNotNullOrEmpty("in_stock", inStock);

Expand Down
2 changes: 1 addition & 1 deletion demo/QuerySource/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sales as s
var lower_limit = new DateTime(2024, 7, 20);

var query = SelectQuery.Parse(sql)
.OverrideSelect("journal_date", (source, item) => $"greatest({item}, {source.Query.AddParameter(":lower_limit", lower_limit)})")
.OverrideSelect("journal_date", (source, item) => $"greatest({item}, {source.Query.Parameter(":lower_limit", lower_limit)})")
.NotExists(["sale_id"], "sale_journals")
.GreaterThanOrEqual("request_timestamp", ":lower_limit")
.ToCteQuery("final", "f", out var final)
Expand Down
16 changes: 16 additions & 0 deletions src/Carbunql/Fluent/SelectQueryConvertExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ namespace Carbunql.Fluent;

public static class SelectQueryConvertExtension
{
/// <summary>
/// Returns a select query that references itself as a CTE (Common Table Expression).
/// </summary>
/// <param name="name">The name of the CTE.</param>
/// <param name="alias">
/// The alias to use. If omitted, the select clause will use a wildcard.
/// </param>
/// <returns>The modified select query with the CTE reference.</returns>
/// <exception cref="InvalidProgramException">
/// Thrown if a CTE with the same name already exists.
/// </exception>
public static SelectQuery ToCteQuery(this SelectQuery source, string name, string alias)
{
return source.ToCteQuery(name, alias, out _);
}

/// <summary>
/// Returns a select query that references itself as a CTE (Common Table Expression).
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Carbunql/Fluent/SelectQueryParameterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

public static class SelectQueryParameterExtensions
{
[Obsolete("AddParameter is obsolete. Please use the 'Parameter' method instead.")]
public static SelectQuery AddParameter(this SelectQuery query, string name, object? value)
{
var prm = new QueryParameter(name, value);
query.Parameters.Add(prm);
return query;
}

public static SelectQuery FluentParameter(this SelectQuery query, string name, object? value)
public static SelectQuery Parameter(this SelectQuery query, string name, object? value)
{
var prm = new QueryParameter(name, value);
query.Parameters.Add(prm);
return query;
}

public static SelectQuery FluentParameter(this SelectQuery query, string name, object? value, Func<string, SelectQuery> func)
public static SelectQuery Parameter(this SelectQuery query, string name, object? value, Func<string, SelectQuery> func)
{
var prm = new QueryParameter(name, value);
query.Parameters.Add(prm);
Expand Down
4 changes: 2 additions & 2 deletions test/Carbunql.Building.Test/QuerySourceFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ sale as s
var query = SelectQuery.Parse(sql)
.IfNotNullOrEmpty(product_name, x =>
{
x.FluentParameter(":product_name"
x.Parameter(":product_name"
, product_name
, name => x.Equal(nameof(product_name), name)
);
Expand All @@ -1402,7 +1402,7 @@ sale AS s
query = SelectQuery.Parse(sql)
.IfNotNullOrEmpty(product_name, x =>
{
x.FluentParameter(":product_name"
x.Parameter(":product_name"
, product_name
, name => x.Equal(nameof(product_name), name)
);
Expand Down

0 comments on commit 3a91af1

Please sign in to comment.