Ejemplo n.º 1
0
        public override object LoadData(DataContext context, IDataReader datareader, object state)
        {
            QueryState queryState = state as QueryState;

            if (this._singleRelationFields.Count > 0)
            {
                queryState.InitialJoinData();
                return(LoadJoinTableData(context, datareader, queryState, string.Empty));
            }
            object item = Activator.CreateInstance(ObjectType);

            foreach (DataFieldMapping field in this._fieldList)
            {
                if (queryState == null)
                {
                    object obj   = datareader[field.Name];
                    object value = field.ToProperty(obj);
                    if (!Object.Equals(value, null))
                    {
                        field.Handler.Set(item, value);
                    }
                }
                else if (queryState.CheckSelectField(field.Name))
                {
                    object obj   = datareader[field.Name];
                    object value = field.ToProperty(obj);
                    if (!Object.Equals(value, null))
                    {
                        field.Handler.Set(item, value);
                    }
                }
            }
            if (_collectionRelationFields.Count > 0)
            {
                foreach (CollectionRelationFieldMapping mapping in _collectionRelationFields)
                {
                    mapping.Handler.Set(item, mapping.ToProperty(context, item, true));
                }
            }
            if (_isDataEntity)
            {
                DataEntity entity = item as DataEntity;
                entity.SetContext(context);
            }
            return(item);
        }
Ejemplo n.º 2
0
        //public virtual void LoadJoinTableData(DataContext context, IDataReader datareader, object item, QueryState queryState, string fieldPath)
        //{
        //    string aliasName = queryState.GetAliasName(fieldPath);
        //    foreach (DataFieldMapping field in this._fieldList) {
        //        string name = string.Format("{0}_{1}", aliasName, field.Name);
        //        if (queryState.CheckSelectField(name)) {
        //            object obj = datareader[name];
        //            object value = field.ToProperty(obj);
        //            if (!Object.Equals(value, null)) {
        //                field.Handler.Set(item, value);
        //            }
        //        }
        //    }
        //    if (_collectionRelationFields.Count > 0) {
        //        foreach (CollectionRelationFieldMapping mapping in _collectionRelationFields) {
        //            mapping.Handler.Set(item, mapping.ToProperty(context, item, true));
        //        }
        //    }

        //    foreach (SingleRelationFieldMapping mapping in _singleRelationFields) {
        //        string fpath = string.Format("{0}.{1}", fieldPath, mapping.FieldName);
        //        object value = mapping.ToProperty(context, datareader, queryState, fpath);
        //        if (!Object.Equals(value, null)) {
        //            mapping.Handler.Set(item, value);
        //        }
        //    }
        //    if (item is DataEntity entity) {
        //        entity.SetContext(context);
        //        entity.LoadDataComplete();
        //    }
        //}

        public virtual object LoadJoinTableData(DataContext context, IDataReader datareader, QueryState queryState, string fieldPath)
        {
            object item = Activator.CreateInstance(this.ObjectType);

            queryState.SetJoinData(fieldPath, item);
            string aliasName = queryState.GetAliasName(fieldPath);

            foreach (DataFieldMapping field in this._fieldList)
            {
                string name = string.Format("{0}_{1}", aliasName, field.Name);
                if (queryState.CheckSelectField(name))
                {
                    object obj   = datareader[name];
                    object value = field.ToProperty(obj);
                    if (!Object.Equals(value, null))
                    {
                        field.Handler.Set(item, value);
                    }
                }
            }
            if (_collectionRelationFields.Count > 0)
            {
                foreach (CollectionRelationFieldMapping mapping in _collectionRelationFields)
                {
                    mapping.Handler.Set(item, mapping.ToProperty(context, item, true));
                }
            }

            foreach (SingleRelationFieldMapping mapping in _singleRelationFields)
            {
                string fpath = string.Format("{0}.{1}", fieldPath, mapping.FieldName);
                object value = mapping.ToProperty(context, datareader, queryState, fpath);
                if (!Object.Equals(value, null))
                {
                    mapping.Handler.Set(item, value);
                }
            }
            if (_isDataEntity)
            {
                DataEntity entity = item as DataEntity;
                entity.SetContext(context);
            }
            return(item);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 读取关联数据
        /// </summary>
        /// <param name="keyName">关联字段属性名称</param>
        /// <param name="extendQuery">扩展查询</param>
        /// <param name="extendOrder">扩展排序</param>
        /// <returns>关联数据</returns>
        private object LoadRelationData(string keyName, QueryExpression extendQuery, OrderExpression extendOrder)
        {
            DataEntityMapping    selfMapping     = DataMapping.GetEntityMapping(this.GetType());
            RelationFieldMapping relationMapping = selfMapping.FindRelateionMapping(keyName);
            QueryExpression      expression      = null;

            foreach (RelationFieldMapping.RelationKeyValue rt in relationMapping.GetRelationKeyValues())
            {
                DataFieldInfo info   = new DataFieldInfo(relationMapping.RelateTableMapping.ObjectType, rt.RelateField.Name);
                object        objkey = rt.MasterField.Handler.Get(this);
                if (Object.Equals(objkey, null))
                {
                    expression &= info.IsNull();
                }
                else
                {
                    expression &= info == objkey;
                }
            }
            if (extendQuery != null)
            {
                expression = QueryExpression.And(expression, extendQuery);
            }
            //判断与关联表的关联关系
            if (relationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
            {
                //一对一关系,直接查询单个对象
                object     obj = this.Context.SelectSingle(relationMapping.RelateTableMapping, expression, extendOrder, 0, SafeLevel.None);
                DataEntity de  = obj as DataEntity;
                if (de == null)
                {
                    return(obj);
                }
                if (relationMapping.RelateRelationMapping != null)                  //判断是否相互关联
                {
                    if (relationMapping.RelateRelationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
                    {
                        de.TempRelationDatas.Add(relationMapping.RelateRelationMapping.RelationName, this);
                    }
                }
                return(de);
            }
            else
            {
                IList list = this.Context.QueryDataList(relationMapping.RelateTableMapping, expression, extendOrder, null, SafeLevel.Default) as IList;
                //if (list.Count == 0) return list;

                if (relationMapping.RelateRelationMapping != null && relationMapping.RelateRelationMapping.MasterTableMapping.IsDataEntity)
                {
                    if (relationMapping.RelateRelationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
                    {
                        foreach (object obj in list)
                        {
                            DataEntity de = obj as DataEntity;
                            de.TempRelationDatas.Add(relationMapping.RelateRelationMapping.RelationName, this);
                        }
                    }
                }
                if (relationMapping.ResultDataKind == RelationFieldMapping.DataKind.IList)
                {
                    return(list);
                }
                else
                {
                    Array array = Array.CreateInstance(relationMapping.RelateTableMapping.ObjectType, list.Count);
                    list.CopyTo(array, 0);
                    return(array);
                }
            }
        }