public WSFilter GetMainFilter(WSRequest Request, Expression _EntityExpression, int _level) { WSCombineFilter mainFilter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); WSFilter BaseFilter = GetBaseFilter(Request, _EntityExpression, _level); if (BaseFilter != null && BaseFilter.IsValid) { mainFilter.Save(BaseFilter); } WSFilter CustomFilter = GetCustomFilter(_EntityExpression, _level); if (CustomFilter != null && CustomFilter.IsValid) { mainFilter.Save(CustomFilter); } return mainFilter != null && mainFilter.IsValid ? mainFilter.Reduce() : null; }
public override WSFilter GetCustomFilter(Expression _EntityExpression, int _level) { WSCombineFilter mainFilter = !IsFiltrable ? null : new WSCombineFilter(CombineMode); if (mainFilter != null && _EntityExpression != null) { Expression member = null; if (_EntityExpression.Type == Source.ReturnType) { member = _EntityExpression; } else if (Parent != null) { WSTableParam paramExt = (WSTableParam)Parent.Source.GetXParam(Name); if (paramExt != null) { member = Expression.Property(_EntityExpression, paramExt.WSColumnRef.NAME); } } if (member != null && IsFiltrable) { if (Fields != null && Fields.Any()) { WSCombineFilter cFields = new WSCombineFilter(Fields.CombineMode); mainFilter.Save(cFields); foreach (WSSchema field in Fields) { cFields.Save(field.GetCustomFilter(member, _level)); } } if (Filters != null && Filters.Any()) { WSCombineFilter cFilters = new WSCombineFilter(Filters.CombineMode); mainFilter.Save(cFilters); foreach (WSSchema field in Filters) { cFilters.Save(field.GetCustomFilter(member, _level)); } } } if (IOBaseOptions != null && !IOBaseOptions.IsEmpty) { mainFilter.Save(IOBaseOptions.GetOptionFilter(Func, member, _level + 1)); } } return(mainFilter != null && mainFilter.IsValid ? mainFilter.Reduce() : mainFilter); }
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 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); }
public bool TrySetRecordValue(string colName, object newValue, WSDataContext DBContext, MetaFunctions CFunc, Func <Exception, bool> AddError = null) { bool done = false; if (!string.IsNullOrEmpty(colName)) { PropertyInfo pInfo = GetType().GetProperty(colName); if (pInfo != null) { object orgValue = pInfo.GetValue(this, null); newValue = fixSpecialCaseValue(colName, newValue, pInfo.PropertyType); try { if ( orgValue == newValue || (orgValue == null && newValue == null) || (orgValue != null && newValue != null && orgValue.ToString().Equals(newValue.ToString())) ) { return(true); } else { if (newValue == null) { if (pInfo.PropertyType.IsNullable()) { pInfo.SetPropertyValue(this, null); done = true; } } else { object newValueConverted = pInfo.PropertyType.IsAssignableFrom(newValue.GetType()) ? newValue : null; if (newValueConverted != null || pInfo.PropertyType.Read(newValue, out newValueConverted, null, null, pInfo.Name)) { try { bool IsAssiciation = false; PropertyInfo association = null; WSTableSource associationSrc = null; WSTableParam associationKey = null; PropertyInfo[] props = GetType().GetProperties(); foreach (PropertyInfo prop in props) { IEnumerable <CustomAttributeData> cAttrs = prop.CustomAttributesData(); foreach (CustomAttributeData cad in cAttrs) { CustomAttributeNamedArgument IsForeignKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("IsForeignKey")); if (IsForeignKey != null && IsForeignKey.TypedValue.Value != null && (true.ToString()).Equals(IsForeignKey.TypedValue.Value.ToString())) { CustomAttributeNamedArgument cana = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("ThisKey")); if (cana != null && pInfo.Name.Equals(cana.TypedValue.Value == null ? null : cana.TypedValue.Value.ToString())) { CustomAttributeNamedArgument canaKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("OtherKey")); if (canaKey != null && canaKey.TypedValue.Value != null) { IsAssiciation = true; association = prop; associationSrc = (WSTableSource)CFunc.GetSourceByType(association.PropertyType); associationKey = (WSTableParam)associationSrc.GetXParam(canaKey.TypedValue.Value.ToString()); } } } } } if (IsAssiciation) { string pName = pInfo.Name; ParameterExpression paramExp = Expression.Parameter(association.PropertyType, "x"); WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); filter.Save(new WSJValue(newValueConverted.ToString()).GetFieldFilter(CFunc, associationKey, paramExp, 0)); object subExpr = (Expression)filter.GetType().GetMethod("ToLambda").MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(filter, new object[] { paramExp }); MethodInfo mInfo = DBContext.GetType().GetMethod("GetTable", new Type[] { }); var tObj = mInfo.MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(DBContext, new object[] { }); Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(associationKey.WSColumnRef.NAME).ToString().Equals(newValueConverted.ToString()); var method = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public) .FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity)); WSDynamicEntity newAssociation = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func }); if (newAssociation != null) { association.SetPropertyValue(this, newAssociation); done = true; } } else { pInfo.SetPropertyValue(this, newValueConverted); done = true; } } catch (Exception e1) { AddError(e1); } } } } } catch (Exception e) { AddError(e); } if (!done) { pInfo.SetPropertyValue(this, orgValue); } } } return(done); }