mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 05:13:27 +08:00
Add SqlFunc.GetSelfAndAutoFill
This commit is contained in:
parent
cf71763d53
commit
c74d460885
@ -15,4 +15,8 @@ namespace OrmTest.Models
|
||||
public string Name { get; set; }
|
||||
public Student Student { get; set; }
|
||||
}
|
||||
public class ViewModelStudent3: Student
|
||||
{
|
||||
public string SchoolName { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ namespace OrmTest.UnitTest
|
||||
single4();
|
||||
single5();
|
||||
Multiple();
|
||||
Multiple2();
|
||||
singleDynamic();
|
||||
MultipleDynamic();
|
||||
}
|
||||
@ -51,8 +52,26 @@ namespace OrmTest.UnitTest
|
||||
},
|
||||
"Select.Multiple Error");
|
||||
}
|
||||
private void Multiple2()
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new ViewModelStudent3() { SchoolName=school.Name,Id=SqlFunc.GetSelfAndAutoFill(it.Id) };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.IsSingle = false;
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" [school].[Name] AS [SchoolName] ,it.*",
|
||||
new List<SugarParameter>(){
|
||||
|
||||
},
|
||||
"Select.Multiple Error");
|
||||
}
|
||||
|
||||
private void MultipleDynamic()
|
||||
|
||||
private void MultipleDynamic()
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
|
@ -179,6 +179,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual string GetAsString(string asName, string fieldValue)
|
||||
{
|
||||
if (fieldValue.Contains(".*")) return fieldValue;
|
||||
return string.Format(" {0} {1} {2} ", GetTranslationColumnName(fieldValue), "AS", GetTranslationColumnName(asName));
|
||||
}
|
||||
|
||||
@ -189,6 +190,7 @@ namespace SqlSugar
|
||||
|
||||
public virtual string GetAsString(string asName, string fieldValue, string fieldShortName)
|
||||
{
|
||||
if (fieldValue.Contains(".*")) return fieldValue;
|
||||
return string.Format(" {0} {1} {2} ", GetTranslationColumnName(fieldShortName + "." + fieldValue), "AS", GetTranslationColumnName(asName));
|
||||
}
|
||||
public virtual void Clear()
|
||||
|
@ -6,7 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class DefaultDbMethod : IDbMethods
|
||||
public partial class DefaultDbMethod : IDbMethods
|
||||
{
|
||||
public virtual string IIF(MethodCallExpressionModel model)
|
||||
{
|
||||
@ -273,7 +273,14 @@ namespace SqlSugar
|
||||
|
||||
public string GuidNew()
|
||||
{
|
||||
return "'"+Guid.NewGuid()+"' ";
|
||||
return "'" + Guid.NewGuid() + "' ";
|
||||
}
|
||||
|
||||
public string GetSelfAndAutoFill(string shortName, bool isSingle)
|
||||
{
|
||||
if (isSingle) return "*";
|
||||
else
|
||||
return string.Format("{0}.*", shortName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ namespace SqlSugar
|
||||
string AggregateMax(MethodCallExpressionModel model);
|
||||
string AggregateCount(MethodCallExpressionModel model);
|
||||
string MappingColumn(MethodCallExpressionModel model);
|
||||
string GetSelfAndAutoFill(MethodCallExpressionModel model);
|
||||
string GetSelfAndAutoFill(string shortName,bool isSingle);
|
||||
string True();
|
||||
string False();
|
||||
string GuidNew();
|
||||
|
@ -101,6 +101,6 @@ namespace SqlSugar
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static TResult GetSelfAndAutoFill<TResult>(object value) { throw new NotSupportedException("This method is not supported by the current parameter"); }
|
||||
public static TResult GetSelfAndAutoFill<TResult>(TResult value) { throw new NotSupportedException("This method is not supported by the current parameter"); }
|
||||
}
|
||||
}
|
||||
|
@ -67,6 +67,7 @@ namespace SqlSugar
|
||||
{
|
||||
case ResolveExpressType.WhereSingle:
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
Check.Exception(name == "GetSelfAndAutoFill", "SqlFunc.GetSelfAndAutoFill can only be used in Select.");
|
||||
Where(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
case ResolveExpressType.SelectSingle:
|
||||
@ -111,21 +112,29 @@ namespace SqlSugar
|
||||
|
||||
private void Select(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
{
|
||||
foreach (var item in args)
|
||||
if (name == "GetSelfAndAutoFill")
|
||||
{
|
||||
var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
|
||||
if (isBinaryExpression)
|
||||
{
|
||||
model.Args.Add(GetMethodCallArgs(parameter, item));
|
||||
}
|
||||
else
|
||||
{
|
||||
Default(parameter, model, item);
|
||||
}
|
||||
var memberValue = (args.First() as MemberExpression).Expression.ToString();
|
||||
model.Args.Add(new MethodCallExpressionArgs() { MemberValue= memberValue, IsMember=true, MemberName= memberValue });
|
||||
}
|
||||
if (appendArgs != null)
|
||||
else
|
||||
{
|
||||
model.Args.AddRange(appendArgs);
|
||||
foreach (var item in args)
|
||||
{
|
||||
var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
|
||||
if (isBinaryExpression)
|
||||
{
|
||||
model.Args.Add(GetMethodCallArgs(parameter, item));
|
||||
}
|
||||
else
|
||||
{
|
||||
Default(parameter, model, item);
|
||||
}
|
||||
}
|
||||
if (appendArgs != null)
|
||||
{
|
||||
model.Args.AddRange(appendArgs);
|
||||
}
|
||||
}
|
||||
parameter.BaseParameter.CommonTempData = GetMdthodValue(name, model);
|
||||
}
|
||||
@ -269,6 +278,9 @@ namespace SqlSugar
|
||||
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 "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);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -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.2.0.9")]
|
||||
[assembly: AssemblyFileVersion("4.2.0.9")]
|
||||
[assembly: AssemblyVersion("4.2.1")]
|
||||
[assembly: AssemblyFileVersion("4.2.1")]
|
||||
|
Loading…
Reference in New Issue
Block a user