/// <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]); } } } }
} //order by ProjectCode /// <summary> /// 排序 /// </summary> /// <param name="fetchType"></param> /// <param name="GetDataTable"></param> /// <param name="order">order by ProjectCode</param> public EntitySet(Fetch fetchType, bool GetDataTable, string order) { string sqlPattern = string.Empty; StringBuilder sbSelectStr = new StringBuilder(); string strLeftJoin = string.Empty; string sql = string.Empty; DataSet ds = null; sbSelectStr.Append(string.Format("{0}.id as E_{0}Id,{0}.*", EInfo.EntityName)); sqlPattern = "select {0} from {1} {2}"; if (!string.IsNullOrEmpty(order)) { sqlPattern += " " + order; } 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); ds = DBSQLiteHelper.ExecuteQuery(sql); if (GetDataTable) { EntityDataTable = this.AddIndexToTable(ds.Tables[0]); return; } else { if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { long rEntityId = Convert.ToInt64(ds.Tables[0].Rows[i][EInfo.IdNameInSelect]); if (i == 0 || (EntityList.Count > 0 && ((BaseEntity)EntityList[EntityList.Count - 1]).Id != rEntityId)) { EntityList.Add((T)DBSQLiteHelper.FetchEntity(EInfo, fetchType, ds.Tables[0], i)); } } } } }