Update core

This commit is contained in:
sunkaixuna
2021-11-13 19:26:06 +08:00
parent c5f6faa004
commit 185e89d7d6
15 changed files with 204 additions and 17 deletions

View File

@@ -191,7 +191,7 @@ namespace SqlSugar
dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) 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.Length != dc.Length && !UtilMethods.GetUnderType(ec.PropertyInfo).IsEnum() && UtilMethods.GetUnderType(ec.PropertyInfo).IsIn(UtilConstants.StringType)) ||
ec.IsNullable != dc.IsNullable || ec.IsNullable != dc.IsNullable ||
IsSamgeType(ec, dc)))).ToList(); IsNoSamgeType(ec, dc)))).ToList();
var renameColumns = entityColumns var renameColumns = entityColumns
.Where(it => !string.IsNullOrEmpty(it.OldDbColumnName)) .Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
.Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase))) .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)) if (!string.IsNullOrEmpty(ec.DataType))
{ {
@@ -365,6 +365,22 @@ namespace SqlSugar
{ {
return false; 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; return properyTypeName != dataType;
} }
private static string GetType(string name) private static string GetType(string name)

View File

@@ -119,6 +119,13 @@ namespace SqlSugar
BindClass(generator, result, columnInfo, ReaderKeys.First(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))); 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 else
{ {
if (this.ReaderKeys.Any(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))) if (this.ReaderKeys.Any(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)))

View File

@@ -21,7 +21,7 @@ namespace SqlSugar
public bool IsEnableDiffLogEvent { get; set; } public bool IsEnableDiffLogEvent { get; set; }
public DiffLogModel diffModel { get; set; } public DiffLogModel diffModel { get; set; }
public List<string> tempPrimaryKeys { get; set; } public List<string> tempPrimaryKeys { get; set; }
private Action RemoveCacheFunc { get; set; } internal Action RemoveCacheFunc { get; set; }
public EntityInfo EntityInfo public EntityInfo EntityInfo
{ {
get get
@@ -272,6 +272,13 @@ namespace SqlSugar
result.deleteobj = this; result.deleteobj = this;
return result; 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) public IDeleteable<T> RemoveDataCache(string likeString)
{ {
this.RemoveCacheFunc = () => this.RemoveCacheFunc = () =>

View File

@@ -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;
}
}
}

View File

@@ -17,7 +17,7 @@ namespace SqlSugar
} }
public EntityInfo GetEntityInfo(Type type) public EntityInfo GetEntityInfo(Type type)
{ {
string cacheKey = "GetEntityInfo" + type.FullName; string cacheKey = "GetEntityInfo"+ type.GetHashCode() + type.FullName;
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
() => () =>
{ {

View File

@@ -359,7 +359,7 @@ namespace SqlSugar
public IInsertable<T> EnableDiffLogEvent(object businessData = null) 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(); diffModel = new DiffLogModel();
this.IsEnableDiffLogEvent = true; this.IsEnableDiffLogEvent = true;
diffModel.BusinessData = businessData; diffModel.BusinessData = businessData;
@@ -769,6 +769,45 @@ namespace SqlSugar
} }
} }
private List<DiffLogTableInfo> GetDiffTable(string sql, long? identity) 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<SugarParameter> parameters = new List<SugarParameter>();
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>(); List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
@@ -786,9 +825,9 @@ namespace SqlSugar
var fielddName = item.DbColumnName; var fielddName = item.DbColumnName;
var filedObject = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null); var filedObject = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null);
var fieldValue = filedObject.ObjToString(); 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 else
{ {
@@ -814,7 +853,7 @@ namespace SqlSugar
DiffLogColumnInfo addItem = new DiffLogColumnInfo(); DiffLogColumnInfo addItem = new DiffLogColumnInfo();
addItem.Value = row[col.ColumnName]; addItem.Value = row[col.ColumnName];
addItem.ColumnName = 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); item.Columns.Add(addItem);
} }
result.Add(item); result.Add(item);
@@ -834,7 +873,6 @@ namespace SqlSugar
}).ToList(); }).ToList();
return new List<DiffLogTableInfo>() { diffTable }; return new List<DiffLogTableInfo>() { diffTable };
} }
} }
public IInsertable<T> CallEntityMethod(Expression<Action<T>> method) public IInsertable<T> CallEntityMethod(Expression<Action<T>> method)

View File

@@ -32,7 +32,7 @@ namespace SqlSugar
public bool IsAs { get; set; } public bool IsAs { get; set; }
public bool IsEnableDiffLogEvent { get; set; } public bool IsEnableDiffLogEvent { get; set; }
public DiffLogModel diffModel { get; set; } public DiffLogModel diffModel { get; set; }
private Action RemoveCacheFunc { get; set; } internal Action RemoveCacheFunc { get; set; }
private int SetColumnsIndex { get; set; } private int SetColumnsIndex { get; set; }
private List<DbColumnInfo> columns { get; set; } private List<DbColumnInfo> columns { get; set; }
#endregion #endregion
@@ -142,7 +142,7 @@ namespace SqlSugar
} }
public IUpdateable<T> EnableDiffLogEvent(object businessData = null) 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(); diffModel = new DiffLogModel();
this.IsEnableDiffLogEvent = true; this.IsEnableDiffLogEvent = true;
diffModel.BusinessData = businessData; diffModel.BusinessData = businessData;
@@ -291,6 +291,19 @@ namespace SqlSugar
#endregion #endregion
#region Update by expression #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) public IUpdateable<T> SetColumns(Expression<Func<T, T>> columns)
{ {
ThrowUpdateByObject(); ThrowUpdateByObject();
@@ -794,8 +807,16 @@ namespace SqlSugar
private List<DiffLogTableInfo> GetDiffTable(string sql, List<SugarParameter> parameters) private List<DiffLogTableInfo> GetDiffTable(string sql, List<SugarParameter> parameters)
{ {
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>(); List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline); DataTable dt = null;
var dt = this.Context.Queryable<T>().Where(whereSql).AddParameters(parameters).ToDataTable(); 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) if (dt.Rows != null && dt.Rows.Count > 0)
{ {
foreach (DataRow row in dt.Rows) foreach (DataRow row in dt.Rows)

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
@@ -10,6 +10,7 @@ namespace SqlSugar
public class SugarParameter : DbParameter public class SugarParameter : DbParameter
{ {
public bool IsRefCursor { get; set; } public bool IsRefCursor { get; set; }
public bool IsClob { get; set; }
public SugarParameter(string name, object value) public SugarParameter(string name, object value)
{ {
this.Value = value; this.Value = value;
@@ -128,6 +129,10 @@ namespace SqlSugar
{ {
this.DbType = System.Data.DbType.Time; this.DbType = System.Data.DbType.Time;
} }
else if (type?.Name=="Geometry")
{
this.DbType = System.Data.DbType.Object;
}
else if (type!=null&&type.IsEnum()) else if (type!=null&&type.IsEnum())
{ {
this.DbType = System.Data.DbType.Int64; this.DbType = System.Data.DbType.Int64;

View File

@@ -37,6 +37,7 @@ namespace SqlSugar
KeyValuePair<string, List<SugarParameter>> ToSql(); KeyValuePair<string, List<SugarParameter>> ToSql();
IDeleteable<T> EnableQueryFilter(); IDeleteable<T> EnableQueryFilter();
SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc); SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
LogicDeleteProvider<T> IsLogic();
void AddQueue(); void AddQueue();
} }
} }

View File

@@ -18,6 +18,7 @@ namespace SqlSugar
ITenant AsTenant(); ITenant AsTenant();
IUpdateable<T> AsUpdateable(List<T> updateObjs); IUpdateable<T> AsUpdateable(List<T> updateObjs);
IUpdateable<T> AsUpdateable(T updateObj); IUpdateable<T> AsUpdateable(T updateObj);
IUpdateable<T> AsUpdateable();
IUpdateable<T> AsUpdateable(T[] updateObjs); IUpdateable<T> AsUpdateable(T[] updateObjs);
int Count(Expression<Func<T, bool>> whereExpression); int Count(Expression<Func<T, bool>> whereExpression);
bool Delete(Expression<Func<T, bool>> whereExpression); bool Delete(Expression<Func<T, bool>> whereExpression);

View File

@@ -66,7 +66,7 @@ namespace SqlSugar
/// <param name="columns"></param> /// <param name="columns"></param>
/// <returns></returns> /// <returns></returns>
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns); 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); IUpdateable<T> UpdateColumnsIF(bool isUpdateColumns,Expression<Func<T, object>> columns);

View File

@@ -360,7 +360,9 @@ namespace SqlSugar
private List<DbColumnInfo> GetColumnInfosByTableName(string tableName) 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; var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false; this.Context.Ado.IsEnableLogEvent = false;
using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql)) using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql))

View File

@@ -16,7 +16,15 @@ namespace SqlSugar
{ {
if (base.UpdateObjs.Count() == 1) 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) else if (base.UpdateObjs.Count() == 0)
{ {

View File

@@ -74,6 +74,10 @@ namespace SqlSugar
{ {
return Context.Updateable<T>(updateObjs); return Context.Updateable<T>(updateObjs);
} }
public IUpdateable<T> AsUpdateable()
{
return Context.Updateable<T>();
}
public IDeleteable<T> AsDeleteable() public IDeleteable<T> AsDeleteable()
{ {
return Context.Deleteable<T>(); return Context.Deleteable<T>();

View File

@@ -619,6 +619,10 @@ namespace SqlSugar
if (db.Context == null) if (db.Context == null)
{ {
db.Context = new SqlSugarProvider(db.ConnectionConfig); db.Context = new SqlSugarProvider(db.ConnectionConfig);
if (_IsAllTran&&db.Context.Ado.Transaction==null)
{
db.Context.Ado.BeginTran();
}
} }
var intiAop=db.Context.Aop; var intiAop=db.Context.Aop;
if (db.Context.CurrentConnectionConfig.AopEvents == null) if (db.Context.CurrentConnectionConfig.AopEvents == null)