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

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: obndra.c 7001200.1 92/12/20 17:46:18 twang Generic<base> $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      obndra.c - <one-line expansion of the name>
  10.    DESCRIPTION
  11.      <short description of component this file declares/defines>
  12.    PUBLIC FUNCTION(S)
  13.      <list of external functions declared/defined - with one-line descriptions>
  14.    PRIVATE FUNCTION(S)
  15.      <list of static functions defined in .c file - with one-line descriptions>
  16.    RETURNS
  17.      <function return values, for .c file with single function>
  18.    NOTES
  19.      <other useful comments, qualifications, etc.>
  20.    MODIFIED   (MM/DD/YY)
  21.     rkooi2     11/28/92 -  Creation 
  22. */
  23. /*  Example program demonstrating an array insert using the obndra
  24.     example from Chapter 4 of the OCI Programmer's Guide. */
  25.  
  26. #include <stdio.h>
  27. #include <oratypes.h>
  28. #include <ocidfn.h>
  29. #include <ocidem.h>
  30.  
  31. Cda_Def cda;
  32. Lda_Def lda;
  33.  
  34.  
  35. /*  set up the table */
  36. text *dt = (text *) "DROP TABLE part_nos";
  37. text *ct = (text *) "CREATE TABLE part_nos (partno NUMBER, description\
  38.             VARCHAR2(20))";
  39.  
  40. text *cp = (text *) "\
  41.     CREATE OR REPLACE PACKAGE update_parts AS\
  42.         TYPE part_number IS TABLE OF part_nos.partno%TYPE\
  43.             INDEX BY BINARY_INTEGER;\
  44.         TYPE part_description IS TABLE OF part_nos.description%TYPE\
  45.             INDEX BY BINARY_INTEGER;\
  46.         PROCEDURE add_parts (n            IN   INTEGER,\
  47.                              descrip      IN   part_description,\
  48.                              partno       IN   part_number);\
  49.     END update_parts;";
  50.  
  51. text *cb = (text *) "\
  52.     CREATE OR REPLACE PACKAGE BODY update_parts AS\
  53.         PROCEDURE add_parts (n            IN   INTEGER,\
  54.                              descrip      IN   part_description,\
  55.                              partno       IN   part_number) is\
  56.         BEGIN\
  57.             FOR i IN 1..n LOOP\
  58.                 INSERT INTO part_nos\
  59.                     VALUES (partno(i), descrip(i));\
  60.             END LOOP;\
  61.         END add_parts;\
  62.     END update_parts;";
  63.  
  64.  
  65. #define DESC_LEN             20
  66. #define MAX_TABLE_SIZE     1200
  67.  
  68. text *pl_sql_block = (text *) "\
  69.     BEGIN\
  70.         update_parts.add_parts(3, :description, :partno);\
  71.     END;";
  72.  
  73. text descrip[3][20] = {"Frammis", "Widget", "Thingie"};
  74. sword  numbers[] = {12125, 23169, 12126};
  75.  
  76. ub2  descrip_alen[3] = {DESC_LEN, DESC_LEN, DESC_LEN};
  77. ub2  descrip_rc[3];
  78. ub4  descrip_cs = (ub4) 3;
  79. ub2  descrip_indp[3];
  80.  
  81. ub2 num_alen[3] = {(ub2) sizeof (sword),
  82.                               (ub2) sizeof (sword),
  83.                               (ub2) sizeof (sword)};
  84. ub2  num_rc[3];
  85. ub4  num_cs = (ub4) 3;
  86. ub2  num_indp[3];
  87.  
  88. dvoid oci_error(void);
  89.  
  90. main()
  91. {
  92.     printf("Connecting to ORACLE...");
  93.     if (olon(&lda, "scott/tiger", -1, NULL, -1, -1)) {
  94.         printf("Cannot logon as scott/tiger. Exiting...\n");
  95.         exit(1);
  96.     }
  97.  
  98.     if (oopen(&cda, &lda, NULL, -1, -1, NULL, -1)) {
  99.         printf("Cannot open cursor, exiting...\n");
  100.  
  101.         exit(1);
  102.     }
  103.  
  104.     /*  Drop the table. */
  105.     printf("\nDropping table...");
  106.     if (oparse(&cda, dt, (sb4) -1, 0, (ub4) 2))
  107.         if (cda.rc != 942)
  108.             oci_error();
  109.     printf("\nCreating table...");
  110.     if (oparse(&cda, ct, (sb4) -1, 0, (ub4) 2))
  111.         oci_error();
  112.  
  113.     /*  Parse and execute the create
  114.         package statement. */
  115.     printf("\nCreating package...");
  116.     if (oparse(&cda, cp, (sb4) -1, 0, (ub4) 2))
  117.         oci_error();
  118.     if (oexec(&cda))
  119.         oci_error();
  120.  
  121.     /*  Parse and execute the create
  122.         package body statement. */
  123.     printf("\nCreating package body...");
  124.     if (oparse(&cda, cb, (sb4) -1, 0, (ub4) 2))
  125.         oci_error();
  126.     if (oexec(&cda))
  127.         oci_error();
  128.  
  129.     /*  Parse the anonymous PL/SQL block
  130.         that calls the stored procedure. */
  131.     printf("\nParsing PL/SQL block...");
  132.     if (oparse(&cda, pl_sql_block, (sb4) -1, 0, (ub4) 2))
  133.         oci_error();
  134.  
  135.     /*  Bind the C arrays to the PL/SQL tables. */
  136.     printf("\nBinding arrays...");
  137.     if (obndra(&cda,
  138.                (text *) ":description",
  139.                -1,
  140.                (ub1 *) descrip,
  141.                DESC_LEN,
  142.                VARCHAR2_TYPE,
  143.                -1,
  144.                descrip_indp,
  145.                descrip_alen,
  146.                descrip_rc,
  147.                (ub4) MAX_TABLE_SIZE,
  148.                &descrip_cs,
  149.                (text *) 0,
  150.                -1,
  151.                -1))
  152.             oci_error();
  153.  
  154.     if (obndra(&cda,
  155.                (text *) ":partno",
  156.                -1,
  157.                (ub1 *) numbers,
  158.                (sword) sizeof (sword),
  159.                INT_TYPE,
  160.                -1,
  161.                num_indp,
  162.                num_alen,
  163.                num_rc,
  164.                (ub4) MAX_TABLE_SIZE,
  165.                &num_cs,
  166.                (text *) 0,
  167.                -1, 
  168.                -1))
  169.             oci_error();
  170.  
  171.     printf("\nExecuting block...");
  172.     if (oexec(&cda))
  173.         oci_error();
  174.  
  175.     printf("\n");
  176.     if (oclose(&cda))
  177.     {
  178.         printf("Error closing cursor!\n");
  179.         return -1;
  180.     }
  181.  
  182.     if (ologof(&lda))
  183.     {
  184.         printf("Error logging off!\n");
  185.         return -1;
  186.     }
  187.     exit(1);
  188. }
  189.  
  190.  
  191. dvoid
  192. oci_error(void)
  193. {
  194.     text msg[600];
  195.     sword rv;
  196.  
  197.     rv = oerhms(&lda, cda.rc, msg, 600);
  198.  
  199.     printf("\n\n%.*s", rv, msg);
  200.     printf("Processing OCI function %s\n", oci_func_tab[cda.fc]);
  201.     if (oclose(&cda))
  202.         printf("Error closing cursor!\n");
  203.     if (ologof(&lda))
  204.         printf("Error logging off!\n");
  205.     exit(1);
  206. }
  207.  
  208.