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.

462 lines
14 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Column.cpp
  5. Abstract:
  6. This file contains the implementation of the JetBlue::Column class.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/17/2000
  9. created
  10. ******************************************************************************/
  11. #include <stdafx.h>
  12. ////////////////////////////////////////////////////////////////////////////////
  13. JetBlue::Column::Column()
  14. {
  15. m_sesid = JET_sesidNil; // JET_SESID m_sesid;
  16. m_tableid = JET_tableidNil; // JET_TABLEID m_tableid;
  17. // MPC::string m_strName;
  18. // JET_COLUMNDEF m_coldef;
  19. ::ZeroMemory( &m_coldef, sizeof(m_coldef) ); m_coldef.cbStruct = sizeof(m_coldef);
  20. }
  21. JetBlue::Column::~Column()
  22. {
  23. }
  24. ////////////////////////////////////////
  25. HRESULT JetBlue::Column::Get( /*[out]*/ CComVariant& vValue )
  26. {
  27. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  28. HRESULT hr;
  29. JET_ERR err;
  30. JET_RETINFO ret;
  31. void* pvData;
  32. unsigned long cbData;
  33. unsigned long cbActual;
  34. BYTE rgBuf[2048+2];
  35. BYTE* pBufLarge = NULL;
  36. vValue.Clear();
  37. switch(m_coldef.coltyp)
  38. {
  39. case JET_coltypBit : pvData = &rgBuf[0] ; cbData = sizeof( rgBuf[0] ) ; break;
  40. case JET_coltypUnsignedByte: pvData = &vValue.bVal ; cbData = sizeof( vValue.bVal ) ; break;
  41. case JET_coltypShort : pvData = &vValue.iVal ; cbData = sizeof( vValue.iVal ) ; break;
  42. case JET_coltypLong : pvData = &vValue.lVal ; cbData = sizeof( vValue.lVal ) ; break;
  43. case JET_coltypCurrency : pvData = &vValue.cyVal ; cbData = sizeof( vValue.cyVal ) ; break;
  44. case JET_coltypIEEESingle : pvData = &vValue.fltVal; cbData = sizeof( vValue.fltVal ) ; break;
  45. case JET_coltypIEEEDouble : pvData = &vValue.dblVal; cbData = sizeof( vValue.dblVal ) ; break;
  46. case JET_coltypDateTime : pvData = &vValue.date ; cbData = sizeof( vValue.date ) ; break;
  47. /////////////////////////////////////////////////////////////////////////////////////////////
  48. case JET_coltypText :
  49. case JET_coltypLongText : pvData = rgBuf ; cbData = sizeof( rgBuf ) - 2; break;
  50. default:
  51. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  52. }
  53. __MPC_JET__MTSAFE(m_sesid, err, ::JetRetrieveColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, &cbActual, JET_bitRetrieveCopy, NULL ));
  54. //
  55. // No value, bail out.
  56. //
  57. if(err == JET_wrnColumnNull)
  58. {
  59. vValue.vt = VT_NULL;
  60. __MPC_SET_ERROR_AND_EXIT(hr, S_OK);
  61. }
  62. __MPC_EXIT_IF_JET_FAILS(hr, err);
  63. if(cbActual > cbData)
  64. {
  65. __MPC_EXIT_IF_ALLOC_FAILS(hr, pBufLarge, new BYTE[cbActual+2]);
  66. pvData = pBufLarge;
  67. cbData = cbActual;
  68. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetRetrieveColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, &cbActual, JET_bitRetrieveCopy, NULL ));
  69. }
  70. switch(m_coldef.coltyp)
  71. {
  72. case JET_coltypBit : vValue.vt = VT_BOOL; vValue.boolVal = rgBuf[0] ? VARIANT_TRUE : VARIANT_FALSE; break;
  73. case JET_coltypUnsignedByte: vValue.vt = VT_UI1; break;
  74. case JET_coltypShort : vValue.vt = VT_I2; break;
  75. case JET_coltypLong : vValue.vt = VT_I4; break;
  76. case JET_coltypCurrency : vValue.vt = VT_CY; break;
  77. case JET_coltypIEEESingle : vValue.vt = VT_R4; break;
  78. case JET_coltypIEEEDouble : vValue.vt = VT_R8; break;
  79. case JET_coltypDateTime : vValue.vt = VT_DATE; break;
  80. case JET_coltypText :
  81. case JET_coltypLongText:
  82. // Put the trailing zeros, just in case...
  83. ((BYTE*)pvData + cbActual)[0] = 0;
  84. ((BYTE*)pvData + cbActual)[1] = 0;
  85. if(m_coldef.cp == 1200) vValue = (LPWSTR)pvData;
  86. else vValue = (LPSTR )pvData;
  87. break;
  88. }
  89. hr = S_OK;
  90. __HCP_FUNC_CLEANUP;
  91. if(pBufLarge) delete [] pBufLarge;
  92. __HCP_FUNC_EXIT(hr);
  93. }
  94. HRESULT JetBlue::Column::Get( /*[out]*/ MPC::CComHGLOBAL& hgValue )
  95. {
  96. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  97. HRESULT hr;
  98. void* pvData;
  99. unsigned long cbData;
  100. unsigned long cbActual;
  101. BYTE rgBuf[256];
  102. BYTE* pBufLarge = NULL;
  103. switch(m_coldef.coltyp)
  104. {
  105. case JET_coltypBinary : break;
  106. case JET_coltypLongBinary: break;
  107. default: __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  108. }
  109. pvData = rgBuf;
  110. cbData = sizeof(rgBuf);
  111. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetRetrieveColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, &cbActual, JET_bitRetrieveCopy, NULL ));
  112. if(cbActual > cbData)
  113. {
  114. __MPC_EXIT_IF_ALLOC_FAILS(hr, pBufLarge, new BYTE[cbActual]);
  115. pvData = pBufLarge;
  116. cbData = cbActual;
  117. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetRetrieveColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, &cbActual, JET_bitRetrieveCopy, NULL ));
  118. }
  119. __MPC_EXIT_IF_METHOD_FAILS(hr, hgValue.New( GMEM_FIXED, cbActual ));
  120. ::CopyMemory( hgValue.Get(), pvData, cbActual );
  121. hr = S_OK;
  122. __HCP_FUNC_CLEANUP;
  123. if(pBufLarge) delete [] pBufLarge;
  124. __HCP_FUNC_EXIT(hr);
  125. }
  126. HRESULT JetBlue::Column::Get( /*[out]*/ MPC::wstring& strValue )
  127. {
  128. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  129. HRESULT hr;
  130. CComVariant vValue;
  131. __MPC_EXIT_IF_METHOD_FAILS(hr, Get( vValue ));
  132. __MPC_EXIT_IF_METHOD_FAILS(hr, vValue.ChangeType( VT_BSTR ));
  133. strValue = SAFEBSTR( vValue.bstrVal );
  134. hr = S_OK;
  135. __HCP_FUNC_CLEANUP;
  136. __HCP_FUNC_EXIT(hr);
  137. }
  138. HRESULT JetBlue::Column::Get( /*[out]*/ MPC::string& strValue )
  139. {
  140. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  141. USES_CONVERSION;
  142. HRESULT hr;
  143. CComVariant vValue;
  144. __MPC_EXIT_IF_METHOD_FAILS(hr, Get( vValue ));
  145. __MPC_EXIT_IF_METHOD_FAILS(hr, vValue.ChangeType( VT_BSTR ));
  146. strValue = W2A( SAFEBSTR( vValue.bstrVal ) );
  147. hr = S_OK;
  148. __HCP_FUNC_CLEANUP;
  149. __HCP_FUNC_EXIT(hr);
  150. }
  151. HRESULT JetBlue::Column::Get( /*[out]*/ long& lValue )
  152. {
  153. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  154. HRESULT hr;
  155. CComVariant vValue;
  156. __MPC_EXIT_IF_METHOD_FAILS(hr, Get( vValue ));
  157. __MPC_EXIT_IF_METHOD_FAILS(hr, vValue.ChangeType( VT_I4 ));
  158. lValue = vValue.lVal;
  159. hr = S_OK;
  160. __HCP_FUNC_CLEANUP;
  161. __HCP_FUNC_EXIT(hr);
  162. }
  163. HRESULT JetBlue::Column::Get( /*[out]*/ short& sValue )
  164. {
  165. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  166. HRESULT hr;
  167. CComVariant vValue;
  168. __MPC_EXIT_IF_METHOD_FAILS(hr, Get( vValue ));
  169. __MPC_EXIT_IF_METHOD_FAILS(hr, vValue.ChangeType( VT_I2 ));
  170. sValue = vValue.iVal;
  171. hr = S_OK;
  172. __HCP_FUNC_CLEANUP;
  173. __HCP_FUNC_EXIT(hr);
  174. }
  175. HRESULT JetBlue::Column::Get( /*[out]*/ BYTE& bValue )
  176. {
  177. __HCP_FUNC_ENTRY( "JetBlue::Column::Get" );
  178. HRESULT hr;
  179. CComVariant vValue;
  180. __MPC_EXIT_IF_METHOD_FAILS(hr, Get( vValue ));
  181. __MPC_EXIT_IF_METHOD_FAILS(hr, vValue.ChangeType( VT_UI1 ));
  182. bValue = vValue.bVal;
  183. hr = S_OK;
  184. __HCP_FUNC_CLEANUP;
  185. __HCP_FUNC_EXIT(hr);
  186. }
  187. ////////////////////////////////////////
  188. HRESULT JetBlue::Column::Put( /*[in]*/ const VARIANT& vValue ,
  189. /*[in]*/ int iIdxPos )
  190. {
  191. __HCP_FUNC_ENTRY( "JetBlue::Column::Put" );
  192. USES_CONVERSION;
  193. HRESULT hr;
  194. const void* pvData;
  195. unsigned long cbData;
  196. BYTE fBool;
  197. VARTYPE vt;
  198. CComVariant vTmp;
  199. const VARIANT* pvPtr;
  200. JET_GRBIT grbit = 0;
  201. if(vValue.vt == VT_NULL)
  202. {
  203. pvData = NULL;
  204. cbData = 0;
  205. }
  206. else
  207. {
  208. switch(m_coldef.coltyp)
  209. {
  210. case JET_coltypBit : vt = VT_BOOL; break;
  211. case JET_coltypUnsignedByte: vt = VT_UI1 ; break;
  212. case JET_coltypShort : vt = VT_I2 ; break;
  213. case JET_coltypLong : vt = VT_I4 ; break;
  214. case JET_coltypCurrency : vt = VT_CY ; break;
  215. case JET_coltypIEEESingle : vt = VT_R4 ; break;
  216. case JET_coltypIEEEDouble : vt = VT_R8 ; break;
  217. case JET_coltypDateTime : vt = VT_DATE; break;
  218. case JET_coltypText : vt = VT_BSTR; break;
  219. case JET_coltypLongText : vt = VT_BSTR; break;
  220. default : __MPC_EXIT_IF_METHOD_FAILS(hr, E_INVALIDARG); break;
  221. }
  222. if(vt != vValue.vt)
  223. {
  224. __MPC_EXIT_IF_METHOD_FAILS(hr, vTmp.ChangeType( vt, &vValue ));
  225. pvPtr = &vTmp;
  226. }
  227. else
  228. {
  229. pvPtr = &vValue;
  230. }
  231. if(vt == VT_BOOL)
  232. {
  233. fBool = pvPtr->boolVal == VARIANT_FALSE ? 0 : 1;
  234. }
  235. switch(m_coldef.coltyp)
  236. {
  237. case JET_coltypBit : pvData = &fBool ; cbData = sizeof( fBool ) ; break;
  238. case JET_coltypUnsignedByte: pvData = &pvPtr->bVal ; cbData = sizeof( pvPtr->bVal ) ; break;
  239. case JET_coltypShort : pvData = &pvPtr->iVal ; cbData = sizeof( pvPtr->iVal ) ; break;
  240. case JET_coltypLong : pvData = &pvPtr->lVal ; cbData = sizeof( pvPtr->lVal ) ; break;
  241. case JET_coltypCurrency : pvData = &pvPtr->cyVal ; cbData = sizeof( pvPtr->cyVal ) ; break;
  242. case JET_coltypIEEESingle : pvData = &pvPtr->fltVal ; cbData = sizeof( pvPtr->fltVal ) ; break;
  243. case JET_coltypIEEEDouble : pvData = &pvPtr->dblVal ; cbData = sizeof( pvPtr->dblVal ) ; break;
  244. case JET_coltypDateTime : pvData = &pvPtr->date ; cbData = sizeof( pvPtr->date ) ; break;
  245. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  246. case JET_coltypText :
  247. case JET_coltypLongText:
  248. if(m_coldef.cp == 1200) { pvData = SAFEBSTR( pvPtr->bstrVal ) ; cbData = wcslen( (LPWSTR)pvData ) * 2; }
  249. else { pvData = W2A( SAFEBSTR( pvPtr->bstrVal ) ); cbData = strlen( (LPSTR )pvData ) ; }
  250. break;
  251. }
  252. if(cbData == 0) grbit = JET_bitSetZeroLength;
  253. }
  254. {
  255. JET_ERR err;
  256. if(iIdxPos == -1)
  257. {
  258. __MPC_JET__MTSAFE(m_sesid, err, ::JetSetColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, grbit, NULL ));
  259. //
  260. // Special case: Jet returns a warning, we want to return an error.
  261. //
  262. if(err == JET_wrnColumnMaxTruncated)
  263. {
  264. __MPC_SET_ERROR_AND_EXIT(hr, HRESULT_BASE_JET | (err & 0xFFFF));
  265. }
  266. }
  267. else
  268. {
  269. __MPC_JET__MTSAFE(m_sesid, err, ::JetMakeKey( m_sesid, m_tableid, pvData, cbData, iIdxPos > 0 ? 0 : JET_bitNewKey ));
  270. }
  271. __MPC_EXIT_IF_JET_FAILS(hr, err);
  272. }
  273. hr = S_OK;
  274. __HCP_FUNC_CLEANUP;
  275. __HCP_FUNC_EXIT(hr);
  276. }
  277. HRESULT JetBlue::Column::Put( /*[out]*/ const MPC::CComHGLOBAL& hgValue )
  278. {
  279. __HCP_FUNC_ENTRY( "JetBlue::Column::Put" );
  280. HRESULT hr;
  281. LPVOID pvData;
  282. unsigned long cbData;
  283. unsigned long cbActual;
  284. BYTE rgBuf[256];
  285. BYTE* pBufLarge = NULL;
  286. pvData = hgValue.Lock();
  287. cbData = hgValue.Size();
  288. switch(m_coldef.coltyp)
  289. {
  290. case JET_coltypBinary : break;
  291. case JET_coltypLongBinary: break;
  292. default: __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  293. }
  294. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetSetColumn( m_sesid, m_tableid, m_coldef.columnid, pvData, cbData, 0, NULL ));
  295. hr = S_OK;
  296. __HCP_FUNC_CLEANUP;
  297. hgValue.Unlock();
  298. __HCP_FUNC_EXIT(hr);
  299. }
  300. HRESULT JetBlue::Column::Put( /*[out]*/ const MPC::wstring& strValue )
  301. {
  302. return Put( strValue.c_str() );
  303. }
  304. HRESULT JetBlue::Column::Put( /*[out]*/ const MPC::string& strValue )
  305. {
  306. return Put( strValue.c_str() );
  307. }
  308. HRESULT JetBlue::Column::Put( /*[out]*/ LPCWSTR szValue )
  309. {
  310. return Put( CComVariant( szValue ) );
  311. }
  312. HRESULT JetBlue::Column::Put( /*[out]*/ long lValue )
  313. {
  314. return Put( CComVariant( lValue ) );
  315. }
  316. HRESULT JetBlue::Column::Put( /*[out]*/ LPCSTR szValue )
  317. {
  318. return Put( CComVariant( szValue ) );
  319. }
  320. HRESULT JetBlue::Column::Put( /*[out]*/ short sValue )
  321. {
  322. return Put( CComVariant( sValue ) );
  323. }
  324. HRESULT JetBlue::Column::Put( /*[out]*/ BYTE bValue )
  325. {
  326. return Put( CComVariant( bValue ) );
  327. }