0

使用 OTL 4.0 的 Oracle11G 中的 SELECT 语句,Column datatype(NUMBER,INTEGER,FLOAT,BINARY_DOUBLE) 返回相同的数据类型 otl_var_double

通过OTLv4连接到Oracle11GR2,选择列(数据类型:NUMBER,INTEGER,FLOAT,BINARY_DOUBLE),默认数据类型映射适用,这些列dadatype都返回double类型。以下示例使用 otl_column_desc 获取列数据类型。

F1 列是 NUMBER 数据类型。当我使用诸如“SELECT F1、F2、F3、F4、F5、F6 FROM TEST_TAB WHERE F1=:F1”之类的绑定变量进行选择时,F1 列需要正确的数据类型。如果给出错误的类型“double”,则选择语句不能使用索引。

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ORA11G_R2 // Compile OTL 4.0/OCI11.2

#if defined(__BORLANDC__)
#define OTL_BIGINT __int64 // Enabling G++ 64-bit integers
#define OTL_UBIGINT unsigned __int64 // Enabling G++ 64-bit integers
#elif !defined(_MSC_VER)
#define OTL_BIGINT long long // Enabling G++ 64-bit integers
#define OTL_UBIGINT unsigned long long // Enabling G++ 64-bit integers
#else
#define OTL_BIGINT __int64 // Enabling VC++ 64-bit integers
#define OTL_UBIGINT unsigned __int64 // Enabling VC++ 64-bit integers
#endif
#include <otlv4.h> // include the OTL 4.0 header file

const OTL_UBIGINT UBIG_VAL1=18446744073709551615ULL;

#include <otlv4.h> // include the OTL 4.0 header file

#pragma comment(lib, "oci.lib")

otl_connect db; // connect object

void insert()
// insert rows into table
{ 
 otl_stream o(50, // buffer size
              "insert into test_tab values(:f1<bigint>,:f2<char[31]>,:f3<ubigint>,:f4<int>,:f5<float>,:f6<double>)", 
                 // SQL statement
              db // connect object
             );
 char tmp[32];

 for(OTL_BIGINT i=1;i<=100;++i){
#if defined(_MSC_VER)
#if (_MSC_VER >= 1400) // VC++ 8.0 or higher
   sprintf_s(tmp,sizeof(tmp),"Name%d",static_cast<int>(i));
#else
   sprintf(tmp,"Name%d",static_cast<int>(i));
#endif
#else
   sprintf(tmp,"Name%d",static_cast<int>(i));
#endif
   o<<i<<tmp<<UBIG_VAL1<<static_cast<int>(i)<<static_cast<float>(i)<<static_cast<double>(i);
 }
}

void select()
{ 
 otl_stream i(50, // buffer size
              "select f1 :#1<bigint>, f2, f3 :#3<ubigint>, f4 :#4<int>, f5 :#5<float>, f6 :#6<double> "
                // the default mapping of f1 and f3 needs to be overridden 
                // explicitly when (u)bigint's are used in a combination with
                // OTL/OCIx, because the default mapping maps
                // Oracle NUMBERs into double containers, which are not 
                // big enough to hold (unsigned) 64-bit integer values.
              "from test_tab "
              "where f1>=:f<bigint> and f1<=:ff<bigint>*2",
                 // SELECT statement
              db // connect object
             ); 
   // create select stream
 
 OTL_BIGINT f1;
 char f2[31];
 OTL_UBIGINT f3;
 int f4;
 float f5;
 double f6;

 i<<static_cast<OTL_BIGINT>(8)
  <<static_cast<OTL_BIGINT>(8); // assigning :f = 8; :ff = 8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the buffer

 while(!i.eof()){ // while not end-of-data
   i>>f1>>f2>>f3>>f4>>f5>>f6;
   printf("f1=%lld, f2=%s, f3=%llu, f4=%d, f5=%f, f6=%lf\n",f1,f2,f3,f4,f5,f6);
 }
    printf("\nExplicit bind variables in output column definitions in SELECT statements\n");
    // get_otl_column_desc      
    int desc_len = 0;
    otl_column_desc* p_desc = i.describe_select(desc_len);              
    for (int n = 0; n < desc_len; n++)
    {
        int datatype = p_desc[n].otl_var_dbtype;
        printf("columnName=%s, dataType=%lld\n",p_desc[n].name,p_desc[n].otl_var_dbtype);
    }

}

void columnDataTypeTest()
{
    printf("\nselect with the default datatype mapping applies\n");
    string sql = "SELECT F1, F2, F3, F4, F5, F6 FROM TEST_TAB WHERE F1 = 1";
    otl_stream otlstream;
    otlstream.open(50, sql.c_str(), db);
    int ret = otlstream.get_rpc();

    // get_otl_column_desc      
    int desc_len = 0;
    otl_column_desc* p_desc = otlstream.describe_select(desc_len);              
    for (int n = 0; n < desc_len; n++)
    {
        int datatype = p_desc[n].otl_var_dbtype;
        printf("columnName=%s, dataType=%lld\n",p_desc[n].name,p_desc[n].otl_var_dbtype);
    }
}

int main()
{
 otl_connect::otl_initialize(); // initialize OCI environment
 try{

  db.rlogon("scott/tiger"); // connect to Oracle

  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30), f3 number, f4 integer, f5 float, f6 binary_double)"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table
  columnDataTypeTest(); // select without explicit bind variable

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from Oracle

 return 0;

}
4

0 回答 0