Skip to content

Commit d7a4c58

Browse files
Handle NULL reference error on clone
1 parent cc0c67c commit d7a4c58

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

QueryBuilder.Tests/ExecutionTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@ public void TimeoutShouldBeCarriedToNewCreatedFactory()
2323
var newFactory = QueryExtensions.CreateQueryFactory(db.Query());
2424
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
2525
}
26+
27+
[Fact(Skip = "timeout over cloned xQuery is not supported yet")]
28+
public void TimeoutShouldBeCarriedToNewCreatedFactoryAfterClone()
29+
{
30+
var db = new QueryFactory();
31+
db.QueryTimeout = 4000;
32+
var newFactory = QueryExtensions.CreateQueryFactory(db.Query().Clone());
33+
Assert.Equal(db.QueryTimeout, newFactory.QueryTimeout);
34+
}
2635
}
2736
}

QueryBuilder.Tests/MySqlExecutionTest.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,71 @@ Color TEXT NULL
5454

5555
db.Statement("DROP TABLE IF EXISTS `Cars`");
5656
}
57+
58+
[Fact]
59+
public void Count()
60+
{
61+
var db = SetupDb();
62+
var sql = @"
63+
CREATE TABLE Cars(
64+
Id INT PRIMARY KEY AUTO_INCREMENT,
65+
Brand TEXT NOT NULL,
66+
Year INT NOT NULL,
67+
Color TEXT NULL
68+
)
69+
";
70+
db.Statement(sql);
71+
72+
db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)");
73+
var count = db.Query("Cars").Count<int>();
74+
Assert.Equal(1, count);
75+
76+
db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Toyota', 2021)");
77+
count = db.Query("Cars").Count<int>();
78+
Assert.Equal(2, count);
79+
80+
int affected = db.Query("Cars").Delete();
81+
Assert.Equal(2, affected);
82+
83+
count = db.Query("Cars").Count<int>();
84+
Assert.Equal(0, count);
85+
86+
db.Statement("DROP TABLE IF EXISTS `Cars`");
87+
}
88+
89+
[Fact]
90+
public void CloneThenCount()
91+
{
92+
var db = SetupDb();
93+
var sql = @"
94+
CREATE TABLE Cars(
95+
Id INT PRIMARY KEY AUTO_INCREMENT,
96+
Brand TEXT NOT NULL,
97+
Year INT NOT NULL,
98+
Color TEXT NULL
99+
)
100+
";
101+
db.Statement(sql);
102+
103+
for (int i = 0; i < 10; i++)
104+
{
105+
db.Query("Cars").Insert(new
106+
{
107+
Brand = "Brand " + i,
108+
Year = "2020",
109+
});
110+
}
111+
112+
var query = db.Query("Cars").Where("Id", "<", 5);
113+
var count = query.Count<int>();
114+
var cloneCount = query.Clone().Count<int>();
115+
116+
Assert.Equal(4, count);
117+
Assert.Equal(4, cloneCount);
118+
119+
db.Statement("DROP TABLE IF EXISTS `Cars`");
120+
}
121+
57122
public QueryFactory SetupDb()
58123
{
59124
var host = System.Environment.GetEnvironmentVariable("SQLKATA_MYSQL_HOST");

SqlKata.Execution/Query.Extensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,16 @@ 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, xQuery.QueryFactory.QueryTimeout);
369+
QueryFactory factory;
370+
371+
if (xQuery.QueryFactory != null)
372+
{
373+
factory = new QueryFactory(xQuery.Connection, xQuery.Compiler, xQuery.QueryFactory.QueryTimeout);
374+
}
375+
else
376+
{
377+
factory = new QueryFactory(xQuery.Connection, xQuery.Compiler);
378+
}
370379

371380
factory.Logger = xQuery.Logger;
372381

SqlKata.Execution/XQuery.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ public class XQuery : Query
1414

1515
public XQuery(IDbConnection connection, Compiler compiler)
1616
{
17+
this.QueryFactory = new QueryFactory(connection, compiler);
1718
this.Connection = connection;
1819
this.Compiler = compiler;
1920
}
2021

2122
public override Query Clone()
2223
{
2324

24-
var query = new XQuery(this.Connection, this.Compiler);
25+
var query = new XQuery(this.QueryFactory.Connection, this.QueryFactory.Compiler);
26+
27+
if (this.QueryFactory?.QueryTimeout != null)
28+
{
29+
query.QueryFactory.QueryTimeout = this.QueryFactory?.QueryTimeout ?? 30;
30+
}
2531

2632
query.Clauses = this.Clauses.Select(x => x.Clone()).ToList();
2733
query.Logger = this.Logger;

0 commit comments

Comments
 (0)