📄 abstract.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> </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("ociwrap/ociwrap",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, "A"), <font
color="#FF0000">COCIType</font>(s1, "A_T"));
<font color="#FF0000">COCIObject</font> b(s1, <font
color="#FF0000">COCITable</font>(s1, "B"), <font
color="#FF0000">COCIType</font>(s1, "B_T"));
<font color="#FF0000">COCIObject</font> c(s1, <font
color="#FF0000">COCITable</font>(s1, "C"), <font
color="#FF0000">COCIType</font>(s1, "C_T"));
<font color="#FF0000">COCIObject</font> d(s1, <font
color="#FF0000">COCITable</font>(s1, "D"), <font
color="#FF0000">COCIType</font>(s1, "D_T"));
<font color="#FF0000">COCIObject</font> e(s1, <font
color="#FF0000">COCITable</font>(s1, "E"), <font
color="#FF0000">COCIType</font>(s1, "E_T"));
<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><<font
color="#FF0000">COCIObject</font>> embed(s1, COCIType(s1, "E_T_1"));
<font color="#FF0000">COCIObject</font> n1(s1, <font
color="#FF0000">COCIType</font>(s1, "E_T"));
n1.set("ID",1);
<font color="#FF0000">COCIObject</font> n2(s1, <font
color="#FF0000">COCIType</font>(s1, "E_T"));
n2.set("ID",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<COCINumber> embed(s1, COCIType(s1, "E_T_1"));
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("E",embed);
a.set("B",b.get_ref());
a.set("ID",10);
<font color="#008000">// Set up the B objects' attributes</font>
b.set("A",a.get_ref());
b.set("NAME","Hello World");
<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("1-APR-1999");
c.set("DOB",dob);
c.set("E",embed);
c.set("B",b.get_ref());
c.set("ID",20);
<font color="#008000"> // D has a reference to C, simply set that using the
// reference obtained from C</font>
d.set("C",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("LOB",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("LOB");
<font color="#008000"> // Create some data to store in our lob</font>
<font color="#FF0000">COCIBinary</font> bin("Howdy World2",strlen("Howdy World2"));
<font color="#008000"> // Stream it into the lob (effectively a data sink in this case,
// i.e. we can write to it.)</font>
clob << bin;
e.set("ID",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("ociwrap/ociwrap",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->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 = "<font color="#0000FF">select ref(r) from a r</font>";
<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("ID");
cout << "A.ID = " << id << 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><<font
color="#FF0000">COCIObject</font>> embed(s2, <font
color="#FF0000">COCIType</font>(s2, "E_T_1"));
embed = a.get("E");
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><<font
color="#FF0000">COCIObject</font>>::iterator current = embed.begin();
current != embed.end();
++current)
{
<font color="#FF0000">COCIObject</font> o(s2, <font
color="#FF0000">COCIType</font>(s2, "E_T"));
o = *current;
int id = 0;
id = o.get("ID");
cout << "A.E.ID[" << index << "] = " << id << endl;
index++;
}
<font color="#FF0000">COCIRef</font> b_ref(s2);
b_ref = a.get("B");
<font color="#FF0000">COCIObject</font> b(s2, <font
color="#FF0000">COCIType</font>(s2, "B_T"));
b = *b_ref;
cout << "A.B.NAME = " << (char*)b.get("NAME") << 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 = "<font color="#0000FF">select ref(r) from d r</font>";
<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("LOB");
<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 >> bin;
cout << "D.LOB = " << bin << endl;
<font color="#FF0000">COCIRef</font> c_ref(s2);
c_ref = d.get("C");
<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 = "<font color="#0000FF">declare\
this_obj C_T;\
begin\
this_obj := :1;\
:2 := this_obj.DAYS_ALIVE();\
end;</font>";
int result = 0;
stmt3.bind(1,c);
stmt3.bind(2,result);
if(stmt3.execute())
{
cout << "C.DAYS_ALIVE() = " << result << endl;
}
<font color="#FF0000">COCIDate</font> dob(s2);
dob = (<font color="#FF0000">COCIDate</font>)c.get("DOB");
cout << "D.C.DOB = " << dob.to_text().c_str() << endl;
}while(stmt2.fetch());
}
t.commit();
}</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -