From 8d5b8a717d7c5931f0846a408025594b5de11a92 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 8 Jul 2017 02:02:58 +0800 Subject: [PATCH] Add SqlFunc.ToTime --- .../UnitTest/ExpressionTest/Method.cs | 16 ++++++++++++++++ .../ExpressionsToSql/Method/DefaultDbMethod.cs | 6 ++++++ .../ExpressionsToSql/Method/IDbMethods.cs | 1 + .../SqlSugar/ExpressionsToSql/Method/SqlFunc.cs | 11 +++++++++++ .../ResolveItems/MethodCallExpressionResolve.cs | 2 ++ 5 files changed, 36 insertions(+) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs index f71e48a4f..1bc756883 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs @@ -70,6 +70,7 @@ namespace OrmTest.UnitTest Substring(); Replace(); Length(); + Time(); } base.End("Method Test"); } @@ -111,6 +112,21 @@ namespace OrmTest.UnitTest }, "Length error"); } + + private void Time() + { + TimeSpan s = TimeSpan.Parse("11:22:22"); + Expression> exp = it => SqlFunc.ToTime("11:12:59")==s; + SqlServerExpressionContext expContext = new SqlServerExpressionContext(); + expContext.Resolve(exp, ResolveExpressType.WhereSingle); + var value = expContext.Result.GetString(); + var pars = expContext.Parameters; + base.Check(value, pars, "(CAST(@MethodConst0 AS TIME) = @Const1 )", new List() { + new SugarParameter("@MethodConst0","11:12:59"),new SugarParameter("@Const1",s) + }, "Time error"); + } + + private void Replace() { var x2 = Guid.NewGuid(); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs index 283c39e2f..676a10fbc 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs @@ -191,6 +191,12 @@ namespace SqlSugar return string.Format(" CAST({0} AS DATETIME)", parameter.MemberName); } + public virtual string ToTime(MethodCallExpressionModel model) + { + var parameter = model.Args[0]; + return string.Format(" CAST({0} AS TIME)", parameter.MemberName); + } + public virtual string ToDecimal(MethodCallExpressionModel model) { var parameter = model.Args[0]; diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs index df69aed5e..394da7101 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs @@ -37,6 +37,7 @@ namespace SqlSugar string ToBool(MethodCallExpressionModel model); string Substring(MethodCallExpressionModel model); string ToDate(MethodCallExpressionModel model); + string ToTime(MethodCallExpressionModel model); string ToDecimal(MethodCallExpressionModel model); string Length(MethodCallExpressionModel model); string Replace(MethodCallExpressionModel model); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs index fab566031..df845d243 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs @@ -69,7 +69,18 @@ namespace SqlSugar public static TResult IIF(bool Expression, TResult thenValue, TResult elseValue) { throw new NotSupportedException("This method is not supported by the current parameter"); } public static int ToInt32(object value) { return value.ObjToInt(); } public static long ToInt64(object value) { return Convert.ToInt64(value); } + /// + /// yyyy-MM-dd HH:mm:ss.fff + /// + /// + /// public static DateTime ToDate(object value) { return value.ObjToDate(); } + /// + ///HH:mm:ss + /// + /// + /// + public static TimeSpan ToTime(object value) { throw new NotSupportedException("This method is not supported by the current parameter"); } public static string ToString(object value) { return value.ObjToString(); } public static decimal ToDecimal(object value) { return value.ObjToDecimal(); } public static Guid ToGuid(object value) { return Guid.Parse(value.ObjToString()); } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index b40cd8e7b..50f91dbf6 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -208,6 +208,8 @@ namespace SqlSugar return this.Context.DbMehtods.ToInt64(model); case "ToDate": return this.Context.DbMehtods.ToDate(model); + case "ToTime": + return this.Context.DbMehtods.ToTime(model); case "ToString": return this.Context.DbMehtods.ToString(model); case "ToDecimal":