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

📄 otl3_ex114.htm

📁 ISO_C++:C++_OTL开发文档
💻 HTM
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
  <meta http-equiv="Content-Type"
 content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Sergei Kuchin">
  <meta name="GENERATOR"
 content="Mozilla/4.76 [en] (Win95; U) [Netscape]">
  <meta name="KeyWords"
 content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
  <title>OTL 4.0, Example 114 (OTL stream pooling, OTL/ODBC)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 114 (OTL stream pooling, OTL/ODBC)</h1>
</center>
<h1>
Example 114 (OTL stream pooling, OTL/ODBC)</h1>
This example demonstrates the OTL <a href="otl3_stream_pooling.htm">stream
pooling</a> for OTL/ODBC. This example works with Oracle ODBC drivers
for
Oracle 8/8i. It also can work with MyODBC/MySQL.
<p>It does NOT work with any of MS SQL Server ODBC drivers, because,
simply
put, they suck. They don't allow the program to keep more than one
fetch
sequence open (SELECT statements, prepared, and ready to be executed
with
new sets of input variables). Even the use of static cursors doesn't
help
in this case.
</p>
<h2>Source Code</h2>
<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>#include &lt;stdio.h&gt;</pre>
<pre>#define OTL_ODBC // Compile OTL 4.0/ODBC<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@my_db"); // 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><hr
 width="100%"></pre>
<center><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a><a
 href="home.htm">Go
Home</a></center>
<p>Copyright &copy; 1996, 2008, Sergei Kuchin, email: <a
 href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,
<a href="mailto:skuchin@gmail.com">skuchin@gmail.com
<script 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>.
</p>
<p><i>Permission to use, copy, modify and redistribute this document
for
any purpose is hereby granted without fee, provided that the above
copyright
notice appear in all copies.</i>
</p>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5456201-1");
pageTracker._trackPageview();
</script>
</body>
</html>

⌨️ 快捷键说明

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