Esempio n. 1
0
        /// <summary>
        /// 建立主檔與明細關連。
        /// </summary>
        /// <param name="provider">資料表關連資訊提供者。</param>
        /// <param name="detailTableName">明細資料表名稱。</param>
        private void BuildDetailTableJoin(GTableJoinProvider provider, string detailTableName)
        {
            // 建立資料表關連
            var oTableJoin = new GTableJoin
            {
                Key             = "Detail",
                LeftTableAlias  = "A",
                LeftFieldName   = SysFields.ID,
                RightTableName  = detailTableName,
                RightTableAlias = "DA",
                RightFieldName  = SysFields.MasterID
            };

            provider.TableJoins.Add(oTableJoin);
        }
Esempio n. 2
0
        /// <summary>
        /// 建立資料表關連。
        /// </summary>
        /// <param name="key">關連鍵值。</param>
        /// <param name="provider">資料表關連資訊提供者。</param>
        /// <param name="fieldDefine">關連欄位定義。</param>
        /// <param name="returnFields">關連取回欄位集合。</param>
        /// <param name="leftTableAlias">左側資料表別名。</param>
        /// <param name="detailTableName">明細資料表名稱。</param>
        /// <param name="destFieldName">目的欄位名稱。</param>
        private void BuildTableJoin(string key, GTableJoinProvider provider, GFieldDefine fieldDefine, GStringHashSet returnFields, string leftTableAlias, string detailTableName, string destFieldName = "")
        {
            // 取得關連程式定義
            var programDefine = CacheFunc.GetProgramDefine(fieldDefine.LinkProgID);

            if (BaseFunc.IsNull(programDefine))
            {
                throw new GException("'{0}' ProgramDefine not found", fieldDefine.LinkProgID);
            }

            // 取得關連資料表定義
            var tableDefine = programDefine.MasterTable;

            foreach (string fieldName in returnFields)
            {
                var linkReturnField = fieldDefine.LinkReturnFields.FindByDestField(fieldName);
                if (BaseFunc.IsNull(linkReturnField))
                {
                    throw new GException("'{0}' FieldDefine's LinkReturnFields not find DestField '{1}'", fieldDefine.FieldName, fieldName);
                }

                var sourceFieldDefine = tableDefine.Fields[linkReturnField.SourceField];
                if (BaseFunc.IsNull(sourceFieldDefine))
                {
                    throw new GException("'{0}' TableDefine not find '{1}' FieldDefine", tableDefine.TableName, linkReturnField.SourceField);
                }

                if (sourceFieldDefine.FieldType == EFieldType.VirtualField)
                {
                    throw new GException("'{0}' TableDefine's '{1}' FieldDefine not allow VirtualField", tableDefine.TableName, sourceFieldDefine.FieldName);
                }

                var tableJoin = provider.TableJoins[key];
                if (BaseFunc.IsNull(tableJoin))
                {
                    // 建立資料表關連
                    tableJoin = new GTableJoin
                    {
                        Key             = key,
                        LeftTableAlias  = leftTableAlias,
                        LeftFieldName   = tableDefine.GetLinkReturnActiveField(fieldDefine).DbFieldName,
                        RightTableName  = tableDefine.DbTableName,
                        RightTableAlias = GetActiveTableAlias()
                    };
                    var sKeyField = tableDefine.Fields.Contains(SysFields.ID) ? SysFields.ID : SysFields.RowID;
                    tableJoin.RightFieldName = tableDefine.Fields[sKeyField].DbFieldName;
                    if (tableDefine.Fields.Contains(SysFields.CompanyID))
                    {
                        tableJoin.RightCompanyID = tableDefine.Fields[SysFields.CompanyID].DbFieldName;
                    }
                    else if (tableDefine.Fields.Contains(SysFields.CommonCompanyID))
                    {
                        tableJoin.RightCompanyID = tableDefine.Fields[SysFields.CommonCompanyID].DbFieldName;
                    }
                    provider.TableJoins.Add(tableJoin);
                }

                // 若來源欄位的欄位類型是 LinkField,則需往上階找關連來源
                if (sourceFieldDefine.FieldType == EFieldType.LinkField)
                {
                    var linkFieldDefine = tableDefine.GetLinkReturnActiveField(sourceFieldDefine);
                    var sKey            = key + "." + linkFieldDefine.LinkProgID;
                    var returnFieldSet  = new GStringHashSet {
                        sourceFieldDefine.DbFieldName
                    };
                    BuildTableJoin(sKey, provider, linkFieldDefine, returnFieldSet, tableJoin.RightTableAlias, detailTableName, fieldName);
                }
                else
                {
                    // 記錄關連欄位對應
                    var linkFieldMapping = new GLinkFieldMapping();
                    linkFieldMapping.FieldName = (StrFunc.StrIsNotEmpty(destFieldName)) ? destFieldName : fieldName;
                    if (StrFunc.StrIsNotEmpty(detailTableName))
                    {
                        linkFieldMapping.FieldName = StrFunc.StrFormat("{0}.{1}", detailTableName, linkFieldMapping.FieldName);
                    }
                    linkFieldMapping.TableAlias      = tableJoin.RightTableAlias;
                    linkFieldMapping.SourceFieldName = sourceFieldDefine.DbFieldName;
                    provider.Mappings.Add(linkFieldMapping);
                }
            }
        }