000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID.  ESCAPE-NAME-PATTERN IS COMMON PROGRAM.

000300

000400* This program demonstrates how to convert a table or

000500* procedure name obtained with an InstantSQL browse

000600* statement into a string pattern for use in another

000700* InstantSQL browse statement.  It is written as a

000800* nested program to be contained within the separately

000900* compiled program that uses InstantSQL.  It is called

001000* as follows:

001100*

001200*    CALL "ESCAPE-NAME-PATTERN"

001300*        USING Input-Name Input-Escape

001400*        GIVING Output-Pattern.

001500*

001600* where, the arguments are as shown in the Linkage

001700* Section of this program. The argument descriptions

001800* were chosen such that they conform to InstantSQL

001900* data structures, for example, Input-Escape matches

002000* sql-ConSearchPatternEscGroup in the extended

002100* connection description group.

002200

002300 DATA DIVISION.

002400 WORKING-STORAGE SECTION.

002500 01 WS-IN-OFFSET        PIC S9(4) BINARY.

002600 01 WS-OUT-OFFSET       PIC S9(4) BINARY.

002700

002800 LINKAGE SECTION.

002900 01 INPUT-NAME.

003000    02 IN-LENGTH        PIC S9(9) BINARY.

003100    02 IN-TEXT          PIC  X(sql-TableNameSize).

003200 01 INPUT-ESCAPE.

003300    02 IE-LENGTH        PIC S9(4) LEADING.

003400    02 IE-TEXT          PIC  X(10).

003500 01 OUTPUT-PATTERN.

003600    02 OUT-LENGTH       PIC S9(9) BINARY.

003700    02 OUT-TEXT         PIC  X(sql-TableNameSize).

003800

003900 PROCEDURE DIVISION

004000     USING INPUT-NAME INPUT-ESCAPE

004100     GIVING OUTPUT-PATTERN.

004200

004300 COPY-NAME-AND-ESCAPES.

004400     MOVE -1 TO OUT-LENGTH.

004500     MOVE SPACES TO OUT-TEXT.

004600     MOVE 0 TO WS-OUT-OFFSET.

004700     PERFORM VARYING WS-IN-OFFSET FROM 1 BY 1

004800         UNTIL WS-IN-OFFSET > IN-LENGTH

004900       EVALUATE IN-TEXT(WS-IN-OFFSET:1)

005000         WHEN "_"

005100         WHEN "%"

005200           PERFORM COPY-ESCAPE

005300           PERFORM COPY-NAME-CHAR

005400         WHEN IE-TEXT(1:1)

005500           IF ((WS-IN-OFFSET + IE-LENGTH - 1) <= IN-LENGTH) AND

005600               (IN-TEXT(WS-IN-OFFSET:IE-LENGTH) =

005700                IE-TEXT(1:IE-LENGTH))

005800             PERFORM COPY-ESCAPE

005900             PERFORM COPY-ESCAPE

006000             COMPUTE WS-IN-OFFSET = WS-IN-OFFSET + IE-LENGTH - 1

006100           ELSE

006200             PERFORM COPY-NAME-CHAR

006300           END-IF

006400         WHEN OTHER

006500           PERFORM COPY-NAME-CHAR

006600       END-EVALUATE

006700     END-PERFORM.

006800     MOVE WS-OUT-OFFSET TO OUT-LENGTH.

006900     EXIT PROGRAM.        *> Successful exit.

007000

007100 COPY-NAME-CHAR.

007200     IF WS-OUT-OFFSET < sql-TableNameSize

007300       ADD 1 TO WS-OUT-OFFSET

007400       MOVE IN-TEXT(WS-IN-OFFSET:1) TO OUT-TEXT(WS-OUT-OFFSET:1)

007500     ELSE

007600       EXIT PROGRAM     *> Overflow exit.

007700     END-IF.

007800

007900 COPY-ESCAPE.

008000     IF (WS-OUT-OFFSET + IE-LENGTH - 1) <= LENGTH OF OUT-TEXT

008100       MOVE IE-TEXT(1:IE-LENGTH) TO

008200           OUT-TEXT(WS-OUT-OFFSET + 1:IE-LENGTH)

008300       ADD IE-LENGTH TO WS-OUT-OFFSET

008400     ELSE

008500       EXIT PROGRAM     *> Overflow exit.

008600     END-IF.

008700

008800 END PROGRAM ESCAPE-NAME-PATTERN.