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

Go to the SVN repository for this file.

1 /* $Id: variant.cpp 93230 2021-03-19 18:27:45Z ucko $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: $Id: variant.cpp 93230 2021-03-19 18:27:45Z ucko $
27 *
28 * Author: Michael Kholodov
29 *
30 * File Description: CVariant class implementation
31 *
32 *
33 *
34 */
35 
36 #include <ncbi_pch.hpp>
37 #include <dbapi/variant.hpp>
38 #include <dbapi/error_codes.hpp>
39 #include <algorithm>
40 //#include <corelib/ncbistr.hpp>
41 
42 
43 #define NCBI_USE_ERRCODE_X Dbapi_Variant
44 
45 
47 
48 //==================================================================
50  : CException(DIAG_COMPILE_INFO, 0, (CException::EErrCode)eVariant, message)
51 {
52 }
53 
54 
55 const char* CVariantException::GetErrCodeString(void) const
56 {
57  switch (GetErrCode()) {
58  case eVariant: return "eVariant";
59  default: return CException::GetErrCodeString();
60  }
61 }
62 
63 
64 //==================================================================
66 {
67  return CVariant(p ? new CDB_BigInt(*p) : new CDB_BigInt());
68 }
69 
71 {
72  return CVariant(n.IsNull() ? new CDB_BigInt() : new CDB_BigInt(n));
73 }
74 
76 {
77  return CVariant(p ? new CDB_Int(*p) : new CDB_Int());
78 }
79 
81 {
82  return CVariant(n.IsNull() ? new CDB_Int() : new CDB_Int(n));
83 }
84 
86 {
87  return CVariant(p ? new CDB_SmallInt(*p) : new CDB_SmallInt());
88 }
89 
91 {
92  return CVariant(n.IsNull() ? new CDB_SmallInt() : new CDB_SmallInt(n));
93 }
94 
96 {
97  return CVariant(p ? new CDB_TinyInt(*p) : new CDB_TinyInt());
98 }
99 
101 {
102  return CVariant(n.IsNull() ? new CDB_TinyInt() : new CDB_TinyInt(n));
103 }
104 
106 {
107  return CVariant(p ? new CDB_Float(*p) : new CDB_Float());
108 }
109 
111 {
112  return CVariant(x.IsNull() ? new CDB_Float() : new CDB_Float(x));
113 }
114 
116 {
117  return CVariant(p ? new CDB_Double(*p) : new CDB_Double());
118 }
119 
121 {
122  return CVariant(x.IsNull() ? new CDB_Double() : new CDB_Double(x));
123 }
124 
126 {
127  return CVariant(p ? new CDB_Bit(*p) : new CDB_Bit());
128 }
129 
131 {
132  return CVariant(b.IsNull() ? new CDB_Bit() : new CDB_Bit(b));
133 }
134 
135 CVariant CVariant::LongChar(const char *p, size_t len)
136 {
137  return CVariant(p ? new CDB_LongChar(len, p) : new CDB_LongChar(len));
138 }
139 
141 {
142  return CVariant(s.IsNull() ? new CDB_LongChar(len)
143  : new CDB_LongChar(len ? len : TStringUCS2::npos, s));
144 }
145 
146 CVariant CVariant::VarChar(const char *p, size_t len)
147 {
148  return CVariant(p ? (len ? new CDB_VarChar(p, len) : new CDB_VarChar(p))
149  : new CDB_VarChar());
150 }
152 {
153  return CVariant(s.IsNull() ? new CDB_VarChar()
154  : (len ? new CDB_VarChar(s, len) : new CDB_VarChar(s)));
155 }
156 
157 CVariant CVariant::VarCharMax(const char *p, size_t len)
158 {
159  return CVariant(p ? (len ? new CDB_VarCharMax(p, len)
160  : new CDB_VarCharMax(p))
161  : new CDB_VarCharMax());
162 }
164  size_t len)
165 {
166  return CVariant(s.IsNull() ? new CDB_VarCharMax()
167  : (len ? new CDB_VarCharMax(s, len)
168  : new CDB_VarCharMax(s)));
169 }
170 
171 CVariant CVariant::Char(size_t size, const char *p)
172 {
173  return CVariant(p ? new CDB_Char(size, p) : new CDB_Char(size));
174 }
175 
177 {
178  return CVariant(s.IsNull() ? new CDB_Char(size) : new CDB_Char(size, s));
179 }
180 
181 CVariant CVariant::LongBinary(size_t maxSize, const void *p, size_t len)
182 {
183  return CVariant(p ? new CDB_LongBinary(maxSize, p, len) : new CDB_LongBinary(maxSize));
184 }
185 
186 CVariant CVariant::VarBinary(const void *p, size_t len)
187 {
188  return CVariant(p ? new CDB_VarBinary(p, len) : new CDB_VarBinary());
189 }
190 
191 CVariant CVariant::VarBinaryMax(const void *p, size_t len)
192 {
193  return CVariant(p ? new CDB_VarBinaryMax(p, len) : new CDB_VarBinaryMax());
194 }
195 
196 CVariant CVariant::Binary(size_t size, const void *p, size_t len)
197 {
198  return CVariant(p ? new CDB_Binary(size, p, len) : new CDB_Binary(size));
199 }
200 
202 {
203  return CVariant(p ? new CDB_SmallDateTime(*p) : new CDB_SmallDateTime());
204 }
205 
207 {
208  return CVariant(t.IsNull() ? new CDB_SmallDateTime()
209  : new CDB_SmallDateTime(t));
210 }
211 
213 {
214  return CVariant(p ? new CDB_DateTime(*p) : new CDB_DateTime());
215 }
216 
218 {
219  return CVariant(t.IsNull() ? new CDB_DateTime() : new CDB_DateTime(t));
220 }
221 
222 inline
224 {
225  switch (fmt) {
226  case eLonger: return CDB_BigDateTime::eDateTime;
227  case eDateOnly: return CDB_BigDateTime::eDate;
228  case eTimeOnly: return CDB_BigDateTime::eTime;
230  default:
232  FORMAT("Unsupported BigDateTime format " << fmt));
233  }
234 }
235 
237 {
238  auto sql_type = s_TranslateDateTimeFormat(fmt);
239  return CVariant(p ? new CDB_BigDateTime(*p, sql_type)
240  : new CDB_BigDateTime(CTime::eEmpty, sql_type));
241 }
242 
244  EDateTimeFormat fmt)
245 {
246  auto sql_type = s_TranslateDateTimeFormat(fmt);
247  return CVariant(t.IsNull() ? new CDB_BigDateTime(CTime::eEmpty, sql_type)
248  : new CDB_BigDateTime(t, sql_type));
249 }
250 
252  unsigned int scale,
253  const char* p)
254 {
255  return CVariant(p ? new CDB_Numeric(precision, scale, p)
256  : new CDB_Numeric());
257 }
258 
259 
261  : m_data(0)
262 {
263  switch ( type ) {
264  case eDB_Int:
265  m_data = new CDB_Int();
266  return;
267  case eDB_SmallInt:
268  m_data = new CDB_SmallInt();
269  return;
270  case eDB_TinyInt:
271  m_data = new CDB_TinyInt();
272  return;
273  case eDB_BigInt:
274  m_data = new CDB_BigInt();
275  return;
276  case eDB_LongChar:
277  if( size == 0 )
278  {
280  eVariant | Retriable(eRetriable_No),
281  "Illegal argument, the size of LONGCHAR should not be 0");
282  }
283  m_data = new CDB_LongChar(size);
284  return;
285  case eDB_VarChar:
286  m_data = new CDB_VarChar();
287  return;
288  case eDB_Char:
289  if( size == 0 )
290  {
292  "Illegal argument, the size of CHAR should not be 0");
293  }
294  m_data = new CDB_Char(size);
295  return;
296  case eDB_LongBinary:
297  if( size == 0 )
298  {
300  "Illegal argument, the size of LONGBINARY should not be 0");
301  }
302  m_data = new CDB_LongBinary(size);
303  return;
304  case eDB_VarBinary:
305  m_data = new CDB_VarBinary();
306  return;
307  case eDB_Binary:
308  if( size == 0 )
309  {
311  "Illegal argument, the size of BINARY should not be 0");
312  }
313  m_data = new CDB_Binary(size);
314  return;
315  case eDB_Float:
316  m_data = new CDB_Float();
317  return;
318  case eDB_Double:
319  m_data = new CDB_Double();
320  return;
321  case eDB_DateTime:
322  m_data = new CDB_DateTime();
323  return;
324  case eDB_BigDateTime:
325  m_data = new CDB_BigDateTime();
326  return;
327  case eDB_SmallDateTime:
328  m_data = new CDB_SmallDateTime();
329  return;
330  case eDB_Text:
331  m_data = new CDB_Text();
332  return;
333  case eDB_Image:
334  m_data = new CDB_Image();
335  return;
336  case eDB_VarCharMax:
337  m_data = new CDB_VarCharMax();
338  return;
339  case eDB_VarBinaryMax:
340  m_data = new CDB_VarBinaryMax();
341  return;
342  case eDB_Bit:
343  m_data = new CDB_Bit();
344  return;
345  case eDB_Numeric:
346  m_data = new CDB_Numeric();
347  return;
348  case eDB_UnsupportedType:
349  break;
350  }
352  string("Unsupported type: ")
353  + CDB_Object::GetTypeName(type, false));
354 }
355 
356 
358  : m_data(o)
359 {
360  return;
361 }
362 
363 
365  : m_data(new CDB_BigInt(v)) {}
366 
367 
369  : m_data(new CDB_Int(v)) {}
370 
371 //CVariant::CVariant(int v)
372 //: m_data(new CDB_Int(v)) {}
373 
375  : m_data(new CDB_SmallInt(v)) {}
376 
378  : m_data(new CDB_TinyInt(v)) {}
379 
381  : m_data(new CDB_Float(v)) {}
382 
384  : m_data(new CDB_Double(v)) {}
385 
387  : m_data(new CDB_Bit(v)) {}
388 
389 CVariant::CVariant(const string& v)
390  : m_data(new CDB_VarChar(v))
391 {
392 }
393 
394 CVariant::CVariant(const char* s)
395  : m_data(new CDB_VarChar(s))
396 {
397 }
398 
400  : m_data(new CDB_VarChar(v))
401 {
402 }
403 
405  : m_data(0)
406 {
407 
408  switch(fmt) {
409  case eShort:
410  m_data = new CDB_SmallDateTime(v);
411  break;
412  case eLong:
413  m_data = new CDB_DateTime(v);
414  break;
415  case eLonger:
416  m_data = new CDB_BigDateTime(v);
417  break;
418  case eDateOnly:
420  break;
421  case eTimeOnly:
423  break;
424  case eDateTimeOffset:
426  break;
427  default:
429  "CVariant::ctor(): unsupported datetime type "
430  + NStr::IntToString(fmt));
431  }
432 
433  if (v.IsEmpty()) {
434  SetNull();
435  }
436 }
437 
438 
440  : m_data(0)
441 {
442  if( v.GetData() != 0 ) {
443  m_data = v.GetData()->Clone();
444  }
445 }
446 
448 {
449  try {
450  delete m_data;
451  }
453 }
454 
455 
457  if( m_data == 0 )
459  "CVariant::GetNonNullData(): null data");
460 
461  return m_data;
462 }
463 
465  delete m_data;
466  m_data = o;
467 }
468 
469 
470 
471 string CVariant::GetString(void) const
472 {
473  string s("");
474 
475  if( IsNull() )
476  {
477  switch( GetType() ) {
478  case eDB_TinyInt:
479  case eDB_SmallInt:
480  case eDB_Int:
481  case eDB_BigInt:
482  case eDB_Numeric:
483  s = "0";
484  break;
485  case eDB_Float:
486  case eDB_Double:
487  s = "0.0";
488  break;
489  default:
490  break;
491  }
492  }
493  else
494  {
495  switch( GetType() ) {
496  case eDB_Char:
497  case eDB_VarChar:
498  case eDB_LongChar:
499  s = ((CDB_String*)GetData())->AsString();
500  break;
501  case eDB_Binary:
502  {
504  s = string((char*)b->Value(), b->Size());
505  break;
506  }
507  case eDB_LongBinary:
508  {
510  s = string((char*)vb->Value(), vb->DataSize());
511  break;
512  }
513  case eDB_VarBinary:
514  {
516  s = string((char*)vb->Value(), vb->Size());
517  break;
518  }
519  case eDB_TinyInt:
520  s = NStr::IntToString(GetByte());
521  break;
522  case eDB_SmallInt:
523  s = NStr::IntToString(GetInt2());
524  break;
525  case eDB_Int:
526  s = NStr::IntToString(GetInt4());
527  break;
528  case eDB_BigInt:
530  break;
531  case eDB_Float:
533  break;
534  case eDB_Double:
536  break;
537  case eDB_Bit:
538  s = NStr::BoolToString(GetBit());
539  break;
540  case eDB_Numeric:
541  s = ((CDB_Numeric*)GetData())->Value();
542  break;
543  case eDB_DateTime:
544  case eDB_BigDateTime:
545  case eDB_SmallDateTime:
546  s = GetCTime().AsString();
547  break;
548  case eDB_Text:
549  case eDB_Image:
550  case eDB_VarCharMax:
551  case eDB_VarBinaryMax:
552  {
553  CDB_Stream* stream = (CDB_Stream*)GetData();
554  size_t n = stream->Size();
555  s.resize(n);
556  size_t n2 = stream->PeekAt(&s[0], 0, n);
557  _ASSERT(n2 == n);
558  s.resize(n2);
559  }
560  break;
561  default:
563  break;
564  }
565  }
566 
567  return s;
568 }
569 
570 
572 {
573  if( !IsNull() )
574  {
575  switch( GetType() ) {
576  case eDB_BigInt:
577  return ((CDB_BigInt*)GetData())->Value();
578  case eDB_Int:
579  return ((CDB_Int*)GetData())->Value();
580  case eDB_SmallInt:
581  return ((CDB_SmallInt*)GetData())->Value();
582  case eDB_TinyInt:
583  return ((CDB_TinyInt*)GetData())->Value();
584  default:
586  }
587  }
588  return 0;
589 }
590 
591 
593 {
594  if( ! IsNull() )
595  {
596  switch( GetType() ) {
597  case eDB_Int:
598  return ((CDB_Int*)GetData())->Value();
599  case eDB_SmallInt:
600  return ((CDB_SmallInt*)GetData())->Value();
601  case eDB_TinyInt:
602  return ((CDB_TinyInt*)GetData())->Value();
603  default:
605  }
606  }
607  return 0;
608 }
609 
611 {
612  if( !IsNull() )
613  {
614  switch( GetType() ) {
615  case eDB_SmallInt:
616  return ((CDB_SmallInt*)GetData())->Value();
617  case eDB_TinyInt:
618  return ((CDB_TinyInt*)GetData())->Value();
619  default:
621  }
622  }
623  return 0;
624 }
625 
627 {
628  if( !IsNull() )
629  {
630  switch( GetType() ) {
631  case eDB_TinyInt:
632  return ((CDB_TinyInt*)GetData())->Value();
633  default:
635  }
636  }
637  return 0;
638 }
639 
640 float CVariant::GetFloat() const
641 {
642  if( !IsNull() )
643  {
644  switch( GetType() ) {
645  case eDB_Float:
646  return ((CDB_Float*)GetData())->Value();
647  case eDB_SmallInt:
648  return ((CDB_SmallInt*)GetData())->Value();
649  case eDB_TinyInt:
650  return ((CDB_TinyInt*)GetData())->Value();
651  default:
653  }
654  }
655  return 0.;
656 }
657 
658 double CVariant::GetDouble() const
659 {
660  if( !IsNull() )
661  {
662  switch( GetType() ) {
663  case eDB_Float:
664  return ((CDB_Float*)GetData())->Value();
665  case eDB_Double:
666  return ((CDB_Double*)GetData())->Value();
667  case eDB_Int:
668  return ((CDB_Int*)GetData())->Value();
669  case eDB_SmallInt:
670  return ((CDB_SmallInt*)GetData())->Value();
671  case eDB_TinyInt:
672  return ((CDB_TinyInt*)GetData())->Value();
673  default:
675  }
676  }
677  return 0.;
678 }
679 
680 bool CVariant::GetBit() const
681 {
682  if( !IsNull() )
683  {
684  x_Verify_AssignType(eDB_Bit, "bool");
685  return ((CDB_Bit*)GetData())->Value() != 0;
686  }
687  return false;
688 }
689 
690 string CVariant::GetNumeric() const
691 {
692  if( !IsNull() )
693  {
694  x_Verify_AssignType(eDB_Numeric, "string");
695  return ((CDB_Numeric*)GetData())->Value();
696  }
697  return "";
698 }
699 
700 const CTime& CVariant::GetCTime() const
701 {
702  CTime *ptr = NULL;
703  switch(GetType()) {
704  case eDB_DateTime:
705  ptr = const_cast<CTime*>(&((CDB_DateTime*)GetData())->Value());
706  break;
707  case eDB_BigDateTime:
708  ptr = const_cast<CTime*>(&((CDB_BigDateTime*)GetData())->GetCTime());
709  break;
710  case eDB_SmallDateTime:
711  ptr = const_cast<CTime*>(&((CDB_SmallDateTime*)GetData())->Value());
712  break;
713  default:
715  }
716  if( IsNull() )
717  ptr->Clear();
718  return *ptr;
719 }
720 
721 string CVariant::AsNotNullString(const string& v) const
722 {
723  if( IsNull() )
724  return v;
725  else
726  return GetString();
727 }
728 
729 bool CVariant::IsNull() const
730 {
731  return GetData() == 0 ? true : GetData()->IsNULL();
732 }
733 
735 {
736  if( GetData() != 0 )
737  GetData()->AssignNULL();
738 }
739 
740 size_t CVariant::Read(void* buf, size_t len) const
741 {
742  if( !IsNull() )
743  {
745  return ((CDB_Stream*)GetData())->Read(buf, len);
746  } else {
747  x_Inapplicable_Method("Read()");
748  }
749  }
750  return 0;
751 }
752 
753 size_t CVariant::Append(const void* buf, size_t len)
754 {
756  return ((CDB_Stream*)GetData())->Append(buf, len);
757  } else {
758  x_Inapplicable_Method("Append()");
759  }
760  return 0;
761 }
762 
763 size_t CVariant::Append(const string& str)
764 {
765  switch(GetType()) {
766  case eDB_Text:
767  return ((CDB_Text*)GetData())->Append(str);
768 
769  case eDB_VarCharMax:
770  return ((CDB_VarCharMax*)GetData())->Append(str);
771 
772  default:
773  x_Inapplicable_Method("Append()");
774  }
775  return 0;
776 }
777 
779 {
780  switch(GetType()) {
781  case eDB_Text:
782  return ((CDB_Text*)GetData())->Append(str);
783 
784  case eDB_VarCharMax:
785  return ((CDB_VarCharMax*)GetData())->Append(str);
786 
787  default:
788  x_Inapplicable_Method("Append()");
789  }
790  return 0;
791 }
792 
793 size_t CVariant::GetBlobSize() const
794 {
796  return ((CDB_Stream*)GetData())->Size();
797  } else {
798  x_Inapplicable_Method("GetBlobSize()");
799  }
800  return 0;
801 }
802 
804 {
806  ((CDB_Stream*)GetData())->Truncate(len);
807  } else {
808  x_Inapplicable_Method("Truncate()");
809  }
810  return;
811 }
812 
813 bool CVariant::MoveTo(size_t pos) const
814 {
816  return ((CDB_Stream*)GetData())->MoveTo(pos);
817  } else {
818  x_Inapplicable_Method("MoveTo()");
819  }
820  return false;
821 }
822 
824 {
826  *((CDB_BigInt*)GetData()) = v;
827  return *this;
828 }
829 
831 {
832  x_Verify_AssignType(eDB_Int, "Int4");
833  *((CDB_Int*)GetData()) = v;
834  return *this;
835 }
836 
838 {
840  *((CDB_SmallInt*)GetData()) = v;
841  return *this;
842 }
843 
845 {
847  *((CDB_TinyInt*)GetData()) = v;
848  return *this;
849 }
850 
852 {
853  x_Verify_AssignType(eDB_Float, "float");
854  *((CDB_Float*)GetData()) = v;
855  return *this;
856 }
857 
858 CVariant& CVariant::operator=(const double& v)
859 {
860  x_Verify_AssignType(eDB_Double, "double");
861  *((CDB_Double*)GetData()) = v;
862  return *this;
863 }
864 
865 CVariant& CVariant::operator=(const string& v)
866 {
867  switch( GetType()) {
868  case eDB_VarChar:
869  *((CDB_VarChar*)GetData()) = v;
870  break;
871  case eDB_LongChar:
872  *((CDB_LongChar*)GetData()) = v;
873  break;
874  case eDB_Char:
875  *((CDB_Char*)GetData()) = v;
876  break;
877  default:
879  }
880 
881  return *this;
882 }
883 
885 {
886  switch( GetType()) {
887  case eDB_VarChar:
888  *((CDB_VarChar*)GetData()) = v;
889  break;
890  case eDB_LongChar:
891  *((CDB_LongChar*)GetData()) = v;
892  break;
893  case eDB_Char:
894  *((CDB_Char*)GetData()) = v;
895  break;
896  default:
897  x_Verify_AssignType(eDB_UnsupportedType, "const char *");
898  }
899 
900  return *this;
901 }
902 
904 {
905  switch( GetType()) {
906  case eDB_VarChar:
907  case eDB_LongChar:
908  case eDB_Char:
909  *((CDB_String*)GetData()) = v;
910  break;
911  default:
913  }
914 
915  return *this;
916 }
917 
919 {
920  x_Verify_AssignType(eDB_Bit, "bool");
921  *((CDB_Bit*)GetData()) = v;
922  return *this;
923 }
924 
926 {
927  switch(GetType()) {
928  case eDB_DateTime:
929  *((CDB_DateTime*)GetData()) = v;
930  break;
931  case eDB_BigDateTime:
932  *((CDB_BigDateTime*)GetData()) = v;
933  break;
934  case eDB_SmallDateTime:
935  *((CDB_SmallDateTime*)GetData()) = v;
936  break;
937  default:
939  }
940  return *this;
941 }
942 
944 {
945  this->m_data->AssignValue(*(v.m_data));
946  return *this;
947 }
948 
949 bool operator<(const CVariant& v1, const CVariant& v2)
950 {
951  bool less = false;
952 
953  if( v1.IsNull() || v2.IsNull() ) {
954  less = v1.IsNull() && !v2.IsNull();
955  }
956  else {
957  if( v1.GetType() != v2.GetType() ) {
959  string("Cannot compare different types ")
960  + CDB_Object::GetTypeName(v1.GetType(), false) + " and "
961  + CDB_Object::GetTypeName(v2.GetType(), false));
962  }
963 
964  switch( v1.GetType() ) {
965  case eDB_Char:
966  case eDB_VarChar:
967  case eDB_LongChar:
968  less = v1.GetString() < v2.GetString();
969  break;
970  case eDB_TinyInt:
971  less = v1.GetByte() < v2.GetByte();
972  break;
973  case eDB_SmallInt:
974  less = v1.GetInt2() < v2.GetInt2();
975  break;
976  case eDB_Int:
977  less = v1.GetInt4() < v2.GetInt4();
978  break;
979  case eDB_BigInt:
980  less = v1.GetInt8() < v2.GetInt8();
981  break;
982  case eDB_Float:
983  less = v1.GetFloat() < v2.GetFloat();
984  break;
985  case eDB_Double:
986  less = v1.GetDouble() < v2.GetDouble();
987  break;
988  case eDB_DateTime:
989  case eDB_BigDateTime:
990  case eDB_SmallDateTime:
991  less = v1.GetCTime() < v2.GetCTime();
992  break;
993  default:
995  string("Type not supported: ")
996  + CDB_Object::GetTypeName(v1.GetType(), false));
997  }
998  }
999  return less;
1000 }
1001 
1002 bool operator==(const CVariant& v1, const CVariant& v2)
1003 {
1004  bool less = false;
1005 
1006  if( v1.IsNull() || v2.IsNull() ) {
1007  less = v1.IsNull() && !v2.IsNull();
1008  }
1009  else {
1010  if( v1.GetType() != v2.GetType() ) {
1012  string("Cannot compare different types ")
1013  + CDB_Object::GetTypeName(v1.GetType(), false) + " and "
1014  + CDB_Object::GetTypeName(v2.GetType(), false));
1015  }
1016 
1017  switch( v1.GetType() ) {
1018  case eDB_Char:
1019  case eDB_VarChar:
1020  case eDB_LongChar:
1021  case eDB_Binary:
1022  case eDB_VarBinary:
1023  less = v1.GetString() == v2.GetString();
1024  break;
1025  case eDB_Bit:
1026  less = v1.GetBit() == v2.GetBit();
1027  break;
1028  case eDB_TinyInt:
1029  less = v1.GetByte() == v2.GetByte();
1030  break;
1031  case eDB_SmallInt:
1032  less = v1.GetInt2() == v2.GetInt2();
1033  break;
1034  case eDB_Int:
1035  less = v1.GetInt4() == v2.GetInt4();
1036  break;
1037  case eDB_BigInt:
1038  less = v1.GetInt8() == v2.GetInt8();
1039  break;
1040  case eDB_Float:
1041  less = v1.GetFloat() == v2.GetFloat();
1042  break;
1043  case eDB_Double:
1044  less = v1.GetDouble() == v2.GetDouble();
1045  break;
1046  case eDB_DateTime:
1047  case eDB_BigDateTime:
1048  case eDB_SmallDateTime:
1049  less = v1.GetCTime() == v2.GetCTime();
1050  break;
1051  default:
1053  string("Type not supported: ")
1054  + CDB_Object::GetTypeName(v1.GetType(), false));
1055  }
1056  }
1057  return less;
1058 }
1059 
1061 {
1062  if ( !GetData() ) {
1063  return eBulkEnc_RawBytes;
1064  }
1065 
1066  switch (GetType()) {
1067  case eDB_VarChar:
1068  case eDB_Char:
1069  case eDB_LongChar:
1070  return ((const CDB_String*)GetData())->GetBulkInsertionEnc();
1071  case eDB_Text:
1072  return ((const CDB_Text*)GetData())->GetEncoding();
1073  case eDB_VarCharMax:
1074  return ((const CDB_VarCharMax*)GetData())->GetEncoding();
1075  default:
1076  return eBulkEnc_RawBytes;
1077  }
1078 }
1079 
1081 {
1082  if ( !GetData() ) {
1083  return;
1084  }
1085 
1086  switch (GetType()) {
1087  case eDB_VarChar:
1088  case eDB_Char:
1089  case eDB_LongChar:
1091  break;
1092  case eDB_Text:
1093  ((CDB_Text*)GetData())->SetEncoding(e);
1094  break;
1095  case eDB_VarCharMax:
1096  ((CDB_VarCharMax*)GetData())->SetEncoding(e);
1097  break;
1098  default:
1099  break;
1100  }
1101 }
1102 
1103 
CTime –.
Definition: ncbitime.hpp:296
CVariant –.
Definition: variant.hpp:99
Retriable manipulator.
Definition: ncbiexpt.hpp:1237
static CDB_BigDateTime::ESQLType s_TranslateDateTimeFormat(EDateTimeFormat fmt)
Definition: variant.cpp:223
#define true
Definition: bool.h:35
static char precision
Definition: genparams.c:28
static const char * str(char *buf, int n)
Definition: stats.c:84
bool IsNull(void) const
Check if the object is unassigned.
Definition: ncbimisc.hpp:686
@ eRetriable_No
It makes no sense to retry the action.
Definition: ncbimisc.hpp:168
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
static bool IsBlobType(EDB_Type db_type)
Definition: types.hpp:320
EDB_Type
Definition: types.hpp:52
virtual void AssignNULL()
Definition: types.cpp:550
size_t Size() const
Definition: types.hpp:726
EBulkEnc
Definition: types.hpp:85
const void * Value() const
Definition: types.hpp:723
size_t DataSize() const
Definition: types.hpp:797
virtual void AssignValue(const CDB_Object &v)=0
virtual CDB_Object * Clone() const =0
static const char * GetTypeName(EDB_Type db_type, bool throw_on_unknown=true)
Definition: types.cpp:586
virtual size_t PeekAt(void *buff, size_t start, size_t nof_bytes) const
Definition: types.cpp:1997
virtual size_t Size() const
Definition: types.cpp:2014
bool IsNULL() const
Definition: types.hpp:303
const void * Value() const
Definition: types.hpp:792
@ eDB_Bit
Definition: types.hpp:68
@ eDB_Char
Definition: types.hpp:58
@ eDB_UnsupportedType
Definition: types.hpp:75
@ eDB_SmallDateTime
Definition: types.hpp:65
@ eDB_VarChar
Definition: types.hpp:57
@ eDB_TinyInt
Definition: types.hpp:55
@ eDB_LongChar
Definition: types.hpp:70
@ eDB_Double
Definition: types.hpp:62
@ eDB_Image
Definition: types.hpp:67
@ eDB_Float
Definition: types.hpp:61
@ eDB_Int
Definition: types.hpp:53
@ eDB_VarCharMax
Definition: types.hpp:72
@ eDB_Numeric
Definition: types.hpp:69
@ eDB_BigInt
Definition: types.hpp:56
@ eDB_BigDateTime
Definition: types.hpp:64
@ eDB_Binary
Definition: types.hpp:60
@ eDB_Text
Definition: types.hpp:66
@ eDB_SmallInt
Definition: types.hpp:54
@ eDB_DateTime
Definition: types.hpp:63
@ eDB_LongBinary
Definition: types.hpp:71
@ eDB_VarBinary
Definition: types.hpp:59
@ eDB_VarBinaryMax
Definition: types.hpp:73
@ eBulkEnc_RawBytes
Definition: types.hpp:86
@ eDateTimeOffset
DATETIMEOFFSET (MS); no Sybase equivalent.
Definition: types.hpp:1086
@ eDateTime
DATETIME2 (MS), BIGDATETIME (Sybase)
Definition: types.hpp:1085
@ eTime
TIME (MS), (BIG)TIME (Sybase)
Definition: types.hpp:1084
@ eDate
DATE (MS, Sybase)
Definition: types.hpp:1083
static CVariant DateTime(CTime *p)
Definition: variant.cpp:212
CVariantException(const string &message)
Definition: variant.cpp:49
void SetData(CDB_Object *o)
Definition: variant.cpp:464
virtual const char * GetErrCodeString(void) const override
Definition: variant.cpp:55
bool MoveTo(size_t pos) const
Definition: variant.cpp:813
bool operator==(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:1002
CDB_Object * GetNonNullData() const
Definition: variant.cpp:456
static CVariant Float(float *p)
Definition: variant.cpp:105
bool IsNull() const
Definition: variant.cpp:729
double GetDouble(void) const
Definition: variant.cpp:658
static CVariant BigDateTime(CTime *p, EDateTimeFormat fmt=eLonger)
Definition: variant.cpp:236
CDB_Object * GetData() const
Definition: variant.hpp:269
bool GetBit(void) const
Definition: variant.cpp:680
void x_Inapplicable_Method(const char *method) const
Definition: variant.hpp:327
static CVariant VarBinaryMax(const void *p, size_t len)
Definition: variant.cpp:191
static CVariant BigInt(Int8 *p)
Definition: variant.cpp:65
EBulkEnc GetBulkInsertionEnc(void) const
Definition: variant.cpp:1060
Uint1 GetByte(void) const
Definition: variant.cpp:626
static CVariant VarChar(const char *p, size_t len=0)
Definition: variant.cpp:146
static CVariant LongChar(const char *p, size_t len=0)
Definition: variant.cpp:135
static CVariant LongBinary(size_t maxSize, const void *p, size_t len)
Definition: variant.cpp:181
string GetNumeric(void) const
Definition: variant.cpp:690
static CVariant VarBinary(const void *p, size_t len)
Definition: variant.cpp:186
EDateTimeFormat
Definition: variant.hpp:81
static CVariant Binary(size_t size, const void *p, size_t len)
Definition: variant.cpp:196
static CVariant SmallInt(Int2 *p)
Definition: variant.cpp:85
void SetNull()
Definition: variant.cpp:734
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
static CVariant Double(double *p)
Definition: variant.cpp:115
static CVariant VarCharMax(const char *p, size_t len=0)
Definition: variant.cpp:157
string GetString(void) const
Definition: variant.cpp:471
static CVariant Char(size_t size, const char *p)
Definition: variant.cpp:171
size_t Append(const void *buf, size_t len)
Definition: variant.cpp:753
string AsNotNullString(const string &v) const
Definition: variant.cpp:721
Int8 GetInt8(void) const
Definition: variant.cpp:571
static CVariant Numeric(unsigned int precision, unsigned int scale, const char *p)
Definition: variant.cpp:251
~CVariant()
Definition: variant.cpp:447
float GetFloat(void) const
Definition: variant.cpp:640
size_t Read(void *buf, size_t len) const
Definition: variant.cpp:740
static CVariant Int(Int4 *p)
Definition: variant.cpp:75
static CVariant TinyInt(Uint1 *p)
Definition: variant.cpp:95
bool operator<(const CVariant &v1, const CVariant &v2)
Definition: variant.cpp:949
void SetBulkInsertionEnc(EBulkEnc e)
Definition: variant.cpp:1080
void Truncate(size_t len=kMax_UInt)
Definition: variant.cpp:803
class CDB_Object * m_data
Definition: variant.hpp:259
CVariant & operator=(const CVariant &v)
Definition: variant.cpp:943
size_t GetBlobSize() const
Definition: variant.cpp:793
static CVariant SmallDateTime(CTime *p)
Definition: variant.cpp:201
static CVariant Bit(bool *p)
Definition: variant.cpp:125
const CTime & GetCTime(void) const
Definition: variant.cpp:700
Int4 GetInt4(void) const
Definition: variant.cpp:592
Int2 GetInt2(void) const
Definition: variant.cpp:610
@ 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
#define DIAG_COMPILE_INFO
Make compile time diagnostic information object to use in CNcbiDiag and CException.
Definition: ncbidiag.hpp:170
#define NCBI_CATCH_ALL_X(err_subcode, message)
Definition: ncbiexpt.hpp:619
#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 FORMAT(message)
Format message using iostreams library.
Definition: ncbiexpt.hpp:672
const CVect2< U > & v2
Definition: globals.hpp:440
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 END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
#define kEmptyStr
Definition: ncbistr.hpp:123
static const string BoolToString(bool value)
Convert bool to string.
Definition: ncbistr.cpp:2815
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
basic_string< TCharUCS2 > TStringUCS2
Type for string in UCS-2 encoding.
Definition: ncbistr.hpp:3849
CTime & Clear(void)
Make the time "empty",.
Definition: ncbitime.cpp:1998
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
bool IsEmpty(void) const
Is time object empty (date and time)?
Definition: ncbitime.hpp:2378
@ eEmpty
Use "empty" time.
Definition: ncbitime.hpp:301
CVariant(void)
Definition: Variant.hpp:86
Definition of all error codes used in dbapi libraries (dbapi_driver.lib and others).
char * buf
yy_size_t n
int len
const struct ncbi::grid::netcache::search::fields::SIZE size
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2107
EIPRangeType t
Definition: ncbi_localip.c:101
static bool less(const CSeq_feat *A, const CSeq_feat *B)
Definition: type.c:6
#define _ASSERT
Modified on Sun Apr 14 05:26:22 2024 by modify_doxy.py rev. 669887