一步一步建立我的MIS系統(2).DataObject,所有數據對象的基類

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Text;

namespace EwEAI.Data
{

 /// <summary>
 /// 數據存在類型
 /// </summary>
 public enum RecordExistsType
 {
  FullFill,//具有完全相同的記錄

  PartialFill,//具有部分相同的記錄

  NotExists //沒有相關的記錄
 }


 /// <summary>
 /// RemoteObject:所有數據對象的基類。
 /// </summary>
    public class DataObject
 {
        public DataObject(string tableName,string userName)
  {  
   _TableName=tableName;
            UserName = userName;
  }

        public DataTable GetFieldAuthority()
        {
            string SQL = "select FieldName from FieldPrivileges"
            + " where TableName='" + TableName + "'"
            + "   and UserName in (select UserName from UserTree('" + UserName + "',1))"
            +"    and FieldName not in(select FieldName from FieldPrivileges "
            + "       where UpdatePrivilege=" + ((int)IT.PrivilegeType.DenyUpdate).ToString() + ")";

            return DBWR.Instance.ReadData(SQL);
           
        }

        public DataTable GetTableAuthority()
        {
            string SQL = "select InsertPrivilege,UpdatePrivilege,DeletePrivilege from TablePrivileges "
            + " where TableName='" + TableName + "'"
            + "  and UserName in(select UserName from UserTree('" + UserName + "',1))";

            return DBWR.Instance.ReadData(SQL);             
        }


        public string TableName
        {
            get
            {
                return _TableName;
            }           
        }
        private string _TableName = "";

        protected string UserName = null;

        private string[] Conditions = new string[3] {
            "SelectCondition",
            "UpdateCondition",
            "DeleteCondition"
        };

        public DataTable GetStruct()
        {
            string SQL = DBWR.Instance.BuildSelectSQL(UserName, TableName,"", "", null);
            if (SQL == null)
                return null;

            DataTable Result=DBWR.Instance.GetStruct(SQL);


            return Result;
        }

        public static DataTable GetCaption(string theTable)
        {
            DataTable Fields = DBWR.Instance.ReadData(
                "select FieldName,Caption from Fields where Tablename='" + theTable + "'");

            return Fields;
        }
       

  public DataTable GetData(string where,ref string ReturnSQL)
  {
            string SQL = DBWR.Instance.BuildSelectSQL(UserName, TableName, GetSelectCondition(where), "", null);

   if(SQL==null)
    return null ;

            DataTable Result = new DataTable(TableName);
   
   DBWR.Instance.ReadData(SQL,ref Result);
            SetCalcColumns(ref Result);

            ReturnSQL = SQL;

            return Result;  
  }

        protected virtual void SetCalcColumns(ref DataTable table)
        {
        }

  private string GetSelectCondition(string where)
  {
            string SQL = "select top 1 SelectCondition from ( "
                           + " select P.SelectCondition,U.Level from TablePrivileges P "
                           + "                join UserTree('" + UserName + "',1) U "
                           + "        on P.UserName=U.UserName "
                           + " where P.TableName='" + TableName + "'"
                           + " ) a "
                           + " order by Level ";
         

            string Define=(string)DBWR.Instance.GetSingleValue(SQL);
            StringBuilder Result = new StringBuilder(Define);

            if (Result.Length != 0)
            {
                ParseParameter(Result);             

                Result.Insert(0, "where ");
            }
          

            if (where != null && where != "")
            {
                if (Result.Length != 0)
                {
                    Result.Append(" and ");
                    Result.Append(where);
                }
                else
                {
                    Result.Append("where ");
                    Result.Append(where);
                }
            }

            return Result.ToString();
  }

        private void ParseParameter(StringBuilder Result)
        {
            Result.Replace("@User", "'"+UserName+"'");
            Result.Replace("@ToDay","'"+ DateTime.Now.ToShortDateString()+"'");
        }  
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章