⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstract.htm

📁 一个通用的oracle OCI开发程序包
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>UML Design</title>
</head>

<body bgcolor="#FFFFC0">

<h1>Abstract Example</h1>

<ul type="disc">
    <li><a href="#UML Design">UML Design</a></li>
    <li><a href="#Oracle Data Definition Language">Oracle Data
        Definition Language</a></li>
    <li><a href="#C++ Code">C++ Code</a></li>
</ul>

<h2><a name="UML Design"></a>UML Design</h2>

<p>The following design is an abstract representation of various
features that can be done with OCI++. It bears no resemblance to
any real-world scenario!</p>

<p align="center"><img src="imagemn2.jpg" width="398"
height="343"></p>

<hr>

<h2 align="left"><a name="Oracle Data Definition Language"></a>Oracle
Data Definition Language</h2>

<p align="left">The following DDL represents the above design.
For a tool that generates DDL try the server generator in
Designer 2000.</p>

<p align="left">(Each object is made up of a table part an
associated type, e.g. Table 'A', is made up of Type 'A_T')</p>

<p align="left"><font color="#0000FF" size="1">CREATE TYPE A_T<br>
/<br>
CREATE TYPE B_T<br>
/<br>
CREATE TYPE C_T<br>
/<br>
CREATE TYPE E_T<br>
/<br>
<br>
<br>
CREATE OR REPLACE TYPE D_T AS OBJECT<br>
(C REF C_T<br>
,LOB CLOB<br>
)<br>
/<br>
<br>
CREATE OR REPLACE TYPE B_T AS OBJECT<br>
(A REF A_T<br>
,NAME VARCHAR2(240)<br>
)<br>
/<br>
<br>
CREATE OR REPLACE TYPE E_T AS OBJECT<br>
(ID NUMBER<br>
)<br>
/<br>
<br>
CREATE TYPE E_T_1 AS TABLE OF E_T<br>
/<br>
<br>
CREATE OR REPLACE TYPE C_T AS OBJECT<br>
(DOB DATE<br>
,E E_T_1<br>
,B REF B_T<br>
,ID NUMBER<br>
,MEMBER FUNCTION DAYS_ALIVE<br>
RETURN NUMBER<br>
<br>
)<br>
/<br>
<br>
CREATE OR REPLACE TYPE A_T AS OBJECT<br>
(E E_T_1<br>
,B REF B_T<br>
,ID NUMBER<br>
)<br>
/<br>
<br>
<br>
CREATE OR REPLACE TYPE BODY C_T IS<br>
MEMBER FUNCTION DAYS_ALIVE<br>
RETURN NUMBER<br>
IS<br>
BEGIN<br>
return (sysdate - DOB);<br>
END DAYS_ALIVE;<br>
<br>
END;<br>
/<br>
<br>
<br>
CREATE TABLE D OF D_T<br>
/<br>
<br>
CREATE TABLE E OF E_T<br>
/<br>
<br>
CREATE TABLE A OF A_T<br>
NESTED TABLE E STORE AS A_NT2<br>
/<br>
<br>
CREATE TABLE B OF B_T<br>
/<br>
<br>
CREATE TABLE C OF C_T<br>
NESTED TABLE E STORE AS C_NT14<br>
/</font></p>

<p>&nbsp;</p>

<h2><a name="C++ Code"></a>C++ Code</h2>

<p>The full code can be found in the demo found in the download.</p>

<h3><a name="Create"></a>Create</h3>

<p>The objects described in the above UML are created on the
server using the following C++ code:-</p>

<pre>void demo_create()
{
  <font color="#008000">// Create a session with the server</font>
  <font color="#FF0000">COCISession</font> s1;
  s1.connect(&quot;ociwrap/ociwrap&quot;,OCI_OBJECT | OCI_THREADED);

<font color="#008000">  // Start a transaction
  // N.B. All objects are alive inside a transaction, don't use
  // them outside of one. The transaction is a unit of database work.</font>
  <font color="#FF0000">COCITransaction</font> t(s1);
  t.start();

  <font color="#008000">// Create the objects in our system.</font>
  <font color="#FF0000">COCIObject</font> a(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;A&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;A_T&quot;));
  <font color="#FF0000">COCIObject</font> b(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;B&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;B_T&quot;));
  <font color="#FF0000">COCIObject</font> c(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;C&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;C_T&quot;));
  <font color="#FF0000">COCIObject</font> d(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;D&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;D_T&quot;));
  <font color="#FF0000">COCIObject</font> e(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;E&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;E_T&quot;));

<font color="#008000">  // Create a nested table and append 2 elements
  // All nested tables are effectively composed of objects, 
  // therefore this method is quite suitable</font>
<font color="#008000">  // Version 1</font>
  <font color="#FF0000">COCINestedTable</font>&lt;<font
color="#FF0000">COCIObject</font>&gt; embed(s1, COCIType(s1, &quot;E_T_1&quot;));
  <font color="#FF0000">COCIObject</font> n1(s1, <font
color="#FF0000">COCIType</font>(s1, &quot;E_T&quot;));
  n1.set(&quot;ID&quot;,1);
  <font color="#FF0000">COCIObject</font> n2(s1, <font
color="#FF0000">COCIType</font>(s1, &quot;E_T&quot;));
  n2.set(&quot;ID&quot;,2);
  embed.append(n1); 
  embed.append(n2);

<font color="#008000">  // As the nested table is made up of numbers, we can choose an 
  // alternative method to construct the nested table, 
  // i.e. construct a nested table of numbers (COCINumber)
  // Version 2
  /*COCINestedTable&lt;COCINumber&gt; embed(s1, COCIType(s1, &quot;E_T_1&quot;));
  COCINumber n1(s1);
  n1 = 1;
  COCINumber n2(s1);
  n2 = 2;
  embed.append(n1); 
  embed.append(n2);*/</font>



  <font color="#008000">// Set up the A objects' attributes</font>
  a.set(&quot;E&quot;,embed);
  a.set(&quot;B&quot;,b.get_ref());
  a.set(&quot;ID&quot;,10);

  <font color="#008000">// Set up the B objects' attributes</font>
  b.set(&quot;A&quot;,a.get_ref());
  b.set(&quot;NAME&quot;,&quot;Hello World&quot;);

<font color="#008000">  // Create a date and store it in the C object
  // This date will be used in a calculation of the number of
  // days alive (DAYS_ALIVE()) function (see demo_query())
  // Set all the other attributes in C</font>
  <font color="#FF0000">COCIDate</font> dob(s1);
  dob.to_date(&quot;1-APR-1999&quot;);
  c.set(&quot;DOB&quot;,dob);
  c.set(&quot;E&quot;,embed);
  c.set(&quot;B&quot;,b.get_ref());
  c.set(&quot;ID&quot;,20);


<font color="#008000">  // D has a reference to C, simply set that using the 
  // reference obtained from C</font>
  d.set(&quot;C&quot;,c.get_ref());

<font color="#008000">  // To perform work on a lob we need to give it to the server
  // (in so doing, it will give us the locator (synonymous with file pointer)
  // for this lob).
  // We must give it a non null attribute (otherwise when we flush and
  // refresh we will still have a null lob!!).</font>
  d.set_attr_ind(&quot;LOB&quot;,OCI_IND_NOTNULL);
  d.flush();
  d.refresh();
  <font color="#FF0000">COCICLob</font> clob(s1);
<font color="#008000">  // Get our lob from the D object after flushing and refreshing the
  // D object to and from the server</font>
  clob = d.get(&quot;LOB&quot;);
<font color="#008000">  // Create some data to store in our lob</font>
  <font color="#FF0000">COCIBinary</font> bin(&quot;Howdy World2&quot;,strlen(&quot;Howdy World2&quot;));
<font color="#008000">  // Stream it into the lob (effectively a data sink in this case, 
  // i.e. we can write to it.)</font>
  clob &lt;&lt; bin;

  e.set(&quot;ID&quot;,30);

  t.commit();

}</pre>

<h3><a name="Query"></a>Query</h3>

<p>The previously generated objects can be queried from the
server using the following piece of code.</p>

<pre>void demo_query()
{

  <font color="#008000">// Create a session with the server</font>
  <font color="#FF0000">COCISession</font> s2;
  s2.connect(&quot;ociwrap/ociwrap&quot;,OCI_OBJECT | OCI_THREADED);

  <font color="#008000">// Create a transaction.</font>
  <font color="#FF0000">COCITransaction</font> t(s2);
  t.start();


  <font color="#008000">// Create a statement</font>
  <font color="#FF0000">COCIStatement</font> stmt(s2);
<font color="#008000">  // Get a reference to the A object
  // i.e. after executing (assuming a reference exists)
  // r-&gt;A  (i.e. r (the reference) points to an A object)
  // The philosophy is then to dereference this reference in order 
  // to retrieve the actual A object (i.e. *r = A). In so doing
  // the A object will be brought from the server into memory.</font>
  stmt = &quot;<font color="#0000FF">select ref(r) from a r</font>&quot;;
  <font color="#FF0000">COCIRef</font> a_ref(s2);
  stmt.define(1,a_ref);
  <font color="#008000">// Have we any data?</font>
  if(stmt.execute())
  {
    <font color="#008000">// Yes...</font>
    do
    {
      <font color="#008000">// Retrieve the A object by dereferencing the reference</font>
      <font color="#FF0000">COCIObject</font> a = *a_ref;
      <font color="#008000">// Get the attributes...</font>
      int id = a.get(&quot;ID&quot;);
      cout &lt;&lt; &quot;A.ID = &quot; &lt;&lt; id &lt;&lt; endl;

<font color="#008000">      // Create a nested table and assign it the value from E in the A
      // object</font>
      <font color="#FF0000">COCINestedTable</font>&lt;<font
color="#FF0000">COCIObject</font>&gt; embed(s2, <font
color="#FF0000">COCIType</font>(s2, &quot;E_T_1&quot;));
      embed = a.get(&quot;E&quot;);

      int index = 0;
<font color="#008000">      // Iterate through the nested table, displaying the elements of it
      // N.B. iteration is based upon STL iteration of collections 
      // (A nested table and VArrays are effectively collections)</font>
      for(<font color="#FF0000">COCINestedTable</font>&lt;<font
color="#FF0000">COCIObject</font>&gt;::iterator current = embed.begin();
          current != embed.end();
          ++current)
      {
          <font color="#FF0000">COCIObject</font> o(s2, <font
color="#FF0000">COCIType</font>(s2, &quot;E_T&quot;));
          o = *current;
          int id = 0;
          id = o.get(&quot;ID&quot;);
          cout &lt;&lt; &quot;A.E.ID[&quot; &lt;&lt; index &lt;&lt; &quot;] = &quot; &lt;&lt; id &lt;&lt; endl;
          index++;
      }

      <font color="#FF0000">COCIRef</font> b_ref(s2);
      b_ref = a.get(&quot;B&quot;);

      <font color="#FF0000">COCIObject</font> b(s2, <font
color="#FF0000">COCIType</font>(s2, &quot;B_T&quot;));
      b = *b_ref;
      cout &lt;&lt; &quot;A.B.NAME = &quot; &lt;&lt; (char*)b.get(&quot;NAME&quot;) &lt;&lt; endl;
    }while(stmt.fetch()); <font color="#008000">// ...while there is more data, fetch some more...</font>
  }

  <font color="#008000">// Create another statement to retrieve the reference to a D object</font>
  <font color="#FF0000">COCIStatement</font> stmt2(s2);
  stmt2 = &quot;<font color="#0000FF">select ref(r) from d r</font>&quot;;
  <font color="#FF0000">COCIRef</font> d_ref(s2);
  stmt2.define(1,d_ref);

  if(stmt2.execute())
  {
    do
    {
      <font color="#FF0000">COCIObject</font> d = *d_ref;

      <font color="#008000">// Get the lob from the D object</font>
      <font color="#FF0000">COCICLob</font> clob(s2);
      clob = d.get(&quot;LOB&quot;);
      <font color="#FF0000">COCIBinary</font> bin;
      bin.resize(20);
      <font color="#008000">// Here the lob acts as a data source (i.e. a we can read from it)</font>
      clob &gt;&gt; bin;
      cout &lt;&lt; &quot;D.LOB = &quot; &lt;&lt;  bin &lt;&lt; endl;

      <font color="#FF0000">COCIRef</font> c_ref(s2);
      c_ref = d.get(&quot;C&quot;);

      <font color="#FF0000">COCIObject</font> c = *c_ref;
   <font color="#008000">   // Here we are going to execute a method on the C object (DAYS_ALIVE())
      // We've previously set a data member with a DOB (1-APR-1999). Now, assuming
      // todays date is the 17-APR-1999, we can determine how long say, this object
      // has been alive (simple calculation (held on the server) SYSDATE - DOB). So,
      // in this case, this will be 16 days.
      // N.B. All variables are input variables.</font>
      <font color="#FF0000">COCIStatement</font> stmt3(s2);
      stmt3 = &quot;<font color="#0000FF">declare\
                this_obj C_T;\
              begin\
                this_obj := :1;\
                :2 := this_obj.DAYS_ALIVE();\
              end;</font>&quot;;

      int result = 0;
      stmt3.bind(1,c);
      stmt3.bind(2,result);
      if(stmt3.execute())
      {
        cout &lt;&lt; &quot;C.DAYS_ALIVE() = &quot; &lt;&lt; result &lt;&lt; endl;
      }


      <font color="#FF0000">COCIDate</font> dob(s2);
      dob = (<font color="#FF0000">COCIDate</font>)c.get(&quot;DOB&quot;);
      cout &lt;&lt; &quot;D.C.DOB = &quot; &lt;&lt; dob.to_text().c_str() &lt;&lt; endl;
    }while(stmt2.fetch());
  }

  t.commit();
}</pre>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -