mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 10:08:19 +08:00
Support custom SqlFunc Method
This commit is contained in:
53
Src/Asp.Net/SqlServerTest/Demos/C_ExtSqlFun.cs
Normal file
53
Src/Asp.Net/SqlServerTest/Demos/C_ExtSqlFun.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
using OrmTest.Models;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class ExtSqlFun : DemoBase
|
||||
{
|
||||
public static SqlSugarClient GetDb()
|
||||
{
|
||||
//Create ext method
|
||||
var expMethods = new List<SqlFuncExternal>();
|
||||
expMethods.Add(new SqlFuncExternal()
|
||||
{
|
||||
UniqueMethodName = "MyToString",
|
||||
MethodValue = (expInfo, dbType, expContext) =>
|
||||
{
|
||||
return string.Format("CAST({0} AS VARCHAR(MAX))", expInfo.Args[0].MemberName);
|
||||
}
|
||||
});
|
||||
|
||||
var config = new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
SqlFuncServices = expMethods//set ext method
|
||||
}
|
||||
};
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(config);
|
||||
return db;
|
||||
}
|
||||
|
||||
public static string MyToString<T>(T str)
|
||||
{
|
||||
throw new NotSupportedException("Can only be used in expressions");
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetDb();
|
||||
var list = db.Queryable<Student>().Where(it => MyToString(it.Id) == "1302583").ToList();
|
||||
var sql = db.Queryable<Student>().Where(it => MyToString(it.Id) == "1302583").ToSql();
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
}
|
@@ -48,6 +48,7 @@ namespace OrmTest
|
||||
OrmTest.Demo.Aop.Init();
|
||||
OrmTest.Demo.MasterSlave.Init();
|
||||
OrmTest.Demo.SharedConnection.Init();
|
||||
OrmTest.Demo.ExtSqlFun.Init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -56,6 +56,7 @@
|
||||
<Compile Include="Demos\4_Delete.cs" />
|
||||
<Compile Include="Demos\9_Aop.cs" />
|
||||
<Compile Include="Demos\B_SharedConnection.cs" />
|
||||
<Compile Include="Demos\C_ExtSqlFun.cs" />
|
||||
<Compile Include="Demos\DemoBase.cs" />
|
||||
<Compile Include="Demos\3_Insert.cs" />
|
||||
<Compile Include="Demos\1_Query.cs" />
|
||||
|
@@ -148,6 +148,7 @@ namespace SqlSugar
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||
resolveExpress.Resolve(expression, resolveType);
|
||||
if (this.Parameters == null)
|
||||
this.Parameters = new List<SugarParameter>();
|
||||
|
@@ -102,6 +102,7 @@ namespace SqlSugar
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||
resolveExpress.Resolve(expression, resolveType);
|
||||
this.Parameters.AddRange(resolveExpress.Parameters);
|
||||
var reval = resolveExpress.Result;
|
||||
|
@@ -216,12 +216,14 @@ namespace SqlSugar
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||
resolveExpress.InitMappingInfo = this.Context.InitMppingInfo;
|
||||
resolveExpress.RefreshMapping = () =>
|
||||
{
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||
};
|
||||
resolveExpress.Resolve(expression, resolveType);
|
||||
this.Parameters.AddRange(resolveExpress.Parameters);
|
||||
|
@@ -118,6 +118,7 @@ namespace SqlSugar
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
|
||||
}
|
||||
resolveExpress.Resolve(expression, resolveType);
|
||||
this.Parameters.AddRange(resolveExpress.Parameters);
|
||||
|
@@ -83,5 +83,7 @@ namespace SqlSugar
|
||||
}
|
||||
set { _DataInfoCache = value; }
|
||||
}
|
||||
|
||||
public List<SqlFuncExternal> SqlFuncServices { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlFuncExternal
|
||||
{
|
||||
public string UniqueMethodName { get; set; }
|
||||
public Func<MethodCallExpressionModel, DbType, ExpressionContext, string> MethodValue { get; set; }
|
||||
}
|
||||
}
|
@@ -41,6 +41,7 @@ namespace SqlSugar
|
||||
public MappingColumnList MappingColumns { get; set; }
|
||||
public MappingTableList MappingTables { get; set; }
|
||||
public IgnoreColumnList IgnoreComumnList { get; set; }
|
||||
public List<SqlFuncExternal> SqlFuncServices { get; set; }
|
||||
public bool IsSingle
|
||||
{
|
||||
get
|
||||
|
@@ -256,6 +256,7 @@ namespace SqlSugar
|
||||
newContext.MappingColumns = this.Context.MappingColumns;
|
||||
newContext.MappingTables = this.Context.MappingTables;
|
||||
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
|
||||
newContext.SqlFuncServices = this.Context.SqlFuncServices;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
|
@@ -14,7 +14,7 @@ namespace SqlSugar
|
||||
var express = base.Expression as MethodCallExpression;
|
||||
var isLeft = parameter.IsLeft;
|
||||
string methodName = express.Method.Name;
|
||||
var isValidNativeMethod = MethodMapping.ContainsKey(methodName) && express.Method.DeclaringType.Namespace == ("System");
|
||||
var isValidNativeMethod = IsValidNativeMethod(express, methodName);
|
||||
List<MethodCallExpressionArgs> appendArgs = null;
|
||||
if (MethodTimeMapping.ContainsKey(methodName))
|
||||
{
|
||||
@@ -55,7 +55,8 @@ namespace SqlSugar
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (IsIfElse(express, methodName)) {
|
||||
else if (IsIfElse(express, methodName))
|
||||
{
|
||||
CaseWhenResolve caseResole = new CaseWhenResolve(express, this.Context, parameter.OppsiteExpression);
|
||||
var appendSql = caseResole.GetSql();
|
||||
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle))
|
||||
@@ -83,6 +84,17 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsValidNativeMethod(MethodCallExpression express, string methodName)
|
||||
{
|
||||
return MethodMapping.ContainsKey(methodName) && express.Method.DeclaringType.Namespace == ("System");
|
||||
}
|
||||
|
||||
private bool IsExtMethod(string methodName)
|
||||
{
|
||||
if (this.Context.SqlFuncServices == null) return false;
|
||||
return this.Context.SqlFuncServices.Select(it => it.UniqueMethodName).Contains(methodName);
|
||||
}
|
||||
|
||||
private bool IsIfElse(MethodCallExpression express, string methodName)
|
||||
{
|
||||
if (methodName == "End"&& express.Object.Type==typeof(CaseWhen))
|
||||
@@ -341,6 +353,21 @@ namespace SqlSugar
|
||||
}
|
||||
|
||||
private object GetMdthodValue(string name, MethodCallExpressionModel model)
|
||||
{
|
||||
if (IsExtMethod(name))
|
||||
{
|
||||
DbType type = DbType.SqlServer;
|
||||
if (this.Context is SqlServerExpressionContext)
|
||||
type = DbType.SqlServer;
|
||||
else if (this.Context is MySqlExpressionContext)
|
||||
type = DbType.MySql;
|
||||
else if (this.Context is SqliteExpressionContext)
|
||||
type = DbType.Sqlite;
|
||||
else if(this.Context is OracleExpressionContext)
|
||||
type = DbType.Oracle;
|
||||
return this.Context.SqlFuncServices.First(it => it.UniqueMethodName == name).MethodValue(model,type,this.Context);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
@@ -447,6 +474,7 @@ namespace SqlSugar
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -495,6 +523,7 @@ namespace SqlSugar
|
||||
}
|
||||
private void CheckMethod(MethodCallExpression expression)
|
||||
{
|
||||
if (IsExtMethod(expression.Method.Name)) return;
|
||||
Check.Exception(expression.Method.ReflectedType().FullName != ExpressionConst.SqlFuncFullName, string.Format(ErrorMessage.MethodError, expression.Method.Name));
|
||||
}
|
||||
}
|
||||
|
@@ -38,6 +38,7 @@ namespace SqlSugar
|
||||
newContext.MappingColumns = context.MappingColumns;
|
||||
newContext.MappingTables = context.MappingTables;
|
||||
newContext.IgnoreComumnList = context.IgnoreComumnList;
|
||||
newContext.SqlFuncServices = context.SqlFuncServices;
|
||||
newContext.Resolve(item, type);
|
||||
context.Index = newContext.Index;
|
||||
context.ParameterIndex = newContext.ParameterIndex;
|
||||
|
@@ -11,6 +11,8 @@ namespace SqlSugar
|
||||
MappingColumnList MappingColumns { get; set; }
|
||||
MappingTableList MappingTables { get; set; }
|
||||
IgnoreColumnList IgnoreComumnList { get; set; }
|
||||
List<SqlFuncExternal> SqlFuncServices { get; set; }
|
||||
|
||||
List<JoinQueryInfo> JoinQueryInfos { get; set; }
|
||||
bool IsSingle { get; set; }
|
||||
SqlSugarClient Context { get; set; }
|
||||
|
@@ -82,6 +82,7 @@
|
||||
<Compile Include="Enum\DbType.cs" />
|
||||
<Compile Include="ExpressionsToSql\CaseWhen\CaseWhen.cs" />
|
||||
<Compile Include="ExpressionsToSql\CaseWhen\CaseWhenResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\DbMethods\SqlFuncExternal.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\ISubOperation.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubAnd.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubCount.cs" />
|
||||
|
Reference in New Issue
Block a user