public override Expression SortTable <TEntity>(MetaFunctions CFunc, WSDataContext dc, List <PropertyInfo> parents, Expression expression, ref WSStatus iostatus) { try { if (dc != null) { parents = parents != null ? parents : new List <PropertyInfo>(); Type srcType = parents.Any() ? parents.LastOrDefault().PropertyType.IsCollection() ? parents.LastOrDefault().PropertyType.GetEntityType() : parents.LastOrDefault().PropertyType : typeof(TEntity); ITable initSource = srcType == null ? null : dc.GetTable(typeof(TEntity)); ITable source = srcType == null ? null : dc.GetTable(srcType); WSTableSource schema = srcType == null ? null : (WSTableSource)CFunc.GetSourceByType(srcType); if (schema != null) { WSParam param = schema.GetXParam(Key); if (param != null && param is WSTableParam) { WSTableParam tParam = (WSTableParam)param; PropertyInfo property = srcType.GetProperties().FirstOrDefault(p => tParam.WSColumnRef.NAME.Equals(p.Name)); if (property == null) { iostatus.AddNote(string.Format("No PropertyInfo found for : [{0}]", tParam.WSColumnRef.NAME)); } else { parents.Add(property); if (tParam.DataType.IsSimple() && tParam.IsSortable) { bool IsDesc = false; if (Value is WSJValue) { IsDesc = ((WSJValue)Value).Value.ToLower().Equals("desc"); } expression = SortPrimitiveType <TEntity>(initSource, source, param, IsDesc, parents, expression, ref iostatus); } else if (tParam.DataType.IsSameOrSubclassOf(typeof(WSEntity)) || tParam.DataType.IsCollectionOf <WSEntity>()) { expression = Value.SortTable <TEntity>(CFunc, dc, parents, expression, ref iostatus); } } } } } } catch (Exception e) { CFunc.RegError(GetType(), e, ref iostatus); } return(expression); }
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 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 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); }
private List <WSMemberSchema> readFieldSchema(WSJProperty json, out bool replace)// json => WSJValue || WSJProperty || WSJObject { replace = true; List <WSMemberSchema> fSchema = new List <WSMemberSchema>(); try { if (json != null && Source != null) { if (!string.IsNullOrEmpty(json.Key)) { WSTableParam param = (WSTableParam)Source.GetXParam(json.Key); if (param != null && param.isValid) { replace = true; if (param.DataType.IsSimple() || param.DataType.IsSimpleCollection()) { fSchema.Add(new WSPrimitiveFieldSchema(Func, param, json, this)); } else if (json.IsValid) { WSTableSource source = (WSTableSource)Func.GetSourceByType(param.DataType.GetEntityType()); if (source != null) { if (param.Match(json.Key)) { if (param.DataType.IsSameOrSubclassOf(typeof(WSEntity))) { fSchema.Add(new WSEntitySchema(source, json, Func, this)); } else if (param.DataType.IsCollectionOf <WSEntity>()) { fSchema.Add(new WSEntityListSchema(param, json, Func, this)); } } } } } else { #region SET '*' [ALL PRIMITIVE FIELDS] Filter if (WSConstants.ALIACES.ALL_PRIMITIVE_FIELDS.Match(json.Key)) { replace = false; IEnumerable <WSTableParam> DBPrimitiveParams = Source.DBPrimitiveParams.Any() ? Source.DBPrimitiveParams.Where(x => Func.IsAccessible(x.READ_ACCESS_MODE.ACCESS_LEVEL)) : new List <WSTableParam>(); IEnumerable <WSJValue> all_primitive_params = DBPrimitiveParams.Any() ? DBPrimitiveParams.Select(x => new WSJValue(x.DISPLAY_NAME)) : new List <WSJValue>(); foreach (WSJValue _item in all_primitive_params) { WSMemberSchema schema = Source.BaseSchema.Fields.FirstOrDefault(x => x.Name.Equals(_item.Value)); if (schema != null) { fSchema.Add(schema); } } } #endregion } } } } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); Func.RegError(GetType(), e, ref status); } return(fSchema); }