Skip to content

Commit

Permalink
Merge pull request #164 from mk3008/163-parsing-does-not-work-when-lo…
Browse files Browse the repository at this point in the history
…gical-operator-is-used-in-case-statementmultiple

Support for parsing case statements with multiple logical operators
  • Loading branch information
mk3008 authored Nov 16, 2022
2 parents 1d0ac90 + b27cc70 commit 4eb29da
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/SqModel/Extension/stringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static string ToSnakeCase(this string source)

public static bool IsConjunction(this string source)
{
var strings = new[] { "||", "=", "!=", "<>", ">=", "<=" }.ToList();
var strings = new[] { "||", "=", "!=", "<>", ">=", "<=", "and", "or", "is" }.ToList();
if (SqlParser.ArithmeticOperatorTokens.Contains(source) || strings.Contains(source)) return true;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SqModel/SqModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/mk3008/SqModel</PackageProjectUrl>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
<Version>0.8.8</Version>
<Version>0.8.9</Version>
<PackageTags>Sql;QueryBuilder;SqlParser;SqlBuilder;</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Description>Parse and build select queries.</Description>
Expand Down
9 changes: 7 additions & 2 deletions test/SqModelTest/AnalysisTest/ParseCaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ public void SelectTest()
[Fact]
public void AndOrTest()
{
using var p = new SqlParser(@"select case when 1 = 1 and 2 = 2 then 1 when 3 = 3 or 4 = 4 then 2 end as val");
using var p = new SqlParser(@"select
case
when 1 = 1 and 2 = 2 and 3 = 3 and 4 = 4 then 'A'
when 1 = 1 or 2 = 2 or 3 = 3 or 4 = 4 then 'B'
when 1 = 1 and 2 = 2 and 3 = 3 or 4 = 4 then 'C'
end as val");
var q = p.ParseSelectQuery();
var text = q.ToQuery().CommandText;
var expect = @"select
case when 1 = 1 and 2 = 2 then 1 when 3 = 3 or 4 = 4 then 2 end as val";
case when 1 = 1 and 2 = 2 and 3 = 3 and 4 = 4 then 'A' when 1 = 1 or 2 = 2 or 3 = 3 or 4 = 4 then 'B' when 1 = 1 and 2 = 2 and 3 = 3 or 4 = 4 then 'C' end as val";
Assert.Equal(expect, text);
}

Expand Down
4 changes: 2 additions & 2 deletions test/SqModelTest/AnalysisTest/ParseLogicalExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void IsNullAnd()
var clause = LogicalExpressionParser.Parse(relationSql);
var text = clause.ToQuery().CommandText;

Assert.Equal("a.id1 is null", text);
Assert.Equal("a.id1 is null and a.id1 is not null", text);
}

[Fact]
Expand All @@ -101,6 +101,6 @@ public void IsNotNullAnd()
var clause = LogicalExpressionParser.Parse(relationSql);
var text = clause.ToQuery().CommandText;

Assert.Equal("a.id1 is not null", text);
Assert.Equal("a.id1 is not null and a.id1 is not null", text);
}
}
19 changes: 15 additions & 4 deletions test/SqModelTest/AnalysisTest/ParseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ public void Brackets()
*
from a
where
((a.c2 = 2) or (a.c3 = 3))
and a.c1 = 1";
((a.c2 = 2) or (a.c3 = 3)) and a.c1 = 1";
Assert.Equal(expect, text);
}

Expand Down Expand Up @@ -362,8 +361,7 @@ public void NullTest()
*
from a
where
a.id is not null
and a.id is null";
a.id is not null and a.id is null";
Assert.Equal(expect, text);
}

Expand Down Expand Up @@ -450,6 +448,19 @@ public void BooleanColumn()
Assert.Equal(expect, text);
}

[Fact]
public void BooleanColumn_operator()
{
var sq = SqlParser.Parse(@"select
1 + 1 = 2 and 2 + 2 = 4 and 3 + 3 = 6 calc1
, 1 + 1 = 2 or 2 * 2 = 2 or 3 + 3 = 3 calc2");
var text = sq.ToQuery().CommandText;
var expect = @"select
1 + 1 = 2 and 2 + 2 = 4 and 3 + 3 = 6 calc1
, 1 + 1 = 2 or 2 * 2 = 2 or 3 + 3 = 3 calc2";
Assert.Equal(expect, text);
}

[Fact]
public void Full()
{
Expand Down
3 changes: 1 addition & 2 deletions test/SqModelTest/AnalysisTest/ParseWhereTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void Group()
var clause = WhereClauseParser.Parse(condition);
var q = clause.ToQuery();
var expect = @"where
(a.id = 1 or a.id = 2)
and (a.value = 1 or (a.id = 3 and a.id = 4))";
(a.id = 1 or a.id = 2) and (a.value = 1 or (a.id = 3 and a.id = 4))";
Assert.Equal(expect, q.CommandText);
}
}

0 comments on commit 4eb29da

Please sign in to comment.