home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / OCI72 / CDEMO3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  8.3 KB  |  293 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: cdemo3.c 7020100.1 94/09/23 22:19:31 cli Generic<base> $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      cdemo3.c - C demo program # 3
  10.    MODIFIED   (MM/DD/YY)
  11.     emendez    04/07/94 -  merge changes from branch 1.6.710.2
  12.     gdoherty   04/06/94 -  merge changes from branch 1.6.710.1
  13.     emendez    02/02/94 -  Fix for bug 157576
  14.     gdoherty   02/02/94 -  make oci header inclusion for ansi or k+r adaptive
  15.     tssmith    03/29/93 -  Removing ANSI C declarations 
  16.     rkooi2     11/27/92 -  Changing e... datatypes to s... 
  17.     lfeng      11/20/92 -  add portability mods
  18.     rkooi2     10/27/92 -  More portability mods 
  19.     sjain      03/16/92 -  Creation 
  20. */
  21. /*
  22.  *  cdemo3.c
  23.  *
  24.  *  Demonstrates using the oflng function to retrieve
  25.  *  a portion of a LONG column.
  26.  *
  27.  *  This example "plays" a digitized voice message
  28.  *  by repeatedly extracting 64 Kbyte chunks of the message
  29.  *  from the table and sending them to a converter buffer
  30.  *  (for example, a digital-to-analog converter's FIFO buffer).
  31.  *
  32.  *  To better understand this example, the table is created by
  33.  *  the program, and some dummy data is inserted into it.
  34.  *
  35.  *  The converter subroutine is only simulated in this example
  36.  *  program.
  37.  *
  38.  */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42.  
  43. #define MSG_SIZE        200000
  44.  
  45. #include <oratypes.h>
  46. #include <ocidfn.h>
  47. #ifdef __STDC__
  48. #include <ociapr.h>
  49. #else
  50. #include <ocikpr.h>
  51. #endif
  52. #include <ocidem.h>
  53.  
  54. Cda_Def cda;
  55. Lda_Def lda;
  56. ub1     hda[HDA_SIZE];
  57.  
  58.  
  59. dvoid do_exit();
  60. dvoid oci_error();
  61. dvoid play_msg();
  62.  
  63.  
  64. void main()
  65. {
  66.     text sql_statement[256];
  67.     register sword i;
  68.     sb2 indp;
  69.     ub2 retl, rcode;
  70.     sword msg_id;
  71.     sb4 msg_len, len, offset;
  72.     ub1 *ucp;
  73.     register ub1 *ucp1;  
  74.     ub4 ret_len;
  75.     
  76.  
  77.     /* Connect to ORACLE. */
  78.     if (orlon(&lda, hda, (text *) "scott/tiger", -1,
  79.               (text *) 0, -1, 0))
  80.     {
  81.         fputs("Cannot connect with username SCOTT. Exiting...\n", stderr);
  82.         exit(EXIT_FAILURE);
  83.     }
  84.     fputs("Connected to ORACLE as user SCOTT.\n", stdout);
  85.  
  86.  
  87.     /* Open a cursor. */
  88.     if (oopen(&cda, &lda, (text *) 0, -1,
  89.               -1, (text *) 0, -1))
  90.     {
  91.         fputs("Cannot open cursor. Exiting...\n", stderr);
  92.         exit(EXIT_FAILURE);
  93.     }
  94.  
  95.     fputs("Program is about to drop the VOICE_MAIL table.\n", stdout);
  96.     fputs("Is this OK (Y or N)? : ", stdout);
  97.     fflush(stdout);
  98.  
  99.     gets((char *) sql_statement);
  100.     if (*sql_statement != 'y' && *sql_statement != 'Y')
  101.         do_exit(EXIT_SUCCESS);
  102.     
  103.     /* Parse, set defflg parameter for non-deferred parse
  104.        (to execute the DDL statement immediately). */
  105.     if (oparse(&cda, (text *) "DROP TABLE voice_mail", -1, FALSE, 2))
  106.     {
  107.         if (cda.rc == 942)
  108.             fputs("Table did not exist.\n", stdout);
  109.         else
  110.             oci_error(&cda);
  111.     }
  112.     else
  113.         fputs("Dropped table \"voice_mail\".\n", stdout);
  114.  
  115.     strcpy((char *) sql_statement, "CREATE TABLE voice_mail\
  116.         (msg_id NUMBER(6), msg_len NUMBER(12), msg LONG RAW)");
  117.     if (oparse(&cda, sql_statement, -1, FALSE, 2))
  118.         oci_error(&cda);
  119.     fputs("Created table \"voice_mail\".\n", stdout);
  120.  
  121.  
  122.     /* Create a dummy message. */ 
  123.     strcpy((char *) sql_statement,
  124.            "INSERT INTO voice_mail (msg_id, msg_len, msg) \
  125.                            VALUES (:1, :2, :3)");
  126.     if (oparse(&cda, sql_statement, -1, FALSE, 2))
  127.         oci_error(&cda);
  128.  
  129.     if (obndrn(&cda, 1, (ub1 *) &msg_id, 4, 3, -1,
  130.                (sb2 *) 0, (text *) 0, 0, -1))
  131.         oci_error(&cda);
  132.  
  133.     /* Set buffer address before binding. */
  134.     ucp = (ub1 *) malloc(MSG_SIZE);
  135.     if (ucp == 0)
  136.     {
  137.         fputs("malloc error\n", stderr);
  138.         do_exit(EXIT_FAILURE);
  139.     }
  140.  
  141.     if (obndrn(&cda, 2, (ub1 *) &msg_len, 4, 3, -1,
  142.                (sb2 *) 0, (text *) 0, 0, -1))
  143.         oci_error(&cda);
  144.  
  145.     if (obndrn(&cda, 3, ucp, MSG_SIZE, 24, -1,
  146.                (sb2 *) 0, (text *) 0, 0, -1))
  147.         oci_error(&cda);
  148.  
  149.     /* Set bind vars before oexn. */
  150.     msg_id  = 100;     
  151.     msg_len = MSG_SIZE;
  152.     for (i = 0, ucp1 = ucp; i < MSG_SIZE; i++)
  153.         *ucp1++ = (ub1) i % 128;
  154.  
  155.     if (oexn(&cda, 1, 0))
  156.         oci_error(&cda);
  157.     fputs("Data inserted in table \"voice_mail\".\n", stdout);
  158.  
  159. /*
  160.  *  After setting up the test data, do the
  161.  *  select and fetch the message chunks
  162.  */
  163.     strcpy((char *) sql_statement, "select msg_id, msg_len, msg\
  164.                   from voice_mail where msg_id = 100");
  165.     if (oparse(&cda, sql_statement, -1, 0, 2))
  166.         oci_error(&cda);
  167.     if (odefin(&cda,
  168.                1,               /* index */
  169.                (ub1 *) &msg_id, /* output variable */
  170.                4,               /* length */
  171.                3,               /* datatype */
  172.                -1,              /* scale */
  173.                (sb2 *) 0,       /* indp */
  174.                (text *) 0,      /* fmt */
  175.                0,               /* fmtl */
  176.                -1,              /* fmtt */
  177.                (ub2 *) 0,       /* retl */
  178.                (ub2 *) 0))      /* rcode */
  179.         oci_error(&cda);
  180.     if (odefin(&cda,
  181.                2,               /* index */
  182.                (ub1 *) &msg_len,/* output variable */
  183.                4,               /* length */
  184.                3,               /* datatype */
  185.                -1,              /* scale */
  186.                (sb2 *) 0,       /* indp */
  187.                (text *) 0,      /* fmt */
  188.                0,               /* fmtl */
  189.                -1,              /* fmtt */
  190.                (ub2 *) 0,       /* retl */
  191.                (ub2 *) 0))      /* rcode */
  192.         oci_error(&cda);
  193.     if (odefin(&cda,
  194.                3,               /* index */
  195.                ucp,             /* output variable */
  196.                100,             /* length */
  197.                24,              /* LONG RAW datatype code */
  198.                -1,              /* scale */
  199.                &indp,           /* indp */
  200.                (text *) 0,      /* fmt */
  201.                0,               /* fmtl */
  202.                -1,              /* fmtt */
  203.                &retl,           /* retl */
  204.                &rcode))         /* rcode */
  205.         oci_error(&cda);
  206.  
  207.  
  208.     /* Do the query, getting the msg_id and the first
  209.        100 bytes of the message. */
  210.     if (oexfet(&cda,
  211.                (ub4) 1,         /* nrows */
  212.                0,               /* cancel (FALSE) */
  213.                0))              /* exact (FALSE) */
  214.     {
  215.             oci_error(&cda);
  216.     }
  217.     fprintf(stdout,
  218.       "Message %d is available, length is %d.\n", msg_id, msg_len);
  219.     fprintf(stdout,
  220.       "indp = %d, rcode = %d, retl = %d\n", indp, rcode, retl);
  221.  
  222.     /* Play the message, looping until there are
  223.        no more data points to output. */
  224.  
  225.     for (offset = (ub4) 0; ; offset += (ub4) 0x10000)
  226.     {
  227.         len = msg_len < 0x10000 ? msg_len : 0x10000;
  228.  
  229.         if (oflng(&cda,
  230.                   3,                /* position */
  231.                   ucp,              /* buf */
  232.                   len,              /* bufl */
  233.                   24,               /* datatype */
  234.                   &ret_len,         /* retl */
  235.                   offset))          /* offset */
  236.             oci_error(&cda);
  237.  
  238.         /* Output the message chunk. */
  239.         play_msg(ucp, len);
  240.         msg_len -= len;
  241.         if (msg_len <= 0)
  242.             break;
  243.     }
  244.     do_exit(EXIT_SUCCESS);
  245. }
  246.  
  247.  
  248. dvoid
  249. play_msg(buf, len)
  250. ub1 *buf;
  251. sword len;
  252. {
  253.     fprintf(stdout, "\"playing\" %d bytes.\n", len);
  254. }
  255.  
  256.  
  257. dvoid
  258. oci_error(cda)
  259. Cda_Def *cda;
  260. {
  261.     text msg[200];
  262.     sword n;
  263.  
  264.     fputs("\n-- ORACLE ERROR --\n", stderr);
  265.     n = oerhms(&lda, (sb2) cda->rc, msg, 200);
  266.     fprintf(stderr, "%.*s", n, msg);
  267.     fprintf(stderr, "Processing OCI function %s\n",
  268.             oci_func_tab[(int) cda->fc]);
  269.     do_exit(EXIT_FAILURE);
  270. }
  271.  
  272.  
  273. dvoid
  274. do_exit(rv)
  275. sword rv;
  276. {
  277.     fputs("Exiting...\n", stdout);
  278.     if (oclose(&cda))
  279.     {
  280.         fputs("Error closing cursor.\n", stderr);
  281.         rv = EXIT_FAILURE;
  282.     }
  283.     if (ologof(&lda))
  284.     {
  285.         fputs("Error logging off.\n", stderr);
  286.         rv = EXIT_FAILURE;
  287.     }
  288.     exit(rv);
  289. }
  290.  
  291.  
  292.  
  293.