Support custom SqlFunc Method

This commit is contained in:
sunkaixuan
2017-11-17 14:08:25 +08:00
parent 2dca1b923d
commit d66e84c911
15 changed files with 215 additions and 105 deletions

View 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);
}
}
}

View File

@@ -48,6 +48,7 @@ namespace OrmTest
OrmTest.Demo.Aop.Init();
OrmTest.Demo.MasterSlave.Init();
OrmTest.Demo.SharedConnection.Init();
OrmTest.Demo.ExtSqlFun.Init();
}
}
}

View File

@@ -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" />

View File

@@ -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>();

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);

View File

@@ -83,5 +83,7 @@ namespace SqlSugar
}
set { _DataInfoCache = value; }
}
public List<SqlFuncExternal> SqlFuncServices { get; set; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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)
{
@@ -438,7 +465,7 @@ namespace SqlSugar
case "IsNull":
return this.Context.DbMehtods.IsNull(model);
case "MergeString":
return this.Context.DbMehtods.MergeString(model.Args.Select(it=>it.MemberName.ObjToString()).ToArray());
return this.Context.DbMehtods.MergeString(model.Args.Select(it => it.MemberName.ObjToString()).ToArray());
case "GetSelfAndAutoFill":
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
return this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle);
@@ -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));
}
}

View File

@@ -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;

View File

@@ -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; }

View File

@@ -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" />