2017-02-28 95 views
0

任何建議從進展4GL大師來看看4GL代碼波紋管塊,看看它是否過度完成?可以這樣做,以便更容易跟蹤/更具可讀性?Openedge進度4GL表加入

我TBLA

Report 
6998077 
6998078 
6998097 
7062816 

而且TBLB

+-----------+------------+-----------+ 
| ID  |  Source| Report | 
+-----------+------------+-----------+ 
| 4976117 | 6998077 | 6998077 | 
| 4976118 | 6998078 | 6998078 | 
| 4976137 | 6998097 | 6998097 | 
| 5107798 | 7062816 | 6998078 | 
| 5107799 | 7062816 | 6998097 | 
+-----------+------------+-----------+ 

,我有,在我看來,一個複雜的進展4GL循環:

def temp-table TblTemp no-undo 
field cTypeOfRec  as char 
field Report   as int 
field Source   as int 
field ID    as int 
index key is unique primary Report Source ID. 

procedure SOOptimize: 
    output stream dOut1 to value("/OutPut.txt"). 
    export stream dOut1 delimiter "|" 
    "Report" 
    "Source" 
    "ID". 
    for each TblA no-lock 
      on error undo, return error on stop undo, return error: 
    for each TblB no-lock where 
      TblB.Source = TblA.Report 
      on error undo, return error on stop undo, return error: 

     find TblTemp exclusive-lock where 
      TblTemp.SrcPltSeq = TblA.Report and 
      TblTemp.RptPltSeq = TblB.Report and 
      TblTemp.ID  = TblB.ID 
      no-error. 
     if NOT available TblTemp 
     then do: 
     create TblTemp. 
     assign 
      TblTemp.cTypeOfRec = "From LoopA" 
      TblTemp.SrcPltSeq = TblA.Report 
      TblTemp.RptPltSeq = TblB.Report 
      TblTemp.ID   = TblB.ID. 
     end. 
    end. 
    for each TblB no-lock where 
      TblB.Report = TblA.Report 
      on error undo, return error on stop undo, return error: 
     find TblTemp exclusive-lock where 
      TblTemp.SrcPltSeq = TblB.Source  and 
      TblTemp.RptPltSeq = TblA.Report  and 
      TblTemp.ID  = TblB.ID 
      no-error. 

     if NOT available TblTemp 
     then do: 
     create TblTemp. 
     assign 
      TblTemp.cTypeOfRec = "From LoopB" 
      TblTemp.SrcPltSeq = TblB.Source  
      TblTemp.RptPltSeq = TblA.Report 
      TblTemp.ID   = TblB.PltSrcSeq.   
     end. 
    end. 
    end. 
    for each TblTemp no-lock 
    on error undo, return error on stop undo, return error: 
    export stream dOut1 delimiter "|" 
     TblTemp.  
    end. 
end procedure. 

然後我進步的輸出代碼是:

+------------+---------+---------+---------+ 
| cTypeOfRec | Source | Report | ID | 
+------------+---------+---------+---------+ 
| From LoopA | 6998077 | 6998077 | 4976117 | 
| From LoopA | 6998078 | 6998078 | 4976118 | 
| From LoopB | 7062816 | 6998078 | 5107798 | 
| From LoopA | 6998097 | 6998097 | 4976137 | 
| From LoopB | 7062816 | 6998097 | 5107799 | 
+------------+---------+---------+---------+ 

我對Progress 4GL的知識非常有限。這段代碼似乎過於完成了嗎?它可以更簡單嗎?

我來自SQL背景。所以在SQL中,我可以快速輕鬆地解決這個問題。我的意思是,所有這塊進展代碼基本上只是說,如果從LoopA中,如果在LoopB中爲NULL,否則說「從LoopB」

下面是我提出的SQL等價物:

Select 
case when B.ID is null then 'From LoopA' 
else B.cTypeOfRec 
End "cTypeOfRec" 
, A.* 
from #TblTemp A 
left join (
    select A.*, 'From LoopB' "cTypeOfRec" from (select * from #TblTemp)A 
    left join (
     select B.Source, A.Report, B.ID from #TblA A 
     Inner join #TblB B 
     on B.Report=A.Report)B 
    on A.Source = B.Report 
    where B.Source is null) B 
on A.Report=B.Report 
and a.ID = b.ID 
and a.Source= b.Source 
order by A.Report 
, case when B.ID is null then 'From LoopA' 
else B.cTypeOfRec 
End 

從進度4GL大師任何意見採取的上述4GL代碼塊上偷看,看看它是否過於做了什麼?可以這樣做,以便更容易遵循/可讀?

但我對任何回答都很開放,如果這是在4GL中編寫代碼以實現最終結果的正確方法,那麼我就可以。

謝謝謝謝

回答

1

你似乎要經歷兩次TblA和TblB。相反,爲每個TblB創建一個TblTemp。然後通過TblA查看它是否符合TblTemp記錄。您可以將臨時表邏輯移出到自己的過程。

def temp-table TblTemp no-undo 
field cTypeOfRec  as char 
field RptPltSeq  as int 
field SrcPltSeq  as int 
field ID    as int 
index key is unique primary RptPltSeq SrcPltSeq ID. 

define stream dOut1. 

run SOOptimize. 

procedure SOOptimize: 
    output stream dOut1 to value("OutPut.txt"). 
    export stream dOut1 delimiter "|" 
    "Report" 
    "Source" 
    "ID". 

    for each TblB no-lock: 
    run updateTempRec (input "From LoopB", input TblB.Source, input TblB.Report, input TblB.ID). 
    end. 

    for each TblA no-lock: 
    run updateTempRec (input "From LoopA", input TblA.Report, input TblA.Report, input 0).  
    end. 

    for each TblTemp no-lock: 
    export stream dOut1 delimiter "|" 
     TblTemp.  
    end. 
end procedure. 

procedure updateTempRec: 
    define input parameter pcType as character no-undo. 
    define input parameter piSrc as integer no-undo. 
    define input parameter piRpt as integer no-undo. 
    define input parameter piID as integer no-undo. 

    find first TblTemp where 
     TblTemp.SrcPltSeq = piSrc and 
     TblTemp.RptPltSeq = piRpt 
     no-error. 

    if available(TblTemp) then 
     TblTemp.cTypeOfRec = pcType. 
    else 
     if piID <> 0 then 
     do: 
     create TblTemp. 

     assign 
      TblTemp.cTypeOfRec = pcType 
      TblTemp.SrcPltSeq = piSrc  
      TblTemp.RptPltSeq = piRpt 
      TblTemp.ID   = piID. 
     end. 
end procedure. 
0

你可以寫類似下面的

for each TblA no-lock, 
    each TblB no-lock where 
      TblB.Source = TblA.Report 
     or TblB.Report = TblA.Report 
      on error undo, return error on stop undo, return error: 
    /* ... */ 
end. 

我不知道這是否已經有所幫助,即。如果你可以跳過臨時表。至少對於一個TblA記錄,您不應該看到相同的TblB記錄兩次(否則將出現TblB.Source = TblB.Report記錄)。 如果我在第一for each TblBTblA.ReportTblB.ReportTblB.Source取代TblA.Report那麼唯一的區別似乎是TblTemp.cTypeOfRecTblTemp.ID所以你應該能夠縮短代碼。