home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a524 / 37.ddi / demo / bank_demo.pc < prev    next >
Encoding:
Text File  |  1991-03-04  |  3.9 KB  |  128 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: bank_demo.pc,v 6.1 90/02/16 18:41:40 nsalah Exp $ bank_demo.pc Copyr (c) 1989 Oracle";
  4. #endif /* RCSID */
  5.  
  6. /* V6PLS10030,DISK$DEV2:[PLS1FREEZE.DEMO.10030] */
  7. /************************************************************************
  8.  *                                              *
  9.  *  EMBEDDED PL/SQL DEBIT TRANSACTION DEMO                *
  10.  *                                                                      *
  11.  *  This program asks the user for an account number and an amount to   *
  12.  *  debit the account.  It makes sure that the account number is valid  *
  13.  *  and that there are sufficient funds to cover the debit before it    *
  14.  *  actually performs the debit.                                        *
  15.  *                                    *
  16.  *  Copyright (c) 1989 by Oracle Corporation.                *
  17.  ************************************************************************/
  18.  
  19. #include <stdio.h>
  20.  
  21.     char   buffer[20];
  22.  
  23. EXEC SQL BEGIN DECLARE SECTION;
  24.     int     acct;                 /* Account number to be debited.  */
  25.     double  debit;                /* Amount to be debited.          */
  26.     double  new_bal;              /* New balance after transaction. */
  27.     varchar status[65];           /* Result of the transaction.     */
  28.     varchar uid[20];
  29.     varchar pwd[20];
  30. EXEC SQL END DECLARE SECTION;
  31.  
  32. EXEC SQL INCLUDE SQLCA;
  33.  
  34. main()
  35. {
  36.     extern double atof();
  37.  
  38.         /* Log on to ORACLE */
  39.     strcpy (uid.arr,"plsqa");
  40.     uid.len=strlen(uid.arr);
  41.     strcpy (pwd.arr,"supersecret");
  42.     pwd.len=strlen(pwd.arr);
  43.  
  44.     printf("\n\n\tEmbedded PL/SQL Debit Transaction Demo\n\n");
  45.     printf("Trying to connect...");
  46.  
  47.     EXEC SQL WHENEVER SQLERROR GOTO errprint;
  48.  
  49.     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  50.     printf(" connected.\n");
  51.  
  52.  
  53. for (;;)    /*  Loop indefinitely */
  54.   {
  55.     printf("\n** Debit which account number? (-1 to end) ");
  56.     gets(buffer);
  57.     acct = atoi(buffer);
  58.     if (acct == -1)     /* exit program if account is -1 */
  59.     {
  60.         EXEC SQL COMMIT WORK RELEASE;    /* log off ORACLE first */
  61.     exit(0);                         /* then exit program    */
  62.     }
  63.  
  64.     printf("   What is the debit amount? ");
  65.     gets(buffer);
  66.     debit = atof(buffer);
  67.  
  68.     /* ---------------------------------- */
  69.     /* ----- Begin the PL/SQL block ----- */
  70.     /* ---------------------------------- */
  71.     EXEC SQL EXECUTE
  72.  
  73.     DECLARE
  74.     insufficient_funds   EXCEPTION;
  75.     old_bal              NUMBER;
  76.     min_bal              CONSTANT NUMBER := 500;
  77.  
  78.     BEGIN
  79.         SELECT bal INTO old_bal FROM accounts
  80.           WHERE account_id = :acct;
  81.            -- If the account doesn't exist, the NO_DATA_FOUND
  82.            -- exception will be automatically raised.
  83.  
  84.         :new_bal := old_bal - :debit;
  85.  
  86.         IF :new_bal >= min_bal THEN
  87.             UPDATE accounts SET bal = :new_bal WHERE account_id = :acct;
  88.             INSERT INTO journal VALUES (:acct, 'Debit', :debit, sysdate);
  89.             :status := 'Transaction completed.';
  90.         ELSE 
  91.             RAISE insufficient_funds;
  92.         END IF;
  93.  
  94.     COMMIT;
  95.  
  96.     EXCEPTION
  97.     WHEN NO_DATA_FOUND THEN
  98.         :status := 'Account not found.';
  99.         :new_bal := -1;
  100.     WHEN insufficient_funds THEN 
  101.         :status := 'Insufficient funds.  Balance cannot be less than $500.';
  102.         :new_bal := old_bal;
  103.         WHEN OTHERS THEN
  104.             ROLLBACK;
  105.             :status := 'Error: ' || SQLERRM;   -- Retrieve full error mesg
  106.             :new_bal := -1;
  107.     END;
  108.  
  109.     END-EXEC;
  110.     /* -------------------------------- */
  111.     /* ----- End the PL/SQL block ----- */
  112.     /* -------------------------------- */
  113.  
  114.     status.arr[status.len] = '\0';          /* null terminate the string */
  115.  
  116.     printf("\n\n   Status:  %s\n", status.arr); 
  117.     if (new_bal >= 0)
  118.         printf("   Balance is now:  $%.2f\n", new_bal);
  119.   }  /* End of loop */
  120.  
  121. errprint:
  122.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  123.     printf("\n\n>>>>> Error during execution:\n");
  124.     printf("%s\n",sqlca.sqlerrm.sqlerrmc);
  125.     EXEC SQL ROLLBACK RELEASE;
  126.     exit(1);
  127. }
  128.