Skip to content

Commit 2af4f96

Browse files
committed
Compilers use dedicated SqlDml methods for Date and Time
1 parent ec00730 commit 2af4f96

6 files changed

Lines changed: 67 additions & 49 deletions

File tree

Orm/Xtensive.Orm.Tests.Sql/DateTimeIntervalTest.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,83 +181,75 @@ public virtual void DateOnlyConstructTest()
181181
DefaultDateOnly);
182182
}
183183

184-
[Test]
185-
public virtual void DateOnlySubtractDateTimeTest()
186-
{
187-
CheckEquality(
188-
SqlDml.DateMinusDate(DefaultDateOnly, SecondDateOnly),
189-
DefaultDateTime - SecondDateTime);
190-
}
191-
192184
[Test]
193185
public virtual void DateOnlyExtractYearTest()
194186
{
195187
CheckEquality(
196-
SqlDml.Extract(SqlDateTimePart.Year, DefaultDateOnly),
188+
SqlDml.Extract(SqlDatePart.Year, DefaultDateOnly),
197189
DefaultDateOnly.Year);
198190
}
199191

200192
[Test]
201193
public virtual void DateOnlyExtractMonthTest()
202194
{
203195
CheckEquality(
204-
SqlDml.Extract(SqlDateTimePart.Month, DefaultDateOnly),
196+
SqlDml.Extract(SqlDatePart.Month, DefaultDateOnly),
205197
DefaultDateOnly.Month);
206198
}
207199

208200
[Test]
209201
public virtual void DateOnlyExtractDayTest()
210202
{
211203
CheckEquality(
212-
SqlDml.Extract(SqlDateTimePart.Day, DefaultDateOnly),
204+
SqlDml.Extract(SqlDatePart.Day, DefaultDateOnly),
213205
DefaultDateOnly.Day);
214206
}
215207

216208
[Test]
217209
public virtual void DateOnlyExtractDayOfWeekTest()
218210
{
219211
CheckEquality(
220-
SqlDml.Extract(SqlDateTimePart.DayOfWeek, DefaultDateOnly),
212+
SqlDml.Extract(SqlDatePart.DayOfWeek, DefaultDateOnly),
221213
(int) DefaultDateOnly.DayOfWeek);
222214
}
223215

224216
[Test]
225217
public virtual void DateOnlyExtractDayOfYearTest()
226218
{
227219
CheckEquality(
228-
SqlDml.Extract(SqlDateTimePart.DayOfYear, DefaultDateOnly),
220+
SqlDml.Extract(SqlDatePart.DayOfYear, DefaultDateOnly),
229221
DefaultDateOnly.DayOfYear);
230222
}
231223

232224
[Test]
233225
public virtual void TimeOnlyExtractHourTest()
234226
{
235227
CheckEquality(
236-
SqlDml.Extract(SqlDateTimePart.Hour, DefaultTimeOnly),
228+
SqlDml.Extract(SqlTimePart.Hour, DefaultTimeOnly),
237229
DefaultTimeOnly.Hour);
238230
}
239231

240232
[Test]
241233
public virtual void TimeOnlyExtractMinuteTest()
242234
{
243235
CheckEquality(
244-
SqlDml.Extract(SqlDateTimePart.Minute, DefaultTimeOnly),
236+
SqlDml.Extract(SqlTimePart.Minute, DefaultTimeOnly),
245237
DefaultTimeOnly.Minute);
246238
}
247239

248240
[Test]
249241
public virtual void TimeOnlyExtractSecondTest()
250242
{
251243
CheckEquality(
252-
SqlDml.Extract(SqlDateTimePart.Second, DefaultTimeOnly),
244+
SqlDml.Extract(SqlTimePart.Second, DefaultTimeOnly),
253245
DefaultTimeOnly.Second);
254246
}
255247

256248
[Test]
257249
public virtual void TimeOnlyExtractMillisecondTest()
258250
{
259251
CheckEquality(
260-
SqlDml.Extract(SqlDateTimePart.Millisecond, DefaultTimeOnly),
252+
SqlDml.Extract(SqlTimePart.Millisecond, DefaultTimeOnly),
261253
DefaultTimeOnly.Millisecond);
262254
}
263255

@@ -268,6 +260,14 @@ public virtual void TimeOnlyConstructTest()
268260
SqlDml.TimeConstruct(DefaultTimeOnly.Hour, DefaultTimeOnly.Minute, DefaultTimeOnly.Second, DefaultTimeOnly.Millisecond),
269261
DefaultTimeOnly);
270262
}
263+
264+
[Test]
265+
public virtual void TimeOnlySubtractTimeOnlyTest()
266+
{
267+
CheckEquality(
268+
SqlDml.TimeMinusTime(DefaultTimeOnly, SecondTimeOnly),
269+
DefaultTimeOnly - SecondTimeOnly);
270+
}
271271
#endif
272272

273273
[Test]

Orm/Xtensive.Orm/Orm/Providers/Expressions/MemberCompilers/DateOnlyCompilers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public static SqlExpression DateOnlyOperatorLessThanOrEqual(
117117

118118
[Compiler(typeof(DateOnly), "AddYears")]
119119
public static SqlExpression DateOnlyAddYears(SqlExpression _this, [Type(typeof(int))] SqlExpression value) =>
120-
SqlDml.DateTimeAddYears(_this, value);
120+
SqlDml.DateAddYears(_this, value);
121121

122122
[Compiler(typeof(DateOnly), "AddMonths")]
123123
public static SqlExpression DateOnlyAddMonths(SqlExpression _this, [Type(typeof(int))] SqlExpression value) =>
124-
SqlDml.DateTimeAddMonths(_this, value);
124+
SqlDml.DateAddMonths(_this, value);
125125

126126
[Compiler(typeof(DateOnly), "AddDays")]
127127
public static SqlExpression DateOnlyAddDays(SqlExpression _this, [Type(typeof(int))] SqlExpression value) =>

Orm/Xtensive.Orm/Orm/Providers/Expressions/MemberCompilers/TimeOnlyCompilers.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,58 +75,58 @@ public static SqlExpression TimeOnlyCtor(
7575

7676
[Compiler(typeof(TimeOnly), Operator.Equality, TargetKind.Operator)]
7777
public static SqlExpression TimeOnlyOperatorEquality(
78-
[Type(typeof(TimeOnly))] SqlExpression d1,
79-
[Type(typeof(TimeOnly))] SqlExpression d2)
78+
[Type(typeof(TimeOnly))] SqlExpression t1,
79+
[Type(typeof(TimeOnly))] SqlExpression t2)
8080
{
81-
return d1 == d2;
81+
return t1 == t2;
8282
}
8383

8484
[Compiler(typeof(TimeOnly), Operator.Inequality, TargetKind.Operator)]
8585
public static SqlExpression TimeOnlyOperatorInequality(
86-
[Type(typeof(TimeOnly))] SqlExpression d1,
87-
[Type(typeof(TimeOnly))] SqlExpression d2)
86+
[Type(typeof(TimeOnly))] SqlExpression t1,
87+
[Type(typeof(TimeOnly))] SqlExpression t2)
8888
{
89-
return d1 != d2;
89+
return t1 != t2;
9090
}
9191

9292
[Compiler(typeof(TimeOnly), Operator.GreaterThan, TargetKind.Operator)]
9393
public static SqlExpression TimeOnlyyOperatorGreaterThan(
94-
[Type(typeof(TimeOnly))] SqlExpression d1,
95-
[Type(typeof(TimeOnly))] SqlExpression d2)
94+
[Type(typeof(TimeOnly))] SqlExpression t1,
95+
[Type(typeof(TimeOnly))] SqlExpression t2)
9696
{
97-
return d1 > d2;
97+
return t1 > t2;
9898
}
9999

100100
[Compiler(typeof(TimeOnly), Operator.GreaterThanOrEqual, TargetKind.Operator)]
101101
public static SqlExpression TimeOnlyOperatorGreaterThanOrEqual(
102-
[Type(typeof(TimeOnly))] SqlExpression d1,
103-
[Type(typeof(TimeOnly))] SqlExpression d2)
102+
[Type(typeof(TimeOnly))] SqlExpression t1,
103+
[Type(typeof(TimeOnly))] SqlExpression t2)
104104
{
105-
return d1 >= d2;
105+
return t1 >= t2;
106106
}
107107

108108
[Compiler(typeof(TimeOnly), Operator.LessThan, TargetKind.Operator)]
109109
public static SqlExpression TimeOnlyOperatorLessThan(
110-
[Type(typeof(TimeOnly))] SqlExpression d1,
111-
[Type(typeof(TimeOnly))] SqlExpression d2)
110+
[Type(typeof(TimeOnly))] SqlExpression t1,
111+
[Type(typeof(TimeOnly))] SqlExpression t2)
112112
{
113-
return d1 < d2;
113+
return t1 < t2;
114114
}
115115

116116
[Compiler(typeof(TimeOnly), Operator.LessThanOrEqual, TargetKind.Operator)]
117117
public static SqlExpression TimeOnlyOperatorLessThanOrEqual(
118-
[Type(typeof(TimeOnly))] SqlExpression d1,
119-
[Type(typeof(TimeOnly))] SqlExpression d2)
118+
[Type(typeof(TimeOnly))] SqlExpression t1,
119+
[Type(typeof(TimeOnly))] SqlExpression t2)
120120
{
121-
return d1 <= d2;
121+
return t1 <= t2;
122122
}
123123

124124
[Compiler(typeof(TimeOnly), Operator.Subtraction, TargetKind.Operator)]
125125
public static SqlExpression TimeOnlyOperatorSubtraction(
126-
[Type(typeof(TimeOnly))] SqlExpression d1,
127-
[Type(typeof(TimeOnly))] SqlExpression d2)
126+
[Type(typeof(TimeOnly))] SqlExpression t1,
127+
[Type(typeof(TimeOnly))] SqlExpression t2)
128128
{
129-
throw new NotImplementedException();
129+
return SqlDml.TimeMinusTime(t1, t2);
130130
}
131131

132132
#endregion
@@ -139,6 +139,10 @@ public static SqlExpression TimeOnlyAddHours(SqlExpression _this, [Type(typeof(d
139139
public static SqlExpression TimeOnlyAddMinutes(SqlExpression _this, [Type(typeof(double))] SqlExpression value) =>
140140
SqlDml.TimeAddMinutes(_this, value);
141141

142+
[Compiler(typeof(TimeOnly), "Add")]
143+
public static SqlExpression TimeOnlyAdd(SqlExpression _this, [Type(typeof(TimeSpan))] SqlExpression value) =>
144+
SqlDml.TimePlusInterval(_this, value);
145+
142146
[Compiler(typeof(TimeOnly), "ToString")]
143147
public static SqlExpression TimeOnlyToStringIso(SqlExpression _this)
144148
{

Orm/Xtensive.Orm/Sql/Internals/SqlValidator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ public static bool IsArithmeticExpression(SqlExpression node)
180180
case SqlNodeType.DateTimeOffsetMinusInterval:
181181
case SqlNodeType.DateTimeOffsetPlusInterval:
182182
case SqlNodeType.DateTimeOffsetMinusDateTimeOffset:
183+
#if NET6_0_OR_GREATER
184+
case SqlNodeType.TimePlusInterval:
185+
case SqlNodeType.TimeMinusTime:
186+
#endif
183187
return true;
184188
case SqlNodeType.Variant:
185189
var variant = (SqlVariant) node;

Orm/Xtensive.Orm/Sql/SqlDml.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,28 +648,35 @@ public static SqlBinary DateTimePlusInterval(SqlExpression left, SqlExpression r
648648
return new SqlBinary(SqlNodeType.DateTimePlusInterval, left, right);
649649
}
650650

651-
public static SqlBinary DateTimeMinusInterval(SqlExpression left, SqlExpression right)
651+
#if NET6_0_OR_GREATER //DO_DATEONLY
652+
public static SqlBinary TimePlusInterval(SqlExpression left, SqlExpression right)
652653
{
653654
ArgumentValidator.EnsureArgumentNotNull(left, "left");
654655
ArgumentValidator.EnsureArgumentNotNull(right, "right");
655-
return new SqlBinary(SqlNodeType.DateTimeMinusInterval, left, right);
656+
return new SqlBinary(SqlNodeType.TimePlusInterval, left, right);
656657
}
657658

658-
public static SqlBinary DateTimeMinusDateTime(SqlExpression left, SqlExpression right)
659+
public static SqlBinary TimeMinusTime(SqlExpression left, SqlExpression right)
659660
{
660661
ArgumentValidator.EnsureArgumentNotNull(left, "left");
661662
ArgumentValidator.EnsureArgumentNotNull(right, "right");
662-
return new SqlBinary(SqlNodeType.DateTimeMinusDateTime, left, right);
663+
return new SqlBinary(SqlNodeType.TimeMinusTime, left, right);
663664
}
665+
#endif
664666

665-
#if NET6_0_OR_GREATER //DO_DATEONLY
666-
public static SqlBinary DateMinusDate(SqlExpression left, SqlExpression right)
667+
public static SqlBinary DateTimeMinusInterval(SqlExpression left, SqlExpression right)
667668
{
668669
ArgumentValidator.EnsureArgumentNotNull(left, "left");
669670
ArgumentValidator.EnsureArgumentNotNull(right, "right");
670-
return new SqlBinary(SqlNodeType.DateMinusDate, left, right);
671+
return new SqlBinary(SqlNodeType.DateTimeMinusInterval, left, right);
672+
}
673+
674+
public static SqlBinary DateTimeMinusDateTime(SqlExpression left, SqlExpression right)
675+
{
676+
ArgumentValidator.EnsureArgumentNotNull(left, "left");
677+
ArgumentValidator.EnsureArgumentNotNull(right, "right");
678+
return new SqlBinary(SqlNodeType.DateTimeMinusDateTime, left, right);
671679
}
672-
#endif
673680

674681
public static SqlFunctionCall DateTimeAddYears(SqlExpression source, SqlExpression years)
675682
{

Orm/Xtensive.Orm/Sql/SqlNodeType.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public enum SqlNodeType
4747
DateTimeOffsetPlusInterval,
4848
DateTimeOffsetMinusInterval,
4949
DateTimeOffsetMinusDateTimeOffset,
50+
#if NET6_0_OR_GREATER //DO_DATEONLY
51+
TimePlusInterval,
52+
TimeMinusTime,
53+
#endif
5054
DeclareCursor,
5155
DefaultValue,
5256
Delete,
@@ -113,6 +117,5 @@ public enum SqlNodeType
113117
While,
114118
Fragment,
115119
Metadata,
116-
DateMinusDate,
117120
}
118121
}

0 commit comments

Comments
 (0)