📄 otl3_ex119.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.75 [en] (Win98; U) [Netscape]"
name="GENERATOR">
<meta
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library"
name="KeyWords">
<title>OTL 4.0, Example 119 (OTL_VALUE_TEMPLATE_ON and
OTL_USER_DEFINED_STRING_CLASS_ON, Oracle 7/8/8i)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 119 (<font size="+2">OTL_VALUE_TEMPLATE_ON and
OTL_USER_DEFINED_STRING_CLASS_ON</font>,
Oracle 7/8/8i)</h1>
</center>
<h1>
Example 119 (<font size="+2">OTL_VALUE_TEMPLATE_ON,
OTL_USER_DEFINED_STRING_CLASS_ON</font>,
Oracle 7/8/8i)</h1>
This example demonstrates OTL streams, <a href="otl3_value.htm">otl_value<T></a>,
a third-party string class, with #define <a
href="otl3_compile.htm#OTL_VALUE_TEMPLATE_ON">OTL_VALUE_TEMPLATE_ON</a>
and #define <a href="otl3_compile.htm#OTL_USER_DEFINED_STRING_CLASS_ON">OTL_USER_DEFINED_STRING_CLASS_ON</a>.
<h2>
Source Code</h2>
<pre>#include <iostream><br>#include <string.h><br>using namespace std; <br><pre>class super_fast_string{<br>public:<br><br> super_fast_string(void)<br> {<br> buf[0]=0;<br> }<br><br> super_fast_string(const char* s)<br> {<br> strcpy(buf,s);<br> }<br><br> super_fast_string(const super_fast_string& s)<br> {<br> strcpy(buf,s.buf);<br> }<br><br> ~super_fast_string(){}<br><br> super_fast_string& operator=(const char* s)<br> {<br> strcpy(buf,s);<br> return *this;<br> }<br><br> super_fast_string& operator=(const super_fast_string& s)<br> {<br> strcpy(buf,s.buf);<br> return *this;<br> }<br><br> const char* c_str(void) const<br> {<br> return buf;<br> }<br><br> void assign(char* temp_buf,int len)<br> {<br> for(int i=0;i<len;++i)<br> buf[i]=temp_buf[i];<br> buf[len]=0;<br> }<br><br> char& operator[](int ndx)<br> {<br> return buf[ndx];<br> }<br><br> int length(void) const <br> {<br> return strlen(buf);<br> }<br><br><br>protected:<br><br> char buf[512];<br><br>};<br><br>ostream& operator<<(ostream& s,const super_fast_string& str)<br>{<br> s<<str.c_str();<br> return s;<br>}<br><br>// Uncomment the line below when OCI7 is used with OTL<br>// #define OTL_ORA7 // Compile OTL 4.0/OCI7 <br><br>#define OTL_ORA8 // Compile OTL 4.0/OCI8<br>#define <a
href="otl3_compile.htm#OTL_VALUE_TEMPLATE_ON">OTL_VALUE_TEMPLATE_ON</a> // Turn on otl_value<T><br>#define <a
href="otl3_compile.htm#OTL_USER_DEFINED_STRING_CLASS_ON">OTL_USER_DEFINED_STRING_CLASS_ON<br></a>#define <a
href="otl3_compile.htm#OTL_USER_DEFINED_STRING_CLASS_ON">USER_DEFINED_STRING_CLASS</a> super_fast_string<br>#include <otlv4.h> // 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>{ <br> <a
href="otl3_stream_class.htm">otl_stream</a> o(50, // buffer size<br> "insert into test_tab "<br> "values(:f1<int>,:f2<char[31]>,:f3<timestamp>)", <br> // SQL statement<br> db // connect object<br> );<br><br> <a
href="otl3_value.htm">otl_value</a><super_fast_string> f2; // otl_value container with a string class<br> <a
href="otl3_value.htm">otl_value</a><<a
href="otl3_stream_class.htm#otl_datetime">otl_datetime</a>> f3; // container of otl_datetime<br><br><br> for(int i=1;i<=100;++i){<br><br> if(i%2==0)<br> f2="NameXXX";<br> else<br> f2=otl_null(); // Assign otl_null() to f2<br><br> if(i%3==0){<br> // Assign a value to f3 via the .v field directly<br> f3.v.year=2001;<br> f3.v.month=1;<br> f3.v.day=1;<br> f3.v.hour=13;<br> f3.v.minute=10;<br> f3.v.second=5;<br> f3.set_non_null(); // Set f3 as a "non-NULL"<br> }else<br> f3.set_null(); // Set f3 as null via .set_null() function<br><br> o<<i<<f2<<f3;<br><br> }<br>}<br><br>void select()<br>{ <br> <a
href="otl3_stream_class.htm">otl_stream</a> i(50, // buffer size<br> "select * from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",<br> // SELECT statement<br> db // connect object<br> ); <br> // create select stream<br><br> int f1; <br> <a
href="otl3_value.htm">otl_value</a><super_fast_string> f2;<br> <a
href="otl3_value.htm">otl_value</a><<a
href="otl3_stream_class.htm#otl_datetime">otl_datetime</a>> f3;<br><br><br> i<<8<<8; // assigning :f11 = :f12 = 8<br><br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i.eof()){ // while not end-of-data<br> i>>f1>>f2>>f3;<br> cout<<"f1="<<f1<<", f2="<<f2<<", f3="<<f3<<endl;<br> }<br><br> i<<4<<4; // assigning :f11 = :f12 = 4<br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i.eof()){ // while not end-of-data<br> i>>f1>>f2>>f3;<br> cout<<"f1="<<f1<<", f2="<<f2<<", f3="<<f3<<endl;<br> }<br><br>}<br><br>int main()<br>{<br> <a
href="otl3_connect_class.htm">otl_connect::otl_initialize</a>(); // initialize the database API environment<br> try{<br><br> db.rlogon("scott/tiger"); // connect to teh database<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "drop table test_tab",<br> otl_exception::disabled // disable OTL exceptions<br> ); // drop table<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "create table test_tab(f1 int, f2 varchar(30), f3 date)"<br> ); // create table<br><br> insert(); // insert records into table<br> select(); // select records from table<br><br> }<br><br> catch(<a
href="otl3_exception_class.htm">otl_exception</a>& p){ // intercept OTL exceptions<br> cerr<<p.msg<<endl; // print out error message<br> cerr<<p.sqlstate<<endl; // print out SQLSTATE<br> cerr<<p.stm_text<<endl; // print out SQL that caused the error<br> cerr<<p.var_info<<endl; // print out the variable that caused the error<br> }<br><br> db.logoff(); // disconnect from teh database<br><br> return 0;<br><br>}</pre>
<h2>
Output</h2>
<pre>f1=8, f2=NameXXX, f3=NULL<br>f1=9, f2=NULL, f3=1/1/2001 13:10:5<br>f1=10, f2=NameXXX, f3=NULL<br>f1=11, f2=NULL, f3=NULL<br>f1=12, f2=NameXXX, f3=1/1/2001 13:10:5<br>f1=13, f2=NULL, f3=NULL<br>f1=14, f2=NameXXX, f3=NULL<br>f1=15, f2=NULL, f3=1/1/2001 13:10:5<br>f1=16, f2=NameXXX, f3=NULL<br>f1=4, f2=NameXXX, f3=NULL<br>f1=5, f2=NULL, f3=NULL<br>f1=6, f2=NameXXX, f3=1/1/2001 13:10:5<br>f1=7, f2=NULL, f3=NULL<br>f1=8, f2=NameXXX, f3=NULL<br><br><br><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 © 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</a>. <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></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 + -