⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opbroker.cs

📁 .net的数据持久层
💻 CS
字号:
using System;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Reflection;
using System.Text;

namespace Platform.Data {

	public class OpBroker {

		private Hashtable _EntiyProps;
		private Hashtable _EntiyFields;
		private Hashtable _TableAttributes;
		private Hashtable _Selects;
		private Hashtable _Updates;
		private Hashtable _Deletes;
		private Hashtable _Inserts;
		private Hashtable _Lists;
		private Hashtable _Ids;

		static OpBroker(){
			_Instance = new OpBroker();
		}

		private static OpBroker _Instance;
		public static OpBroker Instance{
			get { return _Instance; }
		}

		public OpBroker() {
			_EntiyFields = new Hashtable();
			_EntiyProps = new Hashtable();
			_TableAttributes = new Hashtable();
			_Selects = new Hashtable();
			_Deletes = new Hashtable();
			_Inserts = new Hashtable();
			_Updates = new Hashtable();
			_Lists = new Hashtable();
			_Ids = new Hashtable();
		}

		public UpdateStatement GetUpdateStatement( object obj ){
			Type type = obj.GetType();
			UpdateStatement sql = _Updates[ type ] as UpdateStatement;
			if( sql == null ){
				sql = new UpdateStatement( obj );
				_Updates[ type ] = sql;
			}
			return (UpdateStatement)sql.Clone();
		}

		public UpdateStatement GetUpdateStatement( object obj,string _TName)
		{
			Type type = obj.GetType();
			UpdateStatement sql = _Updates[ type ] as UpdateStatement;
			if( sql == null )
			{
				sql = new UpdateStatement( obj ,_TName);
				_Updates[ type ] = sql;
			}
			return (UpdateStatement)sql.Clone();
		}

		public InsertStatement GetInsertStatement( object obj ){
			Type type = obj.GetType();
			InsertStatement sql = _Inserts[ type ] as InsertStatement;
			if( sql == null ){
				sql = new InsertStatement( obj );
				_Inserts[ type ] = sql;
			}
			return (InsertStatement)sql.Clone();
		}
		
		public InsertStatement GetInsertStatement( object obj ,string _TName)
		{
			Type type = obj.GetType();
			InsertStatement sql = _Inserts[ type ] as InsertStatement;
			if( sql == null )
			{
				sql = new InsertStatement( obj ,_TName);
				_Inserts[ type ] = sql;
			}
			return (InsertStatement)sql.Clone();
		}

		public SelectStatement GetSelectStatement( object obj ){
			Type type = obj.GetType();
			SelectStatement sql = _Selects[ type ] as SelectStatement;
			if( sql == null ){
				sql = new SelectStatement( obj );
				_Selects[ type ] = sql;
			}
			return (SelectStatement)sql.Clone();
		}

		public ListStatement GetListStatement( object obj ){
			Type type = obj.GetType();
			ListStatement sql = _Lists[ type ] as ListStatement;
			if( sql == null ){
				sql = new ListStatement( obj );
				_Lists[ type ] = sql;
			}
			return (ListStatement)sql.Clone();
		}
		
		public ListStatement GetListStatement( object obj ,string _TName)
		{
			Type type = obj.GetType();
			ListStatement sql = _Lists[ type ] as ListStatement;
			if( sql == null )
			{
				sql = new ListStatement( obj ,_TName);
				_Lists[ type ] = sql;
			}
			return (ListStatement)sql.Clone();
		}

		public DeleteStatement GetDeleteStatement( object obj ){
			Type type = obj.GetType();
			DeleteStatement sql = _Deletes[ type ] as DeleteStatement;
			if( sql == null ){
				sql = new DeleteStatement( obj );
				_Deletes[ type ] = sql;
			}
			return (DeleteStatement)sql.Clone();
		}
		public DeleteStatement GetDeleteStatement( object obj,string _TName )
		{
			Type type = obj.GetType();
			DeleteStatement sql = _Deletes[ type ] as DeleteStatement;
			if( sql == null )
			{
				sql = new DeleteStatement( obj ,_TName);
				_Deletes[ type ] = sql;
			}
			return (DeleteStatement)sql.Clone();
		}
		public TableAttribute GetTableAttribute( object obj ){
			Type type = obj.GetType();
			TableAttribute ta = _TableAttributes[ type ] as TableAttribute;
			if( ta == null ){
				object[] ts = type.GetCustomAttributes(typeof(TableAttribute), false);
				if( ts == null|| ts.Length == 0 ){
					throw new Exception("object has no TableName Attribute!");
				}
				ta = ((TableAttribute)ts[0]);
				if (typeof(DBO).IsInstanceOfType(obj))	//(obj is DBO)
				{
					ta.Name = ((DBO)obj).PrefixDB + ta.Name;
				}
				
				_TableAttributes[ type ] = ta;
			}
			return ta;
		}

		//修改:返回表名加前缀:数据库名及用户名2005/7/25
		public string GetTableName( object obj ){
//			if (typeof(DBO).IsInstanceOfType(obj))	//(obj is DBO)
//			{
//				return ((DBO)obj).PrefixDB + this.GetTableAttribute(obj).Name;
//			}
//			else
//			{
				return this.GetTableAttribute(obj).Name;
//			}
		}

		public FieldPropCollection GetFields( object obj ){
			Type type = obj.GetType();
			FieldPropCollection pc = _EntiyFields[ type ] as FieldPropCollection;
			if( pc == null ){
				pc = this.ParseFieldProps( type );
				_EntiyFields[ type ] = pc;
			}
			return pc;
		}

		public PropCollection GetProps( object obj ){
			Type type = obj.GetType();
			PropCollection pc = _EntiyProps[ type ] as PropCollection;
			if( pc == null ){
				pc = this.ParseProps( type );
				_EntiyProps[ type ] = pc;
			}
			return pc;
		}

		private FieldPropCollection ParseFieldProps( Type type ){
			FieldPropCollection pc = new FieldPropCollection();
			foreach( PropertyInfo p in type.GetProperties()){
				object[] os = p.GetCustomAttributes(typeof(FieldAttribute), false);
				if( os == null || os.Length==0){
					continue;
				}
				FieldAttribute df = (FieldAttribute)os[0];
				pc.Add( new FieldProp( df.FieldName, df.IsKeyField, df.Size, p ));
			}
			return pc;
		}

		private PropCollection ParseProps( Type type ){
			PropCollection pc = new PropCollection();
			foreach( PropertyInfo p in type.GetProperties()){
				pc.Add( new Prop( p ));
			}
			return pc;
		}

		public bool Retrive( object obj ){
			SelectStatement sql = new SelectStatement( obj );
			sql.FillDataParameter( obj );
			DataSet ds = DataHelper.Query( sql );
			if( ds.Tables[0].Rows.Count <= 0 ){
				return false;
			}
			else{
				Parse( ds.Tables[0].Rows[0], ref obj );
				return true;
			}
		}

		public virtual void Parse( DataRow dr, ref object o ){
			foreach(FieldProp fp in this.GetFields( o ) ){
				object v = Convert.ChangeType( dr[fp.FieldName], fp.Property.PropertyType );
				fp.SetValue( o, v );
			}
		}

		public int Delete( object obj ){
			DeleteStatement sql = this.GetDeleteStatement( obj );
			sql.FillDataParameter( obj );
			return DataHelper.Update( sql );
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="cts"></param>
		/// <param name="_TName"></param>
		/// <param name="_filepath"></param>
		/// <returns></returns>
		public int Delete( object obj , CriteriaCollection cts ,string _TName,string _filepath)
		{
			DeleteStatement sql = this.GetDeleteStatement( obj ,_TName);
			sql.Perform( cts);
			//sql.FillDataParameter( obj );
			return DataHelper.Update( sql ,_filepath);
		}
		public int Update( object obj , CriteriaCollection cts ,string _TName,string _filepath){
			UpdateStatement sql = this.GetUpdateStatement( obj ,_TName);
			sql.Perform( cts);
			//sql.FillDataParameter( obj );
			return DataHelper.Update( sql,_filepath );
		}
		public int Update( object obj )
		{
			UpdateStatement sql = this.GetUpdateStatement( obj );
			sql.FillDataParameter( obj );
			return DataHelper.Update( sql );
		}

		public int Insert( object obj ){
			InsertStatement sql = this.GetInsertStatement( obj );
			sql.FillDataParameter( obj );
			return DataHelper.Update( sql );
		}
		
		public int Insert( object obj ,string _TName,string _filepath)
		{
			InsertStatement sql = this.GetInsertStatement( obj ,_TName);
			sql.FillDataParameter( obj );
			return DataHelper.Update( sql ,_filepath);
		}

		public void PrepareNewId( object obj, string idField, object id)
		{
			FieldPropCollection fps = this.GetFields( obj );
			Debug.Assert( fps [ idField ] != null );
			fps[ idField ].SetValue( obj,  id);
		}


		public int PrepareNewId( object obj, string idField ){
			FieldPropCollection fps = this.GetFields( obj );
			Debug.Assert( fps [ idField ] != null );
			int id = this.GenId( obj, fps[ idField ].FieldName );
			fps[ idField ].SetValue( obj,  id);
			return id;
		}

		public int RemoveList( ICloneable obj, CriteriaCollection cts ){
			ListStatement sql = this.GetListStatement( obj );
			sql.PerformRemove( cts );
			return DataHelper.Update( sql );
		}

		public IList GetList( ICloneable obj ){
			return this.GetList( obj, null );
		}

		public IList GetList( ICloneable obj, CriteriaCollection cts){
			return this.GetList( obj, cts, null);
		}

		public IList GetList( ICloneable obj, CriteriaCollection cts, OrderCriteriaCollection ods){
			return this.GetList( obj, cts, ods, 0, Int32.MaxValue );
		}

		public IList GetList( ICloneable obj, CriteriaCollection cts, OrderCriteriaCollection ods, int from, int to ){
			DataTable dt = this.GetData( obj, cts, ods, from, to );
			IList list = new ArrayList();
			foreach(DataRow dr in dt.Rows){
				object o = obj.Clone();
				this.Parse( dr, ref o );
				list.Add( o );
			}
			return list;
		}

		public DataTable GetData( ICloneable obj, CriteriaCollection cts, OrderCriteriaCollection ods, int from, int to ){
			ListStatement sql = new ListStatement( obj );
			sql.Perform( cts, ods, from, to );
			DataSet ds = DataHelper.Query( sql );
			return ds.Tables[0];
		}
		public DataTable GetData( ICloneable obj, CriteriaCollection cts,string _TName,string _filepath)
		{
			ListStatement sql = new ListStatement( obj ,_TName);
			sql.Perform( cts, null, 0, Int32.MaxValue);
			DataSet ds = DataHelper.Query( sql ,_filepath );
			return ds.Tables[0];
		}
		public DataTable GetData( ICloneable obj,CriteriaCollection cts)
		{
			return GetData(obj,cts,null,0,Int32.MaxValue);
		}
		public DataTable GetData( ICloneable obj)
		{
			return GetData(obj,null);
		}
		public int GenId( object obj, string idField){
			lock(_Ids){
				Type tp = obj.GetType();
				IdItem it = _Ids[ tp ] as IdItem;
				if( it == null ){
					SqlStatement sql =new SqlStatement();
					TableAttribute ta = this.GetTableAttribute(obj);
//					string _TableName = "";
//					if (typeof(DBO).IsInstanceOfType(obj))	//(obj is DBO)
//					{
//						_TableName = ((DBO)obj).PrefixDB + ta.Name;
//					}
//					else
//					{
//						_TableName = ta.Name;
//					}
					FieldPropCollection fps = this.GetFields( obj );
					Debug.Assert( fps[ idField ] != null );
					sql.SqlClause = String.Format("SELECT MAX({0}) FROM {1}", fps[idField ].FieldName, ta.Name);
					object o = DataHelper.QueryScalar( sql );
					int d = (o == null) ? 1 : Convert.ToInt32( o );
					it = new IdItem( ta.Name, d );
					_Ids[ tp ] = it;
				}
				it.Id = it.Id + 1;
				return it.Id;
			}
		}

		public int GetListCount( ICloneable obj, CriteriaCollection cts ){
			ListStatement sql = this.GetListStatement( obj );
			sql.PerformCount( cts );
			object o = DataHelper.QueryScalar( sql );
			return Convert.ToInt32( o );
		}

		public int GetListCount( ICloneable obj, CriteriaCollection cts ,string _TName,string _filepath)
		{
			ListStatement sql = this.GetListStatement( obj ,_TName);
			sql.PerformCount( cts );
			object o = DataHelper.QueryScalar( sql ,_filepath);
			return Convert.ToInt32( o );
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -