From 49a9586a9d446741b2f369d3590a5afcce422886 Mon Sep 17 00:00:00 2001 From: mk3008 Date: Wed, 1 May 2024 17:12:44 +0900 Subject: [PATCH] Fixed a bug where query conversion of IEnumerable did not work. Fixed an issue where the ToSelectQuery method did not work. Fixed an issue where the ToValuesQuery method did not work. --- src/Carbunql/Building/IEnumerableExtension.cs | 4 +- src/Carbunql/Carbunql.csproj | 2 +- .../Carbunql.Building.Test/IEnumerableTest.cs | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Carbunql/Building/IEnumerableExtension.cs b/src/Carbunql/Building/IEnumerableExtension.cs index 6d77431c..2bd78aee 100644 --- a/src/Carbunql/Building/IEnumerableExtension.cs +++ b/src/Carbunql/Building/IEnumerableExtension.cs @@ -19,7 +19,7 @@ public static ValuesQuery ToValuesQuery(this IEnumerable source, string pl { var lst = new List(); 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; @@ -40,7 +40,7 @@ public static SelectQuery ToSelectQuery(this IEnumerable source) public static SelectQuery ToSelectQuery(this IEnumerable source, string placeholderIndentifer, Func 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); diff --git a/src/Carbunql/Carbunql.csproj b/src/Carbunql/Carbunql.csproj index 29b62974..4011619f 100644 --- a/src/Carbunql/Carbunql.csproj +++ b/src/Carbunql/Carbunql.csproj @@ -7,7 +7,7 @@ mk3008net Carbunql provides query parsing and building functionality. - 0.7.7 + 0.7.8 mk3008net https://github.com/mk3008/Carbunql README.md diff --git a/test/Carbunql.Building.Test/IEnumerableTest.cs b/test/Carbunql.Building.Test/IEnumerableTest.cs index 45f591f7..6efa2cb2 100644 --- a/test/Carbunql.Building.Test/IEnumerableTest.cs +++ b/test/Carbunql.Building.Test/IEnumerableTest.cs @@ -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 GenerateDummyResults(int count) { var dummyResults = new List();