internal WSFilter Reduce() { WSCombineFilter cFilter = new WSCombineFilter(Mode, Negate); if (!IsEmpty) { foreach (WSFilter child in this) { WSFilter reduced = (child is WSCombineFilter) ? ((WSCombineFilter)child).Reduce() : child; if (reduced != null) { if (reduced is WSCombineFilter && ((WSCombineFilter)reduced).Any() && ((WSCombineFilter)reduced).Mode == Mode && !((WSCombineFilter)reduced).Negate) { cFilter.SaveRange(((WSCombineFilter)reduced)); } else { cFilter.Save(reduced); } } } } return(!cFilter.Any() ? null : (cFilter.Count == 1 && !cFilter.Negate) ? cFilter[0] : cFilter); }
public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null) { try { WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); filter.SaveRange(Value.Select(x => x.GetFieldFilter(CFunc, param, parent, level, state, negate)).ToList()); /*DONT FORVARD 'negate' parameter to avoid complicatency*/ return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } return(null); }
public override WSFilter GetOptionFilter(MetaFunctions CFunc, Expression parent, int level, string state = null, bool?negate = null) { try { WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.OrElse, negate); filter.SaveRange(Value.Select(x => x.GetOptionFilter(CFunc, parent, level, state, null)).ToList()); return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } return(null); }
public override WSFilter GetOptionFilter(MetaFunctions CFunc, Expression parent, int level, string state = null, bool?negate = null) { WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); try { if (Value != null) { if (WSConstants.ALIACES.NOT.Match(Key)) { return(Value.GetOptionFilter(CFunc, parent, level, state, true)); } else if (WSConstants.ALIACES.ANY.Match(Key)) { return(Value.GetOptionFilter(CFunc, parent, level, Key, true)); } else { if (Value is WSJValue) { filter.Save(((WSJValue)Value).GetOptionFilter(CFunc, parent, level, state, negate)); } else { WSTableSource PSource = (WSTableSource)CFunc.GetSourceByType(parent.Type.GetEntityType()); WSTableParam subParam = PSource == null ? null : (WSTableParam)PSource.GetXParam(Key); //TODO@ANDVO:2016-11-15: implement deep filtering } return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); } } } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } return(null); }
public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null) { try { ///************************************************** //* if(filter array contains any conditional 'IOJFilter' //* like : "[{option_name1:value1},{option_name2:value2}]" (example:"[{max:123},{min:123}]") , - then use 'CombineMode.AndAlso' //* else //* (if all values in the filter array are simple type values) //* like : "{value1,value2,value3,...}" ,- use 'CombineMode.Or' //* ***********************************************/ WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.OrElse, negate); foreach (WSJson j in Value) { WSFilter jFilter = j.GetFieldFilter(CFunc, param, parent, level, state, negate); filter.Save(jFilter); } return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } return(null); }
public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null) { Expression member = Expression.Property(parent, param.WSColumnRef.NAME); WSCombineFilter filter = new WSCombineFilter(); if (param != null && param.isValid) { try { if (WSConstants.ALIACES.EXIST.Match(Value) || WSConstants.ALIACES.EMPTY.Match(Value) || WSConstants.ALIACES.IS_OWN.Match(Value)) { state = Value; } #region Read WSEntity (Ready) if (!string.IsNullOrEmpty(state) && (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) || param.DataType.IsCollectionOf <WSEntity>())) { if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) && WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(state))) { filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(state))) { Value = null }); } else if (param.DataType.IsCollectionOf <WSEntity>() && WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.Any(x => x.Match(state))) { filter.Save(new WSEntityListFFilter(param, member, WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(state))) { Value = null }); } } #endregion #region Read primitive else { dynamic dVal = null; #region Numeric (Ready) if (param.DataType.IsNumeric()) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSNumericFFilter(param, member, WSNumericFFilter.OPERATIONS.NotEqual) { Value = null }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSNumericFFilter(param, member, null) { Value = null }); } #endregion #endregion #region If Value (Ready) else { if (param.DataType.Read(Value, out dVal)) { Type dType = dVal == null ? param.DataType : dVal.GetType(); WSOperation operation = WSNumericFFilter.OPERATIONS.GetOperation(state); if (operation.Match("max") && dType.IsCollection()) { dVal = ((List <dynamic>)dVal).Max(x => x); } else if (operation.Match("min") && dType.IsCollection()) { dVal = ((List <dynamic>)dVal).Min(x => x); } filter.Save(new WSNumericFFilter(param, member, operation) { Value = dVal }); } } #endregion } #endregion #region bool (Ready) else if (typeof(bool?).IsAssignableFrom(param.DataType)) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSBoolFFilter(param, member, WSBoolFFilter.OPERATIONS.NotEqual) { Value = null }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSBoolFFilter(param, member, null) { Value = null }); } #endregion #endregion #region If Value (Ready) else { if (param.DataType.Read(Value, out dVal)) { filter.Save(new WSBoolFFilter(param, member, WSBoolFFilter.OPERATIONS.GetOperation(state)) { Value = dVal }); } } #endregion } #endregion #region string (Ready) else if (typeof(string).IsAssignableFrom(param.DataType)) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value)) { if (param.DataType.IsNullable()) { filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.NotEqual) { Value = null }); } filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.NotEqual) { Value = string.Empty }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value)) { WSCombineFilter cFilter = new WSCombineFilter(WSCombineFilter.SQLMode.Or);; if (param.DataType.IsNullable()) { cFilter.Save(new WSStringFFilter(param, member, null) { Value = null }); } cFilter.Save(new WSStringFFilter(param, member, null) { Value = string.Empty }); filter.Save(cFilter); } #endregion #endregion #region If Value (Ready) else if (param.DataType.IsNullable() || !string.IsNullOrEmpty(Value)) { if (param.DataType.Read(Value, out dVal)) { filter.Save(new WSStringFFilter(param, member, WSStringFFilter.OPERATIONS.GetOperation(state)) { Value = dVal }); } } #endregion } #endregion #region Guid (Ready) else if (typeof(Guid?).IsAssignableFrom(param.DataType)) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSGuidFFilter(param, member, WSGuidFFilter.OPERATIONS.NotEqual) { Value = null }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSGuidFFilter(param, member, null) { Value = null }); } #endregion #endregion #region If Value (Ready) else { if (param.DataType.Read(Value, out dVal, null, WSConstants.GUID_LIST_SEPARATORS)) { filter.Save(new WSGuidFFilter(param, member, WSGuidFFilter.OPERATIONS.GetOperation(state)) { Value = dVal }); } } #endregion } #endregion #region DateTime (Ready) else if (typeof(DateTime?).IsAssignableFrom(param.DataType)) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSDateFFilter(param, member, WSDateFFilter.OPERATIONS.NotEqual) { Value = null }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSDateFFilter(param, member, null) { Value = null }); } #endregion #endregion #region If Value (Ready) else { WSOperation operation = Value.IsTrue() ? WSDateFFilter.OPERATIONS.LessOrEqual : (Value.IsFalse() && !(param.DataType.IsNullable())) ? WSDateFFilter.OPERATIONS.GreaterThanOrEqual : WSDateFFilter.OPERATIONS.GetOperation(state); WSDateFFilter mainFilter = null; if (operation != null && operation.Match(WSDateFFilter.OPERATIONS.WeekDayEqual)) { mainFilter = new WSDateFFilter(param, member, operation) { Value = Value }; } else if (param.DataType.Read(Value, out dVal, new char[] { }, WSConstants.DATE_LIST_SEPARATORS, param.WSColumnRef.NAME)) { Type dType = dVal == null ? param.DataType : dVal.GetType(); if (dType.IsCollection() && operation != null) { dVal = operation.Match(WSDateFFilter.OPERATIONS.LessOrEqual) ? ((List <dynamic>)dVal).Max(x => x) : operation.Match(WSDateFFilter.OPERATIONS.GreaterThanOrEqual) ? ((List <dynamic>)dVal).Min(x => x) : dVal; } mainFilter = new WSDateFFilter(param, member, operation) { Value = dVal }; } if (param.DataType.IsNullable()) { WSCombineFilter cf = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); if (mainFilter.Value != null) { cf.Save(new WSDateFFilter(param, member, WSDateFFilter.OPERATIONS.NotEqual) { Value = null }); } cf.Save(mainFilter); filter.Save(cf); } else { filter.Save(mainFilter); } } #endregion } #endregion #region TimeSpan (Ready) else if (typeof(TimeSpan?).IsAssignableFrom(param.DataType)) { #region SPECIAL CASES #region If Exists (Ready) if (WSConstants.ALIACES.EXIST.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSTimeFFilter(param, member, WSTimeFFilter.OPERATIONS.NotEqual) { Value = null }); } #endregion #region If Empty (Ready) else if (WSConstants.ALIACES.EMPTY.Match(Value) && param.DataType.IsNullable()) { filter.Save(new WSTimeFFilter(param, member, null) { Value = null }); } #endregion #endregion #region If Value (Ready) else { if (param.DataType.Read(Value, out dVal, new char[] { }, WSConstants.DATE_LIST_SEPARATORS)) { WSOperation operation = WSTimeFFilter.OPERATIONS.GetOperation(state); Type dType = dVal == null ? param.DataType : dVal.GetType(); if (dType.IsCollection() && operation != null) { dVal = operation.Match("max") ? ((List <dynamic>)dVal).Max(x => x) : operation.Match("min") ? ((List <dynamic>)dVal).Min(x => x) : dVal; } if (param.DataType.IsNullable()) { WSCombineFilter cf = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); if (Value != null) { cf.Save(new WSTimeFFilter(param, member, WSTimeFFilter.OPERATIONS.NotEqual) { Value = null }); } cf.Save(new WSTimeFFilter(param, member, operation) { Value = dVal }); filter.Save(cf); } else { filter.Save(new WSTimeFFilter(param, member, operation) { Value = dVal }); } } } #endregion } #endregion } #endregion } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); if (CFunc != null) { CFunc.RegError(GetType(), e, ref status); } } } return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); }
public override WSFilter GetFieldFilter(MetaFunctions CFunc, WSTableParam param, Expression parent, int level, string state = null, bool?negate = null) { WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); try { if (param != null && param.isValid && (param.DataType.IsNullable() || Value != null)) { if (param.DataType.IsSimple()) { bool localNegate = WSConstants.ALIACES.NOT.Match(state); negate = negate == null ? localNegate : localNegate != negate; WSFilter pFilter = Value.GetFieldFilter(CFunc, param, parent, level, Key, negate); filter.Save(pFilter); } else if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity)) || param.DataType.IsCollectionOf <WSEntity>()) { if (WSConstants.ALIACES.NOT.Match(Key)) { return(Value.GetFieldFilter(CFunc, param, parent, level, state, true)); } else if (WSConstants.ALIACES.ANY.Match(Key)) { return(Value.GetFieldFilter(CFunc, param, parent, level, Key, true)); } else { WSTableSource PSource = (WSTableSource)CFunc.GetSourceByType/*<WSTableSource>*/ (param.DataType.GetEntityType()); WSTableParam subParam = PSource == null ? null : (WSTableParam)PSource.GetXParam(Key); Expression member = Expression.Property(parent, param.WSColumnRef.NAME); if (subParam == null && WSConstants.SPECIAL_CASES.Any(c => c.Match(Key))) { if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity))) { return(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(Key))) { Value = null }); } else if (param.DataType.IsCollectionOf <WSEntity>()) { return(new WSEntityListFFilter(param, member, WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.FirstOrDefault(x => x.Match(Key))) { Value = null }); } } else { WSFilter subFilter = null; if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity))) { filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.NotEqual) { Value = null }); subFilter = Value.GetFieldFilter(CFunc, subParam, member, level, Key); if (subFilter != null && subFilter.IsValid) { filter.Save(new WSEntityFFilter(param, member, WSEntityFFilter.OPERATIONS.Filter) { Value = subFilter }); } } else if (param.DataType.IsCollectionOf <WSEntity>()) { level++; Type elemType = param.DataType.GetEntityType(); ParameterExpression id = Expression.Parameter(elemType, level.ToHex()); subFilter = Value.GetFieldFilter(CFunc, subParam, id, level, Key, negate); if (subFilter != null && subFilter.IsValid) { dynamic subExpr = subFilter.GetType().GetMethod("ToLambda").MakeGenericMethod(new Type[] { elemType }).Invoke(subFilter, new object[] { id }); filter.Save(new WSEntityListFFilter(subParam, member, WSEntityListFFilter.OPERATIONS.Any) { Value = (subExpr == null) ? true : subExpr }); } } } } } } } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } return(filter.Any() ? (filter.Count == 1 && !filter.Negate) ? filter.FirstOrDefault() : filter : null); }