Skip to content

Commit

Permalink
Begin writing support for DateTime constructors
Browse files Browse the repository at this point in the history
The simple template mapping approach here gets it slightly right, but
it needs to support DateTimeKind in the constructor to begin to get
DateTimeOffset correct.
  • Loading branch information
mfenniak committed Oct 28, 2014
1 parent aecbd58 commit 7c5b7fd
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
54 changes: 54 additions & 0 deletions rethinkdb-net-test/Expressions/DateTimeExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,59 @@ public void DateTimeOffsetUtcNow()
}
);
}

[Test]
public void DateTimeYearMonthDayConstructor()
{
var expr = ExpressionUtils.CreateFunctionTerm<int, DateTime>(queryConverter, (i) => new DateTime(i, i, i));
var variableRefenceTerm = new Term() {
type = Term.TermType.VAR,
args = {
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 2,
}
}
}
};

expr.ShouldBeEquivalentTo(
new Term()
{
type = Term.TermType.FUNC,
args = {
new Term() {
type = Term.TermType.MAKE_ARRAY,
args = {
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 2,
}
}
}
},
new Term() {
type = Term.TermType.TIME,
args = {
variableRefenceTerm,
variableRefenceTerm,
variableRefenceTerm,
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "Z",
}
}
}
}
}
}
);
}
}
}
28 changes: 28 additions & 0 deletions rethinkdb-net-test/Integration/BlankTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,33 @@ public void ConstructedError()

#pragma warning restore 0429
}

[Test]
public void ExpressionDate()
{
var date = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3)));
Assert.That(date, Is.EqualTo(new DateTime(2014, 2, 3)));
}

[Test]
public void ExpressionDateTime()
{
var datetime = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15)));
Assert.That(datetime, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15)));
}

[Test]
public void ExpressionDateTimeOffset()
{
var datetime = connection.Run(Query.Expr(() => new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15))));
Assert.That(datetime, Is.EqualTo(new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15))));
}

[Test]
public void ExpressionDateTimeWithMilliseconds()
{
var datetimems = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15, 500)));
Assert.That(datetimems, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15, 500)));
}
}
}
31 changes: 31 additions & 0 deletions rethinkdb-net/Expressions/DateTimeExpressionConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ public static void RegisterDateTimeConstructors(DefaultExpressionConverterFactor
expressionConverterFactory.RegisterTemplateMapping<DateTimeOffset>(
() => DateTimeOffset.UtcNow,
() => new Term() { type = Term.TermType.NOW });

expressionConverterFactory.RegisterTemplateMapping<int, int, int, DateTime>(
(year, month, day) => new DateTime(year, month, day),
(year, month, day) => new Term() { type = Term.TermType.TIME, args = { year, month, day, String("Z") } });
expressionConverterFactory.RegisterTemplateMapping<int, int, int, int, int, int, DateTime>(
(year, month, day, hour, minute, second) => new DateTime(year, month, day, hour, minute, second),
(year, month, day, hour, minute, second) => new Term() {
type = Term.TermType.TIME,
args = { year, month, day, hour, minute, second, String("Z") } });
expressionConverterFactory.RegisterTemplateMapping<int, int, int, int, int, int, int, DateTime>(
(year, month, day, hour, minute, second, millisecond) => new DateTime(year, month, day, hour, minute, second, millisecond),
(year, month, day, hour, minute, second, millisecond) => new Term() {
type = Term.TermType.TIME,
args = { year, month, day, hour, minute, Add(second, Binary(millisecond, Term.TermType.DIV, 1000)), String("Z") }
});

expressionConverterFactory.RegisterTemplateMapping<DateTime, DateTimeOffset>(
(datetime) => new DateTimeOffset(datetime),
(datetime) => datetime);
}

public static void RegisterDateTimeAddMethods(DefaultExpressionConverterFactory expressionConverterFactory)
Expand Down Expand Up @@ -214,6 +233,18 @@ public static void RegisterTimeSpanConstructors(DefaultExpressionConverterFactor
);
}

private static Term String(string str)
{
return new Term()
{
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_STR,
r_str = str,
}
};
}

private static Term Binary(Term leftTerm, Term.TermType type, double rightTerm)
{
return new Term()
Expand Down
24 changes: 23 additions & 1 deletion rethinkdb-net/Expressions/DefaultExpressionConverterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,29 @@ public void RegisterTemplateMapping<TParameter1, TParameter2, TParameter3, TPara
case ExpressionType.New:
RegisterNewTemplateMapping(
(NewExpression)templateBody,
terms => termConstructor(terms[0], terms[1], terms[2], terms[3], terms[4], terms[6]));
terms => termConstructor(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5]));
break;
default:
throw new NotImplementedException("Template did not match supported pattern");
}
}

public void RegisterTemplateMapping<TParameter1, TParameter2, TParameter3, TParameter4, TParameter5, TParameter6, TParameter7, TReturn>(
Expression<Func<TParameter1, TParameter2, TParameter3, TParameter4, TParameter5, TParameter6, TParameter7, TReturn>> template,
Func<Term, Term, Term, Term, Term, Term, Term, Term> termConstructor)
{
var templateBody = template.Body;
switch (templateBody.NodeType)
{
case ExpressionType.Call:
RegisterMethodCallTemplateMapping(
(MethodCallExpression)templateBody,
terms => termConstructor(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5], terms[6]));
break;
case ExpressionType.New:
RegisterNewTemplateMapping(
(NewExpression)templateBody,
terms => termConstructor(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5], terms[6]));
break;
default:
throw new NotImplementedException("Template did not match supported pattern");
Expand Down

0 comments on commit 7c5b7fd

Please sign in to comment.