Skip to content

Commit

Permalink
Merge pull request #541 from mk3008/540-requestadd-support-for-escape…
Browse files Browse the repository at this point in the history
…-keyword-in-like-clause-parsing

Add support for ESCAPE keyword in LIKE clause parsing
  • Loading branch information
mk3008 authored Oct 3, 2024
2 parents 50ac664 + 5aaba03 commit 25156a6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Carbunql/Analysis/Parser/LikeClauseParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Carbunql.Clauses;
using Carbunql.Extensions;
using Carbunql.Values;

namespace Carbunql.Analysis.Parser;
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/Carbunql/Carbunql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Title></Title>
<Copyright>mk3008net</Copyright>
<Description>Carbunql is an advanced Raw SQL editing library.</Description>
<Version>0.8.11</Version>
<Version>0.8.12</Version>
<Authors>mk3008net</Authors>
<PackageProjectUrl>https://github.com/mk3008/Carbunql</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
15 changes: 14 additions & 1 deletion src/Carbunql/Values/LikeClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public class LikeClause : ValueBase
/// <param name="value">The value to compare.</param>
/// <param name="argument">The argument to compare against.</param>
/// <param name="isNegative">Indicates whether the comparison is negated.</param>
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;
}

/// <summary>
Expand All @@ -36,6 +37,12 @@ public LikeClause(ValueBase value, ValueBase argument, bool isNegative = false)
/// </summary>
public bool IsNegative { get; set; }

/// <summary>
/// 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.
/// </summary>
public string Escape { get; set; }

/// <inheritdoc/>
protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
{
Expand All @@ -58,6 +65,12 @@ public override IEnumerable<Token> 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);
}
}

/// <inheritdoc/>
Expand Down
11 changes: 11 additions & 0 deletions test/Carbunql.Analysis.Test/ValueParseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 25156a6

Please sign in to comment.