diff --git a/SqlServerTest/Demos/Filter.cs b/SqlServerTest/Demos/Filter.cs new file mode 100644 index 000000000..5cb306ac2 --- /dev/null +++ b/SqlServerTest/Demos/Filter.cs @@ -0,0 +1,21 @@ +using OrmTest.Demo; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demos +{ + namespace OrmTest.Demo + { + public class Filter : DemoBase + { + public static void Init() + { + var db = GetInstance(); + // db.QueryFilter.Add(eSqlFilterItem(){ }); + } + } + } +} diff --git a/SqlServerTest/Demos/Update.cs b/SqlServerTest/Demos/Update.cs index fb551f5d3..2f9a678a2 100644 --- a/SqlServerTest/Demos/Update.cs +++ b/SqlServerTest/Demos/Update.cs @@ -55,6 +55,9 @@ namespace OrmTest.Demo //Rename db.Updateable().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand(); //Update Student set Name='jack' Where Id=1 + + //Column is null no update + db.Updateable(updateObj).Where(true).ExecuteCommand(); } } } diff --git a/SqlServerTest/SqlServerTest.csproj b/SqlServerTest/SqlServerTest.csproj index 886c9b553..cb22b1728 100644 --- a/SqlServerTest/SqlServerTest.csproj +++ b/SqlServerTest/SqlServerTest.csproj @@ -48,6 +48,7 @@ + diff --git a/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs b/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs index 45f3092e9..1504305fb 100644 --- a/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs +++ b/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs @@ -199,7 +199,7 @@ namespace SqlSugar { get { - return new List() { "int32", "datetime", "decimal", "double", "byte", "string" }; + return new List() { "int32", "datetime", "decimal", "double", "byte" }; } } public virtual List StringThrow diff --git a/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs b/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs index b13cb3911..92b50dd5d 100644 --- a/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs +++ b/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs @@ -168,7 +168,8 @@ namespace SqlSugar { case CSharpDataType.@int: CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName); - method = isNullableType ? getConvertInt32 : getInt32; + if (bindProperyTypeName.IsContainsIn("int","int32","int64")) + method = isNullableType ? getConvertInt32 : getInt32; break; case CSharpDataType.@bool: if (bindProperyTypeName == "bool" || bindProperyTypeName == "boolean") @@ -220,7 +221,7 @@ namespace SqlSugar method = getConvertString; } if (method == null) - method = getOtherNull.MakeGenericMethod(bindPropertyType); + method =isNullableType? getOtherNull.MakeGenericMethod(bindPropertyType):getOther.MakeGenericMethod(bindPropertyType); generator.Emit(OpCodes.Call, method); if (method == getValueMethod) { diff --git a/SqlSugar/Abstract/FilterProvider/FilterProvider.cs b/SqlSugar/Abstract/FilterProvider/FilterProvider.cs new file mode 100644 index 000000000..57b00ef6d --- /dev/null +++ b/SqlSugar/Abstract/FilterProvider/FilterProvider.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public class QueryFilterProvider + { + internal SqlSugarClient Context{ get; set; } + private List _Filters { get; set; } + + public void Add(SqlFilterItem filter) + { + if (_Filters == null) + _Filters = new List(); + } + + public void Remove(string filterName) + { + if (_Filters == null) + _Filters = new List(); + _Filters.RemoveAll(it => it.FilterName == filterName); + } + + public List GeFilterList + { + get + { + if (_Filters == null) + _Filters = new List(); + return _Filters; + } + } + public void Clear() + { + _Filters = new List(); + } + } +} diff --git a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index b47efa97a..6ffa5c738 100644 --- a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -96,12 +96,12 @@ namespace SqlSugar return this; } - public IInsertable Where(bool isInsertNull, bool isOffIdentity = false) + public IInsertable Where(bool isNoInsertNull, bool isOffIdentity = false) { this.IsOffIdentity = IsOffIdentity; if (this.InsertBuilder.LambdaExpressions == null) this.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); - this.InsertBuilder.IsInsertNull = isInsertNull; + this.InsertBuilder.IsNoInsertNull = isNoInsertNull; return this; } #endregion @@ -139,7 +139,7 @@ namespace SqlSugar { if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List(); var paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType); - if (InsertBuilder.IsInsertNull && paramters.Value == null) + if (InsertBuilder.IsNoInsertNull && paramters.Value == null) { continue; } diff --git a/SqlSugar/Abstract/SqlBuilderProvider/Detail/InsertBuilder.cs b/SqlSugar/Abstract/SqlBuilderProvider/Detail/InsertBuilder.cs index 1c42c99bb..d1df732f2 100644 --- a/SqlSugar/Abstract/SqlBuilderProvider/Detail/InsertBuilder.cs +++ b/SqlSugar/Abstract/SqlBuilderProvider/Detail/InsertBuilder.cs @@ -21,7 +21,7 @@ namespace SqlSugar public List Parameters { get; set; } public string TableWithString { get; set; } public List DbColumnInfoList { get; set; } - public bool IsInsertNull { get; set; } + public bool IsNoInsertNull { get; set; } public bool IsReturnIdentity { get; set; } public EntityInfo EntityInfo { get; set; } @@ -100,7 +100,7 @@ namespace SqlSugar } public virtual string ToSqlString() { - if (IsInsertNull) + if (IsNoInsertNull) { DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList(); } diff --git a/SqlSugar/Abstract/SqlBuilderProvider/Detail/UpdateBuilder.cs b/SqlSugar/Abstract/SqlBuilderProvider/Detail/UpdateBuilder.cs index 80bba249d..f90992120 100644 --- a/SqlSugar/Abstract/SqlBuilderProvider/Detail/UpdateBuilder.cs +++ b/SqlSugar/Abstract/SqlBuilderProvider/Detail/UpdateBuilder.cs @@ -26,7 +26,7 @@ namespace SqlSugar public List DbColumnInfoList { get; set; } public List WhereValues { get; set; } public List> SetValues { get; set; } - public bool IsUpdateNull { get; set; } + public bool IsNoUpdateNull { get; set; } public List PrimaryKeys { get; set; } public bool IsOffIdentity { get; set; } @@ -126,6 +126,10 @@ namespace SqlSugar } public virtual string ToSqlString() { + if (IsNoUpdateNull) + { + DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList(); + } var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList(); var isSingle = groupList.Count() == 1; if (isSingle) diff --git a/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 1428a21a6..a2df6bf4f 100644 --- a/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -122,6 +122,9 @@ namespace SqlSugar public IUpdateable Where(bool isUpdateNull, bool IsOffIdentity = false) { UpdateBuilder.IsOffIdentity = IsOffIdentity; + if (this.UpdateBuilder.LambdaExpressions == null) + this.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); + this.UpdateBuilder.IsNoUpdateNull = isUpdateNull; return this; } public IUpdateable Where(Expression> expression) diff --git a/SqlSugar/Entities/SqlFilter.cs b/SqlSugar/Entities/SqlFilter.cs new file mode 100644 index 000000000..20b45541d --- /dev/null +++ b/SqlSugar/Entities/SqlFilter.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public class SqlFilterItem + { + public string FilterName { get; set; } + public Func GetFilterSql { get; set; } + public bool IsJoinQuery { get; set; } + } + + public class SqlFilterResult + { + public string Sql { get; set; } + public List Parameters { get; set; } + } +} diff --git a/SqlSugar/Interface/IUpdateable.cs b/SqlSugar/Interface/IUpdateable.cs index 9fe03543f..4697ece81 100644 --- a/SqlSugar/Interface/IUpdateable.cs +++ b/SqlSugar/Interface/IUpdateable.cs @@ -12,7 +12,7 @@ namespace SqlSugar int ExecuteCommand(); IUpdateable AS(string tableName); IUpdateable With(string lockString); - IUpdateable Where(bool isUpdateNull,bool IsOffIdentity = false); + IUpdateable Where(bool isNoUpdateNull,bool IsOffIdentity = false); IUpdateable Where(Expression> expression); IUpdateable UpdateColumns(Expression> columns); IUpdateable UpdateColumns(Func updateColumMethod); diff --git a/SqlSugar/SqlSugar.csproj b/SqlSugar/SqlSugar.csproj index 699bce81f..7ffffe491 100644 --- a/SqlSugar/SqlSugar.csproj +++ b/SqlSugar/SqlSugar.csproj @@ -60,10 +60,12 @@ + + diff --git a/SqlSugar/SqlSugarAccessory.cs b/SqlSugar/SqlSugarAccessory.cs index b8a3fad04..4b818051f 100644 --- a/SqlSugar/SqlSugarAccessory.cs +++ b/SqlSugar/SqlSugarAccessory.cs @@ -26,6 +26,7 @@ namespace SqlSugar protected ILambdaExpressions _LambdaExpressions; protected IRewritableMethods _RewritableMethods; protected IDbMaintenance _DbMaintenance; + protected QueryFilterProvider _QueryFilterProvider; #endregion #region Init mppingInfo diff --git a/SqlSugar/SqlSugarClient.cs b/SqlSugar/SqlSugarClient.cs index b900064fd..257393fc6 100644 --- a/SqlSugar/SqlSugarClient.cs +++ b/SqlSugar/SqlSugarClient.cs @@ -283,6 +283,25 @@ namespace SqlSugar } #endregion + #region Gobal Filter + public QueryFilterProvider QueryFilter + { + get + { + if (base._QueryFilterProvider == null) + { + base._QueryFilterProvider = new QueryFilterProvider(); + base._QueryFilterProvider.Context = this; + } + return _QueryFilterProvider; + } + set + { + base._QueryFilterProvider = value; + } + } + #endregion + #region Dispose OR Close public virtual void Close() {