mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update exp to sql
This commit is contained in:
parent
f6eb75f1f3
commit
fc7ac54b7c
@ -190,7 +190,28 @@ namespace OrmTest
|
||||
.Select(it => new Order { Name = it.Name.Replace("0", "1") }).MergeTable().Select<Order>().Where(it => it.Name.Equals("2"))
|
||||
.ToList();
|
||||
|
||||
var list14 = Db.Queryable<Order, Order, Order>((o1, o2, o3) =>
|
||||
new JoinQueryInfos(JoinType.Inner, o1.Id == o2.Id * 2, JoinType.Inner, o1.Id == o3.Id * 4)
|
||||
)
|
||||
.Select((o1, o2, o3) => new
|
||||
{
|
||||
id = o1.Id,
|
||||
x = o1,
|
||||
x2 = o2,
|
||||
x3 = o3
|
||||
}).ToList();
|
||||
|
||||
|
||||
var list15 = Db.Queryable<Order, Order, Order>((o1, o2, o3) =>
|
||||
new JoinQueryInfos(JoinType.Inner, o1.Id == o2.Id * 2, JoinType.Inner, o1.Id == o3.Id * 4)
|
||||
)
|
||||
.Select((o1, o2, o3) => new TestModel1
|
||||
{
|
||||
id = o1.Id.SelectAll(),
|
||||
x = o1,
|
||||
x2 = o2,
|
||||
x3 = o3
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public class UnitEnumTest
|
||||
@ -236,5 +257,15 @@ namespace OrmTest
|
||||
public int id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class TestModel1
|
||||
{
|
||||
public int id { get; set; }
|
||||
public Order x { get; set; }
|
||||
public Order x2 { get; set; }
|
||||
public Order x3 { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -474,7 +474,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]");
|
||||
return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]")|| Regex.IsMatch(sql, @"AS \[\w+\.\w+\.\w+\]");
|
||||
}
|
||||
public string GetSqlQuerySql(string result)
|
||||
{
|
||||
|
@ -443,6 +443,9 @@ namespace SqlSugar
|
||||
}
|
||||
else if (item.Type.IsClass())
|
||||
{
|
||||
var mappingKeys = GetMappingColumns(parameter.CurrentExpression);
|
||||
var isSameType = mappingKeys.Keys.Count>0;
|
||||
CallContextThread<Dictionary<string,string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
@ -462,7 +465,11 @@ namespace SqlSugar
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
@ -503,6 +510,61 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
result.Add(first,last);
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
|
||||
private string GetAsName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
@ -525,7 +587,28 @@ namespace SqlSugar
|
||||
|
||||
return asName;
|
||||
}
|
||||
private string GetAsNameAndShortName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
asName = this.Context.GetTranslationText(shortName+"."+item.Type.Name + "." + propertyName);
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private static bool IsBoolValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.BoolType &&
|
||||
|
@ -271,6 +271,7 @@ namespace SqlSugar
|
||||
private Dictionary<string, object> DataReaderToList<T>(IDataReader reader, Type tType, List<PropertyInfo> classProperties, List<T> reval)
|
||||
{
|
||||
var readerValues = DataReaderToDictionary(reader, tType);
|
||||
var mappingKeys = CallContextThread<Dictionary<string, string>>.GetData("Exp_Select_Mapping_Key");
|
||||
var result = new Dictionary<string, object>();
|
||||
foreach (var item in classProperties)
|
||||
{
|
||||
@ -296,7 +297,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval));
|
||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval, mappingKeys));
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -370,7 +371,7 @@ namespace SqlSugar
|
||||
Regex.IsMatch(readerValues[item.Name.ToLower()].ToString(), @"^\[{.+\}]$");
|
||||
}
|
||||
|
||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval)
|
||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval, Dictionary<string, string> mappingKeys=null)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
var type = item.PropertyType;
|
||||
@ -407,6 +408,11 @@ namespace SqlSugar
|
||||
{
|
||||
var key = typeName + "." + name;
|
||||
var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
if (mappingKeys.ContainsKey(item.Name))
|
||||
{
|
||||
key = mappingKeys[item.Name]+"."+typeName + "." + name;
|
||||
info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
}
|
||||
if (info != null)
|
||||
{
|
||||
var addItem = readerValues[info];
|
||||
|
Loading…
Reference in New Issue
Block a user