📄 types.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: types.cpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 19:19:28 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.17 * PRODUCTION * =========================================================================== *//* $Id: types.cpp,v 1000.2 2004/06/01 19:19:28 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Vladimir Soussov * * File Description: Type conversions * */#include <ncbi_pch.hpp>#include <corelib/ncbitime.hpp>#include <dbapi/driver/exception.hpp>#include <dbapi/driver/util/numeric_convert.hpp>#include "memory_store.hpp"#include <dbapi/driver/types.hpp>#include <string.h>BEGIN_NCBI_SCOPE/////////////////////////////////////////////////////////////////////////////// CDB_Object:://CDB_Object::~CDB_Object(){ return;}void CDB_Object::AssignNULL(){ m_Null = true;}CDB_Object* CDB_Object::Create(EDB_Type type, size_t size){ switch ( type ) { case eDB_Int : return new CDB_Int (); case eDB_SmallInt : return new CDB_SmallInt (); case eDB_TinyInt : return new CDB_TinyInt (); case eDB_BigInt : return new CDB_BigInt (); case eDB_VarChar : return new CDB_VarChar (); case eDB_Char : return new CDB_Char (size); case eDB_VarBinary : return new CDB_VarBinary (); case eDB_Binary : return new CDB_Binary (size); case eDB_Float : return new CDB_Float (); case eDB_Double : return new CDB_Double (); case eDB_DateTime : return new CDB_DateTime (); case eDB_SmallDateTime : return new CDB_SmallDateTime (); case eDB_Text : return new CDB_Text (); case eDB_Image : return new CDB_Image (); case eDB_Bit : return new CDB_Bit (); case eDB_Numeric : return new CDB_Numeric (); case eDB_LongBinary : return new CDB_LongBinary(size); case eDB_LongChar : return new CDB_LongChar (size); case eDB_UnsupportedType : break; } throw CDB_ClientEx(eDB_Error, 2, "CDB_Object::Create", "unknown type");}/////////////////////////////////////////////////////////////////////////////// CDB_Int:://EDB_Type CDB_Int::GetType() const{ return eDB_Int;}CDB_Object* CDB_Int::Clone() const{ return m_Null ? new CDB_Int : new CDB_Int(m_Val);}void CDB_Int::AssignValue(CDB_Object& v){ switch( v.GetType() ) { case eDB_Int : *this= (CDB_Int&)v; break; case eDB_SmallInt: *this= ((CDB_SmallInt&)v).Value(); break; case eDB_TinyInt : *this= ((CDB_TinyInt &)v).Value(); break; default: throw CDB_ClientEx(eDB_Error, 2, "CDB_Int::AssignValue", "wrong type of CDB_Object"); }}/////////////////////////////////////////////////////////////////////////////// CDB_SmallInt:://EDB_Type CDB_SmallInt::GetType() const{ return eDB_SmallInt;}CDB_Object* CDB_SmallInt::Clone() const{ return m_Null ? new CDB_SmallInt : new CDB_SmallInt(m_Val);}void CDB_SmallInt::AssignValue(CDB_Object& v){ switch( v.GetType() ) { case eDB_SmallInt: *this= (CDB_SmallInt&)v; break; case eDB_TinyInt : *this= ((CDB_TinyInt &)v).Value(); break; default: throw CDB_ClientEx(eDB_Error, 2, "CDB_SmallInt::AssignValue", "wrong type of CDB_Object"); } *this= (CDB_SmallInt&)v;}/////////////////////////////////////////////////////////////////////////////// CDB_TinyInt:://EDB_Type CDB_TinyInt::GetType() const{ return eDB_TinyInt;}CDB_Object* CDB_TinyInt::Clone() const{ return m_Null ? new CDB_TinyInt : new CDB_TinyInt(m_Val);}void CDB_TinyInt::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_TinyInt) { throw CDB_ClientEx(eDB_Error, 2, "CDB_TinyInt::AssignValue", "wrong type of CDB_Object"); } *this= (CDB_TinyInt&)v;}/////////////////////////////////////////////////////////////////////////////// CDB_BigInt:://EDB_Type CDB_BigInt::GetType() const{ return eDB_BigInt;}CDB_Object* CDB_BigInt::Clone() const{ return m_Null ? new CDB_BigInt : new CDB_BigInt(m_Val);}void CDB_BigInt::AssignValue(CDB_Object& v){ switch( v.GetType() ) { case eDB_BigInt : *this= (CDB_BigInt&)v; break; case eDB_Int : *this= ((CDB_Int &)v).Value(); break; case eDB_SmallInt: *this= ((CDB_SmallInt&)v).Value(); break; case eDB_TinyInt : *this= ((CDB_TinyInt &)v).Value(); break; default: throw CDB_ClientEx(eDB_Error, 2, "CDB_BigInt::AssignValue", "wrong type of CDB_Object"); }}/////////////////////////////////////////////////////////////////////////////// CDB_VarChar:://EDB_Type CDB_VarChar::GetType() const{ return eDB_VarChar;}CDB_Object* CDB_VarChar::Clone() const{ return m_Null ? new CDB_VarChar : new CDB_VarChar(m_Val);}void CDB_VarChar::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_VarChar) { throw CDB_ClientEx(eDB_Error, 2, "CDB_VarChar::AssignValue", "wrong type of CDB_Object"); } *this= (CDB_VarChar&)v;}/////////////////////////////////////////////////////////////////////////////// CDB_Char:://EDB_Type CDB_Char::GetType() const{ return eDB_Char;}CDB_Object* CDB_Char::Clone() const{ return new CDB_Char(*this);}void CDB_Char::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_Char) { throw CDB_ClientEx(eDB_Error, 2, "CDB_Char::AssignValue", "wrong type of CDB_Object"); } register CDB_Char& cv= (CDB_Char&)v; if(m_Size < cv.m_Size) { delete [] m_Val; m_Val= new char[cv.m_Size+1]; } m_Size= cv.m_Size; if(cv.IsNULL()) { m_Null= true; } else { m_Null= false; memcpy(m_Val, cv.m_Val, m_Size+1); }}CDB_Char::~CDB_Char(){ delete [] m_Val;}/////////////////////////////////////////////////////////////////////////////// CDB_LongChar:://EDB_Type CDB_LongChar::GetType() const{ return eDB_LongChar;}CDB_Object* CDB_LongChar::Clone() const{ return new CDB_LongChar(*this);}void CDB_LongChar::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_LongChar) { throw CDB_ClientEx(eDB_Error, 2, "CDB_LongChar::AssignValue", "wrong type of CDB_Object"); } register CDB_LongChar& cv= (CDB_LongChar&)v; if(m_Size < cv.m_Size) { delete [] m_Val; m_Val= new char[cv.m_Size+1]; } m_Size= cv.m_Size; if(cv.IsNULL()) { m_Null= true; } else { m_Null= false; memcpy(m_Val, cv.m_Val, m_Size+1); }}CDB_LongChar::~CDB_LongChar(){ delete [] m_Val;}/////////////////////////////////////////////////////////////////////////////// CDB_VarBinary:://EDB_Type CDB_VarBinary::GetType() const{ return eDB_VarBinary;}CDB_Object* CDB_VarBinary::Clone() const{ return m_Null ? new CDB_VarBinary : new CDB_VarBinary(m_Val, m_Size);}void CDB_VarBinary::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_VarBinary) { throw CDB_ClientEx(eDB_Error, 2, "CDB_VarBinary::AssignValue", "wrong type of CDB_Object"); } *this= (CDB_VarBinary&)v;}/////////////////////////////////////////////////////////////////////////////// CDB_Binary:://EDB_Type CDB_Binary::GetType() const{ return eDB_Binary;}CDB_Object* CDB_Binary::Clone() const{ return m_Null ? new CDB_Binary(m_Size) : new CDB_Binary(*this);}void CDB_Binary::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_Binary) { throw CDB_ClientEx(eDB_Error, 2, "CDB_Binary::AssignValue", "wrong type of CDB_Object"); } register CDB_Binary& cv= (CDB_Binary&)v; if(m_Size < cv.m_Size) { delete [] m_Val; m_Val= new unsigned char[cv.m_Size]; } m_Size= cv.m_Size; if(cv.IsNULL()) { m_Null= true; } else { m_Null= false; memcpy(m_Val, cv.m_Val, m_Size); }}CDB_Binary::~CDB_Binary(){ delete[] m_Val;}/////////////////////////////////////////////////////////////////////////////// CDB_LongBinary:://EDB_Type CDB_LongBinary::GetType() const{ return eDB_LongBinary;}CDB_Object* CDB_LongBinary::Clone() const{ return new CDB_LongBinary(*this);}void CDB_LongBinary::AssignValue(CDB_Object& v){ if(v.GetType() != eDB_LongBinary) { throw CDB_ClientEx(eDB_Error, 2, "CDB_LongBinary::AssignValue", "wrong type of CDB_Object"); } register CDB_LongBinary& cv= (CDB_LongBinary&)v; if(cv.IsNULL()) { m_Null= true; } else { m_Null= false; m_DataSize= (m_Size < cv.m_DataSize)? m_Size : cv.m_DataSize; memcpy(m_Val, cv.m_Val, m_DataSize); }}CDB_LongBinary::~CDB_LongBinary(){ delete[] m_Val;}/////////////////////////////////////////////////////////////////////////////// CDB_Float:://EDB_Type CDB_Float::GetType() const{ return eDB_Float;}CDB_Object* CDB_Float::Clone() const{ return m_Null ? new CDB_Float : new CDB_Float(m_Val);}void CDB_Float::AssignValue(CDB_Object& v){ switch( v.GetType() ) { case eDB_Float : *this= (CDB_Float&)v; break; case eDB_SmallInt: *this= ((CDB_SmallInt&)v).Value(); break; case eDB_TinyInt : *this= ((CDB_TinyInt &)v).Value(); break; default: throw CDB_ClientEx(eDB_Error, 2, "CDB_Float::AssignValue", "wrong type of CDB_Object"); }}/////////////////////////////////////////////////////////////////////////////// CDB_Double:://EDB_Type CDB_Double::GetType() const{ return eDB_Double;}CDB_Object* CDB_Double::Clone() const{ return m_Null ? new CDB_Double : new CDB_Double(m_Val);}void CDB_Double::AssignValue(CDB_Object& v){ switch( v.GetType() ) { case eDB_Double : *this= (CDB_Double&)v; break; case eDB_Float : *this= ((CDB_Float &)v).Value(); break; case eDB_Int : *this= ((CDB_Int &)v).Value(); break; case eDB_SmallInt: *this= ((CDB_SmallInt&)v).Value(); break; case eDB_TinyInt : *this= ((CDB_TinyInt &)v).Value(); break; default: throw CDB_ClientEx(eDB_Error, 2, "CDB_Double::AssignValue", "wrong type of CDB_Object"); }}/////////////////////////////////////////////////////////////////////////////// CDB_Stream:://CDB_Stream::CDB_Stream() : CDB_Object(true){ m_Store = new CMemStore;}CDB_Stream& CDB_Stream::Assign(const CDB_Stream& v){ m_Null = v.m_Null; m_Store->Truncate(); if ( !m_Null ) { char buff[1024]; CMemStore* s = (CMemStore*) &v.m_Store; size_t pos = s->Tell(); for (size_t n = s->Read((void*) buff, sizeof(buff)); n > 0; n = s->Read((void*) buff, sizeof(buff))) { Append((void*) buff, n); } s->Seek((long) pos, C_RA_Storage::eHead); } return *this;}void CDB_Stream::AssignNULL(){ CDB_Object::AssignNULL(); Truncate();}size_t CDB_Stream::Read(void* buff, size_t nof_bytes){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -