Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for null reference exception on datetime.utcnow.date in where cla… #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Dashing.Tests/Engine/DML/WhereClauseWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,27 @@ public void WhereContainsNullableCheck() {
Assert.Equal(" where ((t_100.[Nullable] is not null) and t_100.[Name] like @l_1)", actual.Sql);
}

[Fact]
public void DateTimedotNowAsItStands() {
var target = MakeTarget();
Expression<Func<Comment, bool>> pred = c => c.CommentDate <= DateTime.UtcNow;
var now = DateTime.UtcNow;
var actual = target.GenerateSql(new[] { pred }, null);
var param = (DateTime)actual.Parameters.GetValue("l_1");
Assert.Equal(" where ([CommentDate] <= @l_1)", actual.Sql);
Assert.True(Math.Abs((param - now).TotalSeconds) < 60); //if it's within a minute, it's just the execution times
}

[Fact]
public void DateTimedotDateFix() {
var target = MakeTarget();
Expression<Func<Comment, bool>> pred = c => c.CommentDate <= DateTime.UtcNow.Date;
var actual = target.GenerateSql(new[] { pred }, null);
var param = (DateTime)actual.Parameters.GetValue("l_1");
Assert.Equal(" where ([CommentDate] <= @l_1)", actual.Sql);
Assert.True(DateTime.UtcNow.Date <= param && param <= DateTime.UtcNow.Date);
}

private static WhereClauseWriter MakeTarget() {
return new WhereClauseWriter(new SqlServerDialect(), MakeConfig());
}
Expand Down
28 changes: 16 additions & 12 deletions Dashing/Engine/DML/WhereClauseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,27 @@ private ISqlElement VisitMethodCall(MethodCallExpression exp) {
}

private ISqlElement VisitMemberAccess(MemberExpression exp) {
if (exp.Expression == null) {
// static property
if (this.isTopOfBinaryOrMethod) {
// quicker path
var propInfo = exp.Member as PropertyInfo;
if (propInfo != null) {
return this.AddParameter(propInfo.GetValue(null));
}
var fieldInfo = exp.Member as FieldInfo;
if (exp.Expression == null) { // static property

//quick path
var propInfo = exp.Member as PropertyInfo;
if (propInfo != null) {
this.value = propInfo.GetValue(null);
} else {
var fieldInfo = exp.Member as FieldInfo;
if (fieldInfo != null) {
return this.AddParameter(fieldInfo.GetValue(null));
this.value = fieldInfo.GetValue(null);
} else {
// slow path
this.value = this.GetDynamicValue(exp);
}
}

// slow path
return this.AddParameter(this.GetDynamicValue(exp));
if (this.isTopOfBinaryOrMethod) {
return this.AddParameter(this.value);
}

this.isConstantExpression = true;
return null;
}

Expand Down