home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / db / rdbms71 / cdemo3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-09  |  8.3 KB  |  291 lines

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