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

Go to the repository for this file.

1 #ifndef DBAPI___VARIANT__HPP
2 #define DBAPI___VARIANT__HPP
3 
4 /* $Id: variant.hpp 107834 2026-10-22 15:29: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(string&& v);
111  explicit CVariant(const char* s);
112  explicit CVariant(const TStringUCS2& v);
113  // explicit CVariant(const TCharUCS2* s);
114 
115  // Factories for different types
116  // NOTE: pass p = 0 to make NULL value
117  static CVariant BigInt (Int8 *p);
118  static CVariant BigInt (const CNullable<Int8>& n);
119  static CVariant Int (Int4 *p);
120  static CVariant Int (const CNullable<Int4>& n);
121  static CVariant SmallInt (Int2 *p);
122  static CVariant SmallInt (const CNullable<Int2>& n);
123  static CVariant TinyInt (Uint1 *p);
124  static CVariant TinyInt (const CNullable<Uint1>& n);
125  static CVariant Float (float *p);
126  static CVariant Float (const CNullable<float>& x);
127  static CVariant Double (double *p);
128  static CVariant Double (const CNullable<double>& x);
129  static CVariant Bit (bool *p);
130  static CVariant Bit (const CNullable<bool>& b);
131  static CVariant LongChar (const char *p, size_t len = 0);
132  static CVariant LongChar (const CNullable<const TStringUCS2&>& s,
133  size_t len = 0);
134  static CVariant VarChar (const char *p, size_t len = 0);
135  static CVariant VarChar (const CNullable<const TStringUCS2&>& s,
136  size_t len = 0);
137  static CVariant VarCharMax (const char *p, size_t len = 0);
138  static CVariant VarCharMax (const CNullable<const TStringUCS2&>& s,
139  size_t len = 0);
140  static CVariant Char (size_t size, const char *p);
141  static CVariant Char (size_t size,
143  static CVariant LongBinary (size_t maxSize, const void *p, size_t len);
144  static CVariant VarBinary (const void *p, size_t len);
145  static CVariant VarBinaryMax (const void *p, size_t len);
146  static CVariant Binary (size_t size, const void *p, size_t len);
147  static CVariant SmallDateTime(CTime *p);
148  static CVariant SmallDateTime(const CNullable<const CTime&>& t);
149  static CVariant DateTime (CTime *p);
150  static CVariant DateTime (const CNullable<const CTime&>& t);
151  static CVariant BigDateTime (CTime *p, EDateTimeFormat fmt = eLonger);
152  static CVariant BigDateTime (const CNullable<const CTime&>& t,
153  EDateTimeFormat fmt = eLonger);
154  static CVariant Numeric (unsigned int precision,
155  unsigned int scale,
156  const char* p);
157 
158  // Make "placeholder" CVariant by type, containing NULL value
159  CVariant(EDB_Type type, size_t size = 0);
160 
161  // Make DATETIME representation in long and short forms
162  CVariant(const class CTime& v, EDateTimeFormat fmt);
163 
164  // Make CVariant from internal CDB_Object
165  explicit CVariant(CDB_Object* obj);
166 
167  // Copy and move constructors
168  CVariant(const CVariant& v);
169  CVariant(CVariant&& v) noexcept;
170 
171  // Destructor
172  ~CVariant();
173 
174  // Get methods
175  EDB_Type GetType() const;
176 
177  Int8 GetInt8(void) const;
178  string GetString(void) const;
179  Int4 GetInt4(void) const;
180  Int2 GetInt2(void) const;
181  Uint1 GetByte(void) const;
182  float GetFloat(void) const;
183  double GetDouble(void) const;
184  bool GetBit(void) const;
185  string GetNumeric(void) const;
186  const CTime& GetCTime(void) const;
187 
188  // Get the argument as default, if the column is NULL
189  string AsNotNullString(const string& v) const;
190 
191  // Status info
192  bool IsNull() const;
193 
194  // NULLify
195  void SetNull();
196 
197  // operators
198  CVariant& operator=(const CVariant& v);
199  CVariant& operator=(CVariant&& v) noexcept;
200  CVariant& operator=(const Int8& v);
201  CVariant& operator=(const Int4& v);
202  CVariant& operator=(const Int2& v);
203  CVariant& operator=(const Uint1& v);
204  CVariant& operator=(const float& v);
205  CVariant& operator=(const double& v);
206  CVariant& operator=(const string& v);
207  CVariant& operator=(const char* v);
208  CVariant& operator=(const TStringUCS2& v);
209  // CVariant& operator=(const TCharUCS2* v);
210  CVariant& operator=(const bool& v);
211  CVariant& operator=(const CTime& v);
212 
213 
214 
215  /// Get pointer to the data buffer. The CVariant will retain
216  /// responsibility for deallocating the buffer regardless of
217  /// @a ownership, which instead indicates whether the caller can
218  /// assume the buffer's location won't change (except via SetData,
219  /// which is protected and has little or no use). Callers should
220  /// explicitly pass @a ownership even if they want its default,
221  /// which is conservative and may eventually change.
222  ///
223  /// @note internal use only!
224  CDB_Object* GetData(EOwnership ownership = eTakeOwnership) const;
225 
226  /// Get pointer to the data buffer. The CVariant will retain
227  /// responsibility for deallocating the buffer regardless of
228  /// @a ownership, which instead indicates whether the caller can
229  /// assume the buffer's location won't change (except via SetData,
230  /// which is protected and has little or no use). Callers should
231  /// explicitly pass @a ownership even if they want its default,
232  /// which is conservative and may eventually change.
233  ///
234  /// @throw CVariantException if buffer is null.
235  ///
236  /// @note internal use only!
237  CDB_Object* GetNonNullData(EOwnership ownership = eTakeOwnership) const;
238 
239  // Methods to work with BLOB data (Text and Image)
240  size_t GetBlobSize() const;
241  size_t Read(void* buf, size_t len) const;
242  size_t Append(const void* buf, size_t len);
243  size_t Append(const string& str);
244  size_t Append(const TStringUCS2& str);
245  // Truncates from buffer end to buffer start.
246  // Truncates everything if no argument
247  void Truncate(size_t len = kMax_UInt);
248  // Moves the internal position pointer
249  bool MoveTo(size_t pos) const;
250 
251  void SetBlobDescriptor(I_BlobDescriptor* descr);
252  I_BlobDescriptor& GetBlobDescriptor(void) const;
253  I_BlobDescriptor* ReleaseBlobDescriptor(void) const;
254 
255  void SetITDescriptor(I_BlobDescriptor* descr) { SetBlobDescriptor(descr); }
257  { return GetBlobDescriptor(); }
259  { return ReleaseBlobDescriptor(); }
260 
261  // Simplify bulk insertion into NVARCHAR columns, which require
262  // explicit handling.
263  EBulkEnc GetBulkInsertionEnc(void) const;
264  void SetBulkInsertionEnc(EBulkEnc e);
265 
266 protected:
267  /// Repoint data buffer. The CVariant will retain responsibility
268  /// for deallocating the buffer regardless of @a ownership, which
269  /// instead indicates whether the caller can assume that only
270  /// another call to SetData will change the buffer's location
271  /// again. Callers should explicitly pass @a ownership even if
272  /// they want its default, which is conservative and may
273  /// eventually change.
274  void SetData(CDB_Object* o, EOwnership ownership = eNoOwnership);
275 
276 private:
277 
278 // void VerifyType(bool e) const;
279  void CheckNull() const;
280 
281  void x_Verify_AssignType(EDB_Type db_type, const char* cxx_type) const;
282  void x_Inapplicable_Method(const char* method) const;
283 
285  mutable unique_ptr<I_BlobDescriptor> m_descr;
286  mutable bool m_exposed;
287 };
288 
289 bool NCBI_DBAPI_EXPORT operator==(const CVariant& v1, const CVariant& v2);
290 bool NCBI_DBAPI_EXPORT operator<(const CVariant& v1, const CVariant& v2);
291 
292 
293 //================================================================
294 inline
296  if (ownership == eTakeOwnership) {
297  m_exposed = true;
298  }
299  return m_data;
300 }
301 
302 inline
304 {
305  return m_data->GetType();
306 }
307 
308 
309 //inline
310 //void CVariant::VerifyType(bool e) const
311 //{
312 // if( !e ) {
313 //#ifdef _DEBUG
314 // _TRACE("CVariant::VerifyType(): Invalid type");
315 // _ASSERT(0);
316 //#else
317 // NCBI_THROW(CVariantException, eVariant, "Invalid type");
318 //#endif
319 // }
320 //}
321 
322 
323 inline void
325 {
326  m_descr.reset(descr);
327 }
328 
329 inline I_BlobDescriptor&
331 {
332  if (m_descr.get() == NULL) {
334  "No blob descriptor available.");
335  }
336  return *m_descr;
337 }
338 
339 inline I_BlobDescriptor*
341 {
342  return m_descr.release();
343 }
344 
345 inline
346 void CVariant::x_Verify_AssignType(EDB_Type db_type, const char* cxx_type) const
347 {
348  if (db_type == eDB_UnsupportedType || db_type != GetType()) {
350  "Cannot assign type '" + string(cxx_type) + "' to type '"
351  + string(CDB_Object::GetTypeName(GetType())) + "'");
352  }
353 }
354 
355 inline
356 void CVariant::x_Inapplicable_Method(const char* method) const
357 {
359  "CVariant::" + string(method) + " is not applicable to type '"
360  + string(CDB_Object::GetTypeName(GetType())) + "'");
361 }
362 
363 
364 inline
365 bool operator!=(const CVariant& v1, const CVariant& v2)
366 {
367  return !(v1 == v2);
368 }
369 
370 inline
371 bool operator>(const CVariant& v1, const CVariant& v2)
372 {
373  return v2 < v1;
374 }
375 
376 inline
377 bool operator<=(const CVariant& v1, const CVariant& v2)
378 {
379  return v1 < v2 || v1 == v2;
380 }
381 
382 inline
383 bool operator>=(const CVariant& v1, const CVariant& v2)
384 {
385  return v2 < v1 || v1 == v2;
386 }
387 
388 
390 
391 #endif // DBAPI___VARIANT__HPP
CTime –.
Definition: ncbitime.hpp:303
CVariant –.
Definition: variant.hpp:99
I_BlobDescriptor::
Definition: interfaces.hpp:364
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:172
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:135
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:134
#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:637
@ eDB_UnsupportedType
Definition: types.hpp:75
CDB_Object * GetData(EOwnership ownership=eTakeOwnership) const
Get pointer to the data buffer.
Definition: variant.hpp:295
I_BlobDescriptor * ReleaseITDescriptor(void) const
Definition: variant.hpp:258
unique_ptr< I_BlobDescriptor > m_descr
Definition: variant.hpp:285
bool operator==(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:1032
bool operator!=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:365
I_BlobDescriptor & GetBlobDescriptor(void) const
Definition: variant.hpp:330
bool operator>(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:371
void x_Inapplicable_Method(const char *method) const
Definition: variant.hpp:356
void SetITDescriptor(I_BlobDescriptor *descr)
Definition: variant.hpp:255
bool operator>=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:383
void CheckNull() const
EDateTimeFormat
Definition: variant.hpp:81
NCBI_EXCEPTION_DEFAULT(CVariantException, CException)
EDB_Type GetType() const
Definition: variant.hpp:303
bool m_exposed
Definition: variant.hpp:286
void x_Verify_AssignType(EDB_Type db_type, const char *cxx_type) const
Definition: variant.hpp:346
void SetBlobDescriptor(I_BlobDescriptor *descr)
Definition: variant.hpp:324
I_BlobDescriptor & GetITDescriptor(void) const
Definition: variant.hpp:256
CVariant(const class CTime &v, EDateTimeFormat fmt)
bool operator<=(const CVariant &v1, const CVariant &v2)
Definition: variant.hpp:377
I_BlobDescriptor * ReleaseBlobDescriptor(void) const
Definition: variant.hpp:340
bool operator<(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:979
class CDB_Object * m_data
Definition: variant.hpp:284
@ 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:104
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:101
basic_string< TCharUCS2 > TStringUCS2
Type for string in UCS-2 encoding.
Definition: ncbistr.hpp:3842
CTime Truncate(const CTime &t)
Definition: ncbitime.hpp:2193
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#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:102
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 Tue Nov 17 14:27:27 2026 by modify_doxy.py rev. 717554