/// <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]); } } } }
public virtual void DeleteAll() { if (this.Id == 0) { throw new OOEngineException("此对象尚未被持久化,无法执行删除操作!"); } string sqlPattern = "delete from {0};"; string sql = string.Format(sqlPattern, EInfo.EntityName); try { DBSQLiteHelper.ExecuteUpdate(sql); } catch (Exception e) { throw new OOEngineException("在删除持久化对象时发生异常!" + e.Message); } }
} //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)); } } } } }
/// <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); }
/// <summary> /// 更新持久化对象的属性 /// </summary> public void Update() { if (this.Id == 0) { throw new OOEngineException("此对象尚未被持久化,请先执行新增操作后再执行此方法!"); } string sqlPattern = "update {0} set {1} where Id={2};"; StringBuilder sbSetValue = new StringBuilder(); foreach (FieldAttribute pinfo in EInfo.Properties) { if (sbSetValue.Length > 0) { sbSetValue.Append(","); } sbSetValue.Append(string.Format("{0}={1}", pinfo.FieldName, DLLAnalysis.GetPropertyValue(this, pinfo))); } foreach (REFAttribute refobj in EInfo.References) { if (sbSetValue.Length > 0) { sbSetValue.Append(","); } sbSetValue.Append(string.Format("{0}={1}", refobj.REFFieldName, DLLAnalysis.GetReferenceEntityId(this, refobj.PropertyName))); } if (EInfo.REFSelf) { if (sbSetValue.Length > 0) { sbSetValue.Append(","); } sbSetValue.Append(string.Format("ParentId={0}", DLLAnalysis.GetReferenceEntityId(this, "Parent" + EInfo.EntityName))); } string sql = string.Format(sqlPattern, EInfo.EntityName, sbSetValue.ToString(), this.Id); DBSQLiteHelper.ExecuteUpdate(sql); }
/// <summary> /// 将新创建的实体插入数据库,如果该实体并不是新建的,则会抛出异常 /// </summary> public virtual void Insert() { if (this.Id > 0) { throw new OOEngineException("对象已经被持久化,不能在此对象上执行新增操作!"); } //try //{ string sqlPattern = "insert into {0}({1}) values({2});select last_insert_rowid() newid;"; StringBuilder sbFieldNameList = new StringBuilder(); StringBuilder sbFieldValueList = new StringBuilder(); //常规属性 foreach (FieldAttribute pinfo in EInfo.Properties) { if (sbFieldNameList.Length > 0) { sbFieldNameList.Append(","); } sbFieldNameList.Append(pinfo.FieldName); if (sbFieldValueList.Length > 0) { sbFieldValueList.Append(","); } sbFieldValueList.Append(DLLAnalysis.GetPropertyValue(this, pinfo)); } //引用属性 foreach (REFAttribute refobj in EInfo.References) { if (sbFieldNameList.Length > 0) { sbFieldNameList.Append(","); } //将实体中的REFEntityName转变为数据库表的字段EntityNameId sbFieldNameList.Append(refobj.REFFieldName); if (sbFieldValueList.Length > 0) { sbFieldValueList.Append(","); } sbFieldValueList.Append(DLLAnalysis.GetReferenceEntityId(this, refobj.PropertyName)); } if (EInfo.REFSelf) { if (sbFieldNameList.Length > 0) { sbFieldNameList.Append(","); } //将实体中的REFEntityName转变为数据库表的字段EntityNameId sbFieldNameList.Append("ParentId"); if (sbFieldValueList.Length > 0) { sbFieldValueList.Append(","); } sbFieldValueList.Append(DLLAnalysis.GetReferenceEntityId(this, "Parent" + EInfo.EntityName)); } string sql = string.Format(sqlPattern, EInfo.EntityName, sbFieldNameList.ToString(), sbFieldValueList.ToString()); int newid = DBSQLiteHelper.ExecuteScalar(sql); this.Id = Convert.ToInt64(newid); //} //catch (Exception e) //{ // throw new OOEngineException("在持久化新实体对象时发生异常,地址:BaseEntity.Insert(),具体信息:"+e.Message); //} }