1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
*********************************************************************
* LOB Demo 1: DMV Database *
* *
* SCENARIO: *
* *
* We consider the example of a database used to store driver's *
* licenses. The licenses are stored as rows of a table containing *
* three columns: the sss number of a person, his/her name and the *
* text summary of the info found in his license. *
* *
* The sss number and the name are the unique social security number *
* and name of an individual. The text summary is a summary of the *
* information on the individual, including his driving record, *
* which can be arbitrarily long and may contain comments and data *
* regarding the person's driving ability. *
* *
* APPLICATION OVERVIEW: *
* *
* This example demonstrate how a Pro*COBOL client can handle the *
* new LOB datatypes. Demonstrated are the mechanisms for accessing *
* and storing lobs to/from tables. *
* *
* To run the demo: *
* *
* 1. Execute the script, lobdemo1.sql in Server Manager *
* 2. Precompile using Pro*COBOL *
* procob lobdemo1 *
* 3. Compile/Link (This step is platform specific) *
* *
* lobdemo1.sql contains the following SQL statements: *
* *
* connect scott/tiger; *
* *
* drop table license_table; *
* *
LOB Sample Program: LOBDEMO1.PCO
13-22 Pro*COBOL Programmers Guide
* create table license_table( *
* sss char(9), *
* name varchar2(50), *
* txt_summary clob); *
* *
* insert into license_table *
* values('971517006', 'Dennis Kernighan', *
* 'Wearing a Bright Orange Shirt'); *
* *
* insert into license_table *
* values('555001212', 'Eight H. Number', *
* 'Driving Under the Influence'); *
* *
* insert into license_table *
* values('010101010', 'P. Doughboy', *
* 'Impersonating An Oracle Employee'); *
* *
* insert into license_table *
* values('555377012', 'Calvin N. Hobbes', *
* 'Driving Under the Influence'); *
* *
* The main program provides the menu of actions that can be *
* performed. The program stops when the number 5 (Quit) option *
* is entered. Depending on the input, this main program calls *
* the appropriate nested program to execute the chosen action. *
* *
*********************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. LOBDEMO1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 USERNAME PIC X(5).
01 PASSWD PIC X(5).
01 CHOICE PIC 9 VALUE 0.
01 SSS PIC X(9).
01 SSSEXISTS PIC 9 VALUE ZERO.
01 LICENSE-TXT SQL-CLOB .
01 NEWCRIME PIC X(35) VARYING.
01 SSSCOUNT PIC S9(4) COMP.
01 THE-STRING PIC X(200) VARYING.
01 TXT-LENGTH PIC S9(9) COMP.
01 CRIMES.
05 FILLER PIC X(35) VALUE "Driving Under the Influence".
05 FILLER PIC X(35) VALUE "Grand Theft Auto".
05 FILLER PIC X(35) VALUE "Driving Without a License".
05 FILLER PIC X(35) VALUE
"Impersonating an Oracle Employee".
05 FILLER PIC X(35) VALUE "Wearing a Bright Orange Shirt".
01 CRIMELIST REDEFINES CRIMES.
05 CRIME PIC X(35) OCCURS 5 TIMES.
01 CRIME-INDEX PIC 9.
01 TXT-LEN PIC S9(9) COMP.
01 CRIME-LEN PIC S9(9) COMP.
01 NAME1 PIC X(50) VARYING.
01 NEWNAME PIC X(50).
*********************************************************************
EXEC SQL INCLUDE SQLCA END-EXEC.
PROCEDURE DIVISION.
A000-CONTROL SECTION.
*********************************************************************
* A000-CONTROL
* Overall control section
*********************************************************************
A000-CNTRL.
EXEC SQL
WHENEVER SQLERROR DO PERFORM Z900-SQLERROR
END-EXEC.
PERFORM B000-LOGON.
PERFORM C000-MAIN UNTIL CHOICE = 5.
PERFORM D000-LOGOFF.
A000-EXIT.
STOP RUN.
B000-LOGON SECTION.
*********************************************************************
* B000-LOGON
* Log on to database.
*********************************************************************
B000-LGN.
DISPLAY '**************************************************'.
DISPLAY '* Welcome to the DMV Database *'.
DISPLAY '**************************************************'.
MOVE "scott" TO USERNAME.
MOVE "tiger" TO PASSWD.
EXEC SQL
CONNECT :USERNAME IDENTIFIED BY :PASSWD
END-EXEC.
DISPLAY " ".
DISPLAY "Connecting to license database account: ",
USERNAME, "/", PASSWD.
DISPLAY " ".
B000-EXIT.
EXIT.
C000-MAIN SECTION.
*********************************************************************
* C000-MAIN
* Display the main menu and action requests
*********************************************************************
C000-MN.
DISPLAY " ".
DISPLAY "License Options:".
DISPLAY "1. List available records by SSS number".
DISPLAY "2. Get information on a particular record".
DISPLAY "3. Add crime to a record".
DISPLAY "4. Insert new record to database".
DISPLAY "5. Quit".
DISPLAY " ".
MOVE ZERO TO CHOICE.
PERFORM Z300-ACCEPT-CHOICE UNTIL CHOICE < 6
AND CHOICE > 0.
IF (CHOICE = 1)
PERFORM C100-LIST-RECORDS.
IF (CHOICE = 2)
PERFORM C200-GET-RECORD.
IF (CHOICE = 3)
PERFORM C300-ADD-CRIME.
LOB Sample Program: LOBDEMO1.PCO
13-24 Pro*COBOL Programmers Guide
IF (CHOICE = 4)
PERFORM C400-NEW-RECORD.
C000-EXIT.
EXIT.
C100-LIST-RECORDS SECTION.
*********************************************************************
* C100-LIST-RECORDS
* Select Social Security Numbers from LICENCSE_TABLE
* and display the list
*********************************************************************
C100-LST.
EXEC SQL DECLARE SSS_CURSOR CURSOR FOR
SELECT SSS FROM LICENSE_TABLE
END-EXEC.
EXEC SQL OPEN SSS_CURSOR END-EXEC.
DISPLAY "Available records:".
PERFORM C110-DISPLAY-RECORDS UNTIL SQLCODE = 1403.
EXEC SQL CLOSE SSS_CURSOR END-EXEC.
C100-EXIT.
EXIT.
C110-DISPLAY-RECORDS SECTION.
*********************************************************************
* C110-DISPLAY-RECORDS
* Fetch the next record from the cursor and display it.
*********************************************************************
C110-DSPLY.
EXEC SQL FETCH SSS_CURSOR INTO :SSS END-EXEC.
IF SQLCODE = 0 THEN
DISPLAY SSS.
C110-EXIT.
EXIT.
C200-GET-RECORD SECTION.
*******************************************************************
* C200-GET-RECORD
* Allocates the global clob LICENSE-TXT then selects
* the name and text which corresponds to the client-supplied
* sss. It then calls Z200-PRINTCRIME to print the information and
* frees the clob.
*******************************************************************
C200-GTRECRD.
PERFORM Z100-GET-SSS.
IF (SSSEXISTS = 1)
EXEC SQL ALLOCATE :LICENSE-TXT END-EXEC
EXEC SQL SELECT NAME, TXT_SUMMARY
INTO :NAME1, :LICENSE-TXT FROM LICENSE_TABLE
WHERE SSS = :SSS END-EXEC
DISPLAY "==================================================
- "========================"
DISPLAY " "
DISPLAY "NAME: ", NAME1-ARR, "SSS: ", SSS
DISPLAY " "
PERFORM Z200-PRINTCRIME
DISPLAY " "
DISPLAY "==================================================
- "========================"
EXEC SQL FREE :LICENSE-TXT END-EXEC
ELSE
DISPLAY "SSS Number Not Found".
C200-EXIT.
EXIT.
C310-GETNEWCRIME SECTION.
*******************************************************************
* C310-GETNEWCRIME
* Provides a list of the possible crimes to the user and
* stores the user's correct response in the variable
* NEWCRIME.
*******************************************************************
C310-GTNWCRM.
EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
DISPLAY " ".
DISPLAY "Select from the following:".
PERFORM C311-DISPLAY-CRIME
VARYING CRIME-INDEX FROM 1 BY 1
UNTIL CRIME-INDEX > 5.
MOVE ZERO TO CHOICE.
PERFORM Z300-ACCEPT-CHOICE UNTIL CHOICE < 6
AND CHOICE > 0.
MOVE CRIME(CHOICE) TO NEWCRIME-ARR.
MOVE 35 TO NEWCRIME-LEN.
MOVE ZERO TO CHOICE.
C310-EXIT.
EXIT.
C311-DISPLAY-CRIME SECTION.
*******************************************************************
* C311-DISPLAY-CRIME
* Display an element of the crime table
*******************************************************************
C311-DSPLYCRM.
DISPLAY "(", CRIME-INDEX, ") ", CRIME(CRIME-INDEX).
C311-EXIT.
EXIT.
C320-APPENDTOCLOB SECTION.
*******************************************************************
* C320-APPENDTOCLOB
* Obtains the length of the global clob LICENSE-TXT and
* uses that in the LOB WRITE statement to append the NEWCRIME
* character buffer to the global clob LICENSE-TXT.
* The name corresponding the global SSS is then selected
* and displayed to the screen along with value of LICENSE-TXT.
* The caller to this function must allocate, select and later
* free the global clob LICENSE-TXT.
*******************************************************************
C320-PPNDTCLB.
EXEC SQL
WHENEVER SQLERROR DO PERFORM Z900-SQLERROR
END-EXEC.
EXEC SQL LOB DESCRIBE :LICENSE-TXT GET LENGTH
INTO :TXT-LEN END-EXEC.
MOVE NEWCRIME-LEN TO CRIME-LEN.
LOB Sample Program: LOBDEMO1.PCO
13-26 Pro*COBOL Programmers Guide
IF (TXT-LEN NOT = 0)
ADD 3 TO TXT-LEN
ELSE
ADD 1 TO TXT-LEN.
EXEC SQL LOB WRITE :CRIME-LEN FROM :NEWCRIME
INTO :LICENSE-TXT AT :TXT-LEN END-EXEC.
EXEC SQL SELECT NAME INTO :NAME1 FROM LICENSE_TABLE
WHERE SSS = :SSS END-EXEC.
DISPLAY " ".
DISPLAY "NAME: ", NAME1-ARR, "SSS: ", SSS.
DISPLAY " ".
PERFORM Z200-PRINTCRIME.
DISPLAY " ".
C320-EXIT.
EXIT.
C300-ADD-CRIME SECTION.
*******************************************************************
* ADD-CRIME
* Obtains a sss and crime from the user and appends
* the crime to the list of crimes of the corresponding sss.
*******************************************************************
C300-DDCRM.
EXEC SQL
WHENEVER SQLERROR DO PERFORM Z900-SQLERROR
END-EXEC.
PERFORM Z100-GET-SSS.
IF (SSSEXISTS = 1)
EXEC SQL ALLOCATE :LICENSE-TXT END-EXEC
PERFORM C310-GETNEWCRIME
EXEC SQL SELECT TXT_SUMMARY INTO :LICENSE-TXT
FROM LICENSE_TABLE WHERE SSS = :SSS
FOR UPDATE END-EXEC
PERFORM C320-APPENDTOCLOB
EXEC SQL FREE :LICENSE-TXT END-EXEC
ELSE
DISPLAY "SSS Number Not Found".
C300-EXIT.
EXIT.
C400-NEW-RECORD SECTION.
*******************************************************************
* C400-NEW-RECORD
* Obtains the sss and name of a new record and inserts them
* along with an empty_clob() for the clob in the table.
*******************************************************************
C400-NWRCRD.
PERFORM Z100-GET-SSS.
IF (SSSEXISTS = 1)
DISPLAY "Record with that sss number already exists"
ELSE
DISPLAY "Name? " WITH NO ADVANCING
ACCEPT NEWNAME
DISPLAY " ".
EXEC SQL ALLOCATE :LICENSE-TXT END-EXEC
EXEC SQL INSERT INTO LICENSE_TABLE
VALUES (:SSS, :NEWNAME, EMPTY_CLOB()) END-EXEC
EXEC SQL SELECT TXT_SUMMARY INTO :LICENSE-TXT
FROM LICENSE_TABLE WHERE SSS = :SSS END-EXEC
DISPLAY "==================================================
- "========================"
DISPLAY "NAME: ", NEWNAME,"SSS: ", SSS
PERFORM Z200-PRINTCRIME
DISPLAY "==================================================
- "========================"
EXEC SQL FREE :LICENSE-TXT END-EXEC.
C400-EXIT.
EXIT.
D000-LOGOFF SECTION.
*******************************************************************
* D000-LOGOFF
* Commit the work done to the database and log off
*******************************************************************
D000-LGFF.
EXEC SQL COMMIT WORK RELEASE END-EXEC.
DISPLAY " ".
DISPLAY "HAVE A GOOD DAY!".
DISPLAY " ".
D000-EXIT.
STOP RUN.
Z100-GET-SSS SECTION.
*******************************************************************
* Z100-GET-SSS
* Fills the global variable SSS with the client-supplied sss.
* Sets the global variable SSSEXISTS to 0 if the sss does not
* correspond to any entry in the database, else sets it to 1.
*******************************************************************
Z100-GTSSS.
DISPLAY "Social Security Number? " WITH NO ADVANCING.
ACCEPT SSS.
DISPLAY " ".
EXEC SQL SELECT COUNT(*) INTO :SSSCOUNT FROM LICENSE_TABLE
WHERE SSS = :SSS END-EXEC.
IF (SSSCOUNT = 0)
MOVE 0 TO SSSEXISTS
ELSE
MOVE 1 TO SSSEXISTS.
Z100-EXIT.
EXIT.
Z200-PRINTCRIME SECTION.
*******************************************************************
* Z200-PRINTCRIME
* Obtains the length of the global clob LICENSE-TXT and
* uses that in the LOB READ statement to read the clob
* into a character buffer to display the contents of the clob.
* The caller to this function must allocate, select and later
* free the global clob LICENSE-TXT.
*******************************************************************
Z200-PRNTCRM.
DISPLAY "=====================".
DISPLAY " CRIME SHEET SUMMARY ".
DISPLAY "=====================".
LOB Sample Program: LOBDEMO1.PCO
13-28 Pro*COBOL Programmers Guide
MOVE SPACE TO THE-STRING-ARR.
EXEC SQL LOB DESCRIBE :LICENSE-TXT GET LENGTH
INTO :TXT-LENGTH END-EXEC.
IF (TXT-LENGTH = 0)
DISPLAY "Record is clean"
ELSE
EXEC SQL LOB READ :TXT-LENGTH FROM :LICENSE-TXT
INTO :THE-STRING END-EXEC
DISPLAY THE-STRING-ARR.
Z200-EXIT.
EXIT.
Z300- |
Partager