@@ -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}
0 commit comments