Update Core

This commit is contained in:
skx
2020-12-17 15:29:46 +08:00
parent 6cf4bd03d2
commit 402fb47a10
10 changed files with 76 additions and 38 deletions

View File

@@ -80,6 +80,7 @@ namespace SqlSugar
#region Check #region Check
public virtual bool IsAnyTable(string tableName, bool isCache = true) public virtual bool IsAnyTable(string tableName, bool isCache = true)
{ {
Check.Exception(string.IsNullOrEmpty(tableName), "IsAnyTable tableName is not null");
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName); tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
var tables = GetTableInfoList(isCache); var tables = GetTableInfoList(isCache);
if (tables == null) return false; if (tables == null) return false;

View File

@@ -146,7 +146,7 @@ namespace SqlSugar
{ {
get get
{ {
return "ORDER BY "+this.Builder.SqlDateNow+" "; return "ORDER BY " + this.Builder.SqlDateNow + " ";
} }
} }
public virtual string OrderByTemplate public virtual string OrderByTemplate
@@ -216,7 +216,8 @@ namespace SqlSugar
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType) public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
{ {
ILambdaExpressions resolveExpress = this.LambdaExpressions; ILambdaExpressions resolveExpress = this.LambdaExpressions;
if (resolveType.IsIn(ResolveExpressType.FieldSingle,ResolveExpressType.FieldMultiple,ResolveExpressType.SelectSingle, ResolveExpressType.SelectMultiple) &&(expression is LambdaExpression)&& (expression as LambdaExpression).Body is BinaryExpression) { if (resolveType.IsIn(ResolveExpressType.FieldSingle, ResolveExpressType.FieldMultiple, ResolveExpressType.SelectSingle, ResolveExpressType.SelectMultiple) && (expression is LambdaExpression) && (expression as LambdaExpression).Body is BinaryExpression)
{
resolveType = resolveType.IsIn(ResolveExpressType.SelectSingle, ResolveExpressType.FieldSingle) ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple; resolveType = resolveType.IsIn(ResolveExpressType.SelectSingle, ResolveExpressType.FieldSingle) ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
} }
this.LambdaExpressions.Clear(); this.LambdaExpressions.Clear();
@@ -246,7 +247,8 @@ namespace SqlSugar
this.Parameters.AddRange(resolveExpress.Parameters); this.Parameters.AddRange(resolveExpress.Parameters);
var result = resolveExpress.Result; var result = resolveExpress.Result;
var isSingleTableHasSubquery = IsSingle() && resolveExpress.SingleTableNameSubqueryShortName.HasValue(); var isSingleTableHasSubquery = IsSingle() && resolveExpress.SingleTableNameSubqueryShortName.HasValue();
if (isSingleTableHasSubquery) { if (isSingleTableHasSubquery)
{
Check.Exception(!string.IsNullOrEmpty(this.TableShortName) && resolveExpress.SingleTableNameSubqueryShortName != this.TableShortName, "{0} and {1} need same name"); Check.Exception(!string.IsNullOrEmpty(this.TableShortName) && resolveExpress.SingleTableNameSubqueryShortName != this.TableShortName, "{0} and {1} need same name");
this.TableShortName = resolveExpress.SingleTableNameSubqueryShortName; this.TableShortName = resolveExpress.SingleTableNameSubqueryShortName;
} }
@@ -256,7 +258,7 @@ namespace SqlSugar
{ {
string oldOrderBy = this.OrderByValue; string oldOrderBy = this.OrderByValue;
string externalOrderBy = oldOrderBy; string externalOrderBy = oldOrderBy;
var isIgnoreOrderBy = this.IsCount&&this.PartitionByValue.IsNullOrEmpty(); var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
AppendFilter(); AppendFilter();
sql = new StringBuilder(); sql = new StringBuilder();
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() "; if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() ";
@@ -294,7 +296,7 @@ namespace SqlSugar
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle())) foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
{ {
var filterResult = item.FilterValue(this.Context); var filterResult = item.FilterValue(this.Context);
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(), filterResult.Sql+UtilConstants.Space)); WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(), filterResult.Sql + UtilConstants.Space));
var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters); var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters);
if (filterParamters.HasValue()) if (filterParamters.HasValue())
{ {
@@ -339,18 +341,18 @@ namespace SqlSugar
public virtual string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false) public virtual string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false)
{ {
string temp = isExternal ? ExternalPageTempalte : PageTempalte; string temp = isExternal ? ExternalPageTempalte : PageTempalte;
return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize+1, pageIndex * pageSize); return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize);
} }
public virtual string GetSelectByItems(List<KeyValuePair<string, object>> items) public virtual string GetSelectByItems(List<KeyValuePair<string, object>> items)
{ {
var array = items.Select(it => { var array = items.Select(it => {
dynamic dynamicObj = this.Context.Utilities.DeserializeObject<dynamic>(this.Context.Utilities.SerializeObject(it.Value)); dynamic dynamicObj = this.Context.Utilities.DeserializeObject<dynamic>(this.Context.Utilities.SerializeObject(it.Value));
var dbName =Builder.GetTranslationColumnName( (string)(dynamicObj.dbName)); var dbName = Builder.GetTranslationColumnName((string)(dynamicObj.dbName));
var asName = Builder.GetTranslationColumnName((string)(dynamicObj.asName)); var asName = Builder.GetTranslationColumnName((string)(dynamicObj.asName));
return string.Format("{0}.{1} AS {2}",it.Key,dbName,asName); return string.Format("{0}.{1} AS {2}", it.Key, dbName, asName);
}); });
return string.Join(",",array); return string.Join(",", array);
} }
public virtual string ToJoinString(JoinQueryInfo joinInfo) public virtual string ToJoinString(JoinQueryInfo joinInfo)
@@ -359,7 +361,7 @@ namespace SqlSugar
this.JoinTemplate, this.JoinTemplate,
joinInfo.JoinType.ToString() + UtilConstants.Space, joinInfo.JoinType.ToString() + UtilConstants.Space,
Builder.GetTranslationTableName(joinInfo.TableName) + UtilConstants.Space, Builder.GetTranslationTableName(joinInfo.TableName) + UtilConstants.Space,
joinInfo.ShortName + UtilConstants.Space + (TableWithString==SqlWith.Null?" ": TableWithString), joinInfo.ShortName + UtilConstants.Space + (TableWithString == SqlWith.Null ? " " : TableWithString),
joinInfo.JoinWhere); joinInfo.JoinWhere);
} }
public virtual void Clear() public virtual void Clear()
@@ -420,7 +422,7 @@ namespace SqlSugar
} }
else else
{ {
if (expression is LambdaExpression && (expression as LambdaExpression).Body is MethodCallExpression&&this.Context.CurrentConnectionConfig.DbType==DbType.SqlServer&&this.OrderByValue.HasValue()) if (expression is LambdaExpression && (expression as LambdaExpression).Body is MethodCallExpression && this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer && this.OrderByValue.HasValue())
{ {
result = result + " AS columnName"; result = result + " AS columnName";
} }
@@ -441,7 +443,7 @@ namespace SqlSugar
var columns = this.Context.EntityMaintenance.GetEntityInfo(this.EntityType).Columns.Where(it => !it.IsIgnore); var columns = this.Context.EntityMaintenance.GetEntityInfo(this.EntityType).Columns.Where(it => !it.IsIgnore);
if (this.IgnoreColumns.HasValue()) if (this.IgnoreColumns.HasValue())
{ {
columns = columns.Where(c => !this.IgnoreColumns.Any(i=>c.PropertyName.Equals(i,StringComparison.CurrentCultureIgnoreCase)||c.DbColumnName.Equals(i,StringComparison.CurrentCultureIgnoreCase))).ToList(); columns = columns.Where(c => !this.IgnoreColumns.Any(i => c.PropertyName.Equals(i, StringComparison.CurrentCultureIgnoreCase) || c.DbColumnName.Equals(i, StringComparison.CurrentCultureIgnoreCase))).ToList();
} }
result = string.Join(",", columns.Select(it => pre + Builder.GetTranslationColumnName(it.EntityName, it.PropertyName))); result = string.Join(",", columns.Select(it => pre + Builder.GetTranslationColumnName(it.EntityName, it.PropertyName)));
} }
@@ -488,7 +490,7 @@ namespace SqlSugar
{ {
result += (TableShortName + UtilConstants.Space); result += (TableShortName + UtilConstants.Space);
} }
if (this.TableWithString.HasValue()&&this.TableWithString!= SqlWith.Null) if (this.TableWithString.HasValue() && this.TableWithString != SqlWith.Null)
{ {
result += TableWithString + UtilConstants.Space; result += TableWithString + UtilConstants.Space;
} }
@@ -516,7 +518,7 @@ namespace SqlSugar
get get
{ {
if (this.OrderByValue == null) return null; if (this.OrderByValue == null) return null;
if (IsCount&&this.PartitionByValue.IsNullOrEmpty()) return null; if (IsCount && this.PartitionByValue.IsNullOrEmpty()) return null;
else else
{ {
return this.OrderByValue; return this.OrderByValue;
@@ -528,7 +530,7 @@ namespace SqlSugar
get get
{ {
if (this.GroupByValue == null) return null; if (this.GroupByValue == null) return null;
if (this.GroupByValue.Last() != ' ' ) if (this.GroupByValue.Last() != ' ')
{ {
return this.GroupByValue + UtilConstants.Space; return this.GroupByValue + UtilConstants.Space;
} }
@@ -546,11 +548,12 @@ namespace SqlSugar
public void CheckExpression(Expression expression, string methodName) public void CheckExpression(Expression expression, string methodName)
{ {
if (IsSingle() == false&& this.JoinExpression!=null) if (IsSingle() == false && this.JoinExpression != null)
{ {
var jsoinParameters = (this.JoinExpression as LambdaExpression).Parameters; var jsoinParameters = (this.JoinExpression as LambdaExpression).Parameters;
var currentParametres = (expression as LambdaExpression).Parameters; var currentParametres = (expression as LambdaExpression).Parameters;
if ((expression as LambdaExpression).Body.ToString() == "True") { if ((expression as LambdaExpression).Body.ToString() == "True")
{
return; return;
} }
if (currentParametres != null && currentParametres.Count > 0) if (currentParametres != null && currentParametres.Count > 0)

View File

@@ -55,7 +55,7 @@ namespace SqlSugar
{ {
if (value == null) return value; if (value == null) return value;
var type = value.GetType(); var type = value.GetType();
if (type.IsEnum()&& type != typeof(DateType)&& type!=typeof(JoinType)&&type!=typeof(OrderByType)) return Convert.ToInt64(value); if (type.IsEnum() && type != typeof(DateType) && type != typeof(JoinType) && type != typeof(OrderByType)) return Convert.ToInt64(value);
else else
return value; return value;
} }
@@ -77,7 +77,7 @@ namespace SqlSugar
} }
public static bool IsLogicOperator(string operatorValue) public static bool IsLogicOperator(string operatorValue)
{ {
return operatorValue == "&&" || operatorValue == "||"||operatorValue == "AND" || operatorValue == "OR"; return operatorValue == "&&" || operatorValue == "||" || operatorValue == "AND" || operatorValue == "OR";
} }
public static bool IsLogicOperator(Expression expression) public static bool IsLogicOperator(Expression expression)
@@ -133,7 +133,8 @@ namespace SqlSugar
} }
// fetch the root object reference: // fetch the root object reference:
var constExpr = expression as ConstantExpression; var constExpr = expression as ConstantExpression;
if (constExpr == null) { if (constExpr == null)
{
return DynamicInvoke(rootExpression); return DynamicInvoke(rootExpression);
} }
object objReference = constExpr.Value; object objReference = constExpr.Value;
@@ -146,7 +147,7 @@ namespace SqlSugar
var objProp = objReference.GetType().GetProperty(mi.Name); var objProp = objReference.GetType().GetProperty(mi.Name);
if (objProp == null) if (objProp == null)
{ {
objReference = DynamicInvoke(expression, rootExpression==null?memberExpr: rootExpression); objReference = DynamicInvoke(expression, rootExpression == null ? memberExpr : rootExpression);
} }
else else
{ {
@@ -158,7 +159,7 @@ namespace SqlSugar
var objField = objReference.GetType().GetField(mi.Name); var objField = objReference.GetType().GetField(mi.Name);
if (objField == null) if (objField == null)
{ {
objReference = DynamicInvoke(expression, rootExpression==null?memberExpr: rootExpression); objReference = DynamicInvoke(expression, rootExpression == null ? memberExpr : rootExpression);
} }
else else
{ {
@@ -205,7 +206,7 @@ namespace SqlSugar
public static bool IsConstExpression(MemberExpression memberExpr) public static bool IsConstExpression(MemberExpression memberExpr)
{ {
var result = false; var result = false;
while (memberExpr!=null&&memberExpr.Expression != null) while (memberExpr != null && memberExpr.Expression != null)
{ {
var isConst = memberExpr.Expression is ConstantExpression; var isConst = memberExpr.Expression is ConstantExpression;
if (isConst) if (isConst)
@@ -248,10 +249,10 @@ namespace SqlSugar
return reval; return reval;
} }
public static object DynamicInvoke(Expression expression,MemberExpression memberExpression=null) public static object DynamicInvoke(Expression expression, MemberExpression memberExpression = null)
{ {
object value = Expression.Lambda(expression).Compile().DynamicInvoke(); object value = Expression.Lambda(expression).Compile().DynamicInvoke();
if (value != null && value.GetType().IsClass() && value.GetType() != UtilConstants.StringType&& memberExpression!=null) if (value != null && value.GetType().IsClass() && value.GetType() != UtilConstants.StringType && memberExpression != null)
{ {
value = Expression.Lambda(memberExpression).Compile().DynamicInvoke(); value = Expression.Lambda(memberExpression).Compile().DynamicInvoke();
} }

View File

@@ -536,6 +536,37 @@ namespace SqlSugar
} }
return this.DeserializeObject<List<T>>(this.SerializeObject(deserializeObject)); return this.DeserializeObject<List<T>>(this.SerializeObject(deserializeObject));
} }
public DataTable ListToDataTable<T>(List<T> list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
//获取类型
Type colType = pi.PropertyType;
//当类型为Nullable<>时
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
result.Columns.Add(pi.Name, colType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
public Dictionary<string, object> DataTableToDictionary(DataTable table) public Dictionary<string, object> DataTableToDictionary(DataTable table)
{ {
return table.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1]); return table.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1]);

View File

@@ -27,6 +27,7 @@ namespace SqlSugar
SqlSugarProvider CopyContext(bool isCopyEvents = false); SqlSugarProvider CopyContext(bool isCopyEvents = false);
dynamic DataTableToDynamic(DataTable table); dynamic DataTableToDynamic(DataTable table);
List<T> DataTableToList<T>(DataTable table); List<T> DataTableToList<T>(DataTable table);
DataTable ListToDataTable<T>(List<T> list);
Dictionary<string, object> DataTableToDictionary(DataTable table); Dictionary<string, object> DataTableToDictionary(DataTable table);
ICacheService GetReflectionInoCacheInstance(); ICacheService GetReflectionInoCacheInstance();
void RemoveCacheAll(); void RemoveCacheAll();

View File

@@ -108,6 +108,7 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nchar",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("nchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("clob",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("clob",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("long",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("long",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nclob",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("nclob",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("rowid",CSharpDataType.@string), new KeyValuePair<string, CSharpDataType>("rowid",CSharpDataType.@string),

View File

@@ -426,7 +426,7 @@ namespace SqlSugar
var comments = this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, var comments = this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
() => () =>
{ {
string sql = "SELECT TVNAME AS TableName, COLNAME,COMMENT$ AS ColumnDescription from SYSCOLUMNCOMMENTS WHERE TVNAME='" + tableName.ToUpper() + "' ORDER BY TVNAME"; string sql = "SELECT TVNAME AS TableName, COLNAME as DbColumnName ,COMMENT$ AS ColumnDescription from SYSCOLUMNCOMMENTS WHERE TVNAME='" + tableName.ToUpper() + "' ORDER BY TVNAME";
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent; var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false; this.Context.Ado.IsEnableLogEvent = false;
var pks = this.Context.Ado.SqlQuery<DbColumnInfo>(sql); var pks = this.Context.Ado.SqlQuery<DbColumnInfo>(sql);

View File

@@ -99,7 +99,7 @@ namespace SqlSugar
sqlParameter.DbType = parameter.DbType; sqlParameter.DbType = parameter.DbType;
if (parameter.Direction == 0) if (parameter.Direction == 0)
{ {
parameter.Direction= ParameterDirection.Input; ; parameter.Direction = ParameterDirection.Input;
} }
sqlParameter.Direction = parameter.Direction; sqlParameter.Direction = parameter.Direction;
//if (sqlParameter.Direction == 0) //if (sqlParameter.Direction == 0)
@@ -107,7 +107,7 @@ namespace SqlSugar
// sqlParameter.Direction = ParameterDirection.Input; // sqlParameter.Direction = ParameterDirection.Input;
//} //}
result[index] = sqlParameter; result[index] = sqlParameter;
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput,ParameterDirection.ReturnValue)) if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
{ {
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>(); if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName); this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);

View File

@@ -230,7 +230,7 @@ namespace SqlSugar
{ {
get get
{ {
throw new NotSupportedException(); return "alter table {0} rename to {1}";
} }
} }

View File

@@ -308,14 +308,14 @@ namespace SqlSugar
} }
public ISugarQueryable<T, T2, T3> Queryable<T, T2,T3>(ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, ISugarQueryable<T3> joinQueryable3, public ISugarQueryable<T, T2, T3> Queryable<T, T2, T3>(ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, ISugarQueryable<T3> joinQueryable3,
JoinType joinType1, Expression<Func<T, T2,T3, bool>> joinExpression1, JoinType joinType1, Expression<Func<T, T2, T3, bool>> joinExpression1,
JoinType joinType2, Expression<Func<T, T2,T3, bool>> joinExpression2) JoinType joinType2, Expression<Func<T, T2, T3, bool>> joinExpression2)
where T : class, new() where T : class, new()
where T2 : class, new() where T2 : class, new()
where T3 : class, new() where T3 : class, new()
{ {
return this.Context.Queryable(joinQueryable1, joinQueryable2,joinQueryable3, joinType1, joinExpression1,joinType2,joinExpression2).With(SqlWith.Null); return this.Context.Queryable(joinQueryable1, joinQueryable2, joinQueryable3, joinType1, joinExpression1, joinType2, joinExpression2).With(SqlWith.Null);
} }
@@ -539,7 +539,7 @@ namespace SqlSugar
public IDbFirst DbFirst => this.Context.DbFirst; public IDbFirst DbFirst => this.Context.DbFirst;
public IDbMaintenance DbMaintenance => this.Context.DbMaintenance; public IDbMaintenance DbMaintenance => this.Context.DbMaintenance;
public EntityMaintenance EntityMaintenance { get { return this.Context.EntityMaintenance; } set { this.Context.EntityMaintenance = value; } } public EntityMaintenance EntityMaintenance { get { return this.Context.EntityMaintenance; } set { this.Context.EntityMaintenance = value; } }
public QueryFilterProvider QueryFilter { get { return this.Context.QueryFilter; }set { this.Context.QueryFilter = value; } } public QueryFilterProvider QueryFilter { get { return this.Context.QueryFilter; } set { this.Context.QueryFilter = value; } }
#endregion #endregion
#region TenantManager #region TenantManager