Skip to content

Commit

Permalink
Merge pull request #456 from mk3008/455-changes-to-sql-comments
Browse files Browse the repository at this point in the history
Change the output location of the comment clause
  • Loading branch information
mk3008 authored Jun 22, 2024
2 parents ef21958 + db88b35 commit fcbf90e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/TypeSafeBuild/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private static void Main(string[] args)
// Debug
var query = SelectSaleReportQuery();
var sale = SelectSaleTestDataQuery();
query.AddHeaderComment("unit test query");
query.With(() => sale);

Console.WriteLine(query.ToText());
Expand Down
1 change: 1 addition & 0 deletions src/Carbunql.TypeSafe/FluentSelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ private string RemoveRootBracketOrDefault(string value)
q.LimitClause = LimitClause;
q.Parameters = Parameters;
q.CommentClause = CommentClause;
q.HeaderCommentClause = HeaderCommentClause;

var clause = TableDefinitionClauseFactory.Create<T>();

Expand Down
2 changes: 1 addition & 1 deletion src/Carbunql/Carbunql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Title></Title>
<Copyright>mk3008net</Copyright>
<Description>Carbunql provides query parsing and building functionality.</Description>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<Authors>mk3008net</Authors>
<PackageProjectUrl>https://github.com/mk3008/Carbunql</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
37 changes: 34 additions & 3 deletions src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,39 @@ public SelectQuery(string query)
public WindowClause? WindowClause { get; set; }

/// <summary>
/// Gets or sets the comment clause of the select query.
/// Gets or sets the comment clause of the select clause.
/// </summary>
/// <remarks>
/// A comment that is output before the selection clause. It is suitable for indicating the nature of the selection query.
/// Since it is output after the With clause, it is less visible than the HeaderCommentClause.
/// </remarks>
[IgnoreMember]
public CommentClause? CommentClause { get; set; }

/// <summary>
/// Gets or sets the comment clause for the select query.
/// This is printed before the With clause, which can be useful for debugging.
/// </summary>
/// <remarks>
/// This is printed before the With clause and is useful for debugging.
/// However, it is not printed if it is not the root query.
/// </remarks>
[IgnoreMember]
public CommentClause? HeaderCommentClause { get; set; }

public void AddHeaderComment(string comment)
{
HeaderCommentClause ??= new CommentClause();
HeaderCommentClause.Add(comment);
}

/// <inheritdoc/>
public override IEnumerable<Token> GetCurrentTokens(Token? parent)
{
if (CommentClause != null)
// If this is a root query, a header comment is printed on the first line.
if (parent == null && HeaderCommentClause != null)
{
foreach (var item in CommentClause.GetTokens(parent))
foreach (var item in HeaderCommentClause.GetTokens(parent))
{
yield return item;
}
Expand All @@ -119,6 +141,15 @@ public override IEnumerable<Token> GetCurrentTokens(Token? parent)
}
}

// Comments will be unified to be displayed just before the selected section.
if (CommentClause != null)
{
foreach (var item in CommentClause.GetTokens(parent))
{
yield return item;
}
}

if (SelectClause == null)
{
// If SelectClause is not specified,
Expand Down

0 comments on commit fcbf90e

Please sign in to comment.