Skip to content

Commit

Permalink
Merge pull request #16 from UiPath/merge_upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
dgeelen-uipath authored Apr 26, 2022
2 parents c59947b + 5bc8386 commit c5fc27a
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 24 deletions.
9 changes: 9 additions & 0 deletions QueryBuilder.Tests/ExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ public void ShouldThrowException()
new Query("Books").Get();
});
}

[Fact]
public void TimeoutShouldBeCarriedToNewCreatedFactory()
{
var db = new QueryFactory();
db.QueryTimeout = 4000;
var newFactory = QueryExtensions.CreateQueryFactory(db.Query());
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
}
}
}
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/Firebird/FirebirdLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public void LimitAndOffset()
Assert.Equal(2, ctx.Bindings.Count);
}
}
}
}
3 changes: 3 additions & 0 deletions QueryBuilder.Tests/Infrastructure/TestCompilersContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ private static class Messages
[EngineCodes.Snowflake] = new SnowflakeCompiler(),
[EngineCodes.Sqlite] = new SqliteCompiler(),
[EngineCodes.SqlServer] = new SqlServerCompiler()
{
UseLegacyPagination = true
}
};

public IEnumerable<string> KnownEngineCodes
Expand Down
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/MySql/MySqlLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ public void WithLimitAndOffset()
Assert.Equal(2, ctx.Bindings.Count);
}
}
}
}
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/ParameterTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ public void CorrectParameterTypeOutput(string rendered, object input)
Assert.Equal($"SELECT * FROM [Table] WHERE [Col] = {rendered}", c[EngineCodes.SqlServer]);
}
}
}
}
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/PostgreSql/PostgreSqlLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ public void WithLimitAndOffset()
Assert.Equal(2, ctx.Bindings.Count);
}
}
}
}
49 changes: 47 additions & 2 deletions QueryBuilder.Tests/SelectTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SqlKata.Compilers;
using SqlKata.Compilers;
using SqlKata.Extensions;
using SqlKata.Tests.Infrastructure;
using System;
Expand Down Expand Up @@ -35,7 +35,6 @@ public void BasicSelectEnumerable()
Assert.Equal("SELECT \"id\", \"name\" FROM \"users\"", c[EngineCodes.Oracle]);
}


[Fact]
public void SelectAs()
{
Expand Down Expand Up @@ -810,6 +809,17 @@ public void MultipleOrHaving()
Assert.Equal("SELECT * FROM [Table1] HAVING [Column1] > 1 OR [Column2] = 1", c[EngineCodes.SqlServer]);
}

[Fact]
public void ShouldUseILikeOnPostgresWhenNonCaseSensitive()
{
var q = new Query("Table1")
.WhereLike("Column1", "%Upper Word%", false);
var c = Compile(q);

Assert.Equal(@"SELECT * FROM [Table1] WHERE LOWER([Column1]) like '%upper word%'", c[EngineCodes.SqlServer]);
Assert.Equal("SELECT * FROM \"Table1\" WHERE \"Column1\" ilike '%Upper Word%'", c[EngineCodes.PostgreSql]);
}

[Fact]
public void EscapedWhereLike()
{
Expand Down Expand Up @@ -899,5 +909,40 @@ public void EscapeClauseThrowsForMultipleCharacters()
.HavingContains("Column1", @"TestString\%", false, @"\aa");
});
}


[Fact]
public void BasicSelectRaw_WithNoTable()
{
var q = new Query().SelectRaw("somefunction() as c1");

var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
Assert.Equal("SELECT somefunction() as c1", c.ToString());
}

[Fact]
public void BasicSelect_WithNoTable()
{
var q = new Query().Select("c1");
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
Assert.Equal("SELECT [c1]", c.ToString());
}

[Fact]
public void BasicSelect_WithNoTableAndWhereClause()
{
var q = new Query().Select("c1").Where("p", 1);
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
Assert.Equal("SELECT [c1] WHERE [p] = 1", c.ToString());
}

[Fact]
public void BasicSelect_WithNoTableWhereRawClause()
{
var q = new Query().Select("c1").WhereRaw("1 = 1");
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
Assert.Equal("SELECT [c1] WHERE 1 = 1", c.ToString());
}

}
}
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/SqlServer/SqlServerLegacyLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public void ShouldKeepTheOrdersAsIsIfPaginationProvided()
Assert.DoesNotContain("(SELECT 0)", compiler.Compile(query).ToString());
}
}
}
}
2 changes: 1 addition & 1 deletion QueryBuilder.Tests/SqlServer/SqlServerLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ public void ShouldKeepTheOrdersAsIsIfPaginationProvided()
Assert.DoesNotContain("(SELECT 0)", compiler.Compile(query).ToString());
}
}
}
}
2 changes: 1 addition & 1 deletion QueryBuilder/Base.Where.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public Q OrWhereNotBetween<T>(string column, T lower, T higher)
public Q WhereIn<T>(string column, IEnumerable<T> values)
{

// If the developer has passed a string most probably he wants List<string>
// If the developer has passed a string they most likely want a List<string>
// since string is considered as List<char>
if (values is string)
{
Expand Down
12 changes: 6 additions & 6 deletions QueryBuilder/Compilers/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,14 @@ public virtual string CompileTableExpression(SqlResult ctx, AbstractFrom from)

public virtual string CompileFrom(SqlResult ctx)
{
if (!ctx.Query.HasComponent("from", EngineCode))
if (ctx.Query.HasComponent("from", EngineCode))
{
throw new InvalidOperationException("No table is set");
}
var from = ctx.Query.GetOneComponent<AbstractFrom>("from", EngineCode);

var from = ctx.Query.GetOneComponent<AbstractFrom>("from", EngineCode);
return "FROM " + CompileTableExpression(ctx, from);
}

return "FROM " + CompileTableExpression(ctx, from);
return string.Empty;
}

public virtual string CompileJoins(SqlResult ctx)
Expand Down Expand Up @@ -659,7 +659,7 @@ public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = fals

public virtual string CompileWheres(SqlResult ctx)
{
if (!ctx.Query.HasComponent("from", EngineCode) || !ctx.Query.HasComponent("where", EngineCode))
if (!ctx.Query.HasComponent("where", EngineCode))
{
return null;
}
Expand Down
57 changes: 57 additions & 0 deletions QueryBuilder/Compilers/PostgresCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Linq;

namespace SqlKata.Compilers
{
public class PostgresCompiler : Compiler
Expand All @@ -10,6 +13,60 @@ public PostgresCompiler()

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


protected override string CompileBasicStringCondition(SqlResult ctx, BasicStringCondition x)
{

var column = Wrap(x.Column);

var value = Resolve(ctx, x.Value) as string;

if (value == null)
{
throw new ArgumentException("Expecting a non nullable string");
}

var method = x.Operator;

if (new[] { "starts", "ends", "contains", "like", "ilike" }.Contains(x.Operator))
{
method = x.CaseSensitive ? "LIKE" : "ILIKE";

switch (x.Operator)
{
case "starts":
value = $"{value}%";
break;
case "ends":
value = $"%{value}";
break;
case "contains":
value = $"%{value}%";
break;
}
}

string sql;

if (x.Value is UnsafeLiteral)
{
sql = $"{column} {checkOperator(method)} {value}";
}
else
{
sql = $"{column} {checkOperator(method)} {Parameter(ctx, value)}";
}

if (!string.IsNullOrEmpty(x.EscapeCharacter))
{
sql = $"{sql} ESCAPE '{x.EscapeCharacter}'";
}

return x.IsNot ? $"NOT ({sql})" : sql;

}


protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition condition)
{
var column = Wrap(condition.Column);
Expand Down
2 changes: 1 addition & 1 deletion QueryBuilder/Compilers/SqlServerCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SqlServerCompiler()
}

public override string EngineCode { get; } = EngineCodes.SqlServer;
public bool UseLegacyPagination { get; set; } = true;
public bool UseLegacyPagination { get; set; } = false;

public /* friend */ override SqlResult CompileSelectQuery(Query query)
{
Expand Down
2 changes: 1 addition & 1 deletion QueryBuilder/Query.Having.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public Query OrHavingNotBetween<T>(string column, T lower, T higher)

public Query HavingIn<T>(string column, IEnumerable<T> values)
{
// If the developer has passed a string most probably he wants List<string>
// If the developer has passed a string they most likely want a List<string>
// since string is considered as List<char>
if (values is string)
{
Expand Down
2 changes: 1 addition & 1 deletion QueryBuilder/SqlResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private string ChangeToSqlValue(object value)
}

// fallback to string
return "'" + value.ToString() + "'";
return "'" + value.ToString().Replace("'","''") + "'";
}
}
}
3 changes: 3 additions & 0 deletions SqlKata.Execution/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("QueryBuilder.Tests")]
2 changes: 1 addition & 1 deletion SqlKata.Execution/Query.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ internal static XQuery CastToXQuery(Query query, string method = null)

internal static QueryFactory CreateQueryFactory(XQuery xQuery)
{
var factory = new QueryFactory(xQuery.Connection, xQuery.Compiler);
var factory = new QueryFactory(xQuery.Connection, xQuery.Compiler, xQuery.QueryFactory.QueryTimeout);

factory.Logger = xQuery.Logger;

Expand Down
10 changes: 5 additions & 5 deletions SqlKata.Execution/QueryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,8 @@ private static IEnumerable<T> handleIncludes<T>(Query query, IEnumerable<T> resu

foreach (var item in dynamicResult)
{
var foreignValue = item[include.ForeignKey].ToString();
item[include.Name] = related.ContainsKey(foreignValue) ? related[foreignValue] : null;
var foreignValue = item[include.ForeignKey]?.ToString();
item[include.Name] = foreignValue != null && related.ContainsKey(foreignValue) ? related[foreignValue] : null;
}
}

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

foreach (var item in dynamicResult)
{
var foreignValue = item[include.ForeignKey].ToString();
item[include.Name] = related.ContainsKey(foreignValue) ? related[foreignValue] : null;
var foreignValue = item[include.ForeignKey]?.ToString();
item[include.Name] = foreignValue != null && related.ContainsKey(foreignValue) ? related[foreignValue] : null;
}
}

Expand Down Expand Up @@ -896,4 +896,4 @@ public void Dispose()
GC.SuppressFinalize(this);
}
}
}
}

0 comments on commit c5fc27a

Please sign in to comment.