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

📄 otlv4.h

📁 一个利用OTL访问ORACLE数据库的例子
💻 H
📖 第 1 页 / 共 5 页
字号:
 otl_var_desc()
 {
  param_type=0;
  ftype=0;
  elem_size=0;
  array_size=0;
  pos=0;
  name_pos=0;
  name[0]=0;
  pl_tab_flag=0;
 }

 ~otl_var_desc(){}

 void copy_name(const char* nm)
 {
  if(!nm)
   name[0]=0;
  else{
   strncpy(name,nm,sizeof(name));
   name[sizeof(name)-1]=0;
  }
 }

};

const int otl_var_none=0;
const int otl_var_char=1;
const int otl_var_double=2;
const int otl_var_float=3;
const int otl_var_int=4;
const int otl_var_unsigned_int=5;
const int otl_var_short=6;
const int otl_var_long_int=7;
const int otl_var_timestamp=8;
const int otl_var_varchar_long=9;
const int otl_var_raw_long=10;
const int otl_var_clob=11;
const int otl_var_blob=12;
const int otl_var_refcur=13;
const int otl_var_long_string=15;
const int otl_var_db2time=16;
const int otl_var_db2date=17;
const int otl_var_tz_timestamp=18;
const int otl_var_ltz_timestamp=19;
const int otl_var_bigint=20;
#if defined(OTL_ORA_UNICODE)
const int otl_var_nchar=21;
const int otl_var_nclob=22;
#else
#endif
const int otl_var_lob_stream=100;

const int otl_bigint_str_size=40;

class otl_long_string{
public:

 unsigned char* v;
 int length;
 int extern_buffer_flag;
 int buf_size;

 otl_long_string(const int buffer_size=32760,const int input_length=0)
 {
   if(buffer_size==0){
     v=0;
     length=0;
     extern_buffer_flag=0;
   }else{
     extern_buffer_flag=0;
     length=input_length;
     buf_size=buffer_size;
     v=new unsigned char[buffer_size+1];
     memset(v,0,buffer_size);
   }
 }

 otl_long_string
 (const void* external_buffer, 
  const int buffer_size,
  const int input_length=0)
 {
  extern_buffer_flag=1;
  length=input_length;
  buf_size=buffer_size;
  v=OTL_RCAST(unsigned char*, OTL_CCAST(void*, external_buffer));
 }

  otl_long_string& operator=(const otl_long_string& s)
  {
    if(s.extern_buffer_flag){
      if(!extern_buffer_flag)
        delete[] v;
      v=s.v;
      length=s.length;
      extern_buffer_flag=s.extern_buffer_flag;
      buf_size=s.buf_size;
    }else{
      if(extern_buffer_flag){
        v=new unsigned char[s.buf_size];
        buf_size=s.buf_size;
      }else if(buf_size<s.buf_size){
        delete[] v;
        v=new unsigned char[s.buf_size];
        buf_size=s.buf_size;
      }
      length=s.length;
      extern_buffer_flag=s.extern_buffer_flag;
      memcpy(v,s.v,length);
      if(length<buf_size&&s.v[length]==0)
        v[length]=0;
    }
    return *this;
  }

  otl_long_string(const otl_long_string& s)
  {
    length=s.length;
    extern_buffer_flag=s.extern_buffer_flag;
    buf_size=s.buf_size;
    if(s.extern_buffer_flag)
      v=s.v;
    else{
      v=new unsigned char[buf_size];
      memcpy(v,s.v,length);
      if(length<buf_size&&s.v[length]==0)
        v[length]=0;
    }
  }
  
  virtual ~otl_long_string()
  {
   if(!extern_buffer_flag)delete[] v;
  }

  void set_len(const int alen=0){length=alen;}
  int len(void)const {return length;}

  unsigned char& operator[](int ndx){return v[ndx];}

  virtual void null_terminate_string(const int alen)
  {
    (*this)[alen]=0;
  }

};

#if defined(OTL_UNICODE)
class otl_long_unicode_string: public otl_long_string{
public:

  otl_long_unicode_string(const int buffer_size=32760,const int input_length=0)
    : otl_long_string(0,0)
  {
    extern_buffer_flag=0;
    length=input_length;
    buf_size=buffer_size;
    v=new unsigned char[(buffer_size+1)*sizeof(OTL_WCHAR)];
    memset(v,0,buffer_size*sizeof(OTL_WCHAR));
  }
  
  otl_long_unicode_string
  (const void* external_buffer, 
   const int buffer_size,
   const int input_length=0)
    : otl_long_string(external_buffer,buffer_size,input_length)
  {
    extern_buffer_flag=1;
    length=input_length;
    buf_size=buffer_size;
    v=OTL_RCAST(unsigned char*, OTL_CCAST(void*, external_buffer));
  }
  
  virtual ~otl_long_unicode_string(){}
  
  OTL_CHAR& operator[](int ndx)
  {
    return OTL_RCAST(OTL_CHAR*,v)[ndx];
  }

  virtual void null_terminate_string(const int alen)
  {
    (*this)[alen]=0;
  }

};

#endif

inline const char* otl_var_type_name(const int ftype)
{
  const char* const_CHAR="CHAR";
  const char* const_DOUBLE="DOUBLE";
  const char* const_FLOAT="FLOAT";
  const char* const_INT="INT";
  const char* const_UNSIGNED_INT="UNSIGNED INT";
  const char* const_SHORT_INT="SHORT INT";
  const char* const_LONG_INT="LONG INT";
  const char* const_TIMESTAMP="TIMESTAMP";
  const char* const_DB2DATE="DB2DATE";
  const char* const_DB2TIME="DB2TIME";
  const char* const_TZ_TIMESTAMP="TIMESTAMP WITH TIME ZONE";
  const char* const_LTZ_TIMESTAMP="TIMESTAMP WITH LOCAL TIME ZONE";
  const char* const_BIGINT="BIGINT";
  const char* const_VARCHAR_LONG="VARCHAR LONG";
  const char* const_RAW_LONG="RAW LONG";
  const char* const_CLOB="CLOB";
  const char* const_BLOB="BLOB";
  const char* const_UNKNOWN="UNKNOWN";
  const char* const_LONG_STRING="otl_long_string()";
  const char* const_LOB_STREAM="otl_lob_stream*&";
  const char* const_USER_DEFINED="User-defined type (object type, VARRAY, Nested Table)";
  
  switch(ftype){
  case otl_var_char:
    return const_CHAR;
  case otl_var_double:
    return const_DOUBLE;
  case otl_var_float:
    return const_FLOAT;
  case otl_var_int:
    return const_INT;
  case otl_var_unsigned_int:
    return const_UNSIGNED_INT;
  case otl_var_short:
    return const_SHORT_INT;
  case otl_var_long_int:
    return const_LONG_INT;
  case otl_var_timestamp:
    return const_TIMESTAMP;
  case otl_var_db2date:
    return const_DB2DATE;
  case otl_var_db2time:
    return const_DB2TIME;
  case otl_var_tz_timestamp:
    return const_TZ_TIMESTAMP;
  case otl_var_ltz_timestamp:
    return const_LTZ_TIMESTAMP;
  case otl_var_bigint:
    return const_BIGINT;
  case otl_var_varchar_long:
    return const_VARCHAR_LONG;
  case otl_var_raw_long:
    return const_RAW_LONG;
  case otl_var_clob:
    return const_CLOB;
  case otl_var_blob:
    return const_BLOB;
  case otl_var_long_string:
    return const_LONG_STRING;
  case otl_var_lob_stream:
    return const_LOB_STREAM;
  case 108:
    return const_USER_DEFINED;
 default:
  return const_UNKNOWN;
 }
}

inline void otl_var_info_var
(const char* name,
 const int ftype,
 const int type_code,
 char* var_info)
{char buf1[128];
 char buf2[128];
 strcpy(buf1,otl_var_type_name(ftype));
 strcpy(buf2,otl_var_type_name(type_code));
 strcpy(var_info,"Variable: ");
 strcat(var_info,name);
 strcat(var_info,"<");
 strcat(var_info,buf1);
 strcat(var_info,">, datatype in operator <</>>: ");
 strcat(var_info,buf2);
}

inline void otl_var_info_var2
(const char* name,
 const int ftype,
 char* var_info)
{char buf1[128];
 strcpy(buf1,otl_var_type_name(ftype));
 strcpy(var_info,"Variable: ");
 strcat(var_info,name);
 strcat(var_info,"<");
 strcat(var_info,buf1);
 strcat(var_info,">");
}

inline void otl_var_info_var3
(const char* name,
 const int ftype,
 const int type_code,
 char* var_info)
{char buf1[128];
 char buf2[128];
 strcpy(buf1,otl_var_type_name(ftype));
 strcpy(buf2,otl_var_type_name(type_code));
 strcpy(var_info,"Variable: ");
 strcat(var_info,name);
 strcat(var_info,"<");
 strcat(var_info,buf1);
 strcat(var_info,">, datatype in otl_stream_read_iterator::get(): ");
 strcat(var_info,buf2);
}

inline void otl_strcpy(
  unsigned char* trg,
  unsigned char* src,
  int& overflow,
  const int inp_size=0,
  const int actual_inp_size=-1
)
{
  OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
  OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
  int out_size=0;
  overflow=0;
  if(actual_inp_size!=-1){
    while(out_size<inp_size-1 && out_size<actual_inp_size){
      *c1=*c2;
      ++c1; ++c2;
      ++out_size;
    }
    *c1=0;
    if(out_size==inp_size-1 && out_size<actual_inp_size)
      overflow=1;
  }else{
    while(*c2 && out_size<inp_size-1){
      *c1=*c2;
      ++c1; ++c2;
      ++out_size;
    }
    *c1=0;
    if(*c2 && out_size==inp_size-1)
      overflow=1;
  }
}

#if defined(OTL_UNICODE) || defined(_MSC_VER)
inline void otl_strcpy
 (unsigned char* trg,
  unsigned char* src)
{
 OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
 OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
 while(*c2){
  *c1=*c2;
  ++c1; ++c2;
 }
 *c1=0;
}
#else
inline void otl_strcpy(unsigned char* trg,unsigned char* src)
{
  strcpy(OTL_RCAST(char*,trg),OTL_RCAST(const char*,src));
}
#endif

#if defined(OTL_UNICODE) && !defined(OTL_ODBC)
inline void otl_strcpy2(
  unsigned char* trg,
  unsigned char* src,
  const int max_src_len
)
{
 OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
 OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
 int src_len=OTL_SCAST(int,*OTL_SCAST(unsigned short*,c2));
 int len=0;
 ++c2;
 while(*c2&&len<max_src_len&&len<src_len){
  *c1=*c2;
  ++c1; 
  ++c2;
  ++len;
 }
 *c1=0;
#else
inline void otl_strcpy2(
  unsigned char* trg,
  unsigned char* src,
  const int /* max_src_len */
)
{
 otl_strcpy(trg,src);
#endif
}

#if defined(OTL_UNICODE)
inline void otl_memcpy(
  unsigned char* trg,
  unsigned char* src,
  const int src_len,
  const int ftype
)
{

  if(ftype==otl_var_raw_long){
    memcpy(trg,src,src_len);
    return;
  }

 OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
 OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
 int len=0;
 while(len<src_len){
  *c1=*c2;
  ++c1; 
  ++c2;
  ++len;
 }

#else
inline void otl_memcpy(
  unsigned char* trg,
  unsigned char* src,
  const int src_len,
  const int /* ftype */
)
{
 memcpy(trg,src,src_len);
#endif
}

#if defined(OTL_UNICODE) && !defined(OTL_ODBC)
inline void otl_strcpy3(
  unsigned char* trg,
  unsigned char* src,
  const int max_src_len,
  int& overflow,
  const int inp_size=0
)
{
 OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
 OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
 int len=0;
 int src_len=OTL_SCAST(int,*OTL_RCAST(unsigned short*,c2));
 ++c2;
 int out_size=0;
 overflow=0;
 while(len<src_len&&len<max_src_len&&out_size<inp_size-1){
  *c1=*c2;
  ++c1; ++c2;
  ++out_size;
  ++len;
 }
 *c1=0;
 if(len<src_len&&out_size==inp_size-1)
  overflow=1;
#else
inline void otl_strcpy3(
  unsigned char* trg,
  unsigned char* src,
  const int /* max_src_len */,
  int& overflow,
  const int inp_size=0
)
{
 OTL_CHAR* c1=OTL_RCAST(OTL_CHAR*,trg);
 OTL_CHAR* c2=OTL_RCAST(OTL_CHAR*,src);
 int out_size=0;
 overflow=0;
 while(*c2&&out_size<inp_size-1){
  *c1=*c2;
  ++c1; ++c2;
  ++out_size;
 }
 *c1=0;
 if(*c2&&out_size==inp_size-1)
  overflow=1;
#endif
}

inline void otl_strcpy4(
  unsigned char* trg,
  unsigned char* src,
  int& overflow,
  const int inp_size=0,
  const int actual_inp_size=-1
)
{
#if  defined(OTL_UNICODE) && !defined(OTL_ODBC)

⌨️ 快捷键说明

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