mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-01-19 17:51:36 +08:00
Update Core
This commit is contained in:
@@ -3,10 +3,12 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
@@ -674,6 +676,22 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual void ExecuteBefore(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
if (this.Context.IsAsyncMethod==false&&this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) {
|
||||
|
||||
var contextId =this.Context.ContextID.ToString();
|
||||
var processId = Thread.CurrentThread.ManagedThreadId.ToString();
|
||||
var cache = new ReflectionInoCacheService();
|
||||
if (!cache.ContainsKey<string>(contextId))
|
||||
{
|
||||
cache.Add(contextId, processId);
|
||||
}
|
||||
else {
|
||||
var cacheValue = cache.Get<string>(contextId);
|
||||
if (processId != cacheValue) {
|
||||
throw new SqlSugarException(this.Context,ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:")+sql,parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.BeforeTime = DateTime.Now;
|
||||
if (this.IsEnableLogEvent)
|
||||
{
|
||||
|
||||
@@ -94,11 +94,11 @@ namespace SqlSugar
|
||||
public virtual void NoExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
//Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||
if (entityInfo.Columns.HasValue())
|
||||
{
|
||||
foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore == false))
|
||||
foreach (var item in entityInfo.Columns.OrderBy(it=>it.IsPrimarykey?0:1).Where(it => it.IsIgnore == false))
|
||||
{
|
||||
DbColumnInfo dbColumnInfo = EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||
columns.Add(dbColumnInfo);
|
||||
@@ -110,7 +110,7 @@ namespace SqlSugar
|
||||
{
|
||||
if (entityInfo.Columns.HasValue())
|
||||
{
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
//Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Multiple primary keys do not support modifications");
|
||||
|
||||
var tableName = GetTableName(entityInfo);
|
||||
var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName);
|
||||
@@ -136,6 +136,9 @@ namespace SqlSugar
|
||||
.ToList();
|
||||
|
||||
|
||||
var isMultiplePrimaryKey = dbColumns.Where(it => it.IsPrimarykey).Count() > 1|| entityColumns.Where(it => it.IsPrimarykey).Count() > 1;
|
||||
|
||||
|
||||
var isChange = false;
|
||||
foreach (var item in addColumns)
|
||||
{
|
||||
@@ -164,7 +167,7 @@ namespace SqlSugar
|
||||
if (dbColumn == null) continue;
|
||||
bool pkDiff, idEntityDiff;
|
||||
KeyAction(item, dbColumn, out pkDiff, out idEntityDiff);
|
||||
if (dbColumn != null && pkDiff && !idEntityDiff)
|
||||
if (dbColumn != null && pkDiff && !idEntityDiff&& isMultiplePrimaryKey==false)
|
||||
{
|
||||
var isAdd = item.IsPrimarykey;
|
||||
if (isAdd)
|
||||
@@ -176,11 +179,19 @@ namespace SqlSugar
|
||||
this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
|
||||
}
|
||||
}
|
||||
else if (pkDiff || idEntityDiff)
|
||||
else if ((pkDiff || idEntityDiff)&& isMultiplePrimaryKey==false)
|
||||
{
|
||||
ChangeKey(entityInfo, tableName, item);
|
||||
}
|
||||
}
|
||||
if (isMultiplePrimaryKey) {
|
||||
var oldPkNames = dbColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it=>it).ToList();
|
||||
var newPkNames = entityColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it=>it).ToList();
|
||||
if (!Enumerable.SequenceEqual(oldPkNames,newPkNames)) {
|
||||
Check.Exception(true, ErrorMessage.GetThrowMessage("Modification of multiple primary key tables is not supported. Delete tables while creating", "不支持修改多主键表,请删除表在创建"));
|
||||
}
|
||||
|
||||
}
|
||||
if (isChange && IsBackupTable)
|
||||
{
|
||||
this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows);
|
||||
|
||||
@@ -140,6 +140,15 @@ namespace SqlSugar
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AddPrimaryKeys(string tableName, string[] columnNames)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
var columnName = string.Join(",", columnNames);
|
||||
string sql = string.Format(this.AddPrimaryKeySql, tableName, string.Format("PK_{0}_{1}", this.SqlBuilder.GetNoTranslationColumnName(columnNames.First()), this.SqlBuilder.GetNoTranslationColumnName(columnNames.First())), columnName);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
@@ -281,6 +290,13 @@ namespace SqlSugar
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool RenameTable(string oldTableName, string newTableName)
|
||||
{
|
||||
string sql = string.Format(this.RenameTableSql, oldTableName,newTableName);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private
|
||||
|
||||
@@ -49,6 +49,7 @@ namespace SqlSugar
|
||||
protected abstract string AddTableRemarkSql { get; }
|
||||
protected abstract string DeleteTableRemarkSql { get; }
|
||||
protected abstract string IsAnyTableRemarkSql { get; }
|
||||
protected abstract string RenameTableSql { get; }
|
||||
#endregion
|
||||
|
||||
#region Check
|
||||
|
||||
@@ -378,6 +378,7 @@ namespace SqlSugar
|
||||
{
|
||||
var asyncContext = this.Context.Utilities.CopyContext(true);
|
||||
asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true;
|
||||
asyncContext.IsAsyncMethod = true;
|
||||
|
||||
var asyncDeleteable = asyncContext.Deleteable<T>();
|
||||
var asyncDeleteBuilder = asyncDeleteable.DeleteBuilder;
|
||||
|
||||
@@ -41,8 +41,11 @@ namespace SqlSugar
|
||||
foreach (var item in InsertBuilder.DbColumnInfoList)
|
||||
{
|
||||
var isPk = pks.Any(y => y.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)) || item.IsPrimarykey;
|
||||
if (isPk && item.PropertyType == UtilConstants.GuidType&&item.Value.ObjToString()==Guid.Empty.ToString()) {
|
||||
if (isPk && item.PropertyType == UtilConstants.GuidType && item.Value.ObjToString() == Guid.Empty.ToString())
|
||||
{
|
||||
item.Value = Guid.NewGuid();
|
||||
if (InsertObjs.First().GetType().GetProperties().Any(it => it.Name == item.PropertyName))
|
||||
InsertObjs.First().GetType().GetProperties().First(it => it.Name == item.PropertyName).SetValue(InsertObjs.First(), item.Value, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -425,7 +428,7 @@ namespace SqlSugar
|
||||
{
|
||||
var asyncContext = this.Context.Utilities.CopyContext(true);
|
||||
asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true;
|
||||
|
||||
asyncContext.IsAsyncMethod = true;
|
||||
var asyncInsertable = asyncContext.Insertable<T>(this.InsertObjs);
|
||||
var asyncInsertableBuilder = asyncInsertable.InsertBuilder;
|
||||
asyncInsertableBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList;
|
||||
|
||||
@@ -146,7 +146,7 @@ namespace SqlSugar
|
||||
/// <returns></returns>
|
||||
public ISugarQueryable<T> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
return WhereClass(new List<ClassType>() { whereClass }, ignoreDefaultValue);
|
||||
return WhereClass(new List<ClassType>() { whereClass },ignoreDefaultValue);
|
||||
}
|
||||
/// <summary>
|
||||
/// if a property that is not empty is a condition
|
||||
@@ -168,9 +168,9 @@ namespace SqlSugar
|
||||
|
||||
var value = column.PropertyInfo.GetValue(item, null);
|
||||
WhereType WhereType = WhereType.And;
|
||||
var isNotNull = ignoreDefaultValue == false && value != null;
|
||||
var isNotNullAndDefault = ignoreDefaultValue && value != null && value.ObjToString() != UtilMethods.DefaultForType(column.PropertyInfo.PropertyType).ObjToString();
|
||||
if (isNotNull || isNotNullAndDefault)
|
||||
var isNotNull = ignoreDefaultValue == false&&value != null ;
|
||||
var isNotNullAndDefault = ignoreDefaultValue&& value!=null && value.ObjToString() != UtilMethods.DefaultForType(column.PropertyInfo.PropertyType).ObjToString();
|
||||
if (isNotNull||isNotNullAndDefault)
|
||||
{
|
||||
if (cons.ConditionalList == null)
|
||||
{
|
||||
@@ -271,7 +271,7 @@ namespace SqlSugar
|
||||
Where(SqlBuilder.SqlFalse);
|
||||
return this;
|
||||
}
|
||||
if (pkValues.Length == 1 && pkValues.First().GetType().FullName.IsCollectionsList() || (pkValues.First() is IEnumerable && pkValues.First().GetType() != UtilConstants.StringType))
|
||||
if (pkValues.Length == 1 && pkValues.First().GetType().FullName.IsCollectionsList()|| (pkValues.First() is IEnumerable&&pkValues.First().GetType()!=UtilConstants.StringType))
|
||||
{
|
||||
var newValues = new List<object>();
|
||||
foreach (var item in pkValues.First() as IEnumerable)
|
||||
@@ -573,6 +573,11 @@ namespace SqlSugar
|
||||
return mergeQueryable.AS(tableName).Select<T>("*");
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
public virtual int Count()
|
||||
{
|
||||
InitMapping();
|
||||
@@ -1329,7 +1334,7 @@ namespace SqlSugar
|
||||
{
|
||||
Action<List<T>> mapper = (entitys) =>
|
||||
{
|
||||
if (entitys.IsNullOrEmpty()) return;
|
||||
if (entitys.IsNullOrEmpty()&&entitys.Any()) return;
|
||||
var entity = entitys.First();
|
||||
var whereCol = filedEntity.Columns.FirstOrDefault(it => it.PropertyName.Equals(filedName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (whereCol == null)
|
||||
@@ -1337,6 +1342,18 @@ namespace SqlSugar
|
||||
whereCol = filedEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = filedEntity.Columns.FirstOrDefault(it => GetPrimaryKeys().Any(pk=>pk.Equals(it.DbColumnName,StringComparison.CurrentCultureIgnoreCase)) );
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = filedEntity.Columns.FirstOrDefault(it => it.PropertyName.Equals("id",StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = filedEntity.Columns.FirstOrDefault(it => (it.PropertyName).Equals(it.EntityName+"id", StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
Check.Exception(true, ".Mapper() parameter error");
|
||||
}
|
||||
@@ -1372,7 +1389,7 @@ namespace SqlSugar
|
||||
{
|
||||
Action<List<T>> mapper = (entitys) =>
|
||||
{
|
||||
if (entitys.IsNullOrEmpty()) return;
|
||||
if (entitys.IsNullOrEmpty()&&entitys.Any()) return;
|
||||
var entity = entitys.First();
|
||||
var tEntity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var whereCol = tEntity.Columns.FirstOrDefault(it => it.PropertyName.Equals(filedName, StringComparison.CurrentCultureIgnoreCase));
|
||||
@@ -1381,6 +1398,18 @@ namespace SqlSugar
|
||||
whereCol = tEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = tEntity.Columns.FirstOrDefault(it => GetPrimaryKeys().Any(pk => pk.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = tEntity.Columns.FirstOrDefault(it => it.PropertyName.Equals("id", StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
whereCol = tEntity.Columns.FirstOrDefault(it => (it.PropertyName).Equals(it.EntityName + "id", StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
if (whereCol == null)
|
||||
{
|
||||
Check.Exception(true, ".Mapper() parameter error");
|
||||
}
|
||||
@@ -1533,12 +1562,17 @@ namespace SqlSugar
|
||||
protected ISugarQueryable<T> CopyQueryable()
|
||||
{
|
||||
var asyncContext = this.Context.Utilities.CopyContext(true);
|
||||
asyncContext.IsAsyncMethod = true;
|
||||
asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true;
|
||||
var asyncQueryable = asyncContext.Queryable<ExpandoObject>().Select<T>(string.Empty).WithCacheIF(IsCache, CacheTime);
|
||||
if (this.MapperAction != null)
|
||||
asyncQueryable.Mapper(MapperAction);
|
||||
if (this.MapperActionWithCache != null)
|
||||
asyncQueryable.Mapper(MapperActionWithCache);
|
||||
if (this.Mappers != null && ((asyncQueryable as QueryableProvider<T>)!=null))
|
||||
{
|
||||
(asyncQueryable as QueryableProvider<T>).Mappers = this.Mappers;
|
||||
}
|
||||
CopyQueryBuilder(asyncQueryable.QueryBuilder); return asyncQueryable;
|
||||
}
|
||||
|
||||
@@ -1559,6 +1593,7 @@ namespace SqlSugar
|
||||
asyncQueryableBuilder.TableShortName = this.QueryBuilder.TableShortName;
|
||||
asyncQueryableBuilder.TableWithString = this.QueryBuilder.TableWithString;
|
||||
asyncQueryableBuilder.GroupByValue = this.QueryBuilder.GroupByValue;
|
||||
asyncQueryableBuilder.IsDistinct = this.QueryBuilder.IsDistinct;
|
||||
asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue;
|
||||
asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter;
|
||||
asyncQueryableBuilder.PartitionByValue = this.QueryBuilder.PartitionByValue;
|
||||
@@ -1567,6 +1602,7 @@ namespace SqlSugar
|
||||
asyncQueryableBuilder.HavingInfos = this.QueryBuilder.HavingInfos;
|
||||
asyncQueryableBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -1860,6 +1896,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T,T2> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -2216,6 +2265,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2,T3> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -2630,6 +2692,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, T4, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2, T3,T4> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -2721,9 +2796,9 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClass"></param>
|
||||
/// <returns></returns>
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
public new ISugarQueryable<T,T2, T3, T4, T5> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
base.WhereClass(whereClass, ignoreDefaultValue);
|
||||
base.WhereClass(whereClass, ignoreDefaultValue);
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -2731,7 +2806,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClassTypes"></param>
|
||||
/// <returns></returns>
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5> WhereClass<ClassType>(List<ClassType> whereClassTypes, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
public new ISugarQueryable<T,T2, T3, T4, T5> WhereClass<ClassType>(List<ClassType> whereClassTypes, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
|
||||
base.WhereClass(whereClassTypes, ignoreDefaultValue);
|
||||
@@ -2971,6 +3046,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, T4, T5, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2, T3, T4,T5> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -3074,7 +3162,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClass"></param>
|
||||
/// <returns></returns>
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5,T6> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
base.WhereClass(whereClass, ignoreDefaultValue);
|
||||
return this;
|
||||
@@ -3343,6 +3431,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, T4, T5, T6, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5,T6> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -3457,7 +3558,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClass"></param>
|
||||
/// <returns></returns>
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> WhereClass<ClassType>(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
base.WhereClass(whereClass, ignoreDefaultValue);
|
||||
return this;
|
||||
@@ -3467,7 +3568,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClassTypes"></param>
|
||||
/// <returns></returns>
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WhereClass<ClassType>(List<ClassType> whereClassTypes, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> WhereClass<ClassType>(List<ClassType> whereClassTypes, bool ignoreDefaultValue = false) where ClassType : class, new()
|
||||
{
|
||||
|
||||
base.WhereClass(whereClassTypes, ignoreDefaultValue);
|
||||
@@ -3746,6 +3847,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, T4, T5, T6, T7, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
@@ -4182,6 +4296,19 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, bool>> expression)
|
||||
{
|
||||
_Where(expression);
|
||||
var result = Any();
|
||||
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
|
||||
return result;
|
||||
}
|
||||
public new ISugarQueryable<T, T2, T3, T4, T5, T6, T7,T8> Distinct()
|
||||
{
|
||||
QueryBuilder.IsDistinct = true;
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type =UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -49,6 +49,7 @@ namespace SqlSugar
|
||||
public string GroupByValue { get; set; }
|
||||
public string PartitionByValue { get; set; }
|
||||
public int WhereIndex { get; set; }
|
||||
public bool IsDistinct { get; set; }
|
||||
public int JoinIndex { get; set; }
|
||||
public bool IsDisabledGobalFilter { get; set; }
|
||||
public virtual List<SugarParameter> Parameters { get; set; }
|
||||
@@ -363,6 +364,7 @@ namespace SqlSugar
|
||||
this._TableNameString = null;
|
||||
this.WhereInfos = null;
|
||||
this.JoinQueryInfos = null;
|
||||
this.IsDistinct = false;
|
||||
}
|
||||
public virtual bool IsComplexModel(string sql)
|
||||
{
|
||||
@@ -388,6 +390,10 @@ namespace SqlSugar
|
||||
{
|
||||
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this._JoinQueryInfos.Select(it => it.TableName));
|
||||
}
|
||||
if (IsDistinct)
|
||||
{
|
||||
result = " DISTINCT " + result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -526,6 +526,7 @@ namespace SqlSugar
|
||||
{
|
||||
var asyncContext = this.Context.Utilities.CopyContext(true);
|
||||
asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true;
|
||||
asyncContext.IsAsyncMethod = true;
|
||||
|
||||
var asyncUpdateable = asyncContext.Updateable<T>(this.UpdateObjs);
|
||||
var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder;
|
||||
|
||||
@@ -44,6 +44,10 @@ namespace SqlSugar
|
||||
/// More Gobal Settings
|
||||
/// </summary>
|
||||
public ConnMoreSettings MoreSettings { get; set; }
|
||||
/// <summary>
|
||||
/// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance
|
||||
/// </summary>
|
||||
public SugarDebugger Debugger { get; set; }
|
||||
}
|
||||
|
||||
public class ConfigureExternalServices
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance
|
||||
/// </summary>
|
||||
public class SugarDebugger
|
||||
{
|
||||
/// <summary>
|
||||
/// If you use the same Db object across threads, you will be prompted
|
||||
/// </summary>
|
||||
public bool EnableThreadSecurityValidation { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,8 @@ namespace SqlSugar
|
||||
case ExpressionType.Multiply:
|
||||
case ExpressionType.MultiplyChecked:
|
||||
return "*";
|
||||
case ExpressionType.Modulo:
|
||||
return "%";
|
||||
case ExpressionType.Coalesce:
|
||||
throw new Exception("Expression no support ?? ,Use SqlFunc.IsNull");
|
||||
default:
|
||||
|
||||
@@ -44,6 +44,10 @@ namespace SqlSugar
|
||||
{
|
||||
return new LambdaExpressionResolve(parameter);
|
||||
}
|
||||
else if (expression is BinaryExpression && expression.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
return new CoalesceResolveItems(parameter);
|
||||
}
|
||||
else if (expression is BinaryExpression)
|
||||
{
|
||||
return new BinaryExpressionResolve(parameter);
|
||||
@@ -129,7 +133,7 @@ namespace SqlSugar
|
||||
if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null)
|
||||
{
|
||||
var oppoSiteExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
|
||||
if (parameter.CurrentExpression is MethodCallExpression)
|
||||
if (parameter.CurrentExpression is MethodCallExpression||parameter.CurrentExpression is ConditionalExpression||parameter.CurrentExpression.NodeType==ExpressionType.Coalesce)
|
||||
{
|
||||
var appendValue = value;
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
@@ -438,7 +442,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (item is MethodCallExpression|| item is UnaryExpression)
|
||||
else if (item is MethodCallExpression|| item is UnaryExpression||item is ConditionalExpression|| item.NodeType==ExpressionType.Coalesce)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
|
||||
@@ -49,6 +49,10 @@ namespace SqlSugar
|
||||
base.Expression = leftExpression;
|
||||
base.IsLeft = true;
|
||||
base.Start();
|
||||
if (leftExpression is UnaryExpression && leftExpression.Type == UtilConstants.BoolType&&!this.Context.Result.Contains(ExpressionConst.ExpressionReplace))
|
||||
{
|
||||
this.Context.Result.AppendFormat(" {0} ", ExpressionTool.GetOperator(expression.NodeType));
|
||||
}
|
||||
base.IsLeft = false;
|
||||
base.Expression = rightExpression;
|
||||
base.Start();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class CoalesceResolveItems : MethodCallExpressionResolve
|
||||
{
|
||||
public CoalesceResolveItems(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
string name = "IsNull";
|
||||
var express = base.Expression as BinaryExpression;
|
||||
var args = new List<Expression>() {
|
||||
express.Left,
|
||||
express.Right
|
||||
};
|
||||
var isLeft = parameter.IsLeft;
|
||||
MethodCallExpressionModel model = new MethodCallExpressionModel();
|
||||
model.Args = new List<MethodCallExpressionArgs>();
|
||||
switch (this.Context.ResolveType)
|
||||
{
|
||||
case ResolveExpressType.WhereSingle:
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
Check.Exception(name == "GetSelfAndAutoFill", "SqlFunc.GetSelfAndAutoFill can only be used in Select.");
|
||||
base.Where(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
case ResolveExpressType.SelectSingle:
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
case ResolveExpressType.Update:
|
||||
base.Select(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
base.Field(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,27 +5,38 @@ using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ConditionalExpressionResolve:BaseResolve
|
||||
public class ConditionalExpressionResolve: MethodCallExpressionResolve
|
||||
{
|
||||
public ConditionalExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
public ConditionalExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
|
||||
string name = "IIF";
|
||||
var express = base.Expression as ConditionalExpression;
|
||||
var args = new List<Expression>() {
|
||||
express.Test,
|
||||
express.IfTrue,
|
||||
express.IfFalse
|
||||
};
|
||||
var isLeft = parameter.IsLeft;
|
||||
switch (base.Context.ResolveType)
|
||||
MethodCallExpressionModel model = new MethodCallExpressionModel();
|
||||
model.Args = new List<MethodCallExpressionArgs>();
|
||||
switch (this.Context.ResolveType)
|
||||
{
|
||||
case ResolveExpressType.None:
|
||||
case ResolveExpressType.WhereSingle:
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
Check.Exception(name == "GetSelfAndAutoFill", "SqlFunc.GetSelfAndAutoFill can only be used in Select.");
|
||||
base.Where(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
case ResolveExpressType.SelectSingle:
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
case ResolveExpressType.Update:
|
||||
base.Select(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
case ResolveExpressType.Join:
|
||||
case ResolveExpressType.ArraySingle:
|
||||
case ResolveExpressType.ArrayMultiple:
|
||||
case ResolveExpressType.Update:
|
||||
base.Field(parameter, isLeft, name, args, model);
|
||||
break;
|
||||
default:
|
||||
Check.Exception(true, "Does not support it.xx==value ? true:false , Use SqlFunc.IIF (it.xx==value,true,false)");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +100,37 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Resolve Where
|
||||
private void ResolveBoolLogic(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
|
||||
{
|
||||
string fieldName = string.Empty;
|
||||
if (isSetTempData)
|
||||
{
|
||||
if (ExpressionTool.IsConstExpression(expression))
|
||||
{
|
||||
var value = ExpressionTool.GetMemberValue(expression.Member, expression);
|
||||
baseParameter.CommonTempData = value+"=1 ";
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldName = GetName(parameter, expression, null, isSingle);
|
||||
baseParameter.CommonTempData = fieldName+"=1 ";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ExpressionTool.IsConstExpression(expression))
|
||||
{
|
||||
var value = ExpressionTool.GetMemberValue(expression.Member, expression);
|
||||
base.AppendValue(parameter, isLeft, value+"=1 ");
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldName = GetName(parameter, expression, isLeft, isSingle);
|
||||
AppendMember(parameter, isLeft, fieldName+"=1 ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveWhereLogic(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
|
||||
{
|
||||
string fieldName = string.Empty;
|
||||
@@ -137,7 +168,14 @@ namespace SqlSugar
|
||||
{
|
||||
expression = expression.Expression as MemberExpression;
|
||||
baseParameter.ChildExpression = expression;
|
||||
ResolveWhereLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
|
||||
if (UtilMethods.GetUnderType(expression.Type) == UtilConstants.BoolType&¶meter.BaseExpression!=null&&ExpressionTool.IsLogicOperator(parameter.BaseExpression))
|
||||
{
|
||||
ResolveBoolLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResolveWhereLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
@@ -248,7 +286,18 @@ namespace SqlSugar
|
||||
methodParamter
|
||||
}
|
||||
});
|
||||
this.Context.Result.Append(result);
|
||||
if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && parameter.IsLeft == true)
|
||||
{
|
||||
if (base.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.FormatSymbol, "");
|
||||
}
|
||||
this.Context.Result.Append(result+" "+ExpressionTool.GetOperator(parameter.BaseExpression.NodeType)+" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(result);
|
||||
}
|
||||
parameter.CommonTempData = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,12 @@ namespace SqlSugar
|
||||
{
|
||||
public class MethodCallExpressionResolve : BaseResolve
|
||||
{
|
||||
int contextIndex = 0;
|
||||
public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
contextIndex = this.Context.Index;
|
||||
var express = base.Expression as MethodCallExpression;
|
||||
if (express == null) return;
|
||||
var isLeft = parameter.IsLeft;
|
||||
string methodName = express.Method.Name;
|
||||
var isValidNativeMethod = IsValidNativeMethod(express, methodName);
|
||||
@@ -59,6 +62,10 @@ namespace SqlSugar
|
||||
{
|
||||
CaseWhenResolve caseResole = new CaseWhenResolve(express, this.Context, parameter.OppsiteExpression);
|
||||
var appendSql = caseResole.GetSql();
|
||||
var isRoot = contextIndex == 2&¶meter.BaseExpression==null;
|
||||
if (isRoot||(parameter.BaseExpression!=null&&ExpressionTool.IsLogicOperator(parameter.BaseExpression))) {
|
||||
appendSql= appendSql+"=1 ";
|
||||
}
|
||||
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle,ResolveExpressType.Update))
|
||||
{
|
||||
parameter.BaseParameter.CommonTempData = appendSql;
|
||||
@@ -103,7 +110,7 @@ namespace SqlSugar
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SqlFuncMethod(ExpressionParameter parameter, MethodCallExpression express, bool? isLeft)
|
||||
protected void SqlFuncMethod(ExpressionParameter parameter, MethodCallExpression express, bool? isLeft)
|
||||
{
|
||||
if (!CheckMethod(express))
|
||||
{
|
||||
@@ -188,7 +195,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private void Field(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
protected void Field(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
{
|
||||
if (this.Context.ResolveType == ResolveExpressType.FieldSingle)
|
||||
{
|
||||
@@ -200,7 +207,7 @@ namespace SqlSugar
|
||||
}
|
||||
Where(parameter, isLeft, name, args, model);
|
||||
}
|
||||
private void Select(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
protected void Select(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
{
|
||||
if (name == "GetSelfAndAutoFill")
|
||||
{
|
||||
@@ -227,7 +234,7 @@ namespace SqlSugar
|
||||
parameter.BaseParameter.CommonTempData = GetMethodValue(name, model);
|
||||
}
|
||||
}
|
||||
private void Where(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
protected void Where(ExpressionParameter parameter, bool? isLeft, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, List<MethodCallExpressionArgs> appendArgs = null)
|
||||
{
|
||||
foreach (var item in args)
|
||||
{
|
||||
@@ -249,6 +256,31 @@ namespace SqlSugar
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
}
|
||||
var isRoot = contextIndex == 2;
|
||||
if (isRoot && parameter.BaseExpression == null &&this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple,ResolveExpressType.WhereSingle)&& (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("ToBool", "ToBoolean")))
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
; }
|
||||
if (isRoot && parameter.BaseExpression == null && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is ConditionalExpression) && ((parameter.CurrentExpression as ConditionalExpression).Type==UtilConstants.BoolType))
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
}
|
||||
if (isRoot && parameter.BaseExpression == null && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("IIF"))&& (parameter.CurrentExpression as MethodCallExpression).Method.ReturnType==UtilConstants.BoolType)
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
}
|
||||
if (parameter.BaseExpression != null&&ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is ConditionalExpression) && ((parameter.CurrentExpression as ConditionalExpression).Type == UtilConstants.BoolType))
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
}
|
||||
if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("IIF")) && (parameter.CurrentExpression as MethodCallExpression).Method.ReturnType == UtilConstants.BoolType)
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
}
|
||||
if (parameter.BaseExpression != null&& ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("ToBool", "ToBoolean")))
|
||||
{
|
||||
methodValue = methodValue + "=1 ";
|
||||
}
|
||||
base.AppendValue(parameter, isLeft, methodValue);
|
||||
}
|
||||
|
||||
@@ -402,6 +434,14 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name == "Parse" && TempParseType.IsIn(UtilConstants.GuidType))
|
||||
{
|
||||
name = "Equals";
|
||||
}
|
||||
else if(name== "Parse")
|
||||
{
|
||||
name = "To"+TempParseType.Name;
|
||||
}
|
||||
switch (name)
|
||||
{
|
||||
case "IIF":
|
||||
@@ -460,6 +500,8 @@ namespace SqlSugar
|
||||
return this.Context.DbMehtods.ToInt64(model);
|
||||
case "ToDate":
|
||||
return this.Context.DbMehtods.ToDate(model);
|
||||
case "ToDateTime":
|
||||
return this.Context.DbMehtods.ToDate(model);
|
||||
case "ToTime":
|
||||
return this.Context.DbMehtods.ToTime(model);
|
||||
case "ToString":
|
||||
@@ -473,6 +515,8 @@ namespace SqlSugar
|
||||
return this.Context.DbMehtods.ToDouble(model);
|
||||
case "ToBool":
|
||||
return this.Context.DbMehtods.ToBool(model);
|
||||
case "ToBoolean":
|
||||
return this.Context.DbMehtods.ToBool(model);
|
||||
case "Substring":
|
||||
return this.Context.DbMehtods.Substring(model);
|
||||
case "Replace":
|
||||
@@ -526,10 +570,35 @@ namespace SqlSugar
|
||||
{
|
||||
if (IsExtMethod(expression.Method.Name))
|
||||
return true;
|
||||
if (IsParseMethod(expression))
|
||||
return true;
|
||||
if (expression.Method.Name == "IsNullOrEmpty"&&expression.Method.DeclaringType==UtilConstants.StringType)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (expression.Method.ReflectedType().FullName != ExpressionConst.SqlFuncFullName)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
private Type TempParseType;
|
||||
public bool IsParseMethod(MethodCallExpression expression)
|
||||
{
|
||||
if (expression.Method.Name == "Parse"&&expression.Method.DeclaringType.IsIn(
|
||||
UtilConstants.DecType,
|
||||
UtilConstants.DateType,
|
||||
UtilConstants.DobType,
|
||||
UtilConstants.GuidType,
|
||||
UtilConstants.FloatType,
|
||||
UtilConstants.ShortType,
|
||||
UtilConstants.LongType,
|
||||
UtilConstants.IntType,
|
||||
UtilConstants.BoolType))
|
||||
{
|
||||
TempParseType = expression.Method.DeclaringType;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ namespace SqlSugar
|
||||
else
|
||||
{
|
||||
var item = model as ConditionalCollections;
|
||||
if (item != null)
|
||||
if (item != null&& item.ConditionalList.HasValue())
|
||||
{
|
||||
foreach (var con in item.ConditionalList)
|
||||
{
|
||||
|
||||
@@ -46,6 +46,7 @@ namespace SqlSugar
|
||||
public Dictionary<string, object> TempItems { get; set; }
|
||||
public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
|
||||
public Guid ContextID { get; set; }
|
||||
internal bool IsAsyncMethod { get; set; }
|
||||
public MappingTableList MappingTables = new MappingTableList();
|
||||
public MappingColumnList MappingColumns = new MappingColumnList();
|
||||
public IgnoreColumnList IgnoreColumns = new IgnoreColumnList();
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace SqlSugar
|
||||
bool AddColumn(string tableName, DbColumnInfo column);
|
||||
bool UpdateColumn(string tableName, DbColumnInfo column);
|
||||
bool AddPrimaryKey(string tableName,string columnName);
|
||||
bool AddPrimaryKeys(string tableName, string [] columnNames);
|
||||
bool DropConstraint(string tableName, string constraintName);
|
||||
bool BackupDataBase(string databaseName,string fullFileName);
|
||||
bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue);
|
||||
@@ -44,6 +45,7 @@ namespace SqlSugar
|
||||
bool AddTableRemark( string tableName, string description);
|
||||
bool DeleteTableRemark(string tableName);
|
||||
bool IsAnyTableRemark(string tableName);
|
||||
bool RenameTable(string oldTableName,string newTableName);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ namespace SqlSugar
|
||||
|
||||
ISugarQueryable<T> Skip(int index);
|
||||
ISugarQueryable<T> Take(int num);
|
||||
ISugarQueryable<T> Distinct();
|
||||
|
||||
T Single();
|
||||
Task<T> SingleAsync();
|
||||
@@ -221,6 +222,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2> With(string withString);
|
||||
new ISugarQueryable<T, T2> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T,T2> Distinct();
|
||||
bool Any(Expression<Func<T,T2, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3> : ISugarQueryable<T>
|
||||
@@ -310,6 +313,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2,T3> Distinct();
|
||||
bool Any(Expression<Func<T, T2,T3, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3, T4> : ISugarQueryable<T>
|
||||
@@ -410,6 +415,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3, T4> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3, T4> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3,T4> Distinct();
|
||||
bool Any(Expression<Func<T, T2, T3,T4, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3, T4, T5> : ISugarQueryable<T>
|
||||
@@ -500,6 +507,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3, T4, T5> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4,T5> Distinct();
|
||||
bool Any(Expression<Func<T, T2, T3, T4,T5, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6> : ISugarQueryable<T>
|
||||
@@ -596,6 +605,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5,T6> Distinct();
|
||||
bool Any(Expression<Func<T, T2, T3, T4, T5,T6, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6, T7> : ISugarQueryable<T>
|
||||
@@ -698,6 +709,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6,T7> Distinct();
|
||||
bool Any(Expression<Func<T, T2, T3, T4, T5, T6,T7, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> : ISugarQueryable<T>
|
||||
@@ -806,6 +819,8 @@ namespace SqlSugar
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> With(string withString);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> WithCache(int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
|
||||
new ISugarQueryable<T, T2, T3, T4, T5, T6, T7,T8> Distinct();
|
||||
bool Any(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, bool>> expression);
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,11 @@ namespace SqlSugar
|
||||
public override void NoExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
//Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||
if (entityInfo.Columns.HasValue())
|
||||
{
|
||||
foreach (var item in entityInfo.Columns)
|
||||
foreach (var item in entityInfo.Columns.OrderBy(it => it.IsPrimarykey ? 0 : 1))
|
||||
{
|
||||
if (item.IsIgnore)
|
||||
continue;
|
||||
|
||||
@@ -224,6 +224,13 @@ namespace SqlSugar
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
protected override string RenameTableSql {
|
||||
get
|
||||
{
|
||||
return "alter table {0} rename {1}";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -63,7 +63,17 @@ namespace SqlSugar
|
||||
this.OrderByValue = oldOrderValue;
|
||||
return result;
|
||||
}
|
||||
|
||||
public override string ToCountSql(string sql)
|
||||
{
|
||||
if (this.GroupByValue.HasValue())
|
||||
{
|
||||
return base.ToCountSql(sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Regex.Replace(sql, "^SELECT .+? FROM ", "SELECT COUNT(*) FROM ");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Get SQL Partial
|
||||
@@ -84,6 +94,10 @@ namespace SqlSugar
|
||||
{
|
||||
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
|
||||
}
|
||||
if (IsDistinct)
|
||||
{
|
||||
result = " DISTINCT " + result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -217,6 +217,13 @@ namespace SqlSugar
|
||||
return "select * from user_tab_comments where Table_Name='{0}'order by Table_Name";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string RenameTableSql {
|
||||
get
|
||||
{
|
||||
return "alter table {0} rename to {1}";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -199,6 +199,8 @@ namespace SqlSugar
|
||||
protected override string DeleteTableRemarkSql => "comment on table {0} is ''";
|
||||
|
||||
protected override string IsAnyTableRemarkSql => throw new NotSupportedException();
|
||||
|
||||
protected override string RenameTableSql => "alter table 表名 {0} to {1}";
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
@@ -229,9 +229,9 @@ namespace SqlSugar
|
||||
" FROM sys.tables A" +
|
||||
" LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id" +
|
||||
" LEFT JOIN sys.columns B ON B.object_id = A.object_id AND C.minor_id = B.column_id" +
|
||||
" INNER JOIN sys.schemas SC ON SC.schema_id = A.schema_id AND SC.name = 'dbo'"+
|
||||
" INNER JOIN sys.schemas SC ON SC.schema_id = A.schema_id AND SC.name = 'dbo'" +
|
||||
" WHERE A.name = '{1}' and b.name = '{0}'";
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,6 +264,14 @@ namespace SqlSugar
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override string RenameTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXEC sp_rename '{0}','{1}'";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
||||
@@ -271,11 +279,19 @@ namespace SqlSugar
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
string sql = GetCreateTableSql(tableName, columns);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
if (isCreatePrimaryKey) {
|
||||
if (isCreatePrimaryKey)
|
||||
{
|
||||
var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
|
||||
foreach (var item in pkColumns)
|
||||
if (pkColumns.Count > 1)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it=>it.DbColumnName).ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in pkColumns)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace SqlSugar
|
||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||
if (entityInfo.Columns.HasValue())
|
||||
{
|
||||
foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore==false))
|
||||
foreach (var item in entityInfo.Columns.OrderBy(it => it.IsPrimarykey ? 0 : 1).Where(it=>it.IsIgnore==false))
|
||||
{
|
||||
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||
columns.Add(dbColumnInfo);
|
||||
|
||||
@@ -209,6 +209,8 @@ namespace SqlSugar
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
protected override string RenameTableSql => throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
@@ -20,6 +20,14 @@ namespace SqlSugar
|
||||
return string.Format("LENGTH({0})", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string Substring(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
var parameter3 = model.Args[2];
|
||||
return string.Format("SUBSTR({0},1 + {1},{2})", parameter.MemberName, parameter2.MemberName, parameter3.MemberName);
|
||||
}
|
||||
|
||||
public override string Contains(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -84,6 +84,10 @@ namespace SqlSugar
|
||||
{
|
||||
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
|
||||
}
|
||||
if (IsDistinct)
|
||||
{
|
||||
reval = " DISTINCT " + reval;
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
|
||||
@@ -321,6 +321,11 @@ namespace SqlSugar
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T> Queryable<T>(ISugarQueryable<T> queryable) where T : class, new()
|
||||
{
|
||||
var sqlobj = queryable.ToSql();
|
||||
return this.SqlQueryable<T>(sqlobj.Key).AddParameters(sqlobj.Value);
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2> Queryable<T, T2>(
|
||||
ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, Expression<Func<T, T2, bool>> joinExpression) where T : class, new() where T2 : class, new()
|
||||
{
|
||||
|
||||
Binary file not shown.
@@ -130,7 +130,7 @@ namespace OrmTest.UnitTest
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(SUBSTRING(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
base.Check(value, pars, "(SUBSTR(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"), new SugarParameter("@MethodConst1",0) , new SugarParameter("@MethodConst2",2),new SugarParameter("@Const3","a")
|
||||
}, "Substring error");
|
||||
}
|
||||
@@ -142,7 +142,7 @@ namespace OrmTest.UnitTest
|
||||
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, "(SUBSTRING(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
base.Check(value, pars, "(SUBSTR(@MethodConst0,1 + @MethodConst1,@MethodConst2) = @Const3 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@MethodConst0","aaaa"), new SugarParameter("@MethodConst1",0) , new SugarParameter("@MethodConst2",2),new SugarParameter("@Const3","a")
|
||||
}, "Substring error");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user