From 13d7dfdcfd9ef477943e1abe87db47a1c8cd6d05 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 9 Jul 2023 17:31:43 +0800 Subject: [PATCH] Synchronization code --- .../ExpressionsToSql/DbMethods/DefaultDbMethod.cs | 6 ++++++ .../SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs | 1 + .../SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs | 4 ++++ .../MethodCallExpressionResolve_Helper.cs | 4 ++++ .../Realization/Dm/SqlBuilder/DmExpressionContext.cs | 5 +++++ .../MySql/SqlBuilder/MySqlExpressionContext.cs | 5 +++++ .../Oracle/SqlBuilder/OracleExpressionContext.cs | 5 +++++ .../SqlBuilder/SqlServerExpressionContext.cs | 5 +++++ .../Sqlite/SqlBuilder/SqliteExpressionContext.cs | 11 ++++++++--- 9 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index a35aeecb0..3d8147f16 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -968,5 +968,11 @@ namespace SqlSugar return result; } } + + public virtual string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $" DATE_PART('week', {parameterNameA})+1 "; + } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index b0b124dae..fb8e084bc 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -113,5 +113,6 @@ namespace SqlSugar string Modulo(MethodCallExpressionModel mode); string Like(MethodCallExpressionModel mode); string ToSingle(MethodCallExpressionModel mode); + string WeekOfYear(MethodCallExpressionModel mode); } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index 02d5ff348..571a165ac 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -9,6 +9,10 @@ namespace SqlSugar { public partial class SqlFunc { + public static int WeekOfYear(DateTime fieldName) + { + throw new NotSupportedException("Can only be used in expressions"); + } public static bool Like(string fieldName, string likeValue) { throw new NotSupportedException("Can only be used in expressions"); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs index 66f43016a..24f486986 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs @@ -855,6 +855,10 @@ namespace SqlSugar case "ToSingle": return this.Context.DbMehtods.ToSingle(model); default: + if (typeof(IDbMethods).GetMethods().Any(it => it.Name == name)) + { + return this.Context.DbMehtods.GetType().GetMethod(name).Invoke(this.Context.DbMehtods,new object[] { model}); + } break; } } diff --git a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs index cad924df0..0010210db 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmExpressionContext.cs @@ -54,6 +54,11 @@ namespace SqlSugar } public partial class DmMethod : DefaultDbMethod, IDbMethods { + public override string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $"TO_NUMBER(TO_CHAR({parameterNameA}, 'WW')) "; + } public override string ParameterKeyWord { get; set; } = ":"; public override string GetStringJoinSelector(string result, string separator) { diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs index 486e03498..d7a04e770 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs @@ -17,6 +17,11 @@ namespace SqlSugar } public class MySqlMethod : DefaultDbMethod, IDbMethods { + public override string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $" WEEK({parameterNameA}) "; + } public override string GetStringJoinSelector(string result, string separator) { return $"group_concat({result} separator '{separator}') "; diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs index 80366fccb..6b18de28a 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs @@ -76,6 +76,11 @@ namespace SqlSugar } public partial class OracleMethod : DefaultDbMethod, IDbMethods { + public override string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $"TO_NUMBER(TO_CHAR({parameterNameA}, 'WW')) "; + } public override string BitwiseAnd(MethodCallExpressionModel model) { var parameter = model.Args[0]; diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs index 07a735a67..8b098389e 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerExpressionContext.cs @@ -20,6 +20,11 @@ namespace SqlSugar } public partial class SqlServerMethod : DefaultDbMethod, IDbMethods { + public override string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $"DATEPART(WEEK, {parameterNameA}) "; + } public override string GetTableWithDataBase(string dataBaseName, string tableName) { return $"{dataBaseName}.dbo.{tableName}"; diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs index bf5acc4d6..3c7f00bc2 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs @@ -17,12 +17,17 @@ namespace SqlSugar } public class SqliteMethod : DefaultDbMethod, IDbMethods { + public override string WeekOfYear(MethodCallExpressionModel mode) + { + var parameterNameA = mode.Args[0].MemberName; + return $"STRFTIME('%W', {parameterNameA})+1 "; + } public override string Equals(MethodCallExpressionModel model) { - var result = base.Equals(model); - if (model.Args.Count == 3 && result.Trim().Last() == ')') + var result= base.Equals(model); + if (model.Args.Count == 3&& result.Trim().Last()==')') { - result = (" " + result.Trim().TrimEnd(')') + " COLLATE NOCASE ) "); + result = (" "+result.Trim().TrimEnd(')') + " COLLATE NOCASE ) "); } return result; }