Skip to content

Commit

Permalink
Handle NULL reference error on clone
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-moussawi committed Sep 9, 2022
1 parent cc0c67c commit d7a4c58
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
9 changes: 9 additions & 0 deletions QueryBuilder.Tests/ExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@ public void TimeoutShouldBeCarriedToNewCreatedFactory()
var newFactory = QueryExtensions.CreateQueryFactory(db.Query());
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
}

[Fact(Skip = "timeout over cloned xQuery is not supported yet")]
public void TimeoutShouldBeCarriedToNewCreatedFactoryAfterClone()
{
var db = new QueryFactory();
db.QueryTimeout = 4000;
var newFactory = QueryExtensions.CreateQueryFactory(db.Query().Clone());
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
}
}
}
65 changes: 65 additions & 0 deletions QueryBuilder.Tests/MySqlExecutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,71 @@ Color TEXT NULL

db.Statement("DROP TABLE IF EXISTS `Cars`");
}

[Fact]
public void Count()
{
var db = SetupDb();
var sql = @"
CREATE TABLE Cars(
Id INT PRIMARY KEY AUTO_INCREMENT,
Brand TEXT NOT NULL,
Year INT NOT NULL,
Color TEXT NULL
)
";
db.Statement(sql);

db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)");
var count = db.Query("Cars").Count<int>();
Assert.Equal(1, count);

db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Toyota', 2021)");
count = db.Query("Cars").Count<int>();
Assert.Equal(2, count);

int affected = db.Query("Cars").Delete();
Assert.Equal(2, affected);

count = db.Query("Cars").Count<int>();
Assert.Equal(0, count);

db.Statement("DROP TABLE IF EXISTS `Cars`");
}

[Fact]
public void CloneThenCount()
{
var db = SetupDb();
var sql = @"
CREATE TABLE Cars(
Id INT PRIMARY KEY AUTO_INCREMENT,
Brand TEXT NOT NULL,
Year INT NOT NULL,
Color TEXT NULL
)
";
db.Statement(sql);

for (int i = 0; i < 10; i++)
{
db.Query("Cars").Insert(new
{
Brand = "Brand " + i,
Year = "2020",
});
}

var query = db.Query("Cars").Where("Id", "<", 5);
var count = query.Count<int>();
var cloneCount = query.Clone().Count<int>();

Assert.Equal(4, count);
Assert.Equal(4, cloneCount);

db.Statement("DROP TABLE IF EXISTS `Cars`");
}

public QueryFactory SetupDb()
{
var host = System.Environment.GetEnvironmentVariable("SQLKATA_MYSQL_HOST");
Expand Down
11 changes: 10 additions & 1 deletion SqlKata.Execution/Query.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,16 @@ internal static XQuery CastToXQuery(Query query, string method = null)

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

if (xQuery.QueryFactory != null)
{
factory = new QueryFactory(xQuery.Connection, xQuery.Compiler, xQuery.QueryFactory.QueryTimeout);
}
else
{
factory = new QueryFactory(xQuery.Connection, xQuery.Compiler);
}

factory.Logger = xQuery.Logger;

Expand Down
8 changes: 7 additions & 1 deletion SqlKata.Execution/XQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ public class XQuery : Query

public XQuery(IDbConnection connection, Compiler compiler)
{
this.QueryFactory = new QueryFactory(connection, compiler);
this.Connection = connection;
this.Compiler = compiler;
}

public override Query Clone()
{

var query = new XQuery(this.Connection, this.Compiler);
var query = new XQuery(this.QueryFactory.Connection, this.QueryFactory.Compiler);

if (this.QueryFactory?.QueryTimeout != null)
{
query.QueryFactory.QueryTimeout = this.QueryFactory?.QueryTimeout ?? 30;
}

query.Clauses = this.Clauses.Select(x => x.Clone()).ToList();
query.Logger = this.Logger;
Expand Down

0 comments on commit d7a4c58

Please sign in to comment.