From 970dae4775e2f8f5ca5dbed9a3319dcfa815a3e2 Mon Sep 17 00:00:00 2001 From: mk3008 Date: Fri, 4 Oct 2024 07:51:06 +0900 Subject: [PATCH 1/2] Add support for ESCAPE keyword in LIKE clause parsing - Implemented handling of the ESCAPE keyword in the LIKE clause. - Added logic to recognize and process escape characters in pattern matching. - Updated test cases to validate LIKE clauses with custom escape characters. --- src/Carbunql/Analysis/Parser/LikeClauseParser.cs | 13 ++++++++++++- src/Carbunql/Values/LikeClause.cs | 15 ++++++++++++++- test/Carbunql.Analysis.Test/ValueParseTest.cs | 11 +++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Carbunql/Analysis/Parser/LikeClauseParser.cs b/src/Carbunql/Analysis/Parser/LikeClauseParser.cs index 45b67af1..90013527 100644 --- a/src/Carbunql/Analysis/Parser/LikeClauseParser.cs +++ b/src/Carbunql/Analysis/Parser/LikeClauseParser.cs @@ -1,4 +1,5 @@ using Carbunql.Clauses; +using Carbunql.Extensions; using Carbunql.Values; namespace Carbunql.Analysis.Parser; @@ -32,6 +33,16 @@ public static LikeClause Parse(ValueBase value, ITokenReader r, bool isNegative) r.Read("like"); var argument = ValueParser.ParseCore(r); - return new LikeClause(value, argument, isNegative); + + if (r.Peek().IsEqualNoCase("escape")) + { + r.Read("escape"); + var escape = r.Read(); + return new LikeClause(value, argument, isNegative, escape); + } + else + { + return new LikeClause(value, argument, isNegative); + } } } diff --git a/src/Carbunql/Values/LikeClause.cs b/src/Carbunql/Values/LikeClause.cs index 76380251..a650b8cb 100644 --- a/src/Carbunql/Values/LikeClause.cs +++ b/src/Carbunql/Values/LikeClause.cs @@ -14,11 +14,12 @@ public class LikeClause : ValueBase /// The value to compare. /// The argument to compare against. /// Indicates whether the comparison is negated. - public LikeClause(ValueBase value, ValueBase argument, bool isNegative = false) + public LikeClause(ValueBase value, ValueBase argument, bool isNegative = false, string escape = "") { Value = value; Argument = argument; IsNegative = isNegative; + Escape = escape; } /// @@ -36,6 +37,12 @@ public LikeClause(ValueBase value, ValueBase argument, bool isNegative = false) /// public bool IsNegative { get; set; } + /// + /// Gets or sets the escape character used in the LIKE clause. + /// This character allows special symbols (such as underscores or percent signs) to be treated as literal characters. + /// + public string Escape { get; set; } + /// protected override IEnumerable GetInternalQueriesCore() { @@ -58,6 +65,12 @@ public override IEnumerable GetCurrentTokens(Token? parent) yield return Token.Reserved(this, parent, "like"); foreach (var item in Argument.GetTokens(parent)) yield return item; + + if (!string.IsNullOrEmpty(Escape)) + { + yield return Token.Reserved(this, parent, "escape"); + yield return new Token(this, parent, Escape); + } } /// diff --git a/test/Carbunql.Analysis.Test/ValueParseTest.cs b/test/Carbunql.Analysis.Test/ValueParseTest.cs index 518cf115..0b34485a 100644 --- a/test/Carbunql.Analysis.Test/ValueParseTest.cs +++ b/test/Carbunql.Analysis.Test/ValueParseTest.cs @@ -525,6 +525,17 @@ public void NotLike() Assert.Equal(6, lst.Count); } + [Fact] + public void LikeEscape() + { + var text = "a.name LIKE 'x%3%' escape 'x'"; + var v = ValueParser.Parse(text); + Monitor.Log(v); + + var lst = v.GetTokens().ToList(); + Assert.Equal(7, lst.Count); + } + [Fact] public void Bracket() { From 5aaba0360bb32fb5dfb14d6b73c19d4931a1c74f Mon Sep 17 00:00:00 2001 From: mk3008 Date: Fri, 4 Oct 2024 07:52:45 +0900 Subject: [PATCH 2/2] version 0.8.12 --- src/Carbunql/Carbunql.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Carbunql/Carbunql.csproj b/src/Carbunql/Carbunql.csproj index 4fe780bd..726906e7 100644 --- a/src/Carbunql/Carbunql.csproj +++ b/src/Carbunql/Carbunql.csproj @@ -8,7 +8,7 @@ mk3008net Carbunql is an advanced Raw SQL editing library. - 0.8.11 + 0.8.12 mk3008net https://github.com/mk3008/Carbunql README.md