Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
4.9 KiB

  1. //---------------------------------------------------------------------------
  2. // FromVar.cpp : GetDataFromDBVariant implementation
  3. //
  4. // Copyright (c) 1996 Microsoft Corporation, All Rights Reserved
  5. // Developed by Sheridan Software Systems, Inc.
  6. //---------------------------------------------------------------------------
  7. #include "stdafx.h"
  8. #include "timeconv.h"
  9. #include "fromvar.h"
  10. #include <math.h>
  11. #include <limits.h>
  12. SZTHISFILE
  13. //=--------------------------------------------------------------------------=
  14. // CoerceToDBVariant
  15. //=--------------------------------------------------------------------------=
  16. // Coerce a CURSOR_DBVARIANT to data suitable for IRowsetFind::GetRowsByValues
  17. //
  18. // Parameters:
  19. // pVar - [in] a pointer to the variant containing the data
  20. // pwType - [out] a pointer to memory in which to return the DBTYPE
  21. // of the data
  22. // ppValue - [out] a pointer to memory in which to return a pointer
  23. // to the date
  24. // pfMemAllocated - [out] a pointer to memory in which to return whether
  25. // new memory was allocated for the data. This is
  26. // assumed to be initialzed to false by caller
  27. //
  28. // Output:
  29. // HRESULT - S_OK if successful
  30. //
  31. HRESULT GetDataFromDBVariant(CURSOR_DBVARIANT * pVar,
  32. DBTYPE * pwType,
  33. BYTE ** ppData,
  34. BOOL * pfMemAllocated)
  35. {
  36. ASSERT_POINTER(pVar, CURSOR_DBVARIANT)
  37. ASSERT_POINTER(pwType, DBTYPE)
  38. ASSERT_POINTER(ppData, BYTE*)
  39. ASSERT_POINTER(pfMemAllocated, BOOL)
  40. if (!pVar || !pwType || !ppData || !pfMemAllocated)
  41. return E_INVALIDARG;
  42. VARTYPE vt = pVar->vt;
  43. BOOL fByRef = FALSE;
  44. BOOL fArray = FALSE;
  45. BOOL fVector = FALSE;
  46. *pwType = 0;
  47. if (vt & VT_VECTOR)
  48. {
  49. vt ^= VT_VECTOR;
  50. *pwType = DBTYPE_VECTOR;
  51. if (vt & VT_BYREF)
  52. {
  53. *pwType |= DBTYPE_BYREF;
  54. vt ^= VT_BYREF;
  55. }
  56. fVector = TRUE;
  57. }
  58. else
  59. if (vt & VT_ARRAY)
  60. {
  61. vt ^= VT_ARRAY;
  62. fArray = TRUE;
  63. *pwType = DBTYPE_ARRAY;
  64. }
  65. else
  66. if (vt & VT_BYREF)
  67. {
  68. vt ^= VT_BYREF;
  69. fByRef = TRUE;
  70. *pwType = DBTYPE_BYREF;
  71. }
  72. *ppData = (BYTE*)&pVar->iVal;
  73. HRESULT hr = S_OK;
  74. switch (vt)
  75. {
  76. case CURSOR_DBTYPE_EMPTY:
  77. *pwType = DBTYPE_EMPTY;
  78. break;
  79. case CURSOR_DBTYPE_NULL:
  80. *pwType = DBTYPE_NULL;
  81. break;
  82. case CURSOR_DBTYPE_I2:
  83. *pwType |= DBTYPE_I2;
  84. break;
  85. case CURSOR_DBTYPE_I4:
  86. *pwType |= DBTYPE_I4;
  87. break;
  88. case CURSOR_DBTYPE_I8:
  89. *pwType |= DBTYPE_I8;
  90. break;
  91. case CURSOR_DBTYPE_R4:
  92. *pwType |= DBTYPE_R4;
  93. break;
  94. case CURSOR_DBTYPE_R8:
  95. *pwType |= DBTYPE_R8;
  96. break;
  97. case CURSOR_DBTYPE_CY:
  98. *pwType |= DBTYPE_CY;
  99. break;
  100. case CURSOR_DBTYPE_DATE:
  101. *pwType |= DBTYPE_DATE;
  102. break;
  103. case CURSOR_DBTYPE_BOOL:
  104. *pwType |= DBTYPE_BOOL;
  105. break;
  106. case CURSOR_DBTYPE_HRESULT:
  107. *pwType |= DBTYPE_ERROR;
  108. break;
  109. case CURSOR_DBTYPE_LPSTR:
  110. *pwType |= DBTYPE_STR;
  111. if (!fByRef && !fArray && !fVector)
  112. *ppData = (BYTE*)pVar->pszVal;
  113. break;
  114. case CURSOR_DBTYPE_LPWSTR:
  115. *pwType |= DBTYPE_WSTR;
  116. if (!fByRef && !fArray && !fVector)
  117. *ppData = (BYTE*)pVar->pwszVal;
  118. break;
  119. case VT_BSTR:
  120. *pwType |= DBTYPE_WSTR;
  121. if (!fByRef && !fArray && !fVector)
  122. *ppData = (BYTE*)pVar->bstrVal;
  123. break;
  124. case CURSOR_DBTYPE_UUID:
  125. *pwType |= DBTYPE_GUID;
  126. break;
  127. case CURSOR_DBTYPE_UI2:
  128. *pwType |= DBTYPE_UI2;
  129. break;
  130. case CURSOR_DBTYPE_UI4:
  131. *pwType |= DBTYPE_UI4;
  132. break;
  133. case CURSOR_DBTYPE_UI8:
  134. *pwType |= DBTYPE_UI8;
  135. break;
  136. case CURSOR_DBTYPE_ANYVARIANT:
  137. *pwType |= DBTYPE_VARIANT;
  138. break;
  139. case CURSOR_DBTYPE_BYTES:
  140. case CURSOR_DBTYPE_CHARS:
  141. case CURSOR_DBTYPE_BLOB:
  142. *pwType |= DBTYPE_BYTES;
  143. break;
  144. case CURSOR_DBTYPE_WCHARS:
  145. *pwType |= DBTYPE_UI2;
  146. break;
  147. case CURSOR_DBTYPE_FILETIME:
  148. {
  149. FILETIME *pFileTime = NULL;
  150. if (fVector)
  151. {
  152. DBVECTOR * pvector = (DBVECTOR*)pVar->byref;
  153. if (pvector)
  154. pFileTime = (FILETIME*)pvector->ptr;
  155. }
  156. else
  157. if (fByRef)
  158. {
  159. pFileTime = (FILETIME*)pVar->byref;
  160. }
  161. else
  162. if (!fArray)
  163. {
  164. pFileTime = (FILETIME*)&pVar->cyVal;
  165. }
  166. if (pFileTime)
  167. {
  168. DBTIMESTAMP * pstamp = (DBTIMESTAMP*)g_pMalloc->Alloc(sizeof(DBTIMESTAMP));
  169. if (pstamp)
  170. {
  171. if (VDConvertToDBTimeStamp(pFileTime, pstamp))
  172. {
  173. *ppData = (BYTE*)pstamp;
  174. *pwType |= DBTYPE_DBTIMESTAMP;
  175. *pfMemAllocated = TRUE;
  176. }
  177. else
  178. {
  179. g_pMalloc->Free(pstamp);
  180. hr = CURSOR_DB_CANTCOERCE;
  181. }
  182. }
  183. else
  184. hr = E_OUTOFMEMORY;
  185. }
  186. else
  187. hr = CURSOR_DB_CANTCOERCE;
  188. break;
  189. }
  190. case CURSOR_DBTYPE_DBEXPR:
  191. case CURSOR_DBTYPE_COLUMNID:
  192. default:
  193. hr = CURSOR_DB_CANTCOERCE;
  194. break;
  195. }
  196. return hr;
  197. }