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

📄 ociobj.cpp

📁 一个通用的oracle OCI开发程序包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#ifndef _COCIObject
#include "ociobj.h"
#endif

#ifndef _COCISession
#include "ocisess.h"
#endif

#ifndef _COCINumber
#include "ocinum.h"
#endif

#ifndef _COCIString
#include "ocistr.h"
#endif

#ifndef _COCILob
#include "ocilob.h"
#endif

#ifndef _COCIDate
#include "ocidate.h"
#endif

#ifndef _COCINestedTable
#include "ocintab.h"
#endif

#ifndef _COCIComplexObject
#include "ocicmplx.h"
#endif


#ifndef ORID_ORACLE
extern "C"
{
  #include "orid.h"
}
#endif


COCIObject::COCIObject(const COCISession& Sess, OCITypeCode typecode, COCIType type)
: m_Session(Sess)
, Ind(OCI_IND_NOTNULL)
, m_parent_object((COCIObject*)0)
, m_Copy(false)
, m_Type(type)
, m_Table(COCITable())
, COCIBase()
, m_TypeCode(typecode)
, m_Transient(true)
{
  CHECK( m_Session.get_error(), OCIObjectNew(m_Session.get_env(),
           m_Session.get_error(),
           m_Session.get_svc(),
           typecode,
           (type.get_tdo()) ? (OCIType*)type.get_tdo() : 0,
           0, // No Table required
           OCI_DURATION_TRANS, TRUE, (dvoid **) &get_instance()) );

}

// Persistent object => an asssociated table
COCIObject::COCIObject(const COCISession& Sess, COCITable Table, COCIType Type, COCIObject* parent)
: m_Session(Sess)
, m_Table(Table)
, Ind(OCI_IND_NOTNULL)
, m_parent_object(parent)
, m_Copy(false)
, COCIBase()
, m_TypeCode(OCI_TYPECODE_OBJECT)
, m_Type(Type)
, m_Transient(false)
{

  CHECK( m_Session.get_error(), OCIObjectNew(m_Session.get_env(),
         m_Session.get_error(),
         m_Session.get_svc(),
         OCI_TYPECODE_OBJECT,
         (OCIType*)Type.get_tdo(), 
         Table.pin(),
         OCI_DURATION_TRANS, FALSE, (dvoid **) &get_instance()) );
  //m_Type = Type;
}


COCIObject::COCIObject(const COCISession& Sess, dvoid* object_instance, COCIType type, COCIObject* parent, OCITypeCode typecode)
: m_Session(Sess)
, m_Type(type)
, m_Table(COCITable())
, Ind(OCI_IND_NOTNULL)
, m_Copy(false)
, COCIBase()
, m_parent_object(parent)
, m_TypeCode(typecode)
, m_Transient(true)
{
  set_instance(object_instance);
}


// Transient object => no associated table
COCIObject::COCIObject(const COCISession& Sess, COCIType Type, COCIObject* parent)
: m_Session(Sess)
, m_Type(Type)
, m_parent_object(parent)
, m_Copy(false)
, COCIBase()
, m_TypeCode(OCI_TYPECODE_OBJECT)
, m_Transient(true)
{
  CHECK( m_Session.get_error(), OCIObjectNew(m_Session.get_env(),
         m_Session.get_error(),
         m_Session.get_svc(),
         OCI_TYPECODE_OBJECT,
         (OCIType*)Type.get_tdo(), 
         0, 
         OCI_DURATION_TRANS, ((parent) ? TRUE : FALSE), (dvoid **) &get_instance()) );

}


#include <iostream.h> // temp for now
// Copy constructor - creates another object
COCIObject::COCIObject(const COCIObject& src)
: m_Session(src.m_Session)
, m_Type(src.m_Type)
, m_Table(src.m_Table)
, Ind(src.Ind)
, m_parent_object(src.m_parent_object)
, m_Copy(false)
, COCIBase()
, m_TypeCode(src.m_TypeCode)
, m_Transient(src.m_Transient)
{

  CHECK( m_Session.get_error(), OCIObjectNew(m_Session.get_env(),
     m_Session.get_error(),
     m_Session.get_svc(),
     src.m_TypeCode,
     (OCIType*)m_Type.get_tdo(), 
     (m_Table.pin()) ? m_Table.pin() : 0, 
     OCI_DURATION_TRANS, FALSE, (dvoid **) &get_instance()) );

  if(m_TypeCode == OCI_TYPECODE_OBJECT)
  {
    dvoid* src_null_structure = 0;
    dvoid* src_instance = ((COCIBase*)&src)->get_instance();
    CHECK(m_Session.get_error(), OCIObjectGetInd(m_Session.get_env(), m_Session.get_error(), src_instance, &src_null_structure));
    dvoid* this_null_structure;
    CHECK(m_Session.get_error(), OCIObjectGetInd(m_Session.get_env(), m_Session.get_error(), get_instance(), &this_null_structure));
  
    CHECK(m_Session.get_error(), OCIObjectCopy(m_Session.get_env(), m_Session.get_error(), m_Session.get_svc(), src_instance, 
                                                 src_null_structure, get_instance(), this_null_structure, (OCIType*)m_Type.get_tdo(), OCI_DURATION_TRANS, 0));
    set_ind(OCI_IND_NOTNULL);
  }
  else if(m_TypeCode == OCI_TYPECODE_NAMEDCOLLECTION) // Need to check other types (collection for now)
  {
    OCIColl* lhs = (OCIColl*)get_instance();
    OCIColl* rhs = (OCIColl*)((COCIBase*)&src)->get_instance();
    // Assign rhs to lhs
    CHECK(m_Session.get_error(), OCICollAssign(m_Session.get_env(), m_Session.get_error(), rhs, 
                      lhs));

  }
  else
  {
    cout << "Undefined type!\n";
  }

}


COCIObject::COCIObject()
: m_Session(m_Session)
, m_Type(COCIType())
, m_Table(COCITable())
, Ind(OCI_IND_NOTNULL)
, m_parent_object((COCIObject*)0)
, m_Copy(false)
, COCIBase()
, m_TypeCode(0)
, m_Transient(true)
{
}


COCIObject::~COCIObject()
{
  if(!m_Transient)
  {
    CHECK( m_Session.get_error(), 
       OCIObjectFree(m_Session.get_env(), m_Session.get_error(), get_instance(), OCI_OBJECTFREE_FORCE));
  } 
}


COCIRef COCIObject::get_ref(void)
{
  COCIObject obj(m_Session, OCI_TYPECODE_REF);
  CHECK( m_Session.get_error(), 
         OCIObjectGetObjectRef(m_Session.get_env(), 
                               m_Session.get_error(), 
                               (dvoid *) get_instance(), (OCIRef *)obj.get_instance()));
  COCIRef o_ref(m_Session,obj.get_instance());
  return o_ref;
}


void COCIObject::get_ind(void)
{

  dvoid* object_instance = get_instance();
  if(m_parent_object)
  {
    // An embedded object requires the indicator structure of the root object
    // So let's go and find it...!
    COCIObject* o = m_parent_object;
    while(o->m_parent_object != (COCIObject*)0)
    {
      o = o->m_parent_object; 
    }

    object_instance = o->get_instance();
  }

  dvoid* null_struct = get_null_struct();
  CHECK( m_Session.get_error(), 
         OCIObjectGetInd(m_Session.get_env(), m_Session.get_error(), (dvoid *) object_instance,
                       (dvoid **) &null_struct));
  set_null_struct(null_struct);

}

void COCIObject::set_ind(OCIInd ind)
{
  get_ind();
  if(get_null_struct())
  {
    if(*((ub2*)get_null_struct()) == OCI_IND_NOTNULL)
    {
      int n = m_Type.get_attr_count();
      for(OCIInd i = 1; i <= n; i++)
      {
        *((ub2*)get_null_struct() + (OCIInd)i) = ind;
      }
    }
  }
}

void COCIObject::set_attr_ind(char* attr, OCIInd ind)
{
  std::string tmp = uppercase(attr);

  get_ind();
  text* Names[1];
  ub4 Lengths[1];

  Names[0] = (text*)tmp.c_str();
  Lengths[0] = strlen(attr);

  dvoid* instance = get_instance();
  dvoid* null_struct = get_null_struct();
  CHECK( m_Session.get_error(), 
         OCIObjectSetAttr(m_Session.get_env(), 
                          m_Session.get_error(),
                          instance,  
                          null_struct,
                          m_Type.tdo, 
                          (const text**)Names, 
                          Lengths, 
                          1, 0, 0, ind, 0, 0) );

}


void COCIObject::set(const char* attr, const int value)
{
  std::string tmp = uppercase(attr);

  get_ind();
  text* Names[1];
  ub4 Lengths[1];

  Names[0] = (text*)tmp.c_str();
  Lengths[0] = strlen(attr);

  COCINumber n(m_Session);
  n = value;

  dvoid* instance = get_instance();
  dvoid* null_struct = get_null_struct();

  OCIInd indicator = OCI_IND_NOTNULL;

  CHECK( m_Session.get_error(), 
         OCIObjectSetAttr(m_Session.get_env(), 
                          m_Session.get_error(),
                          instance,  
                          null_struct,
                          m_Type.tdo, 
                          (const text**)Names, 
                          Lengths, 
                          1, 0, 0, indicator, 0, (dvoid*)n.get_instance()) );
}

void COCIObject::set(const char* attr, const COCINumber& value)
{
  std::string tmp = uppercase(attr);

  get_ind();
  text* Names[1];
  ub4 Lengths[1];

  Names[0] = (text*)tmp.c_str();
  Lengths[0] = strlen(attr);

  dvoid* instance = get_instance();
  dvoid* null_struct = get_null_struct();

  OCIInd indicator = *(((COCINumber&)value).get_indicator());

  CHECK( m_Session.get_error(), 
         OCIObjectSetAttr(m_Session.get_env(), 
                          m_Session.get_error(),
                          instance,  
                          null_struct,
                          m_Type.tdo, 
                          (const text**)Names, 
                          Lengths, 
                          1, 0, 0, indicator, 0, (dvoid*)((COCINumber&)value).get_instance()) );
}


void COCIObject::set(const char* attr, const char* value)
{
  std::string tmp = uppercase(attr);

  get_ind();
  text* Names[1];
  ub4 Lengths[1];

  Names[0] = (text*)tmp.c_str();
  Lengths[0] = strlen(attr);

  COCIString str(m_Session);
  str = value;

  OCIInd indicator = *(str.get_indicator());

  CHECK( m_Session.get_error(), 
         OCIObjectSetAttr(m_Session.get_env(), 
                          m_Session.get_error(),
                          get_instance(),  
                          get_null_struct(),
                          m_Type.tdo, 
                          (const text**)Names, 
                          Lengths, 
                          1, 0, 0, indicator, 0, (dvoid*)str.get_instance()) );

}

void COCIObject::set(const char* attr, const COCIString& value)
{
  std::string tmp = uppercase(attr);

  get_ind();
  text* Names[1];
  ub4 Lengths[1];

  Names[0] = (text*)tmp.c_str();
  Lengths[0] = strlen(attr);

  OCIInd indicator = *(((COCIString&)value).get_indicator());

  CHECK( m_Session.get_error(), 
         OCIObjectSetAttr(m_Session.get_env(), 
                          m_Session.get_error(),
                          get_instance(),  
                          get_null_struct(),

⌨️ 快捷键说明

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