Skip to content

Commit

Permalink
Merge pull request #552 from mk3008/551-unparsable-table-alias-for-ex…
Browse files Browse the repository at this point in the history
…tracted-columns-year-month

Handle Extract function units (e.g., YEAR, MONTH) as reserved keywords.
  • Loading branch information
mk3008 authored Oct 21, 2024
2 parents 8982cf7 + 6a11212 commit b7b554c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Carbunql/Analysis/Parser/FromArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class FromArgumentParser
/// <param name="unit">The unit value.</param>
/// <param name="argument">The SQL text containing the FROM argument.</param>
/// <returns>The parsed FROM argument.</returns>
public static FromArgument Parse(ValueBase unit, string argument)
public static FromArgument Parse(string unit, string argument)
{
var r = new SqlTokenReader(argument);
return Parse(unit, r);
Expand All @@ -26,7 +26,7 @@ public static FromArgument Parse(ValueBase unit, string argument)
/// <param name="unit">The unit value.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed FROM argument.</returns>
public static FromArgument Parse(ValueBase unit, ITokenReader r)
public static FromArgument Parse(string unit, ITokenReader r)
{
var value = ValueParser.Parse(r);
return new FromArgument(unit, value);
Expand Down
2 changes: 1 addition & 1 deletion src/Carbunql/Analysis/Parser/ValueCollectionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static IEnumerable<ValueBase> ReadValues(ITokenReader r)

if (r.ReadOrDefault("from") != null)
{
yield return FromArgumentParser.Parse(v, r);
yield return FromArgumentParser.Parse(v.ToText(), r);
}
else if (r.ReadOrDefault("as") != null)
{
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 is an advanced Raw SQL editing library.</Description>
<Version>0.8.13</Version>
<Version>0.8.13.1</Version>
<Authors>mk3008net</Authors>
<PackageProjectUrl>https://github.com/mk3008/Carbunql</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
33 changes: 5 additions & 28 deletions src/Carbunql/Values/FromArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public FromArgument()
/// </summary>
/// <param name="unit">The unit value.</param>
/// <param name="value">The value.</param>
public FromArgument(ValueBase unit, ValueBase value)
public FromArgument(string unit, ValueBase value)
{
Unit = unit;
Value = value;
Expand All @@ -31,7 +31,7 @@ public FromArgument(ValueBase unit, ValueBase value)
/// <summary>
/// Gets or sets the unit value.
/// </summary>
public ValueBase Unit { get; init; }
public string Unit { get; init; }

/// <summary>
/// Gets or sets the value.
Expand All @@ -41,10 +41,6 @@ public FromArgument(ValueBase unit, ValueBase value)
/// <inheritdoc/>
protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
{
foreach (var item in Unit.GetInternalQueries())
{
yield return item;
}
foreach (var item in Value.GetInternalQueries())
{
yield return item;
Expand All @@ -54,18 +50,16 @@ protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
/// <inheritdoc/>
public override IEnumerable<Token> GetCurrentTokens(Token? parent)
{
foreach (var item in Unit.GetTokens(parent)) yield return item;
if (string.IsNullOrEmpty(Unit)) throw new InvalidProgramException();

yield return Token.Reserved(this, parent, Unit);
yield return Token.Reserved(this, parent, "from");
foreach (var item in Value.GetTokens(parent)) yield return item;
}

/// <inheritdoc/>
protected override IEnumerable<QueryParameter> GetParametersCore()
{
foreach (var item in Unit.GetParameters())
{
yield return item;
}
foreach (var item in Value.GetParameters())
{
yield return item;
Expand All @@ -75,10 +69,6 @@ protected override IEnumerable<QueryParameter> GetParametersCore()
/// <inheritdoc/>
protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
{
foreach (var item in Unit.GetPhysicalTables())
{
yield return item;
}
foreach (var item in Value.GetPhysicalTables())
{
yield return item;
Expand All @@ -88,10 +78,6 @@ protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
/// <inheritdoc/>
protected override IEnumerable<CommonTable> GetCommonTablesCore()
{
foreach (var item in Unit.GetCommonTables())
{
yield return item;
}
foreach (var item in Value.GetCommonTables())
{
yield return item;
Expand All @@ -100,10 +86,6 @@ protected override IEnumerable<CommonTable> GetCommonTablesCore()

internal override IEnumerable<ColumnValue> GetColumnsCore()
{
foreach (var item in Unit.GetColumns())
{
yield return item;
}
foreach (var item in Value.GetColumns())
{
yield return item;
Expand All @@ -114,11 +96,6 @@ public override IEnumerable<ValueBase> GetValues()
{
yield return this;

foreach (var item in Unit.GetValues())
{
yield return item;
}

foreach (var item in Value.GetValues())
{
yield return item;
Expand Down
80 changes: 80 additions & 0 deletions test/Carbunql.Fluent.Test/FluentWhereTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Xunit.Abstractions;

namespace Carbunql.Fluent.Test;

public class FluentWhereTest
{
private readonly QueryCommandMonitor Monitor;

public FluentWhereTest(ITestOutputHelper output)
{
Monitor = new QueryCommandMonitor(output);
}

[Fact]
public void EqualTest()
{
var sq = new SelectQuery("""
select
a.id
, a.value
from
table_a a
""")
.Equal("id", 1);
;

Monitor.Log(sq);

var expect = """
SELECT
a.id,
a.value
FROM
table_a AS a
WHERE
a.id = 1
""";

Assert.Equal(expect, sq.ToText());
}

[Fact]
public void EqualTest_ExtractYear()
{
var sq = new SelectQuery("""
SELECT
o.order_id,
c.customer_name,
o.order_date,
EXTRACT(YEAR FROM o.order_date) AS order_year,
EXTRACT(MONTH FROM o.order_date) AS order_month,
o.amount
FROM
orders o
JOIN
customers c ON o.customer_id = c.customer_id
""")
.Equal("order_year", 1);
;

Monitor.Log(sq);

var expect = """
SELECT
o.order_id,
c.customer_name,
o.order_date,
EXTRACT(YEAR FROM o.order_date) AS order_year,
EXTRACT(MONTH FROM o.order_date) AS order_month,
o.amount
FROM
orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
WHERE
EXTRACT(YEAR FROM o.order_date) = 1
""";

Assert.Equal(expect, sq.ToText());
}
}

0 comments on commit b7b554c

Please sign in to comment.