Skip to content

Commit cb33ae8

Browse files
SqlFunctions DateAdd DatePart
1 parent d238971 commit cb33ae8

6 files changed

Lines changed: 24 additions & 125 deletions

File tree

FluentSql.Tests/SelectStatement/SelectStatementTest.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ public void WhereClauseWithGetDatePartDayOfYear1()
807807
public void WhereClauseWithGetDatePartDayOfYear2()
808808
{
809809
var store = new EntityStore(_dbConnection);
810-
var order = store.GetSingle<Order>(o => SqlFunctions.GetDayOfYear(DateTime.Now) >= SqlFunctions.GetDayOfYear(o.OrderDate));
810+
var dayOfYear = 17;
811+
var order = store.GetSingle<Order>(o => dayOfYear >= SqlFunctions.GetDayOfYear(o.OrderDate));
811812

812813
Xunit.Assert.NotNull(order);
813814
}
@@ -875,27 +876,6 @@ public void WhereClauseWithGetDatePartMillisecond()
875876
Xunit.Assert.NotNull(singleEmployee);
876877
}
877878

878-
[Fact]
879-
public void WhereClauseWithGetDayDiff()
880-
{
881-
var store = new EntityStore(_dbConnection);
882-
var singleEmployee = store.GetSingle<Employee>(e => SqlFunctions.GetDayDiff(e.Birthdate, DateTime.Now) >= 365);
883-
884-
Xunit.Assert.NotNull(singleEmployee);
885-
Xunit.Assert.IsType<Employee>(singleEmployee);
886-
887-
var order = new Order { OrderDate = DateTime.Now };
888-
var singleOrder = store.GetSingle<Order>(o => SqlFunctions.GetDayDiff(o.OrderDate, order.OrderDate) >= 30);
889-
890-
Xunit.Assert.NotNull(singleOrder);
891-
Xunit.Assert.IsType<Order>(singleOrder);
892-
893-
var otherOrder = store.GetSingle<Order>(o => SqlFunctions.GetDayDiff(o.OrderDate, TestConstants.DUMMY_DATE) >= 30);
894-
895-
Xunit.Assert.NotNull(otherOrder);
896-
Xunit.Assert.IsType<Order>(otherOrder);
897-
}
898-
899879
public void Dispose()
900880
{
901881
_dbConnection.Close();

FluentSql/SqlFunctions.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,5 @@ public static int GetMillisecond(DateTime? fieldName)
128128
throw new NotSupportedException(FUNCTION_DIRECT_CALL);
129129
}
130130
#endregion
131-
132-
#region DateDiff Function
133-
public static int GetDayDiff(DateTime? minuendFieldName, DateTime? subtrahendFieldName)
134-
{
135-
throw new NotSupportedException(FUNCTION_DIRECT_CALL);
136-
}
137-
#endregion
138131
}
139132
}

FluentSql/SqlGenerators/ISqlGenerator.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,6 @@ public interface ISqlGenerator
165165
/// <returns></returns>
166166
string GetDatePartFunction(string methodName, Type entityType, string fieldName);
167167

168-
/// <summary>
169-
/// Returns the Sql function that resolves the difference between two date fields
170-
/// </summary>
171-
/// <param name="minuendType"></param>
172-
/// <param name="minuend"></param>
173-
/// <param name="subtrahendType"></param>
174-
/// <param name="subtrahend"></param>
175-
/// <returns></returns>
176-
string GetDateDiffFunction(Type minuendType, string minuend, Type subtrahendType, string subtrahend);
177-
178168
/// <summary>
179169
/// Returns the required field formatting for the specific SQL dialect.
180170
/// </summary>

FluentSql/SqlGenerators/SqlServer/SqlServerSqlGenerator.cs

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -178,38 +178,32 @@ public string GetStringComparisonOperator()
178178
return "LIKE";
179179
}
180180

181-
public string GetDateDiffFunction(Type minuendType, string minuend, Type subtrahendType, string subtrahend)
182-
{
183-
if (minuendType == null || string.IsNullOrEmpty(minuend))
184-
throw new ArgumentNullException("minuendType cannot be null");
185-
186-
if (subtrahendType == null || string.IsNullOrEmpty(subtrahend))
187-
throw new ArgumentNullException("subtrahendType cannot be null");
188-
189-
var datediffFunction = "DATEDIFF({0}, {1}, {2})";
190-
var formattedMinuend = GetDateDiffField(minuendType, minuend);
191-
var formattedSubtrahend = GetDateDiffField(subtrahendType, subtrahend);
192-
var datePart = GetDatePartArgument(Methods.GETDAYDIFF);
193-
194-
return string.Format(datediffFunction, datePart, formattedMinuend, formattedSubtrahend);
195-
196-
}
197-
198181
public string GetDatePartFunction(string methodName, Type entityType, string fieldName)
199182
{
200183
if (string.IsNullOrEmpty(methodName) || entityType == null || string.IsNullOrEmpty(fieldName))
201184
throw new ArgumentNullException("Arguements can not be null.");
202185

186+
if (entityType == typeof(DateTime) || entityType == typeof(DateTime?))
187+
throw new Exception("SqlServerGenerator: DatePart function does not support DateTime functions for arguments.");
188+
203189
var datePartFunction = "DATEPART({0}, {1})";
204-
var verifiedField = EntityMapper.Entities[entityType].Properties.FirstOrDefault(p => p.Name == fieldName);
190+
var dateField = string.Empty;
205191

206-
if (verifiedField == null)
192+
if (EntityMapper.Entities.Keys.Contains(entityType))
193+
{
194+
var verifiedField = EntityMapper.Entities[entityType].Properties.FirstOrDefault(p => p.Name == fieldName);
195+
196+
if (verifiedField == null)
197+
throw new Exception(string.Format("Could not find field {0} in type {1}", fieldName, entityType));
198+
199+
dateField = FormatFieldforSql(entityType, fieldName);
200+
}
201+
else
207202
throw new Exception(string.Format("Could not find field {0} in type {1}", fieldName, entityType));
208203

209-
var formattedField = FormatFieldforSql(entityType, fieldName);
210204
var datePart = GetDatePartArgument(methodName);
211205

212-
return string.Format(datePartFunction, datePart, formattedField);
206+
return string.Format(datePartFunction, datePart, dateField);
213207
}
214208

215209
public string GetDateAddFunction(string methodName, Type entityType, string fieldName, int number)
@@ -223,6 +217,9 @@ public string GetDateAddFunction(string methodName, Type entityType, string fiel
223217
if (string.IsNullOrEmpty(fieldName))
224218
throw new ArgumentNullException("Field Name (fieldName) can not be null.");
225219

220+
if (entityType == typeof(DateTime) || entityType == typeof(DateTime?))
221+
throw new Exception("SqlServerGenerator: DateAdd function does not support DateTime function for arguments.");
222+
226223
var DateFunction = "DATEADD({0}, {1}, {2})";
227224
var verifiedField = EntityMapper.Entities[entityType].Properties.FirstOrDefault(p => p.Name == fieldName);
228225

@@ -272,7 +269,7 @@ private string GetDatePartArgument(string methodName)
272269
else if (methodName == Methods.ADDMONTHS || methodName == Methods.GETMONTH)
273270
return "month";
274271

275-
else if (methodName == Methods.ADDDAYS || methodName == Methods.GETDAY || methodName == Methods.GETDAYDIFF)
272+
else if (methodName == Methods.ADDDAYS || methodName == Methods.GETDAY)
276273
return "day";
277274

278275
if (methodName == Methods.ADDWEEKS || methodName == Methods.GETWEEK)
@@ -297,20 +294,6 @@ private string GetDatePartArgument(string methodName)
297294
throw new NotSupportedException(string.Format("Method Name not supported: {0}", methodName));
298295
}
299296

300-
private string GetDateDiffField(Type operandType, string operandName)
301-
{
302-
if (operandType == null) return null;
303-
304-
if (operandType == typeof(DateTime?) || operandType == typeof(DateTime)) return operandName;
305-
306-
var verifiedField = EntityMapper.Entities[operandType].Properties
307-
.FirstOrDefault(p => p.Name == operandName);
308-
309-
if (verifiedField == null)
310-
throw new Exception(string.Format("Could not find field {0} in type {1}", operandType, operandType));
311-
312-
return FormatFieldforSql(operandType, operandName);
313-
}
314297
#endregion
315298
}
316299
}

FluentSql/Support/Helpers/ExpressionHelper.cs

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -511,62 +511,19 @@ private MethodCallExpression ParseSqlFunctions(MethodCallExpression methodCall)
511511

512512
GetDateOperands(methodCall, 0, ref operandType, ref operand);
513513

514+
if (operandType == typeof(DateTime) || operandType == typeof(DateTime?))
515+
throw new Exception("Expression Helper does not support DateTime functions or variables. It supports DateTime Entity Types");
516+
514517
var datePartFuntion = EntityMapper.SqlGenerator.GetDatePartFunction(methodName, operandType, operand);
515518

516519
_predicateString.Push(datePartFuntion);
517520
}
518-
else if (methodName == Methods.GETDAYDIFF)
519-
{
520-
if (methodCall.Arguments.Count < 2)
521-
throw new Exception(string.Format("Method not implemented: {0}", methodName ?? "Undetermined method name."));
522-
523-
ResolveDateDiffFunction(methodCall);
524-
525-
}
526521
else
527522
throw new Exception(string.Format("Method not implemented: {0}", methodName ?? "Undetermined method name."));
528523

529524
return methodCall;
530525
}
531526

532-
private void ResolveDateDiffFunction(MethodCallExpression methodCall)
533-
{
534-
var method = methodCall.Method;
535-
var methodName = method.Name;
536-
537-
if (methodCall.Arguments.Count < 2)
538-
throw new Exception(string.Format("Method not implemented: {0}", methodName ?? "Undetermined method name."));
539-
540-
Expression fieldTypeExpression = methodCall.Arguments[0];
541-
string minuend = string.Empty;
542-
string subtrahend = string.Empty;
543-
Type minuendType = null;
544-
Type subtrahendType = null;
545-
546-
GetDateOperands(methodCall, 0, ref minuendType, ref minuend);
547-
GetDateOperands(methodCall, 1, ref subtrahendType, ref subtrahend);
548-
549-
if (minuendType == typeof(DateTime) || minuendType == typeof(DateTime?))
550-
{
551-
var paramName = _paramNameGenerator.GetNextParameterName(_parameterName);
552-
553-
QueryParameters.Add(paramName, minuend);
554-
minuend = paramName;
555-
}
556-
557-
if (subtrahendType == typeof(DateTime) || subtrahendType == typeof(DateTime?))
558-
{
559-
var paramName = _paramNameGenerator.GetNextParameterName(_parameterName);
560-
561-
QueryParameters.Add(paramName, subtrahend);
562-
subtrahend = paramName;
563-
}
564-
565-
var dateDiffFunction = EntityMapper.SqlGenerator.GetDateDiffFunction(minuendType, minuend, subtrahendType, subtrahend);
566-
567-
_predicateString.Push(dateDiffFunction);
568-
}
569-
570527
private void GetDateOperands(MethodCallExpression methodCall, int argOrdinalPosition, ref Type operandType, ref string operand)
571528
{
572529
Expression fieldTypeExpression = methodCall.Arguments[argOrdinalPosition];

FluentSql/Support/Helpers/Methods.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public class Methods
4444
public static string GETMILLISECOND = "GetMillisecond";
4545
#endregion
4646

47-
#region
48-
public static string GETDAYDIFF = "GetDayDiff";
49-
#endregion
50-
5147
public static string COMPARETO = "CompareTo";
5248

5349
public static string YEAR = "Year";

0 commit comments

Comments
 (0)