Synchronization code

This commit is contained in:
sunkaixuan
2022-12-04 04:27:40 +08:00
parent 5bedce0f5c
commit 661b6c5088
16 changed files with 144 additions and 66 deletions

View File

@@ -1434,9 +1434,11 @@ 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++)
{ {
var item = result[i]; var item = result[i];
var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList; var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList;
@@ -1472,6 +1474,7 @@ 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);
sql = SqlBuilder.RemoveParentheses(sql);
} }
sql = sql.Replace("@sugarIndex", index + ""); sql = sql.Replace("@sugarIndex", index + "");
sqls.Add(sql); sqls.Add(sql);
@@ -1507,15 +1510,18 @@ namespace SqlSugar
{ {
var item = result[i]; var item = result[i];
var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList; var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList;
var appindex = 0; if (appendValue != null)
foreach (var appValue in appendValue)
{ {
if (appValue[0].Value.ObjToInt() == i) var appindex = 0;
foreach (var appValue in appendValue)
{ {
var addItem = list[appindex]; if (appValue[0].Value.ObjToInt() == i)
setValue.Add(addItem); {
var addItem = list[appindex];
setValue.Add(addItem);
}
appindex++;
} }
appindex++;
} }
var jobj = JObject.FromObject(item); var jobj = JObject.FromObject(item);
var prop = jobj.Property(itemProperty.Name); var prop = jobj.Property(itemProperty.Name);
@@ -1531,15 +1537,18 @@ namespace SqlSugar
foreach (var item in result) foreach (var item in result)
{ {
var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList; var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList;
var appindex = 0; if (appendValue != null)
foreach (var appValue in appendValue)
{ {
if (appValue[0].Value.ObjToInt() == resIndex) var appindex = 0;
foreach (var appValue in appendValue)
{ {
var addItem = list[appindex]; if (appValue[0].Value.ObjToInt() == resIndex)
setValue.Add(addItem); {
var addItem = list[appindex];
setValue.Add(addItem);
}
appindex++;
} }
appindex++;
} }
itemProperty.SetValue(item, setValue); itemProperty.SetValue(item, setValue);
resIndex++; resIndex++;

View File

@@ -586,6 +586,55 @@ namespace SqlSugar
return result; 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 #endregion
#region Get SQL Partial #region Get SQL Partial
@@ -613,54 +662,12 @@ namespace SqlSugar
} }
if (this.SubToListParameters!=null&& this.SubToListParameters.Any()) if (this.SubToListParameters!=null&& this.SubToListParameters.Any())
{ {
List<string> names = new List<string>(); result = SubToListMethod(result);
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; return result;
} }
} }
public virtual string GetSelectValueByExpression() public virtual string GetSelectValueByExpression()
{ {
var expression = this.SelectValue as Expression; var expression = this.SelectValue as Expression;
@@ -843,6 +850,7 @@ namespace SqlSugar
#region NoCopy #region NoCopy
internal bool IsClone { get; set; } internal bool IsClone { get; set; }
public bool NoCheckInclude { get; set; } public bool NoCheckInclude { get; set; }
public virtual bool IsSelectNoAll { get; set; } = false;
#endregion #endregion
private string GetTableName(string entityName) private string GetTableName(string entityName)

View File

@@ -389,7 +389,12 @@ namespace SqlSugar
indexTree++; 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>>(); List<KeyValuePair<WhereType, ConditionalModel>> list = new List<KeyValuePair<WhereType, ConditionalModel>>();
var index = 0; var index = 0;

View File

@@ -111,7 +111,7 @@ namespace SqlSugar
public bool Contains(string value) public bool Contains(string value)
{ {
if (this.Result.Equals(value)) return true; if (this.Result.Equals(value)) return true;
return (this.Result.ToString().Contains(value)); return (this.Result.ToString().ToLower().Contains(value?.ToLower()));
} }
internal void Insert(int index, string value) internal void Insert(int index, string value)

View File

@@ -45,8 +45,24 @@ namespace SqlSugar
{; {;
var exp = expression as MethodCallExpression; var exp = expression as MethodCallExpression;
InitType(exp); InitType(exp);
if (exp.Arguments.Count == 0) var type=expression.Type;
if (type.FullName.IsCollectionsList()
&& exp.Arguments.Count == 0&& type.GenericTypeArguments.Length>0
&& this.Context.SugarContext!=null
&&this.Context.SugarContext.QueryBuilder.IsSelectNoAll)
{
var entity = type.GenericTypeArguments[0];
var columnNames=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(entity).Columns;
var columnsString = string.Join(",", columnNames
.Where(it => it.IsIgnore == false)
.Where(it => it.DbColumnName.HasValue())
.Select(it => this.Context.GetTranslationColumnName(it.DbColumnName)));
return $"{columnsString},@sugarIndex as sugarIndex";
}
else if (exp.Arguments.Count == 0)
{
return "*,@sugarIndex as sugarIndex"; return "*,@sugarIndex as sugarIndex";
}
var argExp = exp.Arguments[0]; var argExp = exp.Arguments[0];
var parametres = (argExp as LambdaExpression).Parameters; var parametres = (argExp as LambdaExpression).Parameters;
if ((argExp as LambdaExpression).Body is UnaryExpression) if ((argExp as LambdaExpression).Body is UnaryExpression)

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

@@ -62,9 +62,14 @@ namespace SqlSugar
{ {
Check.ExceptionEasy("connection string add : AllowLoadLocalInfile=true", "BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 "); Check.ExceptionEasy("connection string add : AllowLoadLocalInfile=true", "BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 ");
} }
else else if (ex.Message == "Loading local data is disabled; this must be enabled on both the client and server sides")
{ {
throw ex; this.Context.Ado.ExecuteCommand("SET GLOBAL local_infile=1");
Check.ExceptionEasy(ex.Message, " 检测到你没有开启文件,已自动执行 SET GLOBAL local_infile=1 在试一次");
}
else
{
throw;
} }
} }
finally finally

View File

@@ -148,6 +148,10 @@ namespace SqlSugar
{ {
result = " DISTINCT " + result; result = " DISTINCT " + result;
} }
if (this.SubToListParameters != null && this.SubToListParameters.Any())
{
result = SubToListMethod(result);
}
return result; return result;
} }
} }

View File

@@ -50,6 +50,14 @@ namespace SqlSugar
var result = base.GetTranslationColumnName(propertyName); var result = base.GetTranslationColumnName(propertyName);
return result.ToUpper(); return result.ToUpper();
} }
public override string RemoveParentheses(string sql)
{
if (sql.StartsWith("(") && sql.EndsWith(")"))
{
sql = sql.Substring(1, sql.Length - 2);
}
return sql;
}
} }
} }

View File

@@ -9,6 +9,7 @@ namespace SqlSugar
{ {
public class OracleQueryBuilder : QueryBuilder public class OracleQueryBuilder : QueryBuilder
{ {
public override bool IsSelectNoAll { get; set; } = true;
public override bool IsComplexModel(string sql) public override bool IsComplexModel(string sql)
{ {
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+"""); return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");

View File

@@ -108,6 +108,10 @@ namespace SqlSugar
{ {
result = "distinct "+result; result = "distinct "+result;
} }
if (this.SubToListParameters != null && this.SubToListParameters.Any())
{
result = SubToListMethod(result);
}
return result; return result;
} }
} }

View File

@@ -148,6 +148,10 @@ namespace SqlSugar
{ {
result = " DISTINCT " + result; result = " DISTINCT " + result;
} }
if (this.SubToListParameters != null && this.SubToListParameters.Any())
{
result = SubToListMethod(result);
}
return result; return result;
} }
} }

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

@@ -148,7 +148,7 @@ namespace SqlSugar
{ {
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig); date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
} }
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"; return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fffffff") + "'";
} }
private object GetDateTimeString(object value) private object GetDateTimeString(object value)

View File

@@ -68,7 +68,7 @@ namespace SqlSugar
} }
return result; return result;
} }
#endregion #endregion
#region Get SQL Partial #region Get SQL Partial
@@ -93,6 +93,10 @@ namespace SqlSugar
{ {
reval = " DISTINCT " + reval; reval = " DISTINCT " + reval;
} }
if (this.SubToListParameters != null && this.SubToListParameters.Any())
{
reval = SubToListMethod(reval);
}
return reval; return reval;
} }
} }

View File

@@ -109,7 +109,7 @@ namespace SqlSugar
{ {
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig); date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
} }
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"; return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fffffff") + "'";
} }
} }
} }