mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-05 21:27:58 +08:00
Update Subquery.ToList
This commit is contained in:
parent
e274f4e0de
commit
865bbad050
@ -13,7 +13,7 @@ using System.Collections.ObjectModel;
|
||||
using NetTaste;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
@ -1434,9 +1434,11 @@ namespace SqlSugar
|
||||
var ps = this.QueryBuilder.Parameters;
|
||||
var itemProperty = typeof(TResult).GetProperty(subPara.Key);
|
||||
var callType = itemProperty.PropertyType.GetGenericArguments()[0];
|
||||
var methodParamters = new object[] { subPara.Value.ObjToString().Replace("@sugarIndex", "0"), ps };
|
||||
var sql = subPara.Value.ObjToString().Replace("@sugarIndex", "0");
|
||||
sql =SqlBuilder.RemoveParentheses(sql);
|
||||
var methodParamters = new object[] { sql, ps };
|
||||
var subList = ExpressionBuilderHelper.CallFunc(callType, methodParamters, this.Clone(), "SubQueryList");
|
||||
for(var i=0;i<result.Count; i++)
|
||||
for (var i = 0; i < result.Count; i++)
|
||||
{
|
||||
var item = result[i];
|
||||
var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList;
|
||||
@ -1472,6 +1474,10 @@ namespace SqlSugar
|
||||
};
|
||||
var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true);
|
||||
sql = sql.Replace(re.Name, value);
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
|
||||
{
|
||||
sql =SqlBuilder.RemoveParentheses(sql);
|
||||
}
|
||||
}
|
||||
sql = sql.Replace("@sugarIndex", index + "");
|
||||
sqls.Add(sql);
|
||||
|
@ -586,6 +586,55 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
}
|
||||
protected string SubToListMethod(string result)
|
||||
{
|
||||
List<string> names = new List<string>();
|
||||
var allShortName = new List<string>();
|
||||
allShortName.Add(this.Builder.SqlTranslationLeft + Builder.GetNoTranslationColumnName(this.TableShortName.ObjToString().ToLower() + this.Builder.SqlTranslationRight + "."));
|
||||
if (this.JoinQueryInfos.HasValue())
|
||||
{
|
||||
foreach (var item in this.JoinQueryInfos)
|
||||
{
|
||||
allShortName.Add(this.Builder.SqlTranslationLeft + Builder.GetNoTranslationColumnName(item.ShortName.ObjToString().ToLower() + this.Builder.SqlTranslationRight + "."));
|
||||
}
|
||||
}
|
||||
else if (this.EasyJoinInfos != null && this.EasyJoinInfos.Any())
|
||||
{
|
||||
Check.ExceptionEasy("No Supprt Subquery.ToList(), Inner Join Or Left Join", "Subquery.ToList请使用Inner方式联表");
|
||||
}
|
||||
if (this.TableShortName == null)
|
||||
{
|
||||
//Empty
|
||||
}
|
||||
else
|
||||
{
|
||||
var name = Builder.GetTranslationColumnName(this.TableShortName) + @"\.";
|
||||
foreach (var paramter in this.SubToListParameters)
|
||||
{
|
||||
var regex = $@"\{Builder.SqlTranslationLeft}[\w]{{1,20}}?\{Builder.SqlTranslationRight}\.\{Builder.SqlTranslationLeft}.{{1,50}}?\{Builder.SqlTranslationRight}";
|
||||
var matches = Regex
|
||||
.Matches(paramter.Value.ObjToString(), regex, RegexOptions.IgnoreCase).Cast<Match>()
|
||||
.Where(it => allShortName.Any(z => it.Value.ObjToString().ToLower().Contains(z)))
|
||||
.Select(it => it.Value).ToList();
|
||||
names.AddRange(matches);
|
||||
}
|
||||
int i = 0;
|
||||
names = names.Distinct().ToList();
|
||||
if (names.Any())
|
||||
{
|
||||
List<QueryableAppendColumn> colums = new List<QueryableAppendColumn>();
|
||||
foreach (var item in names)
|
||||
{
|
||||
result = (result + $",{item} as app_ext_col_{i}");
|
||||
colums.Add(new QueryableAppendColumn() { AsName = $"app_ext_col_{i}", Name = item, Index = i });
|
||||
i++;
|
||||
}
|
||||
this.AppendColumns = colums;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Get SQL Partial
|
||||
@ -613,54 +662,12 @@ namespace SqlSugar
|
||||
}
|
||||
if (this.SubToListParameters!=null&& this.SubToListParameters.Any())
|
||||
{
|
||||
List<string> names = new List<string>();
|
||||
var allShortName = new List<string>();
|
||||
allShortName.Add(this.Builder.SqlTranslationLeft+ Builder.GetNoTranslationColumnName(this.TableShortName.ObjToString().ToLower()+this.Builder.SqlTranslationRight+"."));
|
||||
if (this.JoinQueryInfos.HasValue())
|
||||
{
|
||||
foreach (var item in this.JoinQueryInfos)
|
||||
{
|
||||
allShortName.Add(this.Builder.SqlTranslationLeft + Builder.GetNoTranslationColumnName(item.ShortName.ObjToString().ToLower() + this.Builder.SqlTranslationRight + "."));
|
||||
}
|
||||
}
|
||||
else if (this.EasyJoinInfos!=null&& this.EasyJoinInfos.Any())
|
||||
{
|
||||
Check.ExceptionEasy("No Supprt Subquery.ToList(), Inner Join Or Left Join","Subquery.ToList请使用Inner方式联表");
|
||||
}
|
||||
if (this.TableShortName == null)
|
||||
{
|
||||
//Empty
|
||||
}
|
||||
else
|
||||
{
|
||||
var name = Builder.GetTranslationColumnName(this.TableShortName) + @"\.";
|
||||
foreach (var paramter in this.SubToListParameters)
|
||||
{
|
||||
var regex = $@"\{Builder.SqlTranslationLeft}[\w]{{1,20}}?\{Builder.SqlTranslationRight}\.\{Builder.SqlTranslationLeft}.{{1,50}}?\{Builder.SqlTranslationRight}";
|
||||
var matches = Regex
|
||||
.Matches(paramter.Value.ObjToString(), regex, RegexOptions.IgnoreCase).Cast<Match>()
|
||||
.Where(it => allShortName.Any(z => it.Value.ObjToString().ToLower().Contains(z)))
|
||||
.Select(it => it.Value).ToList();
|
||||
names.AddRange(matches);
|
||||
}
|
||||
int i = 0;
|
||||
names = names.Distinct().ToList();
|
||||
if (names.Any())
|
||||
{
|
||||
List<QueryableAppendColumn> colums = new List<QueryableAppendColumn>();
|
||||
foreach (var item in names)
|
||||
{
|
||||
result = (result + $",{item} as app_ext_col_{i}");
|
||||
colums.Add(new QueryableAppendColumn() { AsName = $"app_ext_col_{i}", Name = item, Index = i });
|
||||
i++;
|
||||
}
|
||||
this.AppendColumns = colums;
|
||||
}
|
||||
}
|
||||
result = SubToListMethod(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string GetSelectValueByExpression()
|
||||
{
|
||||
var expression = this.SelectValue as Expression;
|
||||
|
@ -389,7 +389,12 @@ namespace SqlSugar
|
||||
indexTree++;
|
||||
}
|
||||
|
||||
private ConditionalCollections ToConditionalCollections(ConditionalTree item,ref int indexTree, List<SugarParameter> parameters)
|
||||
public virtual string RemoveParentheses(string sql)
|
||||
{
|
||||
return sql;
|
||||
}
|
||||
|
||||
private ConditionalCollections ToConditionalCollections(ConditionalTree item,ref int indexTree, List<SugarParameter> parameters)
|
||||
{
|
||||
List<KeyValuePair<WhereType, ConditionalModel>> list = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
var index = 0;
|
||||
|
@ -43,5 +43,6 @@ namespace SqlSugar
|
||||
KeyValuePair<string, SugarParameter[]> ConditionalModelToSql(List<IConditionalModel> models, int beginIndex = 0);
|
||||
string GetUnionFomatSql(string sql);
|
||||
Type GetNullType(string tableName,string columnName);
|
||||
string RemoveParentheses(string sql);
|
||||
}
|
||||
}
|
||||
|
@ -22,5 +22,14 @@ namespace SqlSugar
|
||||
return "select DATETIME('now') ";
|
||||
}
|
||||
}
|
||||
public override string RemoveParentheses(string sql)
|
||||
{
|
||||
if (sql.StartsWith("(") && sql.EndsWith(")"))
|
||||
{
|
||||
sql = sql.Substring(1, sql.Length - 2);
|
||||
}
|
||||
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using MySqlX.XDevAPI.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
@ -68,7 +69,7 @@ namespace SqlSugar
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get SQL Partial
|
||||
@ -93,6 +94,10 @@ namespace SqlSugar
|
||||
{
|
||||
reval = " DISTINCT " + reval;
|
||||
}
|
||||
if (this.SubToListParameters != null && this.SubToListParameters.Any())
|
||||
{
|
||||
reval = SubToListMethod(reval);
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user