Skip to content

Commit

Permalink
Merge pull request #399 from mk3008/398-toselectquery-method-does-not…
Browse files Browse the repository at this point in the history
…-work-for-ienumerableanonymous-type

Fixed a bug where query conversion of IEnumerable<anonymous type> did not work.
  • Loading branch information
mk3008 authored May 1, 2024
2 parents ecef31f + 49a9586 commit 3580e8f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Carbunql/Building/IEnumerableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ValuesQuery ToValuesQuery<T>(this IEnumerable<T> source, string pl
{
var lst = new List<ValueBase>();
var c = 0;
foreach (var column in typeof(T).GetProperties().Where(x => x.CanRead && x.CanWrite).Select(x => x.GetValue(row)))
foreach (var column in typeof(T).GetProperties().Where(x => x.CanRead).Select(x => x.GetValue(row)))
{
var name = $"r{r}c{c}";
var v = placeholderIndentifer + name;
Expand All @@ -40,7 +40,7 @@ public static SelectQuery ToSelectQuery<T>(this IEnumerable<T> source)

public static SelectQuery ToSelectQuery<T>(this IEnumerable<T> source, string placeholderIndentifer, Func<string, string> propertyNameConverter)
{
var columns = typeof(T).GetProperties().Where(x => x.CanRead && x.CanWrite).Select(x => propertyNameConverter(x.Name)).ToList();
var columns = typeof(T).GetProperties().Where(x => x.CanRead).Select(x => propertyNameConverter(x.Name));
var vq = source.ToValuesQuery(placeholderIndentifer);
var sq = vq.ToSelectQuery(columns);

Expand Down
2 changes: 1 addition & 1 deletion src/Carbunql/Carbunql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Title></Title>
<Copyright>mk3008net</Copyright>
<Description>Carbunql provides query parsing and building functionality.</Description>
<Version>0.7.7</Version>
<Version>0.7.8</Version>
<Authors>mk3008net</Authors>
<PackageProjectUrl>https://github.com/mk3008/Carbunql</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
54 changes: 54 additions & 0 deletions test/Carbunql.Building.Test/IEnumerableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,60 @@ public void ToSelectQuery_50k()
Output.WriteLine(actual);
}

[Fact]
public void AnonymousTypeTest_ToSelectQuery()
{
var students = new[]
{
new { Name = "Alice", Age = 20 },
new { Name = "Bob", Age = 22 },
new { Name = "Charlie", Age = 21 }
};

var q = students.ToSelectQuery();

var actual = q.ToText(false);
Output.WriteLine(actual);

var expect = @"SELECT
v.name,
v.age
FROM
(
VALUES
(:r0c0, :r0c1),
(:r1c0, :r1c1),
(:r2c0, :r2c1)
) AS v (
name, age
)";

Assert.Equal(expect, actual, true, true, true);
}

[Fact]
public void AnonymousTypeTest_ToValuesQuery()
{
var students = new[]
{
new { Name = "Alice", Age = 20 },
new { Name = "Bob", Age = 22 },
new { Name = "Charlie", Age = 21 }
};

var q = students.ToValuesQuery();

var actual = q.ToText(false);
Output.WriteLine(actual);

var expect = @"VALUES
(:r0c0, :r0c1),
(:r1c0, :r1c1),
(:r2c0, :r2c1)";

Assert.Equal(expect, actual, true, true, true);
}

private static List<ApiResult> GenerateDummyResults(int count)
{
var dummyResults = new List<ApiResult>();
Expand Down

0 comments on commit 3580e8f

Please sign in to comment.