Ejemplo n.º 1
0
 public ReduceContext(string path, ISource source, IDataEntityProperty property, IEnumerable <IDataEntity> ancestors)
 {
     this.Path      = path;
     this.Source    = source;
     this.Property  = property;
     this.Ancestors = ancestors ?? System.Linq.Enumerable.Empty <IDataEntity>();
 }
Ejemplo n.º 2
0
 IDataEntityProperty[] IKeyedCollectionBase <string, IDataEntityProperty> .ToArray()
 {
     IDataEntityProperty[] propertyArray = new IDataEntityProperty[base.Count];
     for (int i = 0; i < base.Count; i++)
     {
         propertyArray[i] = base[i];
     }
     return(propertyArray);
 }
Ejemplo n.º 3
0
        public bool OnUpdate(IDataMutateContextBase context, IDataEntityProperty property, out object value)
        {
            if (_updates.TryGetValue(property.Name, out var factory))
            {
                return(factory.Invoke(context, out value));
            }

            value = null;
            return(false);
        }
Ejemplo n.º 4
0
        public FieldIdentifier CreateField(IDataEntityProperty property)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            return(new FieldIdentifier(this, property.GetFieldName(out var alias), alias)
            {
                Token = new DataEntityPropertyToken(property)
            });
Ejemplo n.º 5
0
 public static IEnumerable <IComplexProperty> GetComplexProperties(this IDataEntityPropertyCollection properties, bool onlyDbProperty = true)
 {
     for (int i = 0; i < properties.Count; i++)
     {
         IDataEntityProperty metadata          = properties[i];
         IComplexProperty    iteratorVariable0 = metadata as IComplexProperty;
         if ((iteratorVariable0 != null) && (!onlyDbProperty || !metadata.IsDbIgnore()))
         {
             yield return(iteratorVariable0);
         }
     }
 }
        private void UpdateForeign()
        {
            var index = this.Role.IndexOf(':');

            if (index < 0)
            {
                _foreign = this.Entity.Metadata.Manager.Entities.Get(this.Role);
            }
            else
            {
                _foreign         = this.Entity.Metadata.Manager.Entities.Get(this.Role.Substring(0, index));
                _foreignProperty = _foreign.Properties.Get(this.Role.Substring(index + 1));
            }
        }
Ejemplo n.º 7
0
        public override IEnumerable <Metadata.DataEntity.IDataEntityProperty> GetDirtyProperties(bool includehasDefault)
        {
            List <IDataEntityProperty> list = new List <IDataEntityProperty>();
            IDataEntityProperty        item = null;

            for (int i = 0; i < this._dirtyArray.Length; i++)
            {
                item = this._properties[i];
                if (this._dirtyArray[i] || (includehasDefault && item.HasDefaultValue))
                {
                    list.Add(item);
                }
            }
            return(list);
        }
        private void UpdateForeign()
        {
            var port  = this.Port;
            var index = port.IndexOf(':');

            if (index < 0)
            {
                _foreign = this.Entity.GetEntity(port);
            }
            else
            {
                _foreign = this.Entity.GetEntity(port.Substring(0, index));

                if (_foreign != null)
                {
                    _foreignProperty = _foreign.Properties.Get(port.Substring(index + 1));
                }
            }
        }
        /// <summary>
        /// 获取指定实体属性对应的字段名以及返回的别名。
        /// </summary>
        /// <param name="property">当前的实体属性。</param>
        /// <param name="alias">输出参数,对应的返回别名。详细说明请参考该方法的备注说明。</param>
        /// <returns>当前属性对应的字段名。</returns>
        /// <remarks>
        ///		<para>注意:如果当前实体属性的字段名不同于属性名,则<paramref name="alias"/>输出参数值即为属性名,必须确保查询返回的字段标识都为对应的属性名,以便后续实体组装时进行字段与属性的匹配。</para>
        /// </remarks>
        public static string GetFieldName(this IDataEntityProperty property, out string alias)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var simplex = property.IsSimplex ?
                          (IDataEntitySimplexProperty)property :
                          throw new ArgumentException($"The specified '{property.Name}' property is not a simplex property, so it has no field name(alias).");

            if (string.IsNullOrEmpty(simplex.Alias))
            {
                alias = null;
                return(property.Name);
            }
            else
            {
                alias = property.Name;
                return(simplex.Alias);
            }
        }
Ejemplo n.º 10
0
        public static ISource From(this IStatement statement, TableIdentifier origin, string memberPath, Func <ISource, IDataEntityComplexProperty, ISource> subqueryFactory, out IDataEntityProperty property)
        {
            var found = origin.Reduce(memberPath, ctx =>
            {
                var source = ctx.Source;

                if (ctx.Ancestors != null)
                {
                    foreach (var ancestor in ctx.Ancestors)
                    {
                        source = statement.Join(source, ancestor, ctx.Path);
                    }
                }

                if (ctx.Property.IsComplex)
                {
                    var complex = (IDataEntityComplexProperty)ctx.Property;

                    if (complex.Multiplicity == DataAssociationMultiplicity.Many)
                    {
                        if (subqueryFactory != null)
                        {
                            return(subqueryFactory(source, complex));
                        }

                        //如果不允许一对多的子查询则抛出异常
                        throw new DataException($"The specified '{ctx.FullPath}' member is a one-to-many composite(navigation) property that cannot appear in the sorting and specific condition clauses.");
                    }

                    source = statement.Join(source, complex, ctx.FullPath);
                }

                return(source);
            });

            if (found.IsFailed)
            {
                throw new DataException($"The specified '{memberPath}' member does not exist in the '{origin.Entity?.Name}' entity and it's inherits.");
            }

            //输出找到的属性元素
            property = found.Property;

            //返回找到的源
            return(found.Source);
        }
 // Methods
 public DataEntityPropertyChangedEventArgs(IDataEntityProperty property)
     : base(property.Name)
 {
     this._property = property;
 }
Ejemplo n.º 12
0
 public static ISource From(this IStatement statement, string memberPath, Func <ISource, IDataEntityComplexProperty, ISource> subqueryFactory, out IDataEntityProperty property)
 {
     return(From(statement, statement.Table, memberPath, subqueryFactory, out property));
 }
Ejemplo n.º 13
0
 private static bool TryGetOldProperty(IDataEntityProperty dp, IDataEntityType dtOldData, out IDataEntityProperty dpOldProperty)
 {
     dpOldProperty = null;
     return(((dtOldData != null) && (dp != null)) && dtOldData.Properties.TryGetValue(dp.Name, out dpOldProperty));
 }
 public DataEntityPropertyChangedEventArgs(IDataEntityProperty property, bool isErrorRaise)
     : base(property.Name)
 {
     this._property     = property;
     this._isErrorRaise = isErrorRaise;
 }
Ejemplo n.º 15
0
        /// <summary>
        /// 把一个实体数据拷贝到另一个实体
        /// </summary>
        /// <param name="dataEntity"></param>
        /// <param name="newEntity"></param>
        /// <param name="copyHandler"></param>
        /// <param name="clearPrimaryKeyValue"></param>
        /// <param name="onlyDbProperty"></param>
        /// <param name="onlyDirtyProperty"></param>
        public static void CopyData(this IDataEntityBase dataEntity, IDataEntityBase newEntity, Func <string, string, bool> copyHandler = null, bool clearPrimaryKeyValue = false, bool onlyDbProperty = false, bool onlyDirtyProperty = false)
        {
            IDataEntityType dataEntityType = dataEntity.GetDataEntityType();
            IDataEntityType type2          = newEntity.GetDataEntityType();

            if (copyHandler == null)
            {
                copyHandler = (dtName, propName) => true;
            }
            IEnumerable <IDataEntityProperty> dirtyProperties = dataEntityType.GetDirtyProperties(dataEntity);

            foreach (ISimpleProperty property in type2.Properties.GetSimpleProperties(onlyDbProperty))
            {
                if (copyHandler(type2.Name, property.Name))
                {
                    IDataEntityProperty dpOldProperty = null;
                    TryGetOldProperty(property, dataEntityType, out dpOldProperty);
                    if ((!onlyDirtyProperty || dirtyProperties.Contains <IDataEntityProperty>(dpOldProperty)) && !(property.IsReadOnly || (dpOldProperty == null)))
                    {
                        property.SetValue(newEntity, dpOldProperty.GetValue(dataEntity));
                    }
                }
            }
            if (clearPrimaryKeyValue)
            {
                ISimpleProperty primaryKey = type2.PrimaryKey;
                if (primaryKey != null)
                {
                    primaryKey.ResetValue(newEntity);
                }
                type2.SetDirty(newEntity, true);
            }
            foreach (IComplexProperty property4 in type2.Properties.GetComplexProperties(onlyDbProperty))
            {
                IDataEntityProperty property5 = null;
                TryGetOldProperty(property4, dataEntityType, out property5);
                IDataEntityBase base2 = property5.GetValue(dataEntity) as IDataEntityBase;
                if (base2 != null)
                {
                    IDataEntityBase base3;
                    if (property4.IsReadOnly)
                    {
                        base3 = property4.GetValue(newEntity) as IDataEntityBase;
                        if (base3 == null)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,只读的属性却返回了NULL值。", "014009000001633", SubSystemType.SL, new object[0]));
                        }
                        base2.CopyData(base3, copyHandler, false, onlyDbProperty, false);
                    }
                    else
                    {
                        base3 = property4.ComplexPropertyType.CreateInstance() as IDataEntityBase;
                        base2.CopyData(base3, copyHandler, clearPrimaryKeyValue, onlyDbProperty, false);
                        property4.SetValue(newEntity, base3);
                    }
                }
            }
            foreach (ICollectionProperty property6 in type2.Properties.GetCollectionProperties(onlyDbProperty))
            {
                IDataEntityProperty property7 = null;
                TryGetOldProperty(property6, dataEntityType, out property7);
                object obj2 = property7.GetValue(dataEntity);
                if (obj2 != null)
                {
                    IEnumerable enumerable2 = obj2 as IEnumerable;
                    if (enumerable2 == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持枚举。", "014009000001634", SubSystemType.SL, new object[0]));
                    }
                    object obj3 = property6.GetValue(newEntity);
                    if (obj3 == null)
                    {
                        if (property6.IsReadOnly)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值为null。", "014009000001635", SubSystemType.SL, new object[0]));
                        }
                        obj3 = Activator.CreateInstance(property6.PropertyType);
                        property6.SetValue(newEntity, obj3);
                    }
                    IList list = obj3 as IList;
                    if (list == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持IList。", "014009000001636", SubSystemType.SL, new object[0]));
                    }
                    list.Clear();
                    foreach (IDataEntityBase base4 in enumerable2)
                    {
                        IDataEntityBase base5 = property6.CollectionItemPropertyType.CreateInstance() as IDataEntityBase;
                        base4.CopyData(base5, copyHandler, clearPrimaryKeyValue, onlyDbProperty, false);
                        list.Add(base5);
                    }
                }
            }
        }
Ejemplo n.º 16
0
 public DataEntityPropertyToken(IDataEntityProperty property, MemberInfo member = null)
 {
     this.Property  = property;
     this.Member    = member;
     this.Converter = Common.Utility.GetConverter(member);
 }
Ejemplo n.º 17
0
        private void CopyData(IDataEntityType dt, object dataEntity, object newEntity, bool clearPrimaryKeyValue)
        {
            IDataEntityType objA = dt;
            IDataEntityType objB = dt;

            if (dataEntity is IDataEntityBase)
            {
                objA = (dataEntity as IDataEntityBase).GetDataEntityType();
            }
            if (newEntity is IDataEntityBase)
            {
                objB = (newEntity as IDataEntityBase).GetDataEntityType();
            }
            if ((dataEntity is DynamicObject) && object.ReferenceEquals(objA, objB))
            {
                ((DynamicObject)newEntity).DataStorage = ((DynamicObject)dataEntity).DataStorage.MemberClone();
            }
            else
            {
                foreach (ISimpleProperty property in dt.Properties.GetSimpleProperties(this._onlyDbProperty))
                {
                    IDataEntityProperty dpOldProperty = null;
                    this.TryGetOldProperty(property, objA, out dpOldProperty);
                    if (!property.IsReadOnly && (dpOldProperty != null))
                    {
                        property.SetValue(newEntity, dpOldProperty.GetValue(dataEntity));
                    }
                }
            }
            if (clearPrimaryKeyValue)
            {
                ISimpleProperty primaryKey = dt.PrimaryKey;
                if (primaryKey != null)
                {
                    primaryKey.ResetValue(newEntity);
                }
                objB.SetDirty(newEntity, true);
            }
            foreach (IComplexProperty property4 in dt.Properties.GetComplexProperties(this._onlyDbProperty))
            {
                if (!property4.IsRefrenceObject())
                {
                    IDataEntityProperty property5 = null;
                    this.TryGetOldProperty(property4, objA, out property5);
                    object obj2 = property5.GetValue(dataEntity);
                    if (obj2 != null)
                    {
                        object          obj3;
                        IDataEntityType dataEntityType;
                        IDataEntityBase base2 = obj2 as IDataEntityBase;
                        if (base2 != null)
                        {
                            dataEntityType = base2.GetDataEntityType();
                        }
                        else
                        {
                            dataEntityType = property4.ComplexPropertyType;
                        }
                        if (property4.IsReadOnly)
                        {
                            obj3 = property4.GetValue(newEntity);
                            if (obj3 == null)
                            {
                                throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,只读的属性却返回了NULL值。", "014009000001633", SubSystemType.SL, new object[0]));
                            }
                            this.CopyData(dataEntityType, obj2, obj3, false);
                        }
                        else
                        {
                            if (property4.IsDbIgnore())
                            {
                                obj3 = this.Clone(dataEntityType, obj2, false);
                            }
                            else
                            {
                                obj3 = this.Clone(dataEntityType, obj2, this._clearPrimaryKeyValue);
                            }
                            property4.SetValue(newEntity, obj3);
                        }
                    }
                }
            }
            foreach (ICollectionProperty property6 in dt.Properties.GetCollectionProperties(this._onlyDbProperty))
            {
                IDataEntityProperty property7 = null;
                this.TryGetOldProperty(property6, objA, out property7);
                object obj4 = property7.GetValue(dataEntity);
                if (obj4 != null)
                {
                    IEnumerable enumerable = obj4 as IEnumerable;
                    if (enumerable == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持枚举。", "014009000001634", SubSystemType.SL, new object[0]));
                    }
                    if (newEntity is DynamicObject)
                    {
                        ((DynamicObject)newEntity).DataStorage.SetLocalValue((DynamicProperty)property6, null);
                    }
                    object obj5 = property6.GetValue(newEntity);
                    if (obj5 == null)
                    {
                        if (property6.IsReadOnly)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值为null。", "014009000001635", SubSystemType.SL, new object[0]));
                        }
                        obj5 = Activator.CreateInstance(property6.PropertyType);
                        property6.SetValue(newEntity, obj5);
                    }
                    IList list = obj5 as IList;
                    if (list == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持IList。", "014009000001636", SubSystemType.SL, new object[0]));
                    }
                    list.Clear();
                    foreach (object obj6 in enumerable)
                    {
                        IDataEntityType collectionItemPropertyType;
                        IDataEntityBase base3 = obj6 as IDataEntityBase;
                        if (base3 == null)
                        {
                            collectionItemPropertyType = property6.CollectionItemPropertyType;
                        }
                        else
                        {
                            collectionItemPropertyType = base3.GetDataEntityType();
                        }
                        list.Add(this.Clone(collectionItemPropertyType, obj6));
                    }
                }
            }
        }
Ejemplo n.º 18
0
 public ReduceResult(ISource source, IDataEntityProperty property)
 {
     this.Source   = source;
     this.Property = property;
 }
Ejemplo n.º 19
0
 bool IKeyedCollectionBase <string, IDataEntityProperty> .Contains(IDataEntityProperty item)
 {
     return(base.Contains((DynamicProperty)item));
 }
Ejemplo n.º 20
0
 int IKeyedCollectionBase <string, IDataEntityProperty> .IndexOf(IDataEntityProperty item)
 {
     return(base.IndexOf((DynamicProperty)item));
 }
Ejemplo n.º 21
0
        /// <summary>
        ///		<para>从指定的表标识对应的实体开始进行路径展开。</para>
        ///		<para>注:展开过程包括对父实体的属性集的搜索。</para>
        /// </summary>
        /// <param name="table">指定的进行展开的起点。</param>
        /// <param name="path">指定要展开的成员路径,支持多级导航属性路径。</param>
        /// <param name="step">指定路径中每个属性的展开回调函数。</param>
        /// <returns>返回找到的结果。</returns>
        public static ReduceResult Reduce(this TableIdentifier table, string path, Func <ReduceContext, ISource> step = null)
        {
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }

            if (table.Entity == null)
            {
                throw new DataException($"The '{table}' table cannot be expanded.");
            }

            if (string.IsNullOrEmpty(path))
            {
                return(ReduceResult.Failure(table));
            }

            ICollection <IDataEntity> ancestors = null;
            IDataEntityProperty       property  = null;
            ISource token      = table;
            var     parts      = path.Split('.');
            var     properties = table.Entity.Properties;

            for (int i = 0; i < parts.Length; i++)
            {
                if (properties == null)
                {
                    return(ReduceResult.Failure(token));
                }

                //如果当前属性集合中不包含指定的属性,则尝试从父实体中查找
                if (!properties.TryGet(parts[i], out property))
                {
                    //尝试从父实体中查找指定的属性
                    property = FindBaseProperty(ref properties, parts[i], ref ancestors);

                    //如果父实体中也不含指定的属性则返回失败
                    if (property == null)
                    {
                        return(ReduceResult.Failure(token));
                    }
                }

                //如果回调函数不为空,则调用匹配回调函数
                //注意:将回调函数返回的结果作为下一次的用户数据保存起来
                if (step != null)
                {
                    token = step(new ReduceContext(string.Join(".", parts, 0, i), token, property, ancestors));
                }

                //清空继承实体链
                if (ancestors != null)
                {
                    ancestors.Clear();
                }

                if (property.IsSimplex)
                {
                    break;
                }
                else
                {
                    properties = GetAssociatedProperties((IDataEntityComplexProperty)property, ref ancestors);
                }
            }

            //返回查找到的结果
            return(new ReduceResult(token, property));
        }
Ejemplo n.º 22
0
        bool IKeyedCollectionBase <string, IDataEntityProperty> .TryGetValue(string key, out IDataEntityProperty value)
        {
            DynamicProperty property;

            if (base.TryGetValue(key, out property))
            {
                value = property;
                return(true);
            }
            value = null;
            return(false);
        }
Ejemplo n.º 23
0
 internal SchemaMember(IDataEntityProperty property, IEnumerable <IDataEntity> ancestors = null)
 {
     this.Token     = new DataEntityPropertyToken(property);
     this.Ancestors = ancestors;
 }