mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-27 03:09:34 +08:00
Update SubQuery
This commit is contained in:
@@ -508,6 +508,8 @@ namespace OrmTest.Demo
|
||||
.OrderBy((st, sc) => st.SchoolId)
|
||||
.Select((st, sc) => sc)
|
||||
.Take(1).ToList();
|
||||
|
||||
var s9 = db.Queryable<Student>().Select(it=>it.Name).First();
|
||||
}
|
||||
private static void Sqlable()
|
||||
{
|
||||
|
||||
@@ -67,10 +67,7 @@ namespace OrmTest.Demo
|
||||
var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == 1).Where(it => it.Id == 1).ExecuteCommand();
|
||||
|
||||
|
||||
var t13 = db.Updateable<Student>().UpdateColumns(it => new Student() {
|
||||
SchoolId = SqlFunc.Subqueryable<School>().Where(s=>s.Id==it.SchoolId).Select(s=>s.Id),
|
||||
Name="newname"
|
||||
}).Where(it => it.Id == 1).ExecuteCommand();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +164,24 @@ namespace OrmTest.UnitTest
|
||||
var t15 = db.Updateable(new StudentTest() { Id = 1, Name = "1" }).AS("student").ToSql();
|
||||
base.Check(@"UPDATE [student] SET
|
||||
[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", null, t15.Key, null, "Update t15 error");
|
||||
|
||||
|
||||
var t16= db.Updateable<Student>().UpdateColumns(it => new Student()
|
||||
{
|
||||
SchoolId = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.SchoolId).Select(s => s.Id),
|
||||
Name = "newname"
|
||||
}).Where(it => it.Id == 1).ToSql();
|
||||
|
||||
var t17 = db.Updateable<Student>().UpdateColumns(it => new Student()
|
||||
{
|
||||
SchoolId = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.SchoolId).Select(s => s.Id),
|
||||
Name = "newname"
|
||||
}).Where(it => it.Id == 1).ToSql();
|
||||
base.Check(@"UPDATE [STudent] SET
|
||||
[SchoolId] = (SELECT TOP 1 [Id] FROM [School] WHERE ( [Id] =[STudent].[SchoolId] )) , [Name] = @Const0 WHERE ( [ID] = @Id1 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Const0","newname"),
|
||||
new SugarParameter("@Id1","1")
|
||||
}, t17.Key, t17.Value, "Update t17 error");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
@@ -58,7 +59,8 @@ namespace SqlSugar
|
||||
}
|
||||
else if (IsMethod(item))
|
||||
{
|
||||
item = (item as UnaryExpression).Operand;
|
||||
if (item is UnaryExpression)
|
||||
item = (item as UnaryExpression).Operand;
|
||||
MethodCall(parameter, memberName, item);
|
||||
}
|
||||
else if (IsConst(item))
|
||||
@@ -104,9 +106,26 @@ namespace SqlSugar
|
||||
|
||||
private void MethodCall(ExpressionParameter parameter, string memberName, Expression item)
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
if (IsSubMethod(item as MethodCallExpression))
|
||||
{
|
||||
UtilMethods.GetOldValue(parameter.CommonTempData, () =>
|
||||
{
|
||||
parameter.CommonTempData = CommonTempDataType.Result;
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
|
||||
if (ResolveExpressType.Update == this.Context.ResolveType) {
|
||||
subSql = Regex.Replace(subSql,@" \[\w+?\]\.",this.Context.GetTranslationTableName(parameter.CurrentExpression.Type.Name,true) +".");
|
||||
}
|
||||
parameter.Context.Result.Append(subSql);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
}
|
||||
|
||||
private void Select(MemberInitExpression expression, ExpressionParameter parameter, bool isSingle)
|
||||
@@ -124,9 +143,9 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsSubMethod(MethodCallExpression express, string methodName)
|
||||
private bool IsSubMethod(MethodCallExpression express)
|
||||
{
|
||||
return SubTools.SubItemsConst.Any(it => it.Name == methodName) && express.Object != null && express.Object.Type.Name == "Subqueryable`1";
|
||||
return SubTools.SubItemsConst.Any(it =>express.Object != null && express.Object.Type.Name == "Subqueryable`1");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace SqlSugar
|
||||
//Check.Exception(!(parameter.BaseExpression is BinaryExpression), "Current expressions are not supported");
|
||||
SubResolve subResolve = new SubResolve(express, this.Context, parameter.OppsiteExpression);
|
||||
var appendSql = subResolve.GetSql();
|
||||
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle))
|
||||
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle)||(parameter.BaseParameter!=null&¶meter.BaseParameter.CommonTempData!=null&¶meter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Result)))
|
||||
{
|
||||
parameter.BaseParameter.CommonTempData = appendSql;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user