//---------------------------------------------------------- private void Load(DataTable table, TMetaEntity me) { TMetaTable mt = me.MetaTable; if (table.Rows.Count == 0) { if (mt.InheritMappingType == TInheritMappingType.TablePerConcreteClass) { throw new TFetchNoneException(this.GetType().ToString() + " Fetch None"); } } if (table.Rows.Count == 1) { Load(table.Rows[0], me); } else if (table.Rows.Count > 1) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < table.Rows.Count; i++) { sb.AppendLine(table.Rows[i]["ID"].ToString()); } throw new TFetchMoreException(String.Format("{0}--Fetch More than one row{1}{2}", GetType(), Environment.NewLine, sb)); } }
//---------------------------------------------------------- // 分表储存 private void SaveTablePerSubClass() { Type t2 = this.GetType(); while (t2 != typeof(CEntity)) { TMetaEntity meta2 = GetMetaEntity(t2); TMetaTable mt2 = meta2.MetaTable; TInheritMappingType inheritType = mt2.InheritMappingType; try { // PerConcrete与PerSubClass同样都调用函数Save(meta) // 在此函数中,去进一步区分MappingType // NG Save(meta2); } catch (TDataSilentlyChangedException e2) { string trace = TEntityConcurrencyTrace.Trace(this, meta2); SimpleLog.Write(trace); SimpleLog.Write(e2); throw e2; } t2 = t2.BaseType; } }
//---------------------------------------------------------- public virtual void Assign(TMetaEntity me, CEntity rhs) { TMetaTable mt = me.MetaTable; TInheritMappingType inheritType = mt.InheritMappingType; TMetaColumnCollection mcs = null; if (inheritType == TInheritMappingType.TablePerConcreteClass) { mcs = mt.FullValueColumns; } else if (inheritType == TInheritMappingType.TablePerSubClass) { mcs = mt.ValueColumns; } CEntity that = rhs as CEntity; foreach (TMetaColumn mc in mcs) { //if (!cm.IsAssign) // continue; if (mc.MemberName == "Captions.Cn (Assign)") { System.Diagnostics.Debug.WriteLine(mc.MemberName); } object v2 = that.GetEpoPropertyValue(mc); this.SetEpoPorpertyValue(mc, v2); } }
//-------------------------------------------------------------- public bool Equals(Type t, object rhs) { if (rhs == null) { return(false); } if (object.ReferenceEquals(this, rhs)) { return(true); } if (this.GetType() != rhs.GetType()) { return(false); } CEntity that = rhs as CEntity; if (that == null) { return(false); } TMetaEntity meta = TMetaRegistry.Default.GetMeta(t); TMetaTable mt = meta.MetaTable; TInheritMappingType inheritType = mt.InheritMappingType; TMetaColumnCollection mcs = null; if (inheritType == TInheritMappingType.TablePerConcreteClass) { mcs = mt.FullValueColumns; } else if (inheritType == TInheritMappingType.TablePerSubClass) { mcs = mt.ValueColumns; } bool b = true; foreach (TMetaColumn mc in mcs) { //if (!cm.IsCompare) // continue; if (mc.MemberName == "Captions.Cn (Equal)") { System.Diagnostics.Debug.WriteLine(mc.MemberName); } object v1 = this.GetEpoPropertyValue(mc); object v2 = that.GetEpoPropertyValue(mc); b = b && (object.Equals(v1, v2)); } b = b && (PersistState == that.PersistState); return(b); }
//--------------------------------------------------------------------- public virtual void Push(DataRow row, TMetaEntity me) { TMetaTable mt = me.MetaTable; TInheritMappingType inheritType = me.MetaTable.InheritMappingType; if (inheritType == TInheritMappingType.TablePerConcreteClass) { Push(row, mt.FullValueColumns); } else if (inheritType == TInheritMappingType.TablePerSubClass) { Push(row, mt.ValueColumns); } }
//--------------------------------------------------------------------- private void SaveColumnsBrutally(TMetaEntity me) { TMetaTable mt = me.MetaTable; CDataAccess access = Context.GetDataAccess(me); access.Context = this.Context; string tableName = mt.TableName; foreach (TMetaColumn mc in mt.Columns) { if (!mc.IsBrutallySave) { continue; } object v = GetEpoPropertyValue(mc); string strValue = null; switch (mc.DataType) { case TDataType.Int32: case TDataType.Int64: case TDataType.Int16: case TDataType.Decimal: strValue = v.ToString(); break; case TDataType.String: strValue = String.Format("'{0}'", v); break; case TDataType.DateTime: strValue = String.Format("'{0}'", v); break; default: strValue = v.ToString(); break; } string cmdText = String.Format("Update {0} set {1}={2} where ID='{3}'", mt.TableName, mc.ColumnName, strValue, Id); access.ExecuteNonQuery(cmdText); } }
//---------------------------------------------------------- public virtual void InitMembers() { TMetaEntity me = GetMetaEntity(this.GetType()); if (me == null) { return; } TMetaTable mt = me.MetaTable; foreach (TMetaColumn mc in mt.FullValueColumns) { TDataType dataType = mc.DataType; if (dataType == TDataType.DateTime || dataType == TDataType.Date) { object nullValue = TConvert.GetNullValue(dataType); this.SetEpoPorpertyValue(mc, nullValue); } } }
////---------------------------------------------------------- // 不支持继承 //public virtual void Load(string id) //{ // DataTable dataTable = DataAccess.SelectById(id); // Load(dataTable); //} //---------------------------------------------------------- // 支持继承 public virtual void Load(string id) { BeforeLoad(); Type t2 = this.GetType(); while (t2 != typeof(CEntity)) { TMetaEntity me2 = GetMetaEntity(t2); TMetaTable mt2 = me2.MetaTable; TInheritMappingType inheritType = mt2.InheritMappingType; ///单表继承--单表存贮 if (inheritType == TInheritMappingType.TablePerConcreteClass) { Load(id, me2); if (IsBakMode) { AcceptChanges(me2); } break; } /// 多表继承--分表存贮 else if (inheritType == TInheritMappingType.TablePerSubClass) { Load(id, me2); if (IsBakMode) { AcceptChanges(me2); } t2 = t2.BaseType; } } PersistState = TPersistState.Opened; AfterLoad(); }
//---------------------------------------------------------- public virtual void Load(int no) { //throw new Exception("The method is not implemented."); BeforeLoad(); Type t2 = this.GetType(); while (t2 != typeof(CEntity)) { TMetaEntity me2 = GetMetaEntity(t2); TMetaTable mt2 = me2.MetaTable; TInheritMappingType inheritType = mt2.InheritMappingType; ///单表继承--单表存贮 if (inheritType == TInheritMappingType.TablePerConcreteClass) { Load(no, me2); if (IsBakMode) { AcceptChanges(me2); } break; } /// 多表继承--分表存贮 else if (inheritType == TInheritMappingType.TablePerSubClass) { Load(no, me2); if (IsBakMode) { AcceptChanges(me2); } t2 = t2.BaseType; } } PersistState = TPersistState.Opened; AfterLoad(); }
//---------------------------------------------------------- public void Save(TMetaEntity me) { // PerConcrete与PerSubClass同样都调用函数Save(meta) // 在此函数中,去进一步区分MappingType // NG /// 注意,Meta分级存贮。分表时要补Id TracSave(me); CDataAccess access = Context.GetDataAccess(me); access.Context = this.Context; TMetaTable mt = me.MetaTable; TDataTable table = Context.CreateDataTable(mt); TDataRow row = table.CreateDataRow(); if (PersistState == TPersistState.Added) { //FillRow(me, row, this); this.Push(row, me); table.Rows.Add(row); access.InsertRow(table); AcceptChanges(me); } else if (PersistState == TPersistState.Opened || PersistState == TPersistState.Modified) { if (me.MetaTable.InheritMappingType == TInheritMappingType.TablePerSubClass) { if (!access.ExistId(Id)) { //FillRow(me, row, this); this.Push(row, me); table.Rows.Add(row); access.InsertRow(table); AcceptChanges(me); return; } } if (IsBakMode) { //FillRow(me, row, this.Bak1); this.Bak1.Push(row, me); table.Rows.Add(row); row.AcceptChanges(); } //FillRow(me, row, this); this.Push(row, me); access.EditRow(table); SaveColumnsBrutally(me); /// AcceptChanges(me); } else if (PersistState == TPersistState.MarkDeleted) { //FillRow(me, row, this.Bak1); this.Bak1.Push(row, me); table.Rows.Add(row); row.AcceptChanges(); row.Delete(); access.Delete(table); AcceptChanges(me); } }