Skip to content

Commit

Permalink
Merge pull request #689 from roddone/multiline-expanded-select
Browse files Browse the repository at this point in the history
Allow expanded select to be multiline
  • Loading branch information
ahmad-moussawi authored Sep 17, 2023
2 parents fcf64ad + 713a973 commit c202922
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 27 additions & 0 deletions QueryBuilder.Tests/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down
6 changes: 3 additions & 3 deletions QueryBuilder/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public static int EnumerableCount(IEnumerable obj)

public static List<string> 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)
{
Expand All @@ -149,7 +149,7 @@ public static List<string> 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();

Expand Down

0 comments on commit c202922

Please sign in to comment.