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

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: sample5.pc,v 1002800.2 90/01/05 18:00:08 nsalah Exp $ sample5.pc Copyr (c) 1989 Oracle";
  4. #endif /* RCSID */
  5.  
  6. /* V6PLS10028,DISK$DEV2:[PLS1FREEZE.DEMO.10028] */
  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 does the debit.                                            *
  15.  *                                    *
  16.  *  Copyright (c) 1989 by Oracle Corporation.                *
  17.  ************************************************************************/
  18.  
  19. #include <stdio.h>
  20.  
  21.     char   buf[20];
  22.  
  23. EXEC SQL BEGIN DECLARE SECTION;
  24.     int     acct;
  25.     double  debit;
  26.     double  new_bal;
  27.     varchar status[65];
  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.     strcpy (uid.arr,"plsqa");
  39.     uid.len=strlen(uid.arr);
  40.     strcpy (pwd.arr,"supersecret");
  41.     pwd.len=strlen(pwd.arr);
  42.  
  43.     printf("\n\n\tEmbedded PL/SQL Debit Transaction Demo\n\n");
  44.     printf("Trying to connect...");
  45.  
  46.     EXEC SQL WHENEVER SQLERROR GOTO errprint;
  47.  
  48.     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  49.     printf(" connected.\n");
  50.  
  51.  
  52. for (;;)    /*  Loop infinitely */
  53. {
  54.     printf("\n** Debit which account number? (-1 to end) ");
  55.     gets(buf);
  56.     acct = atoi(buf);
  57.     if (acct == -1)
  58.     exit(0);    /* exit loop if account is -1 */
  59.  
  60.     printf("   What is the debit amount? ");
  61.     gets(buf);
  62.     debit = atof(buf);
  63.  
  64.     /* ---------------------------------- */
  65.     /* ----- Begin the PL/SQL block ----- */
  66.     /* ---------------------------------- */
  67.     EXEC SQL EXECUTE
  68.  
  69.     DECLARE
  70.     insufficient_funds EXCEPTION;
  71.     old_bal         NUMBER;
  72.     min_bal         NUMBER := 500;
  73.  
  74.     BEGIN
  75.         SELECT bal INTO old_bal FROM accounts
  76.                 WHERE account_id = :acct;
  77.            -- If the account doesn't exist, the NO_DATA_FOUND
  78.            -- exception will be automatically raised.
  79.  
  80.         :new_bal := old_bal - :debit;
  81.  
  82.         IF :new_bal >= min_bal THEN
  83.                 UPDATE accounts SET bal = :new_bal
  84.                         WHERE account_id = :acct;
  85.                 INSERT INTO journal 
  86.             VALUES (:acct, 'Debit', :debit, sysdate);
  87.             :status := 'Transaction completed.';
  88.         ELSE 
  89.                 RAISE insufficient_funds;
  90.         END IF;
  91.  
  92.     COMMIT;
  93.  
  94.     EXCEPTION
  95.     WHEN NO_DATA_FOUND THEN
  96.         :status := 'Account not found.';
  97.         :new_bal := -1;
  98.     WHEN insufficient_funds THEN 
  99.         :status := 'Insufficient funds.';
  100.         :new_bal := old_bal;
  101.         WHEN OTHERS THEN
  102.                 ROLLBACK;
  103.                 :status := 'Error: '
  104.                     || SQLERRM(SQLCODE);
  105.                 :new_bal := -1;
  106.     END;
  107.  
  108.     END-EXEC;
  109.     /* -------------------------------- */
  110.     /* ----- End the PL/SQL block ----- */
  111.     /* -------------------------------- */
  112.  
  113.     status.arr[status.len] = '\0';          /* null terminate the string */
  114.     printf("\n\n   Status:  %s\n", status.arr); 
  115.     if (new_bal >= 0)
  116.         printf("   Balance is now:  $%.2f\n", new_bal);
  117. }  /* End of loop */
  118.  
  119. errprint:
  120.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  121.     printf("\n\n>>>>> Error during execution:\n");
  122.     printf("%s\n",sqlca.sqlerrm.sqlerrmc);
  123.     EXEC SQL ROLLBACK RELEASE;
  124.     exit(1);
  125. }
  126.