Skip to content

Commit a36151d

Browse files
committed
Merge remote-tracking branch 'sqlkata/master' into feature/compiler-performance
# Conflicts: # QueryBuilder.Tests/Infrastructure/TestCompilersContainer.cs
2 parents 6f19ea8 + be679a9 commit a36151d

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
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/SelectTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,5 +814,40 @@ public void EscapeClauseThrowsForMultipleCharacters()
814814
.HavingContains("Column1", @"TestString\%", false, @"\aa");
815815
});
816816
}
817+
818+
819+
[Fact]
820+
public void BasicSelectRaw_WithNoTable()
821+
{
822+
var q = new Query().SelectRaw("somefunction() as c1");
823+
824+
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
825+
Assert.Equal("SELECT somefunction() as c1", c.ToString());
826+
}
827+
828+
[Fact]
829+
public void BasicSelect_WithNoTable()
830+
{
831+
var q = new Query().Select("c1");
832+
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
833+
Assert.Equal("SELECT [c1]", c.ToString());
834+
}
835+
836+
[Fact]
837+
public void BasicSelect_WithNoTableAndWhereClause()
838+
{
839+
var q = new Query().Select("c1").Where("p", 1);
840+
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
841+
Assert.Equal("SELECT [c1] WHERE [p] = 1", c.ToString());
842+
}
843+
844+
[Fact]
845+
public void BasicSelect_WithNoTableWhereRawClause()
846+
{
847+
var q = new Query().Select("c1").WhereRaw("1 = 1");
848+
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
849+
Assert.Equal("SELECT [c1] WHERE 1 = 1", c.ToString());
850+
}
851+
817852
}
818853
}

QueryBuilder/Base.Where.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public Q OrWhereNotBetween<T>(string column, T lower, T higher)
381381
public Q WhereIn<T>(string column, IEnumerable<T> values)
382382
{
383383

384-
// If the developer has passed a string most probably he wants List<string>
384+
// If the developer has passed a string they most likely want a List<string>
385385
// since string is considered as List<char>
386386
if (values is string)
387387
{

QueryBuilder/Compilers/Compiler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,14 +649,14 @@ public virtual string CompileTableExpression(SqlResult ctx, AbstractFrom from)
649649

650650
public virtual string CompileFrom(SqlResult ctx)
651651
{
652-
if (!ctx.Query.HasComponent("from", EngineCode))
652+
if (ctx.Query.HasComponent("from", EngineCode))
653653
{
654-
throw new InvalidOperationException("No table is set");
655-
}
654+
var from = ctx.Query.GetOneComponent<AbstractFrom>("from", EngineCode);
656655

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

659-
return "FROM " + CompileTableExpression(ctx, from);
659+
return string.Empty;
660660
}
661661

662662
public virtual string CompileJoins(SqlResult ctx)
@@ -689,7 +689,7 @@ public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = fals
689689

690690
public virtual string CompileWheres(SqlResult ctx)
691691
{
692-
if (!ctx.Query.HasComponent("from", EngineCode) || !ctx.Query.HasComponent("where", EngineCode))
692+
if (!ctx.Query.HasComponent("where", EngineCode))
693693
{
694694
return null;
695695
}

QueryBuilder/Query.Having.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public Query OrHavingNotBetween<T>(string column, T lower, T higher)
360360

361361
public Query HavingIn<T>(string column, IEnumerable<T> values)
362362
{
363-
// If the developer has passed a string most probably he wants List<string>
363+
// If the developer has passed a string they most likely want a List<string>
364364
// since string is considered as List<char>
365365
if (values is string)
366366
{

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)