home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / object10 / ovalue.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  9.6 KB  |  501 lines

  1. /* Copyright (c) Oracle Corporation 1994.  All Rights Reserved */
  2.  
  3. /*
  4.     This source code is provided as a debugging aid for developers
  5.     who have purchased Oracle Objects for OLE    .  Please see the
  6.     online help for documentation of these classes.
  7. */
  8.  
  9. /*
  10.     Oracle Objects for OLE     C++ Classes
  11.     
  12.     This file implements the OValue class
  13.     It also implements the internal OOLEvar class
  14.                            
  15.     CREATED    ********   11/22/94
  16.     RWOOLARD    MODIFIED    03/20/95
  17.                 bug#    260396    SetValue() does not allocate for empty string
  18.                         262214    Various GPF's when dealing with NULL pointers
  19. */
  20.  
  21. #include "windows.h"
  22. #include <ole2.h>
  23. #include <olenls.h>       
  24. #include <dispatch.h>  
  25.  
  26. #ifndef ORACL_ORACLE
  27. #include "oracl.h"
  28. #endif
  29.  
  30. #ifndef ORAOBJI_ORACLE
  31. #include "oraobji.h"
  32. #endif
  33.  
  34. #ifndef _OracleInProcServer_H_
  35. #include <oratlb.h>
  36. #endif
  37.  
  38. // ----- OValue -----------------------------------------------
  39.  
  40. OValue::OValue(void)
  41. {
  42.     m_value = new OOLEvar;
  43. }
  44.  
  45. OValue::OValue(const OValue &other)
  46. {
  47.     m_value = new OOLEvar;
  48.     Copy(other);
  49. }
  50.  
  51. OValue::OValue(int intvalue)
  52. {
  53.     m_value = new OOLEvar;
  54.     m_value->SetValue(intvalue);
  55. }
  56.  
  57. OValue::OValue(long longvalue)
  58. {
  59.     m_value = new OOLEvar;
  60.     m_value->SetValue(longvalue);
  61. }
  62.  
  63. OValue::OValue(double doublevalue)
  64. {
  65.     m_value = new OOLEvar;
  66.     m_value->SetValue(doublevalue);
  67. }
  68.  
  69. OValue::OValue(const char *tval)
  70. {
  71.     m_value = new OOLEvar;
  72.     m_value->SetValue(tval);
  73. }
  74.  
  75. OValue::~OValue(void)
  76. {
  77.     Cleanup();
  78.     
  79.     // cleanup doesn't free m_value pointer - so get rid of it
  80.     if (m_value)
  81.     {
  82.         delete m_value;
  83.     }
  84.     
  85. }
  86.  
  87. OValue &OValue::operator=(const OValue &other)
  88. {
  89.     if (&other == this)
  90.         return(*this);
  91.     
  92.     // clear current value
  93.     m_value->Clear();
  94.     
  95.     Copy(other);
  96.     
  97.     return(*this);
  98. }
  99.  
  100. int OValue::operator==(const OValue &other) const
  101. {
  102.     return(m_value->IsEqual(other.m_value));
  103. }
  104.  
  105. int OValue::operator!=(const OValue &other) const
  106. {
  107.     return(!(m_value->IsEqual(other.m_value)));
  108. }
  109.  
  110. oresult OValue::Copy(const OValue &other)
  111.     // we are copying only the other's value
  112.     
  113.     return(FromLocalType((void *) ((other.m_value)->GetVariant())));
  114. }
  115.  
  116. oresult OValue::Cleanup(void)
  117. {
  118.     // clean out the value (but nothing else)
  119.     return(m_value->Clear());
  120. }
  121.  
  122. oresult OValue::Clear(void)
  123. {
  124.     return(m_value->Clear());
  125. }
  126.  
  127. oresult OValue::SetValue(const OValue &val)
  128. {
  129.     // clear current value
  130.     m_value->Clear();
  131.     
  132.     return(Copy(val));
  133. }
  134.  
  135. oresult OValue::SetValue(int val)
  136. {
  137.     OOLEvar var;
  138.     var.SetValue(val);
  139.     
  140.     return(FromLocalType((void *) &var));
  141. }
  142.  
  143. oresult OValue::SetValue(long val)
  144. {   
  145.     OOLEvar var;
  146.     var.SetValue(val);
  147.     
  148.     return(FromLocalType((void *) &var));
  149. }
  150.  
  151. oresult OValue::SetValue(double val)
  152. {   
  153.     OOLEvar var;
  154.     var.SetValue(val);
  155.     
  156.     return(FromLocalType((void *) &var));
  157. }
  158.  
  159. oresult OValue::SetValue(const char *val)
  160. {
  161.     OOLEvar var;
  162.     var.SetValue(val);
  163.     
  164.     return(FromLocalType((void *) &var));
  165. }
  166.  
  167. // copy local representation (VARIANT in Windows case) to OValue
  168. oresult OValue::FromLocalType(void *localv)
  169. {
  170.     if (m_value->SetValue((VARIANT *) localv) != OSUCCESS)
  171.         return(OFAILURE);
  172.  
  173.     return(OSUCCESS);
  174. }
  175.  
  176. oboolean OValue::IsNull(void) const
  177. {
  178.     return(m_value->IsNull());
  179. }
  180.  
  181. OValue::operator long() const
  182. {
  183.     long val;
  184.     m_value->GetValue(&val);
  185.     return(val);
  186. }
  187.  
  188. OValue::operator int() const
  189. {
  190.     int val;
  191.     m_value->GetValue(&val);
  192.     return(val);
  193. }
  194.  
  195. OValue::operator double() const
  196. {
  197.     double val;
  198.     m_value->GetValue(&val);
  199.     return(val);
  200. }
  201.  
  202. OValue::operator const char *() const
  203. {
  204.     const char *val;
  205.     m_value->GetValue(&val);
  206.     return(val);
  207. }
  208.  
  209. void *OValue::Internal(void) const
  210. {
  211.     return((void *) m_value);
  212. }
  213.  
  214. // ----- OOLEvar -----------------------------------------------
  215.  
  216. OOLEvar::OOLEvar(void)
  217. {
  218.     m_pmain = &m_mainv;
  219.     m_pconv = &m_convertedv;
  220.     VariantInit(m_pmain);
  221.     VariantInit(m_pconv);
  222.     m_isconv = FALSE;
  223.  
  224.     // set to database null value
  225.     V_VT(m_pmain) = VT_NULL;
  226. }
  227.  
  228. OOLEvar::~OOLEvar(void)
  229. {
  230.     Clear();
  231. }
  232.  
  233. VARIANT *OOLEvar::GetVariant(void)
  234. {
  235.     return(m_pmain);
  236. }
  237.  
  238. void OOLEvar::HaveSetVariant(void)
  239. {
  240.     m_isconv = FALSE;  // the converted variant is no longer valid
  241.     
  242.     return;
  243. }
  244.  
  245. oresult OOLEvar::Clear(void)
  246. {
  247.     VariantClear(m_pmain);
  248.     VariantClear(m_pconv);
  249.     
  250.     // set to database null value
  251.     V_VT(m_pmain) = VT_NULL;
  252.     
  253.     m_isconv = FALSE;
  254.     
  255.     return(OSUCCESS);
  256. }
  257.  
  258. oboolean OOLEvar::IsNull(void) const
  259. {
  260.     return (V_VT(m_pmain) == VT_NULL || V_VT(m_pmain) == VT_EMPTY);
  261. }
  262.  
  263. oboolean OOLEvar::IsEqual(OOLEvar *other)
  264.     // handle case of NULL
  265.     if (IsNull())
  266.     {
  267.         return(other->IsNull());
  268.     }
  269.     
  270.     switch(V_VT(m_pmain))
  271.     {
  272.     case VT_I2:
  273.         int ii;
  274.         other->GetValue(&ii);
  275.         return(V_I2(m_pmain) == ii);
  276.     case VT_I4:
  277.         long ll;
  278.         other->GetValue(&ll);
  279.         return(V_I4(m_pmain) == ll);
  280.     case VT_R8:
  281.         double dd;
  282.         other->GetValue(&dd);
  283.         return(V_R8(m_pmain) == dd);
  284.     case VT_BSTR:
  285.         const char *cp;
  286.         other->GetValue(&cp);
  287.         return(0 == strcmp(V_BSTR(m_pmain), cp));
  288.     default:
  289.         // huh?
  290.         return(FALSE);
  291.     }
  292.     
  293.     return(FALSE);  // shouldn't get here
  294. }
  295.                      
  296. oresult OOLEvar::SetValue(int val)
  297. {
  298.     VariantClear(m_pmain);
  299.     m_isconv = FALSE;
  300.     V_VT(m_pmain) = VT_I2;
  301.     V_I2(m_pmain) = (short) val;
  302.     return(OSUCCESS);
  303. }
  304.  
  305. oresult OOLEvar::SetValue(long val)
  306. {
  307.     VariantClear(m_pmain);
  308.     m_isconv = FALSE;
  309.     V_VT(m_pmain) = VT_I4;
  310.     V_I4(m_pmain) = val;
  311.     return(OSUCCESS);
  312. }
  313.  
  314. oresult OOLEvar::SetValue(double val)
  315. {
  316.     VariantClear(m_pmain);
  317.     m_isconv = FALSE;
  318.     V_VT(m_pmain) = VT_R8;
  319.     V_R8(m_pmain) = val;
  320.     return(OSUCCESS);
  321. }
  322.  
  323. oresult OOLEvar::SetValue(const char *val)
  324. {
  325.     VariantClear(m_pmain);
  326.     m_isconv = FALSE;
  327.     char *cp;
  328. //BUG #262214 and 260396    
  329.     if (!val)
  330.     {
  331.         cp = SysAllocString("");
  332.         // cp = "";
  333.     }
  334.     else
  335.     {
  336.         cp = SysAllocString(val);
  337.     }
  338.     if (!cp)
  339.     {
  340.         return(OFAILURE);  // out of memory
  341.     }
  342.     
  343.     // set value
  344.     V_VT(m_pmain) = VT_BSTR;
  345.     V_BSTR(m_pmain) = cp;
  346.     return(OSUCCESS);
  347. }
  348.  
  349. oresult OOLEvar::SetValue(VARIANT *val)
  350. {
  351.     HRESULT hr = VariantCopy(m_pmain, val);
  352.     VariantClear(m_pconv);
  353.     m_isconv = FALSE;
  354.     return(FAILED(hr) ? OFAILURE : OSUCCESS);
  355. }
  356.  
  357. oresult OOLEvar::GetValue(int *val)
  358. {
  359.     oresult ores = OSUCCESS;
  360.     
  361.     if (IsNull())
  362.     {
  363.         *val = 0;
  364.         ores = OSUCCESS;
  365.     }
  366.     else if (V_VT(m_pmain) == VT_I2)
  367.         *val = V_I2(m_pmain);
  368.     else if (m_isconv && V_VT(m_pconv) == VT_I2)
  369.         *val = V_I2(m_pconv);
  370.     else
  371.     { // we need to do a conversion
  372.     
  373.         HRESULT hr = VariantChangeType(m_pconv, m_pmain, 0, VT_I2);
  374.         if (FAILED(hr))
  375.         {
  376.             ores = OFAILURE;
  377.             *val = 0;
  378.         }
  379.         else
  380.         { // succesful conversion
  381.             m_isconv = TRUE;
  382.             *val = V_I2(m_pconv);
  383.         }
  384.     }
  385.     
  386.     return(ores);
  387. }
  388.  
  389. oresult OOLEvar::GetValue(long *val)
  390. {
  391.     oresult ores = OSUCCESS;
  392.     
  393.     if (IsNull())
  394.     {
  395.         *val = 0;
  396.         ores = OSUCCESS;
  397.     }
  398.     else if (V_VT(m_pmain) == VT_I4)
  399.         *val = V_I4(m_pmain);
  400.     else if (m_isconv && V_VT(m_pconv) == VT_I4)
  401.         *val = V_I4(m_pconv);
  402.     else if (V_VT(m_pmain) == VT_I2)
  403.         *val = V_I2(m_pmain);
  404.     else if (m_isconv && V_VT(m_pconv) == VT_I2)
  405.         *val = V_I2(m_pconv);
  406.     else
  407.     { // we need to do a conversion
  408.     
  409.         HRESULT hr = VariantChangeType(m_pconv, m_pmain, 0, VT_I4);
  410.         if (FAILED(hr))
  411.         {
  412.             ores = OFAILURE;
  413.             *val = 0;
  414.         }
  415.         else
  416.         { // succesful conversion
  417.             m_isconv = TRUE;
  418.             *val = V_I4(m_pconv);
  419.         }
  420.     }
  421.     
  422.     return(ores);
  423. }
  424.  
  425. oresult OOLEvar::GetValue(double *val)
  426. {
  427.     oresult ores = OSUCCESS;
  428.     
  429.     if (IsNull())
  430.     {
  431.         *val = 0.0;
  432.         ores = OSUCCESS;
  433.     }
  434.     else if (V_VT(m_pmain) == VT_R8)
  435.         *val = V_R8(m_pmain);
  436.     else if (m_isconv && V_VT(m_pconv) == VT_R8)
  437.         *val = V_R8(m_pconv);
  438.     else
  439.     { // we need to do a conversion
  440.     
  441.         HRESULT hr = VariantChangeType(m_pconv, m_pmain, 0, VT_R8);
  442.         if (FAILED(hr))
  443.         {
  444.             ores = OFAILURE;
  445.             *val = 0;
  446.         }
  447.         else
  448.         { // succesful conversion
  449.             m_isconv = TRUE;
  450.             *val = V_R8(m_pconv);
  451.         }
  452.     }
  453.     
  454.     return(ores);
  455. }
  456.  
  457. oresult OOLEvar::GetValue(const char **val)
  458. {
  459.     oresult ores = OSUCCESS;
  460.     
  461.     if (IsNull())
  462.     {
  463.         *val = NULL;
  464.         ores = OSUCCESS;
  465.     }
  466.     else if (V_VT(m_pmain) == VT_BSTR)
  467.         *val = V_BSTR(m_pmain);
  468.     else if (m_isconv && V_VT(m_pconv) == VT_BSTR)
  469.         *val = V_BSTR(m_pconv);
  470.     else
  471.     { // we need to do a conversion
  472.     
  473.         HRESULT hr = VariantChangeType(m_pconv, m_pmain, 0, VT_BSTR);
  474.         if (FAILED(hr))
  475.         {
  476.             ores = OFAILURE;
  477.             *val = NULL;
  478.         }
  479.         else
  480.         { // succesful conversion
  481.             m_isconv = TRUE;
  482.             *val = V_BSTR(m_pconv);
  483.         }
  484.     }
  485.     
  486.     return(ores);
  487. }
  488.  
  489. unsigned int OOLEvar::GetStringLength(void) const
  490. {
  491.     if (V_VT(m_pmain) == VT_BSTR)
  492.         return(SysStringLen(V_BSTR(m_pmain)));
  493.     else if (m_isconv && V_VT(m_pconv) == VT_BSTR)
  494.         return(SysStringLen(V_BSTR(m_pconv)));
  495.     else
  496.         return(0);
  497. }
  498.  
  499.