Skip to content

Commit 9abddb6

Browse files
authored
Merge branch 'sqlkata:master' into master
2 parents 2482659 + a1f8e3f commit 9abddb6

File tree

9 files changed

+90
-7
lines changed

9 files changed

+90
-7
lines changed

QueryBuilder.Tests/ExecutionTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ public void ShouldThrowException()
1414
new Query("Books").Get();
1515
});
1616
}
17+
18+
[Fact]
19+
public void TimeoutShouldBeCarriedToNewCreatedFactory()
20+
{
21+
var db = new QueryFactory();
22+
db.QueryTimeout = 4000;
23+
var newFactory = QueryExtensions.CreateQueryFactory(db.Query());
24+
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
25+
}
1726
}
1827
}

QueryBuilder.Tests/Infrastructure/TestCompilersContainer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ private static class Messages
2121
[EngineCodes.PostgreSql] = new PostgresCompiler(),
2222
[EngineCodes.Sqlite] = new SqliteCompiler(),
2323
[EngineCodes.SqlServer] = new SqlServerCompiler()
24+
{
25+
UseLegacyPagination = true
26+
}
2427
};
2528

2629
public IEnumerable<string> KnownEngineCodes

QueryBuilder.Tests/SelectTests.cs

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

717+
[Fact]
718+
public void ShouldUseILikeOnPostgresWhenNonCaseSensitive()
719+
{
720+
var q = new Query("Table1")
721+
.WhereLike("Column1", "%Upper Word%", false);
722+
var c = Compile(q);
723+
724+
Assert.Equal(@"SELECT * FROM [Table1] WHERE LOWER([Column1]) like '%upper word%'", c[EngineCodes.SqlServer]);
725+
Assert.Equal("SELECT * FROM \"Table1\" WHERE \"Column1\" ilike '%Upper Word%'", c[EngineCodes.PostgreSql]);
726+
}
727+
717728
[Fact]
718729
public void EscapedWhereLike()
719730
{

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);

QueryBuilder/Compilers/SqlServerCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public SqlServerCompiler()
1212
}
1313

1414
public override string EngineCode { get; } = EngineCodes.SqlServer;
15-
public bool UseLegacyPagination { get; set; } = true;
15+
public bool UseLegacyPagination { get; set; } = false;
1616

1717
protected override SqlResult CompileSelectQuery(Query query)
1818
{

QueryBuilder/SqlResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private string ChangeToSqlValue(object value)
8181
}
8282

8383
// fallback to string
84-
return "'" + value.ToString() + "'";
84+
return "'" + value.ToString().Replace("'","''") + "'";
8585
}
8686
}
8787
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("QueryBuilder.Tests")]

SqlKata.Execution/Query.Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ internal static XQuery CastToXQuery(Query query, string method = null)
366366

367367
internal static QueryFactory CreateQueryFactory(XQuery xQuery)
368368
{
369-
var factory = new QueryFactory(xQuery.Connection, xQuery.Compiler);
369+
var factory = new QueryFactory(xQuery.Connection, xQuery.Compiler, xQuery.QueryFactory.QueryTimeout);
370370

371371
factory.Logger = xQuery.Logger;
372372

SqlKata.Execution/QueryFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,8 @@ private static IEnumerable<T> handleIncludes<T>(Query query, IEnumerable<T> resu
749749

750750
foreach (var item in dynamicResult)
751751
{
752-
var foreignValue = item[include.ForeignKey].ToString();
753-
item[include.Name] = related.ContainsKey(foreignValue) ? related[foreignValue] : null;
752+
var foreignValue = item[include.ForeignKey]?.ToString();
753+
item[include.Name] = foreignValue != null && related.ContainsKey(foreignValue) ? related[foreignValue] : null;
754754
}
755755
}
756756

@@ -843,8 +843,8 @@ private static async Task<IEnumerable<T>> handleIncludesAsync<T>(Query query, IE
843843

844844
foreach (var item in dynamicResult)
845845
{
846-
var foreignValue = item[include.ForeignKey].ToString();
847-
item[include.Name] = related.ContainsKey(foreignValue) ? related[foreignValue] : null;
846+
var foreignValue = item[include.ForeignKey]?.ToString();
847+
item[include.Name] = foreignValue != null && related.ContainsKey(foreignValue) ? related[foreignValue] : null;
848848
}
849849
}
850850

0 commit comments

Comments
 (0)