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

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