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

📄 otl3_ex113.htm

📁 otl是c++数据库封装好的一个数据库接口
💻 HTM
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
  <meta content="text/html; charset=iso-8859-1"
 http-equiv="Content-Type">
  <meta content="Sergei Kuchin" name="Author">
  <meta content="Mozilla/4.76 [en] (Win95; U) [Netscape]"
 name="GENERATOR">
  <meta
 content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library"
 name="KeyWords">
  <title>OTL 4.0, Example 113 (OTL stream pooling, Oracle 8/8i)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 113 (OTL stream pooling, Oracle 7/8/8i)</h1>
</center>
<h1>
Example 113 (OTL stream pooling, Oracle 7/8/8i)</h1>
This example demonstrates the OTL <a href="otl3_stream_pooling.htm">stream
pooling</a> for Oracle 8/8i. The same example can be used with Oracle
7,
if #define OTL_ORA7 is used instead of #define OTL_ORA8 (OTL_ORA8I).
<h2>Source Code</h2>
<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>#include &lt;stdio.h&gt;<br><pre>// Uncomment the line below when OCI7 is used with OTL<br>// #define OTL_ORA7 // Compile OTL 4.0/OCI7 <br>#define OTL_ORA8 // Compile OTL 4.0/OCI8<br>#define OTL_STL // turn on OTL in the STL compliance mode<br>#define <a
 href="otl3_compile.htm#OTL_STREAM_POOLING_ON">OTL_STREAM_POOLING_ON</a>&nbsp;<br>&nbsp;// turn on OTL stream pooling.<br>&nbsp;// #define OTL_STREAM_POOLING_ON line&nbsp;<br>&nbsp;// can be commented out the number of iterations in<br>&nbsp;// the select() loop can be increased, and the difference&nbsp;<br>&nbsp;// in performace with and without OTL_STREAM_POOLING_ON can<br>&nbsp;// be benchmarked. The difference should grow with the overall<br>&nbsp;// number of streams to be used in one program.<br><br>#include &lt;otlv4.h&gt; // include the OTL 4.0 header file<br><br><a
 href="otl3_connect_class.htm">otl_connect</a> db; // connect object<br><br>void insert()<br>// insert rows into table<br>{&nbsp;<br>&nbsp;<a
 href="otl3_stream_class.htm">otl_stream</a> o(50, // buffer size<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "insert into test_tab values(:f1&lt;int&gt;,:f2&lt;char[31]&gt;)",&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SQL statement<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db // connect object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<br>&nbsp;char tmp[32];<br><br>&nbsp;for(int i=1;i&lt;=100;++i){<br>&nbsp; sprintf(tmp,"Name%d",i);<br>&nbsp; o&lt;&lt;i&lt;&lt;tmp;<br>&nbsp;}<br>#ifdef OTL_STREAM_POOLING_ON<br>&nbsp;o.<a
 href="otl3_stream_class.htm#close">close</a>(false); // do not save the stream in the stream pool.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // in other words, destroy it on the spot, since<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the stream is not going to be reused later.<br>#else<br>&nbsp;o.close();<br>#endif<br>}<br><br>void select()<br>{ // when this function is called in a loop,<br>&nbsp; // on the second iteration of the loop the streams i1, i2 will<br>&nbsp; // will get the instances of the OTL stream from the stream<br>&nbsp; // pool, "fast reopen", so to speak.<br><br>&nbsp;<a
 href="otl3_stream_class.htm">otl_stream</a> i1(50, // buffer size<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "select * from test_tab where f1&gt;=:f11&lt;int&gt; and f1&lt;=:f12&lt;int&gt;*2",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SELECT statement<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db // connect object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );&nbsp;<br>&nbsp;&nbsp; // create select stream<br>&nbsp;<br>&nbsp;<a
 href="otl3_stream_class.htm">otl_stream</a> i2(33, // buffer size<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "select f1,f2 from test_tab where f1&gt;=:f11&lt;int&gt; and f1&lt;=:f12&lt;int&gt;*2",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SELECT statement<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db // connect object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );&nbsp;<br>&nbsp;&nbsp; // create select stream<br><br>// i1 and i2 are NOT similar, because their buffer sizes as well<br>// as SQL statements are not equal. It will generate two entry points in the<br>// OTL stream pool.<br>&nbsp;<br>&nbsp;int f1;<br>&nbsp;char f2[31];<br><br>&nbsp;i1&lt;&lt;2&lt;&lt;2; // assigning :f11 = 2, :f12 = 2<br>&nbsp;&nbsp; // SELECT automatically executes when all input variables are<br>&nbsp;&nbsp; // assigned. First portion of output rows is fetched to the buffer<br><br>&nbsp;while(!i1.eof()){ // while not end-of-data<br>&nbsp; i1&gt;&gt;f1&gt;&gt;f2;<br>&nbsp; cout&lt;&lt;"I1==&gt; f1="&lt;&lt;f1&lt;&lt;", f2="&lt;&lt;f2&lt;&lt;endl;<br>&nbsp;}<br><br>&nbsp;i2&lt;&lt;3&lt;&lt;3; // assigning :f11 = 2, :f12 = 2<br>&nbsp;&nbsp; // SELECT automatically executes when all input variables are<br>&nbsp;&nbsp; // assigned. First portion of output rows is fetched to the buffer<br><br>&nbsp;while(!i2.eof()){ // while not end-of-data<br>&nbsp; i2&gt;&gt;f1&gt;&gt;f2;<br>&nbsp; cout&lt;&lt;"I2==&gt; f1="&lt;&lt;f1&lt;&lt;", f2="&lt;&lt;f2&lt;&lt;endl;<br>&nbsp;}<br><br>} // destructors of i1, i2 will call the close()<br>&nbsp; // function for both of the streams and the OTL stream<br>&nbsp; // instances will be placed in the stream pool.<br><br>int main()<br>{<br>&nbsp;<a
 href="otl3_connect_class.htm">otl_connect::otl_initialize</a>(); // initialize the environment<br>&nbsp;try{<br><br>&nbsp; db.rlogon("scott/tiger"); // connect to the database<br>#ifdef OTL_STREAM_POOLING_ON<br>&nbsp; db.<a
 href="otl3_connect_class.htm#set_stream_pool_size">set_stream_pool_size</a>(2);&nbsp;<br>&nbsp;&nbsp; // set the maximum stream pool size and actually initializes&nbsp;<br>&nbsp;&nbsp; // the stream pool.<br>&nbsp;&nbsp; // if this function is not called, the stream pool<br>&nbsp;&nbsp; // gets initialized anyway, with the default size of 32 entries.<br>#endif<br><br>&nbsp; <a
 href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db,<br>&nbsp;&nbsp;&nbsp; "drop table test_tab",<br>&nbsp;&nbsp;&nbsp; otl_exception::disabled // disable OTL exceptions<br>&nbsp;&nbsp; ); // drop table<br><br>&nbsp; <a
 href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db,<br>&nbsp;&nbsp;&nbsp; "create table test_tab(f1 int, f2 varchar(30))"<br>&nbsp;&nbsp;&nbsp; );&nbsp; // create table<br><br>&nbsp; insert(); // insert records into table<br>&nbsp; for(int i=1;i&lt;=10; ++i){<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"===================&gt; Iteration: "&lt;&lt;i&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; select(); // select records from table<br>&nbsp; }<br>&nbsp;}<br><br>&nbsp;catch(<a
 href="otl3_exception_class.htm">otl_exception</a>&amp; p){ // intercept OTL exceptions<br>&nbsp; cerr&lt;&lt;p.msg&lt;&lt;endl; // print out error message<br>&nbsp; cerr&lt;&lt;p.stm_text&lt;&lt;endl; // print out SQL that caused the error<br>&nbsp; cerr&lt;&lt;p.var_info&lt;&lt;endl; // print out the variable that caused the error<br>&nbsp;}<br><br>&nbsp;db.logoff(); // disconnect from the database<br><br>&nbsp;return 0;<br><br>}</pre>
<h2>
Output</h2>
<pre>===================&gt; Iteration: 1<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 2<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 3<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 4<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 5<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 6<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 7<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 8<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 9<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br>===================&gt; Iteration: 10<br>I1==&gt; f1=2, f2=Name2<br>I1==&gt; f1=3, f2=Name3<br>I1==&gt; f1=4, f2=Name4<br>I2==&gt; f1=3, f2=Name3<br>I2==&gt; f1=4, f2=Name4<br>I2==&gt; f1=5, f2=Name5<br>I2==&gt; f1=6, f2=Name6<br><br><br><br><br><br><br><br><br><br><hr
 width="100%"></pre>
<center><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a><a
 href="home.htm">Go<br>Home</a></center>
<p>Copyright &copy; 1996, 2008, Sergei Kuchin, email: <a
 href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,<br><a
 href="mailto:skuchin@gmail.com">skuchin@gmail.com<bgmailript
 language="JavaScript"><!-- hide from old browsers
 var modDate = new Date(document.lastModified)
 document.write("<i> Last Updated:</i> " + (modDate.getMonth()+1) + "/" + 
                modDate.getDate() + "/" + "0"+(modDate.getYear())%100+".");
 //-->
 </script></a>.<br></p>
<p><i>Permission to use, copy, modify and redistribute this document<br>for<br>any purpose is hereby granted without fee, provided that the above<br>copyright<br>notice appear in all copies.</i>
</p>
</pre>
</body>
</html>

⌨️ 快捷键说明

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