mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 20:27:56 +08:00
Update core
This commit is contained in:
parent
c5f6faa004
commit
185e89d7d6
@ -191,7 +191,7 @@ namespace SqlSugar
|
||||
dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName)
|
||||
&& ((ec.Length != dc.Length && !UtilMethods.GetUnderType(ec.PropertyInfo).IsEnum() && UtilMethods.GetUnderType(ec.PropertyInfo).IsIn(UtilConstants.StringType)) ||
|
||||
ec.IsNullable != dc.IsNullable ||
|
||||
IsSamgeType(ec, dc)))).ToList();
|
||||
IsNoSamgeType(ec, dc)))).ToList();
|
||||
var renameColumns = entityColumns
|
||||
.Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
|
||||
.Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
@ -343,7 +343,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
|
||||
protected virtual bool IsNoSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ec.DataType))
|
||||
{
|
||||
@ -365,6 +365,22 @@ namespace SqlSugar
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (properyTypeName?.ToLower() == "varchar" && dataType?.ToLower() == "string")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (properyTypeName?.ToLower() == "number" && dataType?.ToLower() == "decimal")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (properyTypeName?.ToLower() == "int" && dataType?.ToLower() == "decimal"&&dc.Length==22&&dc.Scale==0&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (properyTypeName?.ToLower() == "date" && dataType?.ToLower() == "datetime")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return properyTypeName != dataType;
|
||||
}
|
||||
private static string GetType(string name)
|
||||
|
@ -119,6 +119,13 @@ namespace SqlSugar
|
||||
BindClass(generator, result, columnInfo, ReaderKeys.First(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
}
|
||||
else if (!isGemo && columnInfo.IsJson && columnInfo.PropertyInfo.PropertyType != UtilConstants.StringType)
|
||||
{ //json is struct
|
||||
if (this.ReaderKeys.Any(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
BindClass(generator, result, columnInfo, ReaderKeys.First(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.ReaderKeys.Any(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
|
@ -21,7 +21,7 @@ namespace SqlSugar
|
||||
public bool IsEnableDiffLogEvent { get; set; }
|
||||
public DiffLogModel diffModel { get; set; }
|
||||
public List<string> tempPrimaryKeys { get; set; }
|
||||
private Action RemoveCacheFunc { get; set; }
|
||||
internal Action RemoveCacheFunc { get; set; }
|
||||
public EntityInfo EntityInfo
|
||||
{
|
||||
get
|
||||
@ -272,6 +272,13 @@ namespace SqlSugar
|
||||
result.deleteobj = this;
|
||||
return result;
|
||||
}
|
||||
public LogicDeleteProvider<T> IsLogic()
|
||||
{
|
||||
LogicDeleteProvider<T> result = new LogicDeleteProvider<T>();
|
||||
result.DeleteBuilder = this.DeleteBuilder;
|
||||
result.Deleteable = this;
|
||||
return result;
|
||||
}
|
||||
public IDeleteable<T> RemoveDataCache(string likeString)
|
||||
{
|
||||
this.RemoveCacheFunc = () =>
|
||||
|
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class LogicDeleteProvider<T> where T : class, new()
|
||||
{
|
||||
public DeleteableProvider<T> Deleteable { get; set; }
|
||||
public DeleteBuilder DeleteBuilder { get; set; }
|
||||
|
||||
public int ExecuteCommand(string LogicFieldName = null)
|
||||
{
|
||||
ISqlSugarClient db;
|
||||
List<SugarParameter> pars;
|
||||
string where;
|
||||
LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars);
|
||||
var updateable = db.Updateable<T>().SetColumns(LogicFieldName, "@IsDeleted");
|
||||
if (pars != null)
|
||||
updateable.UpdateBuilder.Parameters.AddRange(pars);
|
||||
Convert(updateable as UpdateableProvider<T>);
|
||||
var result = updateable.Where(where, new { IsDeleted = true }).ExecuteCommand();
|
||||
return result;
|
||||
}
|
||||
public async Task<int> ExecuteCommandAsync(string LogicFieldName = null)
|
||||
{
|
||||
ISqlSugarClient db;
|
||||
List<SugarParameter> pars;
|
||||
string where;
|
||||
LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars);
|
||||
var updateable = db.Updateable<T>().SetColumns(LogicFieldName, "@IsDeleted");
|
||||
if (pars != null)
|
||||
updateable.UpdateBuilder.Parameters.AddRange(pars);
|
||||
Convert(updateable as UpdateableProvider<T>);
|
||||
var result =await updateable.Where(where, new { IsDeleted = true }).ExecuteCommandAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void Convert(UpdateableProvider<T> updateable)
|
||||
{
|
||||
updateable.IsEnableDiffLogEvent = Deleteable.IsEnableDiffLogEvent;
|
||||
updateable.diffModel = Deleteable.diffModel;
|
||||
updateable.UpdateBuilder.TableWithString = DeleteBuilder.TableWithString;
|
||||
updateable.RemoveCacheFunc = Deleteable.RemoveCacheFunc;
|
||||
}
|
||||
|
||||
private string _ExecuteCommand(string LogicFieldName, out ISqlSugarClient db, out string where, out List<SugarParameter> pars)
|
||||
{
|
||||
var entityInfo = Deleteable.EntityInfo;
|
||||
db = Deleteable.Context;
|
||||
where = DeleteBuilder.GetWhereString.Substring(5);
|
||||
pars = DeleteBuilder.Parameters;
|
||||
if (LogicFieldName.IsNullOrEmpty())
|
||||
{
|
||||
var column = entityInfo.Columns.FirstOrDefault(it =>
|
||||
it.PropertyName.EqualCase("isdelete") ||
|
||||
it.PropertyName.EqualCase("isdeleted") ||
|
||||
it.DbColumnName.EqualCase("isdelete") ||
|
||||
it.DbColumnName.EqualCase("isdeleted"));
|
||||
if (column != null)
|
||||
{
|
||||
LogicFieldName = column.DbColumnName;
|
||||
}
|
||||
}
|
||||
Check.Exception(LogicFieldName == null, ErrorMessage.GetThrowMessage(
|
||||
$"{entityInfo.EntityName} is not isdelete or isdeleted"
|
||||
, $"{entityInfo.EntityName} 没有IsDelete或者IsDeleted 的属性, 你也可以用 IsLogic().ExecuteCommand(\"列名\")"));
|
||||
return LogicFieldName;
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ namespace SqlSugar
|
||||
}
|
||||
public EntityInfo GetEntityInfo(Type type)
|
||||
{
|
||||
string cacheKey = "GetEntityInfo" + type.FullName;
|
||||
string cacheKey = "GetEntityInfo"+ type.GetHashCode() + type.FullName;
|
||||
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
||||
() =>
|
||||
{
|
||||
|
@ -359,7 +359,7 @@ namespace SqlSugar
|
||||
|
||||
public IInsertable<T> EnableDiffLogEvent(object businessData = null)
|
||||
{
|
||||
Check.Exception(this.InsertObjs.HasValue() && this.InsertObjs.Count() > 1, "DiffLog does not support batch operations");
|
||||
//Check.Exception(this.InsertObjs.HasValue() && this.InsertObjs.Count() > 1, "DiffLog does not support batch operations");
|
||||
diffModel = new DiffLogModel();
|
||||
this.IsEnableDiffLogEvent = true;
|
||||
diffModel.BusinessData = businessData;
|
||||
@ -769,6 +769,45 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
private List<DiffLogTableInfo> GetDiffTable(string sql, long? identity)
|
||||
{
|
||||
|
||||
if (GetIdentityKeys().HasValue() && this.InsertObjs.Count() > 1)
|
||||
{
|
||||
return GetDiffTableByEntity();
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetDiffTableBySql(identity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<DiffLogTableInfo> GetDiffTableByEntity()
|
||||
{
|
||||
List<SugarParameter> parameters = new List<SugarParameter>();
|
||||
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
|
||||
var dt2 = this.Context.Utilities.ListToDataTable<T>(this.InsertObjs.ToList());
|
||||
foreach (DataRow row in dt2.Rows)
|
||||
{
|
||||
DiffLogTableInfo item = new DiffLogTableInfo();
|
||||
item.TableDescription = this.EntityInfo.TableDescription;
|
||||
item.TableName = this.EntityInfo.DbTableName;
|
||||
item.Columns = new List<DiffLogColumnInfo>();
|
||||
foreach (DataColumn col in dt2.Columns)
|
||||
{
|
||||
|
||||
DiffLogColumnInfo addItem = new DiffLogColumnInfo();
|
||||
addItem.Value = row[col.ColumnName];
|
||||
addItem.ColumnName = col.ColumnName;
|
||||
addItem.ColumnDescription = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).FirstOrDefault(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase))?.ColumnDescription;
|
||||
item.Columns.Add(addItem);
|
||||
}
|
||||
result.Add(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<DiffLogTableInfo> GetDiffTableBySql(long? identity)
|
||||
{
|
||||
List<SugarParameter> parameters = new List<SugarParameter>();
|
||||
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
|
||||
@ -786,9 +825,9 @@ namespace SqlSugar
|
||||
var fielddName = item.DbColumnName;
|
||||
var filedObject = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null);
|
||||
var fieldValue = filedObject.ObjToString();
|
||||
if (filedObject != null && filedObject.GetType() != typeof(string)&&this.Context.CurrentConnectionConfig.DbType==DbType.PostgreSQL)
|
||||
if (filedObject != null && filedObject.GetType() != typeof(string) && this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL)
|
||||
{
|
||||
cons.Add(new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = fielddName, FieldValue = fieldValue,FieldValueConvertFunc= it => UtilMethods.ChangeType2(it, filedObject.GetType()) });
|
||||
cons.Add(new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = fielddName, FieldValue = fieldValue, FieldValueConvertFunc = it => UtilMethods.ChangeType2(it, filedObject.GetType()) });
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -814,7 +853,7 @@ namespace SqlSugar
|
||||
DiffLogColumnInfo addItem = new DiffLogColumnInfo();
|
||||
addItem.Value = row[col.ColumnName];
|
||||
addItem.ColumnName = col.ColumnName;
|
||||
addItem.ColumnDescription = this.EntityInfo.Columns.Where(it=>it.DbColumnName!=null).First(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription;
|
||||
addItem.ColumnDescription = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).First(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription;
|
||||
item.Columns.Add(addItem);
|
||||
}
|
||||
result.Add(item);
|
||||
@ -834,7 +873,6 @@ namespace SqlSugar
|
||||
}).ToList();
|
||||
return new List<DiffLogTableInfo>() { diffTable };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IInsertable<T> CallEntityMethod(Expression<Action<T>> method)
|
||||
|
@ -32,7 +32,7 @@ namespace SqlSugar
|
||||
public bool IsAs { get; set; }
|
||||
public bool IsEnableDiffLogEvent { get; set; }
|
||||
public DiffLogModel diffModel { get; set; }
|
||||
private Action RemoveCacheFunc { get; set; }
|
||||
internal Action RemoveCacheFunc { get; set; }
|
||||
private int SetColumnsIndex { get; set; }
|
||||
private List<DbColumnInfo> columns { get; set; }
|
||||
#endregion
|
||||
@ -142,7 +142,7 @@ namespace SqlSugar
|
||||
}
|
||||
public IUpdateable<T> EnableDiffLogEvent(object businessData = null)
|
||||
{
|
||||
Check.Exception(this.UpdateObjs.HasValue() && this.UpdateObjs.Count() > 1, "DiffLog does not support batch operations");
|
||||
//Check.Exception(this.UpdateObjs.HasValue() && this.UpdateObjs.Count() > 1, "DiffLog does not support batch operations");
|
||||
diffModel = new DiffLogModel();
|
||||
this.IsEnableDiffLogEvent = true;
|
||||
diffModel.BusinessData = businessData;
|
||||
@ -291,6 +291,19 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Update by expression
|
||||
public IUpdateable<T> SetColumns(string fieldName, object fieldValue)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
var columnInfo = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(fieldName));
|
||||
if (columnInfo != null)
|
||||
{
|
||||
fieldName = columnInfo.DbColumnName;
|
||||
}
|
||||
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(fieldName, fieldValue+""));
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
|
||||
AppendSets();
|
||||
return this;
|
||||
}
|
||||
public IUpdateable<T> SetColumns(Expression<Func<T, T>> columns)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
@ -794,8 +807,16 @@ namespace SqlSugar
|
||||
private List<DiffLogTableInfo> GetDiffTable(string sql, List<SugarParameter> parameters)
|
||||
{
|
||||
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
|
||||
var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline);
|
||||
var dt = this.Context.Queryable<T>().Where(whereSql).AddParameters(parameters).ToDataTable();
|
||||
DataTable dt = null;
|
||||
if (this.UpdateParameterIsNull)
|
||||
{
|
||||
var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline);
|
||||
dt = this.Context.Queryable<T>().Where(whereSql).AddParameters(parameters).ToDataTable();
|
||||
}
|
||||
else
|
||||
{
|
||||
dt=this.Context.Queryable<T>().WhereClassByPrimaryKey(this.UpdateObjs.ToList()).ToDataTable();
|
||||
}
|
||||
if (dt.Rows != null && dt.Rows.Count > 0)
|
||||
{
|
||||
foreach (DataRow row in dt.Rows)
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@ -10,6 +10,7 @@ namespace SqlSugar
|
||||
public class SugarParameter : DbParameter
|
||||
{
|
||||
public bool IsRefCursor { get; set; }
|
||||
public bool IsClob { get; set; }
|
||||
public SugarParameter(string name, object value)
|
||||
{
|
||||
this.Value = value;
|
||||
@ -128,6 +129,10 @@ namespace SqlSugar
|
||||
{
|
||||
this.DbType = System.Data.DbType.Time;
|
||||
}
|
||||
else if (type?.Name=="Geometry")
|
||||
{
|
||||
this.DbType = System.Data.DbType.Object;
|
||||
}
|
||||
else if (type!=null&&type.IsEnum())
|
||||
{
|
||||
this.DbType = System.Data.DbType.Int64;
|
||||
|
@ -37,6 +37,7 @@ namespace SqlSugar
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
IDeleteable<T> EnableQueryFilter();
|
||||
SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
|
||||
LogicDeleteProvider<T> IsLogic();
|
||||
void AddQueue();
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ namespace SqlSugar
|
||||
ITenant AsTenant();
|
||||
IUpdateable<T> AsUpdateable(List<T> updateObjs);
|
||||
IUpdateable<T> AsUpdateable(T updateObj);
|
||||
IUpdateable<T> AsUpdateable();
|
||||
IUpdateable<T> AsUpdateable(T[] updateObjs);
|
||||
int Count(Expression<Func<T, bool>> whereExpression);
|
||||
bool Delete(Expression<Func<T, bool>> whereExpression);
|
||||
|
@ -66,7 +66,7 @@ namespace SqlSugar
|
||||
/// <param name="columns"></param>
|
||||
/// <returns></returns>
|
||||
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns);
|
||||
|
||||
IUpdateable<T> SetColumns(string fieldName,object fieldValue);
|
||||
|
||||
|
||||
IUpdateable<T> UpdateColumnsIF(bool isUpdateColumns,Expression<Func<T, object>> columns);
|
||||
|
@ -360,7 +360,9 @@ namespace SqlSugar
|
||||
|
||||
private List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
|
||||
{
|
||||
string sql = "select * from " +SqlBuilder.GetTranslationTableName(tableName) + " WHERE 1=2 ";
|
||||
string sql = "select * /* " + Guid.NewGuid() + " */ from " + SqlBuilder.GetTranslationTableName(tableName) + " WHERE 1=2 ";
|
||||
this.Context.Utilities.RemoveCache<List<DbColumnInfo>>("DbMaintenanceProvider.GetFieldComment."+tableName);
|
||||
this.Context.Utilities.RemoveCache<List<string>>("DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower());
|
||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||
this.Context.Ado.IsEnableLogEvent = false;
|
||||
using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql))
|
||||
|
@ -16,7 +16,15 @@ namespace SqlSugar
|
||||
{
|
||||
if (base.UpdateObjs.Count() == 1)
|
||||
{
|
||||
return base.ExecuteCommand();
|
||||
var resultl= base.ExecuteCommand();
|
||||
if (resultl == -1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return resultl;
|
||||
}
|
||||
}
|
||||
else if (base.UpdateObjs.Count() == 0)
|
||||
{
|
||||
|
@ -74,6 +74,10 @@ namespace SqlSugar
|
||||
{
|
||||
return Context.Updateable<T>(updateObjs);
|
||||
}
|
||||
public IUpdateable<T> AsUpdateable()
|
||||
{
|
||||
return Context.Updateable<T>();
|
||||
}
|
||||
public IDeleteable<T> AsDeleteable()
|
||||
{
|
||||
return Context.Deleteable<T>();
|
||||
|
@ -619,6 +619,10 @@ namespace SqlSugar
|
||||
if (db.Context == null)
|
||||
{
|
||||
db.Context = new SqlSugarProvider(db.ConnectionConfig);
|
||||
if (_IsAllTran&&db.Context.Ado.Transaction==null)
|
||||
{
|
||||
db.Context.Ado.BeginTran();
|
||||
}
|
||||
}
|
||||
var intiAop=db.Context.Aop;
|
||||
if (db.Context.CurrentConnectionConfig.AopEvents == null)
|
||||
|
Loading…
Reference in New Issue
Block a user