예제 #1
0
        /// <summary>
        /// 根据ID载入实体
        /// </summary>
        /// <param name="id">实体对象的Id</param>
        /// <param name="fetchType">加载对象的方式,例如是否加载引用对象和集合属性</param>
        public void Load(long id, Fetch fetchType)
        {
            this.Id = id;
            string        sqlPattern  = string.Empty;
            string        sql         = string.Empty;
            StringBuilder sbSelectStr = new StringBuilder();
            string        strLeftJoin = string.Empty;
            DataSet       ds          = null;

            sbSelectStr.Append(string.Format("{0}.id as E_{0}Id,{0}.*", EInfo.EntityName));
            sqlPattern   = "select {0} from {1} {2} where {1}.id={3}";
            strLeftJoin  = DBSQLiteHelper.GenerateLeftJoinSQL(EInfo.References, ref sbSelectStr, fetchType);
            strLeftJoin += DBSQLiteHelper.GenerateSetSQL(EInfo.Sets, ref sbSelectStr, fetchType);
            if (EInfo.REFSelf)
            {
                strLeftJoin += DBSQLiteHelper.GenerateREFSelfSQL(EInfo, ref sbSelectStr, fetchType);
            }
            sql = string.Format(sqlPattern, sbSelectStr.ToString(), EInfo.EntityName, strLeftJoin, id);
            ds  = DBSQLiteHelper.ExecuteQuery(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (FieldAttribute pinfo in EInfo.Properties)
                {
                    DBSQLiteHelper.FetchPropertyValue(this, pinfo, ds.Tables[0], 0);
                }
                foreach (REFAttribute refAtt in EInfo.References)
                {
                    if ((fetchType == Fetch.Default && !refAtt.LazyLoad) || fetchType == Fetch.REFS || fetchType == Fetch.REFSandSets)
                    {
                        DBSQLiteHelper.FetchREFEntity(this, refAtt, ds.Tables[0]);
                    }
                }
                foreach (SETAttribute setAtt in EInfo.Sets)
                {
                    if ((fetchType == Fetch.Default && !setAtt.LazyLoad) || fetchType == Fetch.SETS || fetchType == Fetch.REFSandSets)
                    {
                        DBSQLiteHelper.FetchSet(this, setAtt, ds.Tables[0]);
                    }
                }
                //如果实体有对自身实体对象的引用
                if (EInfo.REFSelf)
                {
                    if (fetchType == Fetch.REFS)
                    {
                        DBSQLiteHelper.FetchParent(this, ds.Tables[0], 0);
                    }
                    else if (fetchType == Fetch.SETS)
                    {
                        DBSQLiteHelper.FetchChildren(this, ds.Tables[0]);
                    }
                    else if (fetchType == Fetch.REFSandSets)
                    {
                        DBSQLiteHelper.FetchParent(this, ds.Tables[0], 0);
                        DBSQLiteHelper.FetchChildren(this, ds.Tables[0]);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 根据从数据库检索出的数据集从中组装出实体对象实例
        /// </summary>
        /// <param name="einfo">实体对象媒数据</param>
        /// <param name="fetchType">关联对象加载方式</param>
        /// <param name="table">数据集</param>
        /// <param name="rowIndex">实体对象所处的数据集行数</param>
        /// <returns>实体对象实例</returns>
        public static BaseEntity FetchEntity(EntityInfo einfo, Fetch fetchType, DataTable table, int rowIndex)
        {
            BaseEntity entity = (BaseEntity)DLLAnalysis.GetEntityInstance(einfo.EntityName);

            entity.Id = Convert.ToInt64(table.Rows[rowIndex][einfo.IdNameInSelect]);
            foreach (FieldAttribute pinfo in einfo.Properties)
            {
                DBSQLiteHelper.FetchPropertyValue(entity, pinfo, table, rowIndex);
            }
            foreach (REFAttribute refAtt in einfo.References)
            {
                if ((fetchType == Fetch.Default && !refAtt.LazyLoad) || fetchType == Fetch.REFS || fetchType == Fetch.REFSandSets)
                {
                    DBSQLiteHelper.FetchREFEntity(entity, refAtt, table);
                }
            }
            foreach (SETAttribute setAtt in einfo.Sets)
            {
                if ((fetchType == Fetch.Default && !setAtt.LazyLoad) || fetchType == Fetch.SETS || fetchType == Fetch.REFSandSets)
                {
                    DBSQLiteHelper.FetchSet(entity, setAtt, table);
                }
            }
            //如果实体有对自身实体对象的引用
            if (einfo.REFSelf)
            {
                if (fetchType == Fetch.REFS)
                {
                    FetchParent(entity, table, rowIndex);
                }
                else if (fetchType == Fetch.SETS)
                {
                    FetchChildren(entity, table);
                }
                else if (fetchType == Fetch.REFSandSets)
                {
                    FetchParent(entity, table, rowIndex);
                    FetchChildren(entity, table);
                }
            }
            return(entity);
        }