using System;
using System.Collections;
using BP.DA;
namespace BP.En
{
///
/// 属性列表
///
public class EntityOIDAttr
{
///
/// OID
///
public const string OID = "OID";
}
///
/// 属性列表
///
public class EntityOIDMyFileAttr : EntityOIDAttr
{
///
/// MyFileName
///
public const string MyFileName = "MyFileName";
///
/// MyFilePath
///
public const string MyFilePath = "MyFilePath";
///
/// MyFileExt
///
public const string MyFileExt = "MyFileExt";
}
///
/// OID实体,只有一个实体这个实体只有一个主键属性。
///
abstract public class EntityOID : Entity
{
#region 属性
///
/// 是否是自动增长列
///
public virtual bool IsInnKey
{
get
{
return false;
}
}
///
/// 主键
///
public override string PK
{
get
{
return "OID";
}
}
///
/// OID, 如果是空的就返回 0 .
///
public int OID
{
get
{
return this.GetValIntByKey(EntityOIDAttr.OID);
}
set
{
this.SetValByKey(EntityOIDAttr.OID, value);
}
}
#endregion
#region 构造函数
///
/// 构造一个空实例
///
protected EntityOID()
{
}
///
/// 根据OID构造实体
///
/// oid
protected EntityOID(int oid)
{
this.SetValByKey(EntityOIDAttr.OID, oid);
this.Retrieve();
}
#endregion
#region override 方法
public override int DirectInsert()
{
this.OID = DBAccess.GenerOID();
return base.DirectInsert();
}
public void InsertAsNew()
{
this.OID = 0;
this.Insert();
}
public override bool IsExits
{
get
{
if (this.OID == 0)
return false;
try
{
// 生成数据库判断语句。
string selectSQL = "SELECT " + this.PKField + " FROM " + this.EnMap.PhysicsTable + " WHERE OID=" + this.HisDBVarStr + "v";
Paras ens = new Paras();
ens.Add("v", this.OID);
// 从数据库里面查询,判断有没有。
switch (this.EnMap.EnDBUrl.DBUrlType)
{
case DBUrlType.AppCenterDSN:
return DBAccess.IsExits(selectSQL, ens);
default:
throw new Exception("没有设计到。" + this.EnMap.EnDBUrl.DBType);
}
}
catch (Exception ex)
{
this.CheckPhysicsTable();
throw ex;
}
/* DEL BY PENG 2008-04-27
// 生成数据库判断语句。
string selectSQL="SELECT "+this.PKField + " FROM "+ this.EnMap.PhysicsTable + " WHERE " ;
switch(this.EnMap.EnDBUrl.DBType )
{
case DBType.MSSQL:
selectSQL +=SqlBuilder.GetKeyConditionOfMS(this);
break;
case DBType.Access:
selectSQL +=SqlBuilder.GetKeyConditionOfOLE(this);
break;
case DBType.Oracle:
selectSQL +=SqlBuilder.GetKeyConditionOfOracle(this);
break;
default:
throw new Exception("没有设计到。"+this.EnMap.EnDBUrl.DBType);
}
// 从数据库里面查询,判断有没有。
switch(this.EnMap.EnDBUrl.DBUrlType )
{
case DBUrlType.AppCenterDSN:
return DBAccess.IsExits( selectSQL) ;
case DBUrlType.DBAccessOfMSSQL:
return DBAccessOfMSSQL.IsExits( selectSQL) ;
case DBUrlType.DBAccessOfOLE:
return DBAccessOfOLE.IsExits( selectSQL) ;
case DBUrlType.DBAccessOfOracle:
return DBAccessOfOracle.IsExits( selectSQL) ;
default:
throw new Exception("没有设计到。"+this.EnMap.EnDBUrl.DBType);
}
*/
}
}
///
/// 删除之前的操作。
///
///
protected override bool beforeDelete()
{
if (base.beforeDelete() == false)
return false;
try
{
if (this.OID < 0)
throw new Exception("@实体[" + this.EnDesc + "]没有被实例化,不能Delete().");
return true;
}
catch (Exception ex)
{
throw new Exception("@[" + this.EnDesc + "].beforeDelete err:" + ex.Message);
}
}
protected override bool beforeUpdateInsertAction()
{
return base.beforeUpdateInsertAction();
}
///
/// beforeInsert 之前的操作。
///
///
protected override bool beforeInsert()
{
if (this.OID == -999)
return base.beforeInsert();
if (this.OID > 0)
throw new Exception("@[" + this.ToString() + " - " + this.EnDesc + "], 实体已经被实例化 OID=[" + this.OID + "],不能Insert.");
if (this.IsInnKey)
this.OID = -1;
else
this.OID = DBAccess.GenerOID();
return base.beforeInsert();
}
///
/// beforeUpdate
///
///
protected override bool beforeUpdate()
{
if (base.beforeUpdate() == false)
return false;
/*
if (this.OID <= 0 )
throw new Exception("@实体["+this.EnDesc+"]没有被实例化,不能Update().");
*/
return true;
}
protected virtual string SerialKey
{
get
{
return "OID";
}
}
#endregion
#region public 方法
///
/// 作为一个新的实体保存。
///
public void SaveAsNew()
{
try
{
this.OID = DBAccess.GenerOIDByKey32(this.SerialKey);
this.RunSQL(SqlBuilder.Insert(this));
}
catch (System.Exception ex)
{
this.CheckPhysicsTable();
throw ex;
}
}
///
/// 按照指定的OID Insert.
///
public void InsertAsOID(int oid)
{
this.SetValByKey("OID", oid);
try
{
this.RunSQL(SqlBuilder.Insert(this));
}
catch (Exception ex)
{
this.CheckPhysicsTable();
throw ex;
}
}
public void InsertAsOID(Int64 oid)
{
try
{
//先设置一个标记值,为的是不让其在[beforeInsert]产生oid.
this.SetValByKey("OID", -999);
//调用方法.
this.beforeInsert();
//设置主键.
this.SetValByKey("OID", oid);
this.RunSQL(SqlBuilder.Insert(this));
this.afterInsert();
}
catch (Exception ex)
{
this.CheckPhysicsTable();
throw ex;
}
}
///
/// 按照指定的OID 保存
///
///
public void SaveAsOID(int oid)
{
this.SetValByKey("OID", oid);
if (this.Update() == 0)
this.InsertAsOID(oid);
// this.SetValByKey("OID",oid);
// if (this.IsExits==false)
// this.InsertAsOID(oid);
//this.Update();
}
#endregion
}
abstract public class EntitiesOID : Entities
{
///
/// 构造
///
public EntitiesOID()
{
}
#region 查询方法, 专用于与语言有关的实体
///
/// 查询出来, 所有中文的实例 .
///
public void RetrieveAllCNEntities()
{
this.RetrieveByLanguageNo("CH");
}
///
/// 按语言查询。
///
public void RetrieveByLanguageNo(string LanguageNo)
{
QueryObject qo = new QueryObject(this);
qo.AddWhere("LanguageNo", LanguageNo);
qo.DoQuery();
}
#endregion
}
}