Skip to content

Commit 9f63662

Browse files
use ilike for case insensitive search in PostgreSql
1 parent bad6733 commit 9f63662

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

QueryBuilder.Tests/SelectTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,17 @@ public void MultipleOrHaving()
700700
Assert.Equal("SELECT * FROM [Table1] HAVING [Column1] > 1 OR [Column2] = 1", c[EngineCodes.SqlServer]);
701701
}
702702

703+
[Fact]
704+
public void ShouldUseILikeOnPostgresWhenNonCaseSensitive()
705+
{
706+
var q = new Query("Table1")
707+
.WhereLike("Column1", "%Upper Word%", false);
708+
var c = Compile(q);
709+
710+
Assert.Equal(@"SELECT * FROM [Table1] WHERE LOWER([Column1]) like '%upper word%'", c[EngineCodes.SqlServer]);
711+
Assert.Equal("SELECT * FROM \"Table1\" WHERE \"Column1\" ilike '%Upper Word%'", c[EngineCodes.PostgreSql]);
712+
}
713+
703714
[Fact]
704715
public void EscapedWhereLike()
705716
{

QueryBuilder/Compilers/PostgresCompiler.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.Linq;
3+
14
namespace SqlKata.Compilers
25
{
36
public class PostgresCompiler : Compiler
@@ -9,6 +12,60 @@ public PostgresCompiler()
912

1013
public override string EngineCode { get; } = EngineCodes.PostgreSql;
1114

15+
16+
protected override string CompileBasicStringCondition(SqlResult ctx, BasicStringCondition x)
17+
{
18+
19+
var column = Wrap(x.Column);
20+
21+
var value = Resolve(ctx, x.Value) as string;
22+
23+
if (value == null)
24+
{
25+
throw new ArgumentException("Expecting a non nullable string");
26+
}
27+
28+
var method = x.Operator;
29+
30+
if (new[] { "starts", "ends", "contains", "like", "ilike" }.Contains(x.Operator))
31+
{
32+
method = x.CaseSensitive ? "LIKE" : "ILIKE";
33+
34+
switch (x.Operator)
35+
{
36+
case "starts":
37+
value = $"{value}%";
38+
break;
39+
case "ends":
40+
value = $"%{value}";
41+
break;
42+
case "contains":
43+
value = $"%{value}%";
44+
break;
45+
}
46+
}
47+
48+
string sql;
49+
50+
if (x.Value is UnsafeLiteral)
51+
{
52+
sql = $"{column} {checkOperator(method)} {value}";
53+
}
54+
else
55+
{
56+
sql = $"{column} {checkOperator(method)} {Parameter(ctx, value)}";
57+
}
58+
59+
if (!string.IsNullOrEmpty(x.EscapeCharacter))
60+
{
61+
sql = $"{sql} ESCAPE '{x.EscapeCharacter}'";
62+
}
63+
64+
return x.IsNot ? $"NOT ({sql})" : sql;
65+
66+
}
67+
68+
1269
protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition condition)
1370
{
1471
var column = Wrap(condition.Column);

0 commit comments

Comments
 (0)