Browse Source

Add SqlFunc.MergeString

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
183bc4d94e
  1. 1
      Src/Asp.Net/MySqlTest/Demos/1_Query.cs
  2. 1
      Src/Asp.Net/SqlServerTest/Demos/1_Query.cs
  3. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs
  4. 6
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs
  5. 4
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs
  6. 10
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs
  7. 4
      Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs
  8. BIN
      Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite

1
Src/Asp.Net/MySqlTest/Demos/1_Query.cs

@ -333,6 +333,7 @@ namespace OrmTest.Demo
var db = GetInstance(); var db = GetInstance();
var t1 = db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Name) == SqlFunc.ToLower("JACK")).ToList(); var t1 = db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Name) == SqlFunc.ToLower("JACK")).ToList();
var t2 = db.Queryable<Student>().Where(it => SqlFunc.IsNull(it.Name, "nullvalue") == "nullvalue").ToList(); var t2 = db.Queryable<Student>().Where(it => SqlFunc.IsNull(it.Name, "nullvalue") == "nullvalue").ToList();
var t3 = db.Queryable<Student>().Where(it => SqlFunc.MergeString("a", it.Name) == "nullvalue").ToList();
//SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) ) //SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) )
/***More Functions***/ /***More Functions***/

1
Src/Asp.Net/SqlServerTest/Demos/1_Query.cs

@ -434,6 +434,7 @@ namespace OrmTest.Demo
var db = GetInstance(); var db = GetInstance();
var t1 = db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Name) == SqlFunc.ToLower("JACK")).ToList(); var t1 = db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Name) == SqlFunc.ToLower("JACK")).ToList();
var t2 = db.Queryable<Student>().Where(it => SqlFunc.IsNull(it.Name,"nullvalue")=="nullvalue").ToList(); var t2 = db.Queryable<Student>().Where(it => SqlFunc.IsNull(it.Name,"nullvalue")=="nullvalue").ToList();
var t3 = db.Queryable<Student>().Where(it => SqlFunc.MergeString("a",it.Name) == "nullvalue").ToList();
//SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) ) //SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) )
/***More Functions***/ /***More Functions***/

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs

@ -302,7 +302,7 @@ namespace SqlSugar
public virtual string MergeString(params string[] strings) public virtual string MergeString(params string[] strings)
{ {
return string.Join("", strings); return string.Join("+", strings);
} }
public virtual string Pack(string sql) public virtual string Pack(string sql)

6
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs

@ -68,6 +68,12 @@ namespace SqlSugar
public static bool Between(object value, object start, object end) { throw new NotSupportedException("Can only be used in expressions"); } public static bool Between(object value, object start, object end) { throw new NotSupportedException("Can only be used in expressions"); }
public static TResult IIF<TResult>(bool Expression, TResult thenValue, TResult elseValue) { throw new NotSupportedException("Can only be used in expressions"); } public static TResult IIF<TResult>(bool Expression, TResult thenValue, TResult elseValue) { throw new NotSupportedException("Can only be used in expressions"); }
public static TResult IsNull<TResult>(TResult thisValue, TResult ifNullValue) { throw new NotSupportedException("Can only be used in expressions"); } public static TResult IsNull<TResult>(TResult thisValue, TResult ifNullValue) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1,string value2) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1, string value2,string value3) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1, string value2,string value3,string value4) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1, string value2, string value3, string value4,string value5) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1, string value2, string value3, string value4, string value5,string value6) { throw new NotSupportedException("Can only be used in expressions"); }
public static string MergeString(string value1, string value2, string value3, string value4, string value5, string value6,string value7) { throw new NotSupportedException("Can only be used in expressions"); }
public static int ToInt32(object value) { return value.ObjToInt(); } public static int ToInt32(object value) { return value.ObjToInt(); }
public static long ToInt64(object value) { return Convert.ToInt64(value); } public static long ToInt64(object value) { return Convert.ToInt64(value); }
/// <summary> /// <summary>

4
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

@ -204,9 +204,9 @@ namespace SqlSugar
}; };
AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString( AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString(
this.GetDateValue(parameter.CommonTempData, DateType.Year), this.GetDateValue(parameter.CommonTempData, DateType.Year),
"+'-'+", "'-'",
this.GetDateValue(parameter.CommonTempData, DateType.Month), this.GetDateValue(parameter.CommonTempData, DateType.Month),
"+'-'+", "'-'",
this.GetDateValue(parameter.CommonTempData, DateType.Day)))); this.GetDateValue(parameter.CommonTempData, DateType.Day))));
} }
parameter.CommonTempData = oldCommonTempDate; parameter.CommonTempData = oldCommonTempDate;

10
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -45,7 +45,7 @@ namespace SqlSugar
//Check.Exception(!(parameter.BaseExpression is BinaryExpression), "Current expressions are not supported"); //Check.Exception(!(parameter.BaseExpression is BinaryExpression), "Current expressions are not supported");
SubResolve subResolve = new SubResolve(express, this.Context, parameter.OppsiteExpression); SubResolve subResolve = new SubResolve(express, this.Context, parameter.OppsiteExpression);
var appendSql = subResolve.GetSql(); var appendSql = subResolve.GetSql();
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple,ResolveExpressType.SelectSingle)) if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle))
{ {
parameter.BaseParameter.CommonTempData = appendSql; parameter.BaseParameter.CommonTempData = appendSql;
} }
@ -284,7 +284,7 @@ namespace SqlSugar
base.Start(); base.Start();
var methodCallExpressionArgs = new MethodCallExpressionArgs() var methodCallExpressionArgs = new MethodCallExpressionArgs()
{ {
IsMember = parameter.ChildExpression is MemberExpression&&!ExpressionTool.IsConstExpression(parameter.ChildExpression as MemberExpression), IsMember = parameter.ChildExpression is MemberExpression && !ExpressionTool.IsConstExpression(parameter.ChildExpression as MemberExpression),
MemberName = parameter.CommonTempData MemberName = parameter.CommonTempData
}; };
if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now") if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now")
@ -409,6 +409,8 @@ namespace SqlSugar
return mappingColumnResult; return mappingColumnResult;
case "IsNull": case "IsNull":
return this.Context.DbMehtods.IsNull(model); return this.Context.DbMehtods.IsNull(model);
case "MergeString":
return this.Context.DbMehtods.MergeString(model.Args.Select(it=>it.MemberName.ObjToString()).ToArray());
case "GetSelfAndAutoFill": case "GetSelfAndAutoFill":
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString()); 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); return this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle);
@ -452,12 +454,12 @@ namespace SqlSugar
{ "AddMilliseconds",DateType.Millisecond} { "AddMilliseconds",DateType.Millisecond}
}; };
private bool IsContainsArray(MethodCallExpression express, string methodName, bool isValidNativeMethod) private bool IsContainsArray(MethodCallExpression express, string methodName, bool isValidNativeMethod)
{ {
return !isValidNativeMethod && express.Method.DeclaringType.Namespace.IsIn("System.Linq", "System.Collections.Generic") && methodName == "Contains"; return !isValidNativeMethod && express.Method.DeclaringType.Namespace.IsIn("System.Linq", "System.Collections.Generic") && methodName == "Contains";
} }
private bool IsSubMethod(MethodCallExpression express, string methodName) private bool IsSubMethod(MethodCallExpression express, string methodName)
{ {
return SubTools.SubItemsConst.Any(it => it.Name == methodName) && express.Object != null && express.Object.Type.Name == "Subqueryable`1"; return SubTools.SubItemsConst.Any(it => it.Name == methodName) && express.Object != null && express.Object.Type.Name == "Subqueryable`1";
} }

4
Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs

@ -164,5 +164,9 @@ namespace SqlSugar
return string.Format("NVL({0},{1})", parameter.MemberName, parameter1.MemberName); return string.Format("NVL({0},{1})", parameter.MemberName, parameter1.MemberName);
} }
public override string MergeString(params string[] strings)
{
return string.Join("||", strings).Replace("+", "");
}
} }
} }

BIN
Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite

Binary file not shown.
Loading…
Cancel
Save