Update Subquery.ToList

This commit is contained in:
sunkaixuan
2022-12-03 23:43:19 +08:00
parent e274f4e0de
commit 865bbad050
6 changed files with 83 additions and 50 deletions

View File

@@ -13,7 +13,7 @@ using System.Collections.ObjectModel;
using NetTaste; using NetTaste;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Xml.Linq; using System.Xml.Linq;
using Microsoft.Data.SqlClient;
namespace SqlSugar namespace SqlSugar
{ {
@@ -1434,7 +1434,9 @@ namespace SqlSugar
var ps = this.QueryBuilder.Parameters; var ps = this.QueryBuilder.Parameters;
var itemProperty = typeof(TResult).GetProperty(subPara.Key); var itemProperty = typeof(TResult).GetProperty(subPara.Key);
var callType = itemProperty.PropertyType.GetGenericArguments()[0]; 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"); 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++)
{ {
@@ -1472,6 +1474,10 @@ namespace SqlSugar
}; };
var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true); var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true);
sql = sql.Replace(re.Name, value); sql = sql.Replace(re.Name, value);
if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
{
sql =SqlBuilder.RemoveParentheses(sql);
}
} }
sql = sql.Replace("@sugarIndex", index + ""); sql = sql.Replace("@sugarIndex", index + "");
sqls.Add(sql); sqls.Add(sql);

View File

@@ -586,32 +586,7 @@ namespace SqlSugar
return result; return result;
} }
} }
#endregion protected string SubToListMethod(string result)
#region Get SQL Partial
public string Offset { get; set; }
public virtual string GetSelectValue
{
get
{
string result = string.Empty;
if (this.SelectValue == null || this.SelectValue is string)
{
result = GetSelectValueByString();
}
else
{
result = GetSelectValueByExpression();
}
if (this.SelectType == ResolveExpressType.SelectMultiple)
{
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this._JoinQueryInfos.Select(it => it.TableName));
}
if (IsDistinct)
{
result = " DISTINCT " + result;
}
if (this.SubToListParameters!=null&& this.SubToListParameters.Any())
{ {
List<string> names = new List<string>(); List<string> names = new List<string>();
var allShortName = new List<string>(); var allShortName = new List<string>();
@@ -657,10 +632,42 @@ namespace SqlSugar
this.AppendColumns = colums; this.AppendColumns = colums;
} }
} }
return result;
}
#endregion
#region Get SQL Partial
public string Offset { get; set; }
public virtual string GetSelectValue
{
get
{
string result = string.Empty;
if (this.SelectValue == null || this.SelectValue is string)
{
result = GetSelectValueByString();
}
else
{
result = GetSelectValueByExpression();
}
if (this.SelectType == ResolveExpressType.SelectMultiple)
{
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this._JoinQueryInfos.Select(it => it.TableName));
}
if (IsDistinct)
{
result = " DISTINCT " + result;
}
if (this.SubToListParameters!=null&& this.SubToListParameters.Any())
{
result = SubToListMethod(result);
} }
return result; return result;
} }
} }
public virtual string GetSelectValueByExpression() public virtual string GetSelectValueByExpression()
{ {
var expression = this.SelectValue as Expression; var expression = this.SelectValue as Expression;

View File

@@ -389,6 +389,11 @@ namespace SqlSugar
indexTree++; indexTree++;
} }
public virtual string RemoveParentheses(string sql)
{
return sql;
}
private ConditionalCollections ToConditionalCollections(ConditionalTree item,ref int indexTree, List<SugarParameter> parameters) private ConditionalCollections ToConditionalCollections(ConditionalTree item,ref int indexTree, List<SugarParameter> parameters)
{ {
List<KeyValuePair<WhereType, ConditionalModel>> list = new List<KeyValuePair<WhereType, ConditionalModel>>(); List<KeyValuePair<WhereType, ConditionalModel>> list = new List<KeyValuePair<WhereType, ConditionalModel>>();

View File

@@ -43,5 +43,6 @@ namespace SqlSugar
KeyValuePair<string, SugarParameter[]> ConditionalModelToSql(List<IConditionalModel> models, int beginIndex = 0); KeyValuePair<string, SugarParameter[]> ConditionalModelToSql(List<IConditionalModel> models, int beginIndex = 0);
string GetUnionFomatSql(string sql); string GetUnionFomatSql(string sql);
Type GetNullType(string tableName,string columnName); Type GetNullType(string tableName,string columnName);
string RemoveParentheses(string sql);
} }
} }

View File

@@ -22,5 +22,14 @@ namespace SqlSugar
return "select DATETIME('now') "; return "select DATETIME('now') ";
} }
} }
public override string RemoveParentheses(string sql)
{
if (sql.StartsWith("(") && sql.EndsWith(")"))
{
sql = sql.Substring(1, sql.Length - 2);
}
return sql;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using System.Linq; using MySqlX.XDevAPI.Common;
using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -93,6 +94,10 @@ namespace SqlSugar
{ {
reval = " DISTINCT " + reval; reval = " DISTINCT " + reval;
} }
if (this.SubToListParameters != null && this.SubToListParameters.Any())
{
reval = SubToListMethod(reval);
}
return reval; return reval;
} }
} }