NCBI C++ ToolKit
variant.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef DBAPI___VARIANT__HPP
2 #define DBAPI___VARIANT__HPP
3 
4 /* $Id: variant.hpp 92985 2021-02-25 16:59:22Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Michael Kholodov
30  *
31  * File Description: CVariant class implementation
32  *
33  */
34 
35 #include <corelib/ncbiobj.hpp>
36 #include <corelib/ncbitype.h>
37 #include <corelib/ncbitime.hpp>
38 #include <dbapi/driver/types.hpp>
40 
41 
42 /** @addtogroup DbVariant
43  *
44  * @{
45  */
46 
47 
49 
50 
51 /////////////////////////////////////////////////////////////////////////////
52 //
53 // CVariantException::
54 //
55 //
56 
57 //class NCBI_DBAPI_EXPORT CVariantException : public std::exception
59 {
60 
61 public:
62  enum EErrCode {
63  eVariant
64  };
65 
66  CVariantException(const string& message);
67 
68  virtual const char* GetErrCodeString(void) const override;
69 
71 };
72 
73 
74 /////////////////////////////////////////////////////////////////////////////
75 //
76 // EDateTimeFormat::
77 //
78 // DateTime format
79 //
80 
88 };
89 
90 
91 /////////////////////////////////////////////////////////////////////////////
92 //
93 // CVariant::
94 //
95 // CVariant data type
96 //
97 
99 {
100 public:
101  // Contructors to create CVariant from various primitive types
102  explicit CVariant(Int8 v);
103  explicit CVariant(Int4 v);
104  explicit CVariant(Int2 v);
105  explicit CVariant(Uint1 v);
106  explicit CVariant(float v);
107  explicit CVariant(double v);
108  explicit CVariant(bool v);
109  explicit CVariant(const string& v);
110  explicit CVariant(const char* s);
111  explicit CVariant(const TStringUCS2& v);
112  // explicit CVariant(const TCharUCS2* s);
113 
114  // Factories for different types
115  // NOTE: pass p = 0 to make NULL value
116  static CVariant BigInt (Int8 *p);
117  static CVariant BigInt (const CNullable<Int8>& n);
118  static CVariant Int (Int4 *p);
119  static CVariant Int (const CNullable<Int4>& n);
120  static CVariant SmallInt (Int2 *p);
121  static CVariant SmallInt (const CNullable<Int2>& n);
122  static CVariant TinyInt (Uint1 *p);
123  static CVariant TinyInt (const CNullable<Uint1>& n);
124  static CVariant Float (float *p);
125  static CVariant Float (const CNullable<float>& x);
126  static CVariant Double (double *p);
127  static CVariant Double (const CNullable<double>& x);
128  static CVariant Bit (bool *p);
129  static CVariant Bit (const CNullable<bool>& b);
130  static CVariant LongChar (const char *p, size_t len = 0);
131  static CVariant LongChar (const CNullable<const TStringUCS2&>& s,
132  size_t len = 0);
133  static CVariant VarChar (const char *p, size_t len = 0);
134  static CVariant VarChar (const CNullable<const TStringUCS2&>& s,
135  size_t len = 0);
136  static CVariant VarCharMax (const char *p, size_t len = 0);
137  static CVariant VarCharMax (const CNullable<const TStringUCS2&>& s,
138  size_t len = 0);
139  static CVariant Char (size_t size, const char *p);
140  static CVariant Char (size_t size,
142  static CVariant LongBinary (size_t maxSize, const void *p, size_t len);
143  static CVariant VarBinary (const void *p, size_t len);
144  static CVariant VarBinaryMax (const void *p, size_t len);
145  static CVariant Binary (size_t size, const void *p, size_t len);
146  static CVariant SmallDateTime(CTime *p);
147  static CVariant SmallDateTime(const CNullable<const CTime&>& t);
148  static CVariant DateTime (CTime *p);
149  static CVariant DateTime (const CNullable<const CTime&>& t);
150  static CVariant BigDateTime (CTime *p, EDateTimeFormat fmt = eLonger);
151  static CVariant BigDateTime (const CNullable<const CTime&>& t,
152  EDateTimeFormat fmt = eLonger);
153  static CVariant Numeric (unsigned int precision,
154  unsigned int scale,
155  const char* p);
156 
157  // Make "placeholder" CVariant by type, containing NULL value
158  CVariant(EDB_Type type, size_t size = 0);
159 
160  // Make DATETIME representation in long and short forms
161  CVariant(const class CTime& v, EDateTimeFormat fmt);
162 
163  // Make CVariant from internal CDB_Object
164  explicit CVariant(CDB_Object* obj);
165 
166  // Copy constructor
167  CVariant(const CVariant& v);
168 
169  // Destructor
170  ~CVariant();
171 
172  // Get methods
173  EDB_Type GetType() const;
174 
175  Int8 GetInt8(void) const;
176  string GetString(void) const;
177  Int4 GetInt4(void) const;
178  Int2 GetInt2(void) const;
179  Uint1 GetByte(void) const;
180  float GetFloat(void) const;
181  double GetDouble(void) const;
182  bool GetBit(void) const;
183  string GetNumeric(void) const;
184  const CTime& GetCTime(void) const;
185 
186  // Get the argument as default, if the column is NULL
187  string AsNotNullString(const string& v) const;
188 
189  // Status info
190  bool IsNull() const;
191 
192  // NULLify
193  void SetNull();
194 
195  // operators
196  CVariant& operator=(const CVariant& v);
197  CVariant& operator=(const Int8& v);
198  CVariant& operator=(const Int4& v);
199  CVariant& operator=(const Int2& v);
200  CVariant& operator=(const Uint1& v);
201  CVariant& operator=(const float& v);
202  CVariant& operator=(const double& v);
203  CVariant& operator=(const string& v);
204  CVariant& operator=(const char* v);
205  CVariant& operator=(const TStringUCS2& v);
206  // CVariant& operator=(const TCharUCS2* v);
207  CVariant& operator=(const bool& v);
208  CVariant& operator=(const CTime& v);
209 
210 
211 
212  // Get pointer to the data buffer
213  // NOTE: internal use only!
214  CDB_Object* GetData() const;
215 
216  // Get pointer to the data buffer, throws CVariantException if buffer is 0
217  // NOTE: internal use only!
218  CDB_Object* GetNonNullData() const;
219 
220  // Methods to work with BLOB data (Text and Image)
221  size_t GetBlobSize() const;
222  size_t Read(void* buf, size_t len) const;
223  size_t Append(const void* buf, size_t len);
224  size_t Append(const string& str);
225  size_t Append(const TStringUCS2& str);
226  // Truncates from buffer end to buffer start.
227  // Truncates everything if no argument
228  void Truncate(size_t len = kMax_UInt);
229  // Moves the internal position pointer
230  bool MoveTo(size_t pos) const;
231 
232  void SetBlobDescriptor(I_BlobDescriptor* descr);
233  I_BlobDescriptor& GetBlobDescriptor(void) const;
234  I_BlobDescriptor* ReleaseBlobDescriptor(void) const;
235 
236  void SetITDescriptor(I_BlobDescriptor* descr) { SetBlobDescriptor(descr); }
238  { return GetBlobDescriptor(); }
240  { return ReleaseBlobDescriptor(); }
241 
242  // Simplify bulk insertion into NVARCHAR columns, which require
243  // explicit handling.
244  EBulkEnc GetBulkInsertionEnc(void) const;
245  void SetBulkInsertionEnc(EBulkEnc e);
246 
247 protected:
248  // Set methods
249  void SetData(CDB_Object* o);
250 
251 private:
252 
253 // void VerifyType(bool e) const;
254  void CheckNull() const;
255 
256  void x_Verify_AssignType(EDB_Type db_type, const char* cxx_type) const;
257  void x_Inapplicable_Method(const char* method) const;
258 
260  mutable unique_ptr<I_BlobDescriptor> m_descr;
261 };
262 
263 bool NCBI_DBAPI_EXPORT operator==(const CVariant& v1, const CVariant& v2);
264 bool NCBI_DBAPI_EXPORT operator<(const CVariant& v1, const CVariant& v2);
265 
266 
267 //================================================================
268 inline
270  return m_data;
271 }
272 
273 inline
275 {
276  return m_data->GetType();
277 }
278 
279 
280 //inline
281 //void CVariant::VerifyType(bool e) const
282 //{
283 // if( !e ) {
284 //#ifdef _DEBUG
285 // _TRACE("CVariant::VerifyType(): Invalid type");
286 // _ASSERT(0);
287 //#else
288 // NCBI_THROW(CVariantException, eVariant, "Invalid type");
289 //#endif
290 // }
291 //}
292 
293 
294 inline void
296 {
297  m_descr.reset(descr);
298 }
299 
300 inline I_BlobDescriptor&
302 {
303  if (m_descr.get() == NULL) {
305  "No blob descriptor available.");
306  }
307  return *m_descr;
308 }
309 
310 inline I_BlobDescriptor*
312 {
313  return m_descr.release();
314 }
315 
316 inline
317 void CVariant::x_Verify_AssignType(EDB_Type db_type, const char* cxx_type) const
318 {
319  if (db_type == eDB_UnsupportedType || db_type != GetType()) {
321  "Cannot assign type '" + string(cxx_type) + "' to type '"
322  + string(CDB_Object::GetTypeName(GetType())) + "'");
323  }
324 }
325 
326 inline
327 void CVariant::x_Inapplicable_Method(const char* method) const
328 {
330  "CVariant::" + string(method) + " is not applicable to type '"
331  + string(CDB_Object::GetTypeName(GetType())) + "'");
332 }
333 
334 
335 inline
336 bool operator!=(const CVariant& v1, const CVariant& v2)
337 {
338  return !(v1 == v2);
339 }
340 
341 inline
342 bool operator>(const CVariant& v1, const CVariant& v2)
343 {
344  return v2 < v1;
345 }
346 
347 inline
348 bool operator<=(const CVariant& v1, const CVariant& v2)
349 {
350  return v1 < v2 || v1 == v2;
351 }
352 
353 inline
354 bool operator>=(const CVariant& v1, const CVariant& v2)
355 {
356  return v2 < v1 || v1 == v2;
357 }
358 
359 
361 
362 #endif // DBAPI___VARIANT__HPP
CTime –.
Definition: ncbitime.hpp:296
CVariant –.
Definition: variant.hpp:99
I_BlobDescriptor::
Definition: interfaces.hpp:369
Retriable manipulator.
Definition: ncbiexpt.hpp:1237
static char precision
Definition: genparams.c:28
static const char * str(char *buf, int n)
Definition: stats.c:84
@ eRetriable_No
It makes no sense to retry the action.
Definition: ncbimisc.hpp:168
#define NULL
Definition: ncbistd.hpp:225
#define Char
Definition: ncbistd.hpp:124
EDB_Type
Definition: types.hpp:52
EBulkEnc
Definition: types.hpp:85
virtual EDB_Type GetType() const =0
static const char * GetTypeName(EDB_Type db_type, bool throw_on_unknown=true)
Definition: types.cpp:586
@ eDB_UnsupportedType
Definition: types.hpp:75
I_BlobDescriptor * ReleaseITDescriptor(void) const
Definition: variant.hpp:239
unique_ptr< I_BlobDescriptor > m_descr
Definition: variant.hpp:260
bool operator==(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:1002
bool operator!=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:336
I_BlobDescriptor & GetBlobDescriptor(void) const
Definition: variant.hpp:301
bool operator>(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:342
CDB_Object * GetData() const
Definition: variant.hpp:269
void x_Inapplicable_Method(const char *method) const
Definition: variant.hpp:327
void SetITDescriptor(I_BlobDescriptor *descr)
Definition: variant.hpp:236
bool operator>=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:354
void CheckNull() const
EDateTimeFormat
Definition: variant.hpp:81
NCBI_EXCEPTION_DEFAULT(CVariantException, CException)
EDB_Type GetType() const
Definition: variant.hpp:274
void x_Verify_AssignType(EDB_Type db_type, const char *cxx_type) const
Definition: variant.hpp:317
void SetBlobDescriptor(I_BlobDescriptor *descr)
Definition: variant.hpp:295
I_BlobDescriptor & GetITDescriptor(void) const
Definition: variant.hpp:237
CVariant(const class CTime &v, EDateTimeFormat fmt)
bool operator<=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:348
I_BlobDescriptor * ReleaseBlobDescriptor(void) const
Definition: variant.hpp:311
bool operator<(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:949
class CDB_Object * m_data
Definition: variant.hpp:259
@ eDateTimeOffset
Definition: variant.hpp:87
@ eShort
Definition: variant.hpp:82
@ eTimeOnly
Definition: variant.hpp:86
@ eLong
Definition: variant.hpp:83
@ eLonger
Definition: variant.hpp:84
@ eDateOnly
Definition: variant.hpp:85
virtual const char * GetType(void) const
Get class name as a string.
Definition: ncbiexpt.cpp:268
CException & operator=(const CException &)
Private assignment operator to prohibit assignment.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
const CVect2< U > & v2
Definition: globals.hpp:440
void Read(CObjectIStream &in, TObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:60
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
int16_t Int2
2-byte (16-bit) signed integer
Definition: ncbitype.h:100
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#define kMax_UInt
Definition: ncbi_limits.h:185
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
basic_string< TCharUCS2 > TStringUCS2
Type for string in UCS-2 encoding.
Definition: ncbistr.hpp:3849
CTime Truncate(const CTime &t)
Definition: ncbitime.hpp:2195
#define NCBI_DBAPI_EXPORT
Definition: ncbi_export.h:432
char * buf
yy_size_t n
int len
const struct ncbi::grid::netcache::search::fields::SIZE size
static PyObject * Binary(PyObject *self, PyObject *args)
EIPRangeType t
Definition: ncbi_localip.c:101
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Defines: CTimeFormat - storage class for time format.
Defines Limits for the types used in NCBI C/C++ toolkit.
Definition: type.c:6
Modified on Sun Apr 14 05:24:49 2024 by modify_doxy.py rev. 669887