Merge branch 'dev' of github.com:sunkaixuan/SqlSugar

This commit is contained in:
sunkaixuan 2017-11-23 15:04:36 +08:00
commit a7d9bc54fb
22 changed files with 237 additions and 115 deletions

View File

@ -124,6 +124,14 @@ namespace OrmTest.UnitTest
new SugarParameter("@Id0",1),
new SugarParameter("@Const1",1)
}, t13.Key, t13.Value, "single t13 error ");
var t14 = db.Queryable<Student>()
.Where(it => it.Name == "a" && SqlFunc.HasValue(it.Name)).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE (( [Name] = @Name0 ) AND ( [Name]<>'' AND [Name] IS NOT NULL ))",
new List<SugarParameter>() {
new SugarParameter("@Name0","a")
}, t14.Key, t14.Value, "single t14 error ");
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -247,13 +248,15 @@ namespace SqlSugar
}
public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{
var seChar= Path.DirectorySeparatorChar.ToString();
Check.ArgumentNullException(directoryPath, "directoryPath can't null");
var classStringList = ToClassStringList(nameSpace);
if (classStringList.IsValuable())
{
foreach (var item in classStringList)
{
FileHelper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
var filePath = directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format(seChar + "{0}.cs", item.Key);
FileHelper.CreateFile(filePath, item.Value, Encoding.UTF8);
}
}
}

View File

@ -209,7 +209,7 @@ namespace SqlSugar
model.Args.AddRange(appendArgs);
}
var methodValue = GetMdthodValue(name, model);
if (parameter.BaseExpression is BinaryExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType&&name=="HasValue") {
if (parameter.BaseExpression is BinaryExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType&&name=="HasValue"&&!(parameter.OppsiteExpression is BinaryExpression)) {
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
new KeyValuePair<string, string>("Return","1"),

View File

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.6.0.5")]
[assembly: AssemblyFileVersion("4.6.0.5")]
[assembly: AssemblyVersion("4.6.0.6")]
[assembly: AssemblyFileVersion("4.6.0.6")]

View File

@ -2,7 +2,7 @@
<package >
<metadata>
<id>sqlSugar</id>
<version>4.6.0.5</version>
<version>4.6.0.6</version>
<title>SqlSugar .Net Framework 4.0+ ORM </title>
<authors>sun kaixuan</authors>
<owners>landa</owners>

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

@ -39,6 +39,7 @@ namespace SqlSeverTest
OrmTest.Demo.CodeFirst.Init();
OrmTest.Demo.MasterSlave.Init();
OrmTest.Demo.SharedConnection.Init();
OrmTest.Demo.ExtSqlFun.Init();
}
}
}

View File

@ -140,7 +140,7 @@ namespace SqlSugar
public virtual string GetPropertyTypeName(string dbTypeName)
{
dbTypeName = dbTypeName.ToLower();
var propertyTypes = MappingTypes.Where(it => it.Key == dbTypeName);
var propertyTypes = MappingTypes.Where(it => it.Key.Equals(dbTypeName,StringComparison.CurrentCultureIgnoreCase));
if (dbTypeName == "int32")
{
return "int";

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -247,13 +248,15 @@ namespace SqlSugar
}
public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{
var seChar= Path.DirectorySeparatorChar.ToString();
Check.ArgumentNullException(directoryPath, "directoryPath can't null");
var classStringList = ToClassStringList(nameSpace);
if (classStringList.IsValuable())
{
foreach (var item in classStringList)
{
FileHelper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
var filePath = directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format(seChar + "{0}.cs", item.Key);
FileHelper.CreateFile(filePath, item.Value, Encoding.UTF8);
}
}
}

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))
@ -197,7 +209,7 @@ namespace SqlSugar
model.Args.AddRange(appendArgs);
}
var methodValue = GetMdthodValue(name, model);
if (parameter.BaseExpression is BinaryExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType&&name=="HasValue") {
if (parameter.BaseExpression is BinaryExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType&&name=="HasValue"&&!(parameter.OppsiteExpression is BinaryExpression)) {
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
new KeyValuePair<string, string>("Return","1"),
@ -342,110 +354,126 @@ namespace SqlSugar
private object GetMdthodValue(string name, MethodCallExpressionModel model)
{
switch (name)
if (IsExtMethod(name))
{
case "IIF":
return this.Context.DbMehtods.IIF(model);
case "HasNumber":
return this.Context.DbMehtods.HasNumber(model);
case "HasValue":
return this.Context.DbMehtods.HasValue(model);
case "IsNullOrEmpty":
return this.Context.DbMehtods.IsNullOrEmpty(model);
case "ToLower":
return this.Context.DbMehtods.ToLower(model);
case "ToUpper":
return this.Context.DbMehtods.ToUpper(model);
case "Trim":
return this.Context.DbMehtods.Trim(model);
case "Contains":
return this.Context.DbMehtods.Contains(model);
case "ContainsArray":
var caResult = this.Context.DbMehtods.ContainsArray(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
return caResult;
case "Equals":
return this.Context.DbMehtods.Equals(model);
case "DateIsSame":
if (model.Args.Count == 2)
return this.Context.DbMehtods.DateIsSameDay(model);
else
{
var dsResult = this.Context.DbMehtods.DateIsSameByType(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
return dsResult;
}
case "DateAdd":
if (model.Args.Count == 2)
return this.Context.DbMehtods.DateAddDay(model);
else
{
var daResult = this.Context.DbMehtods.DateAddByType(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
return daResult;
}
case "DateValue":
var dvResult = this.Context.DbMehtods.DateValue(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
return dvResult;
case "Between":
return this.Context.DbMehtods.Between(model);
case "StartsWith":
return this.Context.DbMehtods.StartsWith(model);
case "EndsWith":
return this.Context.DbMehtods.EndsWith(model);
case "ToInt32":
return this.Context.DbMehtods.ToInt32(model);
case "ToInt64":
return this.Context.DbMehtods.ToInt64(model);
case "ToDate":
return this.Context.DbMehtods.ToDate(model);
case "ToTime":
return this.Context.DbMehtods.ToTime(model);
case "ToString":
Check.Exception(model.Args.Count > 1, "ToString (Format) is not supported, Use ToString().If time formatting can be used it.Date.Year+\"-\"+it.Data.Month+\"-\"+it.Date.Day ");
return this.Context.DbMehtods.ToString(model);
case "ToDecimal":
return this.Context.DbMehtods.ToDecimal(model);
case "ToGuid":
return this.Context.DbMehtods.ToGuid(model);
case "ToDouble":
return this.Context.DbMehtods.ToDouble(model);
case "ToBool":
return this.Context.DbMehtods.ToBool(model);
case "Substring":
return this.Context.DbMehtods.Substring(model);
case "Replace":
return this.Context.DbMehtods.Replace(model);
case "Length":
return this.Context.DbMehtods.Length(model);
case "AggregateSum":
return this.Context.DbMehtods.AggregateSum(model);
case "AggregateAvg":
return this.Context.DbMehtods.AggregateAvg(model);
case "AggregateMin":
return this.Context.DbMehtods.AggregateMin(model);
case "AggregateMax":
return this.Context.DbMehtods.AggregateMax(model);
case "AggregateCount":
return this.Context.DbMehtods.AggregateCount(model);
case "MappingColumn":
var mappingColumnResult = this.Context.DbMehtods.MappingColumn(model);
var isValid = model.Args[0].IsMember && model.Args[1].IsMember == false;
Check.Exception(!isValid, "SqlFunc.MappingColumn parameters error, The property name on the left, string value on the right");
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
return mappingColumnResult;
case "IsNull":
return this.Context.DbMehtods.IsNull(model);
case "MergeString":
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);
case "GetDate":
return this.Context.DbMehtods.GetDate();
default:
break;
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)
{
case "IIF":
return this.Context.DbMehtods.IIF(model);
case "HasNumber":
return this.Context.DbMehtods.HasNumber(model);
case "HasValue":
return this.Context.DbMehtods.HasValue(model);
case "IsNullOrEmpty":
return this.Context.DbMehtods.IsNullOrEmpty(model);
case "ToLower":
return this.Context.DbMehtods.ToLower(model);
case "ToUpper":
return this.Context.DbMehtods.ToUpper(model);
case "Trim":
return this.Context.DbMehtods.Trim(model);
case "Contains":
return this.Context.DbMehtods.Contains(model);
case "ContainsArray":
var caResult = this.Context.DbMehtods.ContainsArray(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
return caResult;
case "Equals":
return this.Context.DbMehtods.Equals(model);
case "DateIsSame":
if (model.Args.Count == 2)
return this.Context.DbMehtods.DateIsSameDay(model);
else
{
var dsResult = this.Context.DbMehtods.DateIsSameByType(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
return dsResult;
}
case "DateAdd":
if (model.Args.Count == 2)
return this.Context.DbMehtods.DateAddDay(model);
else
{
var daResult = this.Context.DbMehtods.DateAddByType(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
return daResult;
}
case "DateValue":
var dvResult = this.Context.DbMehtods.DateValue(model);
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
return dvResult;
case "Between":
return this.Context.DbMehtods.Between(model);
case "StartsWith":
return this.Context.DbMehtods.StartsWith(model);
case "EndsWith":
return this.Context.DbMehtods.EndsWith(model);
case "ToInt32":
return this.Context.DbMehtods.ToInt32(model);
case "ToInt64":
return this.Context.DbMehtods.ToInt64(model);
case "ToDate":
return this.Context.DbMehtods.ToDate(model);
case "ToTime":
return this.Context.DbMehtods.ToTime(model);
case "ToString":
Check.Exception(model.Args.Count > 1, "ToString (Format) is not supported, Use ToString().If time formatting can be used it.Date.Year+\"-\"+it.Data.Month+\"-\"+it.Date.Day ");
return this.Context.DbMehtods.ToString(model);
case "ToDecimal":
return this.Context.DbMehtods.ToDecimal(model);
case "ToGuid":
return this.Context.DbMehtods.ToGuid(model);
case "ToDouble":
return this.Context.DbMehtods.ToDouble(model);
case "ToBool":
return this.Context.DbMehtods.ToBool(model);
case "Substring":
return this.Context.DbMehtods.Substring(model);
case "Replace":
return this.Context.DbMehtods.Replace(model);
case "Length":
return this.Context.DbMehtods.Length(model);
case "AggregateSum":
return this.Context.DbMehtods.AggregateSum(model);
case "AggregateAvg":
return this.Context.DbMehtods.AggregateAvg(model);
case "AggregateMin":
return this.Context.DbMehtods.AggregateMin(model);
case "AggregateMax":
return this.Context.DbMehtods.AggregateMax(model);
case "AggregateCount":
return this.Context.DbMehtods.AggregateCount(model);
case "MappingColumn":
var mappingColumnResult = this.Context.DbMehtods.MappingColumn(model);
var isValid = model.Args[0].IsMember && model.Args[1].IsMember == false;
Check.Exception(!isValid, "SqlFunc.MappingColumn parameters error, The property name on the left, string value on the right");
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
return mappingColumnResult;
case "IsNull":
return this.Context.DbMehtods.IsNull(model);
case "MergeString":
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);
case "GetDate":
return this.Context.DbMehtods.GetDate();
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

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.6.0.5</Version>
<Version>4.6.0.6</Version>
<Copyright>sun_kai_xuan</Copyright>
<PackageProjectUrl>https://github.com/sunkaixuan/SqlSugar</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>

View File

@ -2,7 +2,7 @@
<package >
<metadata>
<id>sqlSugarCore</id>
<version>4.6.0.5</version>
<version>4.6.0.6</version>
<authors>sunkaixuan</authors>
<owners>Landa</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>