home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / ORACLE15.ZIP / SAMPLE.PC < prev    next >
Encoding:
Text File  |  1987-07-06  |  6.8 KB  |  238 lines

  1. /* Copyright (c) 1984 by Oracle Corporation. */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. /*
  7. NAME
  8.   sample
  9. FUNCTION
  10.   Simple PCC sample pgm from PCC HLI Ref Man.
  11. NOTES
  12.  
  13.    sample  is a simple example program which adds new employee
  14.        records to the personnel data base.    Checking
  15.        is done to insure the integrity of the data base.
  16.        The employee numbers are automatically selected using
  17.        the current maximum employee number + 10.
  18.  
  19.        The program queries the user for data as follows:
  20.  
  21.        Enter employee name:
  22.        Enter employee job:
  23.        Enter employee salary:
  24.        Enter employee dept:
  25.  
  26.        The program terminates if control Z (end of file) or null
  27.        string (<return> key) is entered when the employee name
  28.        is requested.
  29.  
  30.        If the record is successfully inserted, the following
  31.        is printed:
  32.  
  33.        ename added to department dname as employee # nnnnnn
  34.  
  35.        To build and run SAMPLE (assumes that SQLCA.H is in your
  36.        current directory) under MS-DOS using the Microsoft 'C'
  37.        compiler:
  38.  
  39.            C> pcc iname=sample.pc host=c
  40.            C> cl -AL -c sample.c
  41.            C> link sample,,,sqlmsc /se:512 /stack:10000 /map;
  42.            C> sample
  43.  
  44.        These guidelines assume that the Microsoft 'C' compiler,
  45.        libraries, and linker are setup according to the compiler's
  46.        User's Guide.
  47.  
  48. OWNER
  49.   Clare
  50. DATE
  51.   05/01/84
  52. MODIFIED
  53.   03/26/87  RPatterson  Updated error handling.
  54.   02/20/86  RPatterson    Port: support Microsoft 'C' compiler and sqlmsc.lib.
  55.   09/19/84  Clare    Port: remove VMSisms.
  56.   12/26/84  Clare    Port IBM: fflush() prompts.
  57. */
  58.  
  59. EXEC SQL BEGIN DECLARE SECTION;
  60. VARCHAR uid[80];
  61.                        /* username                  */
  62. VARCHAR pwd[20];
  63.                        /* password                  */
  64.  
  65. int    empno;                   /* employee number              */
  66. VARCHAR ename[15];
  67.                        /* employee name               */
  68. int    deptno;                /* department number              */
  69. VARCHAR dname[15];
  70.                        /* department name              */
  71.  
  72. VARCHAR job[15];
  73.                        /* employee job                  */
  74. int    sal;                   /* employee salary              */
  75. EXEC SQL END DECLARE SECTION;
  76. EXEC SQL INCLUDE SQLCA;
  77.  
  78. main()
  79. {
  80.  
  81. /* -----------------------------------------------------------------------------
  82.    logon to ORACLE, and open the cursors. The program exits if any errors occur.
  83. ----------------------------------------------------------------------------- */
  84.  
  85.    strcpy(uid.arr,"SCOTT");
  86.    uid.len = strlen(uid.arr);
  87.    strcpy(pwd.arr,"TIGER");
  88.    pwd.len = strlen(pwd.arr);
  89.  
  90.    EXEC SQL WHENEVER SQLERROR GOTO errexit;
  91.    EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  92.  
  93. /* -----------------------------------------------------------------------------
  94.    Retrieve the current maximum employee number
  95. ----------------------------------------------------------------------------- */
  96.  
  97.    EXEC SQL SELECT NVL(MAX(EMPNO),0) + 10
  98.           INTO :empno
  99.           FROM EMP;
  100.  
  101. /* -----------------------------------------------------------------------------
  102.    read the user's input from STDIN.  If the employee name is
  103.    not entered, exit.
  104.    Verify that the entered department number is valid and echo the
  105.    department's name
  106. ----------------------------------------------------------------------------- */
  107.  
  108.    for( ; ; empno+=10 )
  109.      {
  110.      int l;
  111.  
  112.      /* Get employee name to be inserted.
  113.  
  114.     IMPORTANT NOTE: beware of coding as follows (I got burnt, 1st time):
  115.  
  116.       ename.len = asks("Enter employee name  : ", ename.arr);
  117.       if ( ename.len <= 0 )
  118.         ..etc..
  119.  
  120.     In the above, asks() returns an int, but ename.len is an unsigned
  121.     short (see SQLCA). Therefore, the "if" fails for <EOF> (which returns
  122.     -1) because, by definition, the unsigned short can't be negative.
  123.      */
  124.      l = asks("Enter employee name  : ",ename.arr);
  125.  
  126.      if ( l <= 0 )
  127.        break;
  128.  
  129.      ename.len = l;
  130.  
  131.      job.len = asks("Enter employee job   : ",job.arr);
  132.      askn("Enter employee salary: ",&sal);
  133.      for ( ; ; )
  134.        {
  135.        if ( askn("Enter employee dept  :   ",&deptno) < 0 )
  136.      break;
  137.  
  138.        EXEC SQL WHENEVER NOT FOUND GOTO nodept;
  139.        EXEC SQL SELECT DNAME
  140.           INTO :dname
  141.           FROM DEPT
  142.           WHERE DEPTNO = :deptno;
  143.  
  144.        dname.arr[dname.len] = '\0';
  145.  
  146.        EXEC SQL WHENEVER NOT FOUND STOP;
  147.  
  148.        /* Here if deptno was found in dbs. Insert new employee into dbs. */
  149.  
  150.        EXEC SQL INSERT INTO EMP(EMPNO,ENAME,JOB,SAL,DEPTNO)
  151.           VALUES (:empno,:ename,:job,:sal,:deptno);
  152.  
  153.        printf("\n%s added to the %s department as employee number %d\n",
  154.      ename.arr,dname.arr,empno);
  155.        break;
  156.  
  157.        /* Here if deptno NOT found in dbs */
  158.        nodept:
  159.      printf("\nNo such department\n");
  160.      continue;
  161.        }
  162.      }
  163.  
  164. /* -----------------------------------------------------------------------------
  165.    close the cursors and log off from ORACLE
  166. ----------------------------------------------------------------------------- */
  167.  
  168.    EXEC SQL COMMIT WORK RELEASE;
  169.    printf ("\nEnd of the C/ORACLE example program.\n");
  170.    return;
  171.  
  172. errexit:
  173.    errrpt();
  174.    EXEC SQL WHENEVER SQLERROR CONTINUE;
  175.    EXEC SQL ROLLBACK WORK RELEASE;
  176.    return;
  177. }
  178.  
  179. /*------------------------------------------------------------------------------
  180. COUNT askn(text,variable)
  181.  
  182.    print the 'text' on STDOUT and read an integer variable from
  183.    SDTIN.
  184.  
  185.    text points to the null terminated string to be printed
  186.    variable points to an integer variable
  187.  
  188.    askn returns a 1 if the variable was read successfully or a
  189.        -1 if -eof- was encountered
  190. ----------------------------------------------------------------------------- */
  191.  
  192. int askn(text,variable)
  193.    char text[];
  194.    int    *variable;
  195.    {
  196.    char s[20];
  197.    printf(text);
  198.    fflush(stdout);
  199.    if ( gets(s) == (char *)0 )
  200.      return(EOF);
  201.  
  202.    *variable = atoi(s);
  203.    return(1);
  204.    }
  205.  
  206. /* -----------------------------------------------------------------------------
  207. COUNT asks(text,variable)
  208.  
  209.    print the 'text' on STDOUT and read up to 'len' characters into
  210.    the buffer pointed to by variable from STDIN.
  211.  
  212.    text points to the null terminated string to be printed
  213.    variable points to a buffer of at least 'len'+1 characters
  214.  
  215.    asks returns the number of character read into the string, or a
  216.        -1 if -eof- was encountered
  217. ----------------------------------------------------------------------------- */
  218.  
  219. int asks(text,variable)
  220.    char text[],variable[];
  221.    {
  222.    printf(text);
  223.    fflush(stdout);
  224.    return( gets(variable) == (char *)0 ? EOF : strlen(variable) );
  225.    }
  226.  
  227. /* -----------------------------------------------------------------------------
  228. VOID errrpt()
  229.  
  230.    errrpt prints the ORACLE error msg and number.
  231. ----------------------------------------------------------------------------- */
  232.  
  233. errrpt()
  234.    {
  235.    printf("%.70s (%d)\n", sqlca.sqlerrm.sqlerrmc, -sqlca.sqlcode);
  236.    return(0);
  237.    }
  238.