Exemplo n.º 1
0
        /// <summary>
        /// 建立符合過濾條件的 Delete 語法的資料庫命令。
        /// </summary>
        /// <param name="tableName">資料表名稱。</param>
        /// <param name="filterItems">資料過濾條件項目集合. </param>
        public virtual DbCommand BuildDeleteCommand(string tableName, GFilterItemCollection filterItems)
        {
            IDbCommandHelper oHelper;
            StringBuilder    oBuffer;
            GTableDefine     oTableDefine;
            GFilterBuilder   oFilterBuilder;
            GFilterInputArgs oFilterInputArgs;
            string           sFilter;
            string           sJoin;

            // 無資料表定義或不啟用資料庫命令,則傳回 Null
            oTableDefine = this.GeGTableDefine(tableName);
            if (BaseFunc.IsNull(oTableDefine))
            {
                return(null);
            }

            // 建立資料庫命令輔助類別
            oHelper = DatabaseFunc.CreateDbCommandHelper(this.DatabaseType);

            oBuffer = new StringBuilder();

            if (this.SessionInfo.DatabaseType == EDatabaseType.Oracle)
            {
                oBuffer.AppendLine(StrFunc.StrFormat("DELETE FROM {0} A ", oTableDefine.DbTableName));
            }
            else
            {
                oBuffer.AppendLine(StrFunc.StrFormat("DELETE FROM {0} FROM {0} A WITH ( ROWLOCK ) ", oTableDefine.DbTableName));
            }

            // 處理過濾條件
            if (!BaseFunc.IsEmpty(filterItems))
            {
                oFilterInputArgs = new GFilterInputArgs(oHelper, this.ProgramDefine, tableName, null, filterItems, this.SessionInfo, true);
                oFilterBuilder   = new GFilterBuilder(oFilterInputArgs);
                sFilter          = oFilterBuilder.GetFilter(out sJoin);

                if (StrFunc.StrIsNotEmpty(sFilter))
                {
                    oBuffer.AppendLine("Where " + sFilter);
                }
            }

            oHelper.SetCommandText(oBuffer.ToString());
            return(oHelper.DbCommand);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 建立 Select 語法的資料庫命令。
        /// </summary>
        /// <param name="tableName">資料表名稱。</param>
        /// <param name="selectFields">要取得的欄位集合字串,以逗點分隔欄位名稱,空字串表示取得所有欄位。</param>
        /// <param name="filterItems">資料過濾條件項目集合。</param>
        /// <param name="userFilter">自訂過濾條件。</param>
        /// <param name="isOrderBy">執行排序</param>
        public virtual DbCommand BuildSelectCommand(string tableName, string selectFields,
                                                    GFilterItemCollection filterItems, string userFilter, bool isOrderBy = true)
        {
            IDbCommandHelper     oHelper;
            GTableJoinProvider   oTableJoinProvider;
            StringBuilder        oBuffer;
            StringBuilder        oFilterBuffer;
            IFilterBuilder       oFilterBuilder;
            GTableDefine         oTableDefine;
            GSortFieldCollection oSortFields;
            GFilterInputArgs     oFilterInputArgs;
            string sCommandText;
            string sFilter;
            string sSort;
            string sJoin;

            // 無資料表定義則離開
            oTableDefine = this.GeGTableDefine(tableName);
            if (BaseFunc.IsNull(oTableDefine))
            {
                return(null);
            }

            // 建立資料庫命令輔助類別
            oHelper = DatabaseFunc.CreateDbCommandHelper(this.DatabaseType);

            // 取得排序欄位集合
            oSortFields = GetSortFields(oTableDefine);

            // 建置資料表關連資訊
            oTableJoinProvider = new GTableJoinProvider();
            oTableJoinProvider.Execute(this.ProgramDefine, tableName, selectFields, filterItems, oSortFields);

            oBuffer = new StringBuilder();

            // 產生 SELECT 欄位的語法
            sCommandText = GetSelectFieldsCommandText(oHelper, oTableJoinProvider, tableName, selectFields);
            oBuffer.AppendLine(sCommandText);


            oFilterBuffer = new StringBuilder();
            // 查詢過濾條件
            oFilterInputArgs = new GFilterInputArgs(oHelper, this.ProgramDefine, tableName, oTableJoinProvider, filterItems, this.SessionInfo, true);
            oFilterBuilder   = new GFilterBuilder(oFilterInputArgs);
            sFilter          = oFilterBuilder.GetFilter(out sJoin);
            AddFilter(oFilterBuffer, sFilter);

            // 自訂過濾條件
            AddFilter(oFilterBuffer, userFilter);

            // 產生 FROM 及 JOIN 語法
            sCommandText = GeGTableJoinCommandText(oHelper, oTableJoinProvider, oTableDefine);
            oBuffer.Append(sCommandText);

            if (StrFunc.StrIsNotEmpty(sJoin))
            {
                oBuffer.Append(sJoin);
            }

            // 加入過濾條件
            if (oFilterBuffer.Length > 0)
            {
                oBuffer.AppendLine(" Where ");
                oBuffer.AppendLine(oFilterBuffer.ToString());
            }

            // 加入排序語法
            if (isOrderBy)
            {
                sSort = GetSortCommandText(oHelper, oTableJoinProvider, oTableDefine, oSortFields);
                if (StrFunc.StrIsNotEmpty(sSort))
                {
                    oBuffer.Append(sSort);
                }
            }

            oHelper.SetCommandText(oBuffer.ToString());
            return(oHelper.DbCommand);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 建構函式。
 /// </summary>
 /// <param name="filterInputArgs">過濾條件字串輸入參數。</param>
 public GFilterBuilder(GFilterInputArgs filterInputArgs)
 {
     Args = filterInputArgs;
 }