データベースの設定をする…
と言っても、設定と言うか、各ファイルのデータベースの命令を、COBOLのコピー句としてまとめています。
こちらでは、その内容を解説していきます。
※データベースはPervasive SQL ですが、うちの会社で採用したての頃は、Btrieve Ver4 でした。
なので、解説中には、Btrieve と記述していたり、Pervasiveと記述していたりします。
オペレーション コード (固定のワークエリア)
Btrieveのオペレーションを行うためのワークエリアです。
データベースを使用するプログラムでは必ず、使用します。
ファイル名は、CPBTRDT.CPY です。
例えば、Btrieve のファイルオープンのコードは、”0″ ですが、B-OPEN とすることで、わかりやすくなります。
*────────────────────────────────
* Btrieve Operation Codes
*────────────────────────────────
77 B-OPEN PIC 9999 COMP-5 VALUE 0.
77 B-CLOSE PIC 9999 COMP-5 VALUE 1.
77 B-INSERT PIC 9999 COMP-5 VALUE 2.
77 B-UPDATE PIC 9999 COMP-5 VALUE 3.
77 B-DELETE PIC 9999 COMP-5 VALUE 4.
77 B-GETEQ PIC 9999 COMP-5 VALUE 5.
77 B-GETNEXT PIC 9999 COMP-5 VALUE 6.
77 B-GETPREV PIC 9999 COMP-5 VALUE 7.
77 B-GETGT PIC 9999 COMP-5 VALUE 8.
77 B-GETGE PIC 9999 COMP-5 VALUE 9.
77 B-GETLT PIC 9999 COMP-5 VALUE 10.
77 B-GETLE PIC 9999 COMP-5 VALUE 11.
77 B-GETFIR PIC 9999 COMP-5 VALUE 12.
77 B-GETLAST PIC 9999 COMP-5 VALUE 13.
77 B-CREATE PIC 9999 COMP-5 VALUE 14.
77 B-ST PIC 9999 COMP-5 VALUE 15.
*────────────────────────────────
* Variables for Btrieve
*────────────────────────────────
77 B-OPERATE PIC 9999 COMP-5.
77 B-STAT PIC 9999 COMP-5.
77 B-DAT-BUF PIC X(4096).
77 B-BUF-LEN PIC 9999 COMP-5.
77 B-KEY-VAL PIC X(80).
77 B-KEY-NO PIC 9999 COMP-5.
01 II PIC 9(03) COMP.
01 POSITION-BLOCK OCCURS 999.
03 B-POS-BLK PIC X(128).
*────────────────────────────────
* DISP-WORK FOR BTRIEVE ACCESS ERROR
*────────────────────────────────
01 MESSAGE-AREA.
03 ERR-MSG.
05 ERR-OPE PIC X(13).
05 PIC X(02) VALUE SPACE.
05 PIC X(04) VALUE "STS=".
05 ERR-STS PIC 9(02).
05 PIC X(02).
05 ERR-FIL PIC X(30).
03 ERR00 PIC X(13) VALUE "OPEN ERROR".
03 ERR01 PIC X(13) VALUE "CLOSE ERROR".
03 ERR02 PIC X(13) VALUE "INSERT ERROR".
03 ERR03 PIC X(13) VALUE "UPDATE ERROR".
03 ERR04 PIC X(13) VALUE "DELETE ERROR".
03 ERR05 PIC X(13) VALUE "GET-EQ ERROR".
03 ERR06 PIC X(13) VALUE "GETNEXT ERROR".
03 ERR07 PIC X(13) VALUE "GETPREV ERROR".
03 ERR08 PIC X(13) VALUE "GET-GT ERROR".
03 ERR09 PIC X(13) VALUE "GET-GE ERROR".
03 ERR10 PIC X(13) VALUE "GET-LT ERROR".
03 ERR11 PIC X(13) VALUE "GET-LE ERROR".
03 ERR12 PIC X(13) VALUE "GET-FIR ERROR".
03 ERR13 PIC X(13) VALUE "GET-LST ERROR".
03 ERR14 PIC X(13) VALUE "CREATE ERROR".
03 ERR15 PIC X(13) VALUE "STAT ERROR".
77 EOD PIC 9(001).
Btrieve (Pervasive)を呼び出す
実際に、Btrieve(Pervasive)を呼び出して、データベースの操作しているのがこの部分になります。
ただし、設定してしまえば、意識しなくても大丈夫です。
*────────────────────────────────
* Btrieve Call
*────────────────────────────────
BCALL-EN.
DISPLAY ITS-END.
CALL '_BTRV' USING
B-OPERATE, B-STAT, B-POS-BLK (II), B-DAT-BUF,
B-BUF-LEN, B-KEY-VAL, B-KEY-NO.
DISPLAY ITS-START.
BCALL-EX.
EXIT.
BERR-EN.
MOVE B-STAT TO ERR-STS.
MOVE B-KEY-VAL TO ERR-FIL.
DISPLAY "ALARM|23|7|" ERR-MSG.
BERR-EX.
EXIT.
得意先マスタの命令をまとめる
ファイルごとに、オープン命令、リード命令…といった内容を、まとめています。 まとめることで、ファイルをオープンするときは、 PERFORM OPEN-TOKUI THRU OPEN-TOKUI-EX. と記述すればよくなるのです。 ※ソースファイル名は、AAABTTO.CPY です。お客様コード(AAA)+Btrieve(BT)+ファイルの内部コード(TO)。
*────────────────────────────────
* Btrieve Call
*────────────────────────────────
OPEN-TOKUI.
MOVE 2 TO II.
MOVE B-OPEN TO B-OPERATE.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 0 TO B-BUF-LEN.
MOVE FIL-NAME2 TO B-KEY-VAL.
MOVE 0 TO B-KEY-NO.
MOVE ERR00 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
IF B-STAT NOT = 0
PERFORM BERR-EN THRU BERR-EX
END-IF.
OPEN-TOKUI-EX.
EXIT.
CLOSE-TOKUI.
MOVE 2 TO II.
MOVE B-CLOSE TO B-OPERATE.
MOVE 0 TO B-STAT.
MOVE FIL-NAME2 TO B-KEY-VAL.
MOVE 0 TO B-KEY-NO.
MOVE ERR01 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
CLOSE-TOKUI-EX.
EXIT.
STAT-TOKUI.
MOVE 2 TO II.
MOVE B-ST TO B-OPERATE.
MOVE 1024 TO B-BUF-LEN.
MOVE ERR15 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
IF B-STAT NOT = ZERO
PERFORM BERR-EN THRU BERR-EX
END-IF.
STAT-TOKUI-EX.
EXIT.
CREATE-TOKUI.
MOVE 2 TO II.
MOVE B-CREATE TO B-OPERATE.
MOVE FIL-NAME2 TO B-KEY-VAL.
MOVE ERR14 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
IF B-STAT NOT = ZERO
PERFORM BERR-EN THRU BERR-EX
END-IF.
CREATE-TOKUI-EX.
EXIT.
INSERT-TOKUI.
MOVE 2 TO II.
MOVE B-INSERT TO B-OPERATE.
MOVE 0 TO B-KEY-NO.
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR02 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
INSERT-TOKUI-EX.
EXIT.
UPDATE-TOKUI.
MOVE 2 TO II.
MOVE B-UPDATE TO B-OPERATE.
MOVE 0 TO B-KEY-NO.
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR03 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
UPDATE-TOKUI-EX.
EXIT.
DELETE-TOKUI.
MOVE 2 TO II.
MOVE B-DELETE TO B-OPERATE.
MOVE 0 TO B-KEY-NO.
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR04 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
DELETE-TOKUI-EX.
EXIT.
GETEQ-TOKUI.
MOVE 2 TO II.
MOVE B-GETEQ TO B-OPERATE.
MOVE 0 TO B-KEY-NO
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR05 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETEQ-TOKUI-EX.
EXIT.
GETNEXT-TOKUI.
MOVE 2 TO II.
MOVE B-GETNEXT TO B-OPERATE.
MOVE 0 TO B-KEY-NO
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR06 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETNEXT-TOKUI-EX.
EXIT.
GETPREV-TOKUI.
MOVE 2 TO II.
MOVE B-GETPREV TO B-OPERATE.
MOVE 0 TO B-KEY-NO
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR07 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETPREV-TOKUI-EX.
EXIT.
GETGE-TOKUI.
MOVE 2 TO II.
MOVE B-GETGE TO B-OPERATE.
MOVE 0 TO B-KEY-NO
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR09 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETGE-TOKUI-EX.
EXIT.
GETLE-TOKUI.
MOVE 2 TO II.
MOVE B-GETLE TO B-OPERATE.
MOVE 0 TO B-KEY-NO
MOVE TO-KEY TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR11 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETLE-TOKUI-EX.
EXIT.
GETNEXT1-TOKUI.
MOVE 2 TO II.
MOVE B-GETNEXT TO B-OPERATE.
MOVE 1 TO B-KEY-NO
MOVE TO-KEY1 TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR06 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETNEXT1-TOKUI-EX.
EXIT.
GETPREV1-TOKUI.
MOVE 2 TO II.
MOVE B-GETPREV TO B-OPERATE.
MOVE 1 TO B-KEY-NO
MOVE TO-KEY1 TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR07 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETPREV1-TOKUI-EX.
EXIT.
GETGE1-TOKUI.
MOVE 2 TO II.
MOVE B-GETGE TO B-OPERATE.
MOVE 1 TO B-KEY-NO
MOVE TO-KEY1 TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR09 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETGE1-TOKUI-EX.
EXIT.
GETLE1-TOKUI.
MOVE 2 TO II.
MOVE B-GETLE TO B-OPERATE.
MOVE 1 TO B-KEY-NO
MOVE TO-KEY1 TO B-KEY-VAL.
MOVE 0 TO B-STAT.
MOVE TO-REC TO B-DAT-BUF.
MOVE 2030 TO B-BUF-LEN.
MOVE ERR11 TO ERR-OPE.
PERFORM BCALL-EN THRU BCALL-EX.
MOVE B-DAT-BUF TO TO-REC.
GETLE1-TOKUI-EX.
EXIT.