From 713a973efef2c10813544b94a2bb4d28fa7c1909 Mon Sep 17 00:00:00 2001 From: Romain ODDONE Date: Tue, 12 Sep 2023 15:05:17 +0200 Subject: [PATCH] allow expanded select to be multiline --- QueryBuilder.Tests/SelectTests.cs | 27 +++++++++++++++++++++++++++ QueryBuilder/Helper.cs | 6 +++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/QueryBuilder.Tests/SelectTests.cs b/QueryBuilder.Tests/SelectTests.cs index 3f3c28ba..647187b9 100644 --- a/QueryBuilder.Tests/SelectTests.cs +++ b/QueryBuilder.Tests/SelectTests.cs @@ -74,6 +74,20 @@ public void ExpandedSelect() Assert.Equal("SELECT `users`.`id`, `users`.`name`, `users`.`age` FROM `users`", c[EngineCodes.MySql]); } + [Fact] + public void ExpandedSelectMultiline() + { + var q = new Query().From("users").Select(@"users.{ + id, + name as Name, + age + }"); + var c = Compile(q); + + Assert.Equal("SELECT [users].[id], [users].[name] AS [Name], [users].[age] FROM [users]", c[EngineCodes.SqlServer]); + Assert.Equal("SELECT `users`.`id`, `users`.`name` AS `Name`, `users`.`age` FROM `users`", c[EngineCodes.MySql]); + } + [Fact] public void ExpandedSelectWithSchema() { @@ -83,6 +97,19 @@ public void ExpandedSelectWithSchema() Assert.Equal("SELECT [dbo].[users].[id], [dbo].[users].[name], [dbo].[users].[age] FROM [users]", c[EngineCodes.SqlServer]); } + [Fact] + public void ExpandedSelectMultilineWithSchema() + { + var q = new Query().From("users").Select(@"dbo.users.{ + id, + name as Name, + age + }"); + var c = Compile(q); + + Assert.Equal("SELECT [dbo].[users].[id], [dbo].[users].[name] AS [Name], [dbo].[users].[age] FROM [users]", c[EngineCodes.SqlServer]); + } + [Fact] public void NestedEmptyWhereAtFirstCondition() { diff --git a/QueryBuilder/Helper.cs b/QueryBuilder/Helper.cs index 218a95e3..9ebef915 100644 --- a/QueryBuilder/Helper.cs +++ b/QueryBuilder/Helper.cs @@ -136,8 +136,8 @@ public static int EnumerableCount(IEnumerable obj) public static List ExpandExpression(string expression) { - var regex = @"^(?:\w+\.){1,2}{(.*)}"; - var match = Regex.Match(expression, regex); + var regex = @"^(?:\w+\.){1,2}{([^}]*)}"; + var match = Regex.Match(expression, regex, RegexOptions.Multiline); if (!match.Success) { @@ -149,7 +149,7 @@ public static List ExpandExpression(string expression) var captures = match.Groups[1].Value; - var cols = Regex.Split(captures, @"\s*,\s*") + var cols = Regex.Split(captures, @"\s*,\s*", RegexOptions.Multiline) .Select(x => $"{table}.{x.Trim()}") .ToList();