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.

902 lines
28 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Table.cpp
  5. Abstract:
  6. This file contains the implementation of the JetBlue::Table class.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/17/2000
  9. created
  10. ******************************************************************************/
  11. #include <stdafx.h>
  12. ////////////////////////////////////////////////////////////////////////////////
  13. static HRESULT AdjustReturnCode( HRESULT hr, bool *pfFound )
  14. {
  15. if(pfFound)
  16. {
  17. if(hr == JetBlue::JetERRToHRESULT(JET_errNoCurrentRecord) ||
  18. hr == JetBlue::JetERRToHRESULT(JET_errRecordNotFound ) )
  19. {
  20. hr = S_OK;
  21. *pfFound = false;
  22. }
  23. else
  24. {
  25. *pfFound = (SUCCEEDED(hr)) ? true : false;
  26. }
  27. }
  28. return hr;
  29. }
  30. ////////////////////////////////////////////////////////////////////////////////
  31. JetBlue::Table::Table()
  32. {
  33. m_sesid = JET_sesidNil; // JET_SESID m_sesid;
  34. m_dbid = JET_dbidNil; // JET_DBID m_dbid;
  35. m_tableid = JET_tableidNil; // JET_TABLEID m_tableid;
  36. // MPC::string m_strName;
  37. // ColumnVector m_vecColumns;
  38. // IndexVector m_vecIndexes;
  39. m_idxSelected = NULL; // Index* m_idxSelected;
  40. // Column m_fakeCol;
  41. // Index m_fakeIdx;
  42. }
  43. JetBlue::Table::Table( /*[in]*/ JET_SESID sesid ,
  44. /*[in]*/ JET_DBID dbid ,
  45. /*[in]*/ LPCSTR szName )
  46. {
  47. m_sesid = sesid; // JET_SESID m_sesid;
  48. m_dbid = dbid; // JET_DBID m_dbid;
  49. m_tableid = JET_tableidNil; // JET_TABLEID m_tableid;
  50. m_strName = szName; // MPC::string m_strName;
  51. // ColumnVector m_vecColumns;
  52. // IndexVector m_vecIndexes;
  53. m_idxSelected = NULL; // Index* m_idxSelected;
  54. // Column m_fakeCol;
  55. // Index m_fakeIdx;
  56. }
  57. JetBlue::Table::~Table()
  58. {
  59. (void)Close( true );
  60. }
  61. ////////////////////////////////////////
  62. HRESULT JetBlue::Table::Duplicate( /*[in]*/ Table& tbl )
  63. {
  64. __HCP_FUNC_ENTRY( "JetBlue::Table::Duplicate" );
  65. HRESULT hr;
  66. int iColMax = tbl.m_vecColumns.size();
  67. int iIdxMax = tbl.m_vecIndexes.size();
  68. int iCol;
  69. int iIdx;
  70. m_sesid = tbl.m_sesid; // JET_SESID m_sesid;
  71. m_dbid = tbl.m_dbid; // JET_DBID m_dbid;
  72. m_tableid = JET_tableidNil; // JET_TABLEID m_tableid;
  73. m_strName.erase(); // MPC::string m_strName;
  74. m_vecColumns.resize( iColMax ); // ColumnVector m_vecColumns;
  75. m_vecIndexes.resize( iIdxMax ); // IndexVector m_vecIndexes;
  76. // Column m_fakeCol;
  77. // Index m_fakeIdx;
  78. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetDupCursor( m_sesid, tbl.m_tableid, &m_tableid, 0 ));
  79. //
  80. // Copy the columns and indexes, updating the tableid.
  81. //
  82. for(iCol=0; iCol<iColMax; iCol++)
  83. {
  84. Column& colSrc = tbl.m_vecColumns[iCol];
  85. Column& colDst = m_vecColumns[iCol];
  86. colDst.m_sesid = m_sesid; // JET_SESID m_sesid;
  87. colDst.m_tableid = m_tableid; // JET_TABLEID m_tableid;
  88. colDst.m_strName = colSrc.m_strName; // MPC::string m_strName;
  89. colDst.m_coldef = colSrc.m_coldef; // JET_COLUMNDEF m_coldef;
  90. }
  91. for(iIdx=0; iIdx<iIdxMax; iIdx++)
  92. {
  93. Index& idxSrc = tbl.m_vecIndexes[iIdx];
  94. Index& idxDst = m_vecIndexes[iIdx];
  95. idxDst.m_sesid = m_sesid; // JET_SESID m_sesid;
  96. idxDst.m_tableid = m_tableid; // JET_TABLEID m_tableid;
  97. idxDst.m_strName = idxSrc.m_strName; // MPC::string m_strName;
  98. idxDst.m_grbitIndex = idxSrc.m_grbitIndex; // JET_GRBIT m_grbitIndex;
  99. idxDst.m_cKey = idxSrc.m_cKey; // LONG m_cKey;
  100. idxDst.m_cEntry = idxSrc.m_cEntry; // LONG m_cEntry;
  101. idxDst.m_cPage = idxSrc.m_cPage; // LONG m_cPage;
  102. // ColumnVector m_vecColumns;
  103. // Column m_fake;
  104. iColMax = idxSrc.m_vecColumns.size();
  105. idxDst.m_vecColumns.resize( iColMax );
  106. for(iCol=0; iCol<iColMax; iCol++)
  107. {
  108. Column& colSrc = idxSrc.m_vecColumns[iCol];
  109. Column& colDst = idxDst.m_vecColumns[iCol];
  110. colDst.m_sesid = m_sesid; // JET_SESID m_sesid;
  111. colDst.m_tableid = m_tableid; // JET_TABLEID m_tableid;
  112. colDst.m_strName = colSrc.m_strName; // MPC::string m_strName;
  113. colDst.m_coldef = colSrc.m_coldef; // JET_COLUMNDEF m_coldef;
  114. }
  115. }
  116. hr = S_OK;
  117. __HCP_FUNC_CLEANUP;
  118. __HCP_FUNC_EXIT(hr);
  119. }
  120. HRESULT JetBlue::Table::Refresh()
  121. {
  122. __HCP_FUNC_ENTRY( "JetBlue::Table::Refresh" );
  123. HRESULT hr;
  124. JET_COLUMNLIST infoCols; ::ZeroMemory( &infoCols, sizeof(infoCols) ); infoCols.cbStruct = sizeof(infoCols); infoCols.tableid = JET_tableidNil;
  125. JET_INDEXLIST infoIdxs; ::ZeroMemory( &infoIdxs, sizeof(infoIdxs) ); infoIdxs.cbStruct = sizeof(infoIdxs); infoIdxs.tableid = JET_tableidNil;
  126. m_vecColumns.clear();
  127. if(m_tableid != JET_tableidNil)
  128. {
  129. ////////////////////////////////////////////////////////////////////////////////
  130. //
  131. // Read column definition.
  132. //
  133. // JET_COLUMNLIST
  134. // {
  135. // unsigned long cbStruct;
  136. // JET_TABLEID tableid;
  137. // unsigned long cRecord;
  138. // JET_COLUMNID columnidPresentationOrder;
  139. // JET_COLUMNID columnidcolumnname;
  140. // JET_COLUMNID columnidcolumnid;
  141. // JET_COLUMNID columnidcoltyp;
  142. // JET_COLUMNID columnidCountry;
  143. // JET_COLUMNID columnidLangid;
  144. // JET_COLUMNID columnidCp;
  145. // JET_COLUMNID columnidCollate;
  146. // JET_COLUMNID columnidcbMax;
  147. // JET_COLUMNID columnidgrbit;
  148. // JET_COLUMNID columnidDefault;
  149. // JET_COLUMNID columnidBaseTableName;
  150. // JET_COLUMNID columnidBaseColumnName;
  151. // JET_COLUMNID columnidDefinitionName;
  152. // }
  153. //
  154. // JET_COLUMNDEF
  155. // {
  156. // unsigned long cbStruct;
  157. // JET_COLUMNID columnid;
  158. // JET_COLTYP coltyp;
  159. // unsigned short wCountry;
  160. // unsigned short langid;
  161. // unsigned short cp;
  162. // unsigned short wCollate; /* Must be 0 */
  163. // unsigned long cbMax;
  164. // JET_GRBIT grbit;
  165. // };
  166. //
  167. // JET_RETRIEVECOLUMN
  168. // {
  169. // JET_COLUMNID columnid;
  170. // void *pvData;
  171. // unsigned long cbData;
  172. // unsigned long cbActual;
  173. // JET_GRBIT grbit;
  174. // unsigned long ibLongValue;
  175. // unsigned long itagSequence;
  176. // JET_COLUMNID columnidNextTagged;
  177. // JET_ERR err;
  178. // };
  179. {
  180. JET_RETRIEVECOLUMN rc [9 ]; ::ZeroMemory( &rc, sizeof(rc) );
  181. char colName[JET_cbNameMost+1];
  182. JET_COLUMNDEF colDef;
  183. int i;
  184. #ifdef USE_WHISTLER_VERSION
  185. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetGetTableColumnInfo( m_sesid, m_tableid, NULL, &infoCols, sizeof(infoCols), JET_ColInfoListSortColumnid ));
  186. #else
  187. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetGetTableColumnInfo( m_sesid, m_tableid, NULL, &infoCols, sizeof(infoCols), JET_ColInfoList ));
  188. #endif
  189. __MPC_JET_INIT_RETRIEVE_COL( rc, 0, infoCols.columnidcolumnname, colName , JET_cbNameMost );
  190. __MPC_JET_INIT_RETRIEVE_COL( rc, 1, infoCols.columnidcolumnid , &colDef.columnid, sizeof(colDef.columnid) );
  191. __MPC_JET_INIT_RETRIEVE_COL( rc, 2, infoCols.columnidcoltyp , &colDef.coltyp , sizeof(colDef.coltyp ) );
  192. __MPC_JET_INIT_RETRIEVE_COL( rc, 3, infoCols.columnidCountry , &colDef.wCountry, sizeof(colDef.wCountry) );
  193. __MPC_JET_INIT_RETRIEVE_COL( rc, 4, infoCols.columnidLangid , &colDef.langid , sizeof(colDef.langid ) );
  194. __MPC_JET_INIT_RETRIEVE_COL( rc, 5, infoCols.columnidCp , &colDef.cp , sizeof(colDef.cp ) );
  195. __MPC_JET_INIT_RETRIEVE_COL( rc, 6, infoCols.columnidCollate , &colDef.wCollate, sizeof(colDef.wCollate) );
  196. __MPC_JET_INIT_RETRIEVE_COL( rc, 7, infoCols.columnidcbMax , &colDef.cbMax , sizeof(colDef.cbMax ) );
  197. __MPC_JET_INIT_RETRIEVE_COL( rc, 8, infoCols.columnidgrbit , &colDef.grbit , sizeof(colDef.grbit ) );
  198. m_vecColumns.resize( infoCols.cRecord );
  199. for(i=0; i<infoCols.cRecord; i++)
  200. {
  201. Column& col = m_vecColumns[i];
  202. ::ZeroMemory( colName, sizeof(colName) );
  203. ::ZeroMemory( &colDef , sizeof(colDef ) );
  204. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetMove ( m_sesid, infoCols.tableid, (i == 0 ? JET_MoveFirst : JET_MoveNext), 0 ));
  205. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetRetrieveColumns( m_sesid, infoCols.tableid, rc, ARRAYSIZE(rc) ));
  206. col.m_sesid = m_sesid;
  207. col.m_tableid = m_tableid;
  208. col.m_strName = colName;
  209. col.m_coldef.columnid = colDef.columnid;
  210. col.m_coldef.coltyp = colDef.coltyp;
  211. col.m_coldef.wCountry = colDef.wCountry;
  212. col.m_coldef.langid = colDef.langid;
  213. col.m_coldef.cp = colDef.cp;
  214. col.m_coldef.wCollate = colDef.wCollate;
  215. col.m_coldef.cbMax = colDef.cbMax;
  216. col.m_coldef.grbit = colDef.grbit;
  217. }
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. //
  221. // Read index definition.
  222. //
  223. // JET_INDEXLIST
  224. // {
  225. // unsigned long cbStruct;
  226. // JET_TABLEID tableid;
  227. // unsigned long cRecord;
  228. // JET_COLUMNID columnidindexname; # JET_coltypText # LPSTR INDEX
  229. // JET_COLUMNID columnidgrbitIndex; # JET_coltypLong # JET_GRBIT INDEX
  230. // JET_COLUMNID columnidcKey; # JET_coltypLong # LONG INDEX
  231. // JET_COLUMNID columnidcEntry; # JET_coltypLong # LONG INDEX
  232. // JET_COLUMNID columnidcPage; # JET_coltypLong # LONG INDEX
  233. // JET_COLUMNID columnidcColumn; # JET_coltypLong # LONG INDEX
  234. // JET_COLUMNID columnidiColumn; # JET_coltypLong # ULONG COLUMN
  235. // JET_COLUMNID columnidcolumnid; # JET_coltypLong # JET_COLUMNID COLUMN
  236. // JET_COLUMNID columnidcoltyp; # JET_coltypLong # JET_COLTYP COLUMN
  237. // JET_COLUMNID columnidCountry; # JET_coltypShort # WORD INDEX
  238. // JET_COLUMNID columnidLangid; # JET_coltypShort # LANGID INDEX
  239. // JET_COLUMNID columnidCp; # JET_coltypShort # USHORT COLUMN
  240. // JET_COLUMNID columnidCollate; # JET_coltypShort # WORD INDEX
  241. // JET_COLUMNID columnidgrbitColumn; # JET_coltypLong # JET_GRBIT COLUMN
  242. // JET_COLUMNID columnidcolumnname; # JET_coltypText # LPSTR COLUMN
  243. // JET_COLUMNID columnidLCMapFlags; # JET_coltypLong # DWORD INDEX
  244. // };
  245. {
  246. JET_RETRIEVECOLUMN rc [14 ]; ::ZeroMemory( &rc, sizeof(rc) );
  247. char idxName[JET_cbNameMost+1];
  248. char colName[JET_cbNameMost+1];
  249. Index* idx = NULL;
  250. Column* col;
  251. JET_COLUMNDEF colDef;
  252. JET_GRBIT grbit;
  253. LONG cKey;
  254. LONG cEntry;
  255. LONG cPage;
  256. LONG cColumn;
  257. int iIdx = 0;
  258. int iCol = 0;
  259. int i;
  260. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetGetTableIndexInfo( m_sesid, m_tableid, NULL, &infoIdxs, sizeof(infoIdxs), JET_IdxInfoList ));
  261. __MPC_JET_INIT_RETRIEVE_COL( rc, 0, infoIdxs.columnidindexname , idxName , JET_cbNameMost );
  262. __MPC_JET_INIT_RETRIEVE_COL( rc, 1, infoIdxs.columnidgrbitIndex , &grbit , sizeof(grbit ) );
  263. __MPC_JET_INIT_RETRIEVE_COL( rc, 2, infoIdxs.columnidcKey , &cKey , sizeof(cKey ) );
  264. __MPC_JET_INIT_RETRIEVE_COL( rc, 3, infoIdxs.columnidcEntry , &cEntry , sizeof(cEntry ) );
  265. __MPC_JET_INIT_RETRIEVE_COL( rc, 4, infoIdxs.columnidcPage , &cPage , sizeof(cPage ) );
  266. __MPC_JET_INIT_RETRIEVE_COL( rc, 5, infoIdxs.columnidcColumn , &cColumn , sizeof(cColumn ) );
  267. __MPC_JET_INIT_RETRIEVE_COL( rc, 6, infoIdxs.columnidcolumnname , colName , JET_cbNameMost );
  268. __MPC_JET_INIT_RETRIEVE_COL( rc, 7, infoIdxs.columnidcolumnid , &colDef.columnid, sizeof(colDef.columnid) );
  269. __MPC_JET_INIT_RETRIEVE_COL( rc, 8, infoIdxs.columnidcoltyp , &colDef.coltyp , sizeof(colDef.coltyp ) );
  270. __MPC_JET_INIT_RETRIEVE_COL( rc, 9, infoIdxs.columnidCountry , &colDef.wCountry, sizeof(colDef.wCountry) );
  271. __MPC_JET_INIT_RETRIEVE_COL( rc, 10, infoIdxs.columnidLangid , &colDef.langid , sizeof(colDef.langid ) );
  272. __MPC_JET_INIT_RETRIEVE_COL( rc, 11, infoIdxs.columnidCp , &colDef.cp , sizeof(colDef.cp ) );
  273. __MPC_JET_INIT_RETRIEVE_COL( rc, 12, infoIdxs.columnidCollate , &colDef.wCollate, sizeof(colDef.wCollate) );
  274. __MPC_JET_INIT_RETRIEVE_COL( rc, 13, infoIdxs.columnidgrbitColumn, &colDef.grbit , sizeof(colDef.grbit ) );
  275. m_vecIndexes.resize( infoIdxs.cRecord );
  276. for(i=0; i<infoIdxs.cRecord; i++)
  277. {
  278. ::ZeroMemory( idxName, sizeof(idxName) );
  279. ::ZeroMemory( colName, sizeof(colName) );
  280. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetMove ( m_sesid, infoIdxs.tableid, (i == 0 ? JET_MoveFirst : JET_MoveNext), 0 ));
  281. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetRetrieveColumns( m_sesid, infoIdxs.tableid, rc, ARRAYSIZE(rc) ));
  282. if(idx == NULL || idx->m_strName != idxName)
  283. {
  284. iCol = 0;
  285. idx = &m_vecIndexes[iIdx++];
  286. idx->m_sesid = m_sesid;
  287. idx->m_tableid = m_tableid;
  288. idx->m_strName = idxName;
  289. idx->m_grbitIndex = grbit;
  290. idx->m_cKey = cKey;
  291. idx->m_cEntry = cEntry;
  292. idx->m_cPage = cPage;
  293. idx->m_vecColumns.resize( cColumn );
  294. }
  295. col = &idx->m_vecColumns[iCol++];
  296. col->m_sesid = m_sesid;
  297. col->m_tableid = m_tableid;
  298. col->m_strName = colName;
  299. col->m_coldef.columnid = colDef.columnid;
  300. col->m_coldef.coltyp = colDef.coltyp;
  301. col->m_coldef.wCountry = colDef.wCountry;
  302. col->m_coldef.langid = colDef.langid;
  303. col->m_coldef.cp = colDef.cp;
  304. col->m_coldef.wCollate = colDef.wCollate;
  305. col->m_coldef.cbMax = colDef.cbMax;
  306. col->m_coldef.grbit = colDef.grbit;
  307. }
  308. m_vecIndexes.resize( iIdx ); // Trim down the size to the real one.
  309. }
  310. }
  311. hr = S_OK;
  312. __HCP_FUNC_CLEANUP;
  313. if(infoCols.tableid != JET_tableidNil)
  314. {
  315. __MPC_JET__MTSAFE_NORESULT(m_sesid, ::JetCloseTable( m_sesid, infoCols.tableid ));
  316. }
  317. if(infoIdxs.tableid != JET_tableidNil)
  318. {
  319. __MPC_JET__MTSAFE_NORESULT(m_sesid, ::JetCloseTable( m_sesid, infoIdxs.tableid ));
  320. }
  321. __HCP_FUNC_EXIT(hr);
  322. }
  323. HRESULT JetBlue::Table::Close( /*[in]*/ bool fForce )
  324. {
  325. __HCP_FUNC_ENTRY( "JetBlue::Table::Close" );
  326. HRESULT hr;
  327. if(m_tableid != JET_tableidNil)
  328. {
  329. JET_ERR err = ::JetCloseTable( m_sesid, m_tableid ); if(!fForce) __MPC_EXIT_IF_JET_FAILS(hr, err);
  330. m_tableid = JET_tableidNil;
  331. }
  332. m_idxSelected = NULL;
  333. m_vecColumns.clear();
  334. hr = S_OK;
  335. __HCP_FUNC_CLEANUP;
  336. __HCP_FUNC_EXIT(hr);
  337. }
  338. ////////////////////////////////////////
  339. HRESULT JetBlue::Table::Attach( /*[in]*/ JET_TABLEID tableid )
  340. {
  341. __HCP_FUNC_ENTRY( "JetBlue::Table::Attach" );
  342. HRESULT hr;
  343. __MPC_EXIT_IF_METHOD_FAILS(hr, Close());
  344. m_tableid = tableid;
  345. __MPC_EXIT_IF_METHOD_FAILS(hr, Refresh());
  346. hr = S_OK;
  347. __HCP_FUNC_CLEANUP;
  348. __HCP_FUNC_EXIT(hr);
  349. }
  350. HRESULT JetBlue::Table::Open()
  351. {
  352. __HCP_FUNC_ENTRY( "JetBlue::Table::Open" );
  353. HRESULT hr;
  354. if(m_tableid == JET_tableidNil)
  355. {
  356. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetOpenTable( m_sesid, m_dbid, m_strName.c_str(), NULL, 0, JET_bitTableUpdatable, &m_tableid ));
  357. __MPC_EXIT_IF_METHOD_FAILS(hr, Refresh());
  358. }
  359. hr = S_OK;
  360. __HCP_FUNC_CLEANUP;
  361. __HCP_FUNC_EXIT(hr);
  362. }
  363. HRESULT JetBlue::Table::Create()
  364. {
  365. __HCP_FUNC_ENTRY( "JetBlue::Table::Create" );
  366. HRESULT hr;
  367. if(m_tableid == JET_tableidNil)
  368. {
  369. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetCreateTable( m_sesid, m_dbid, m_strName.c_str(), 10, 80, &m_tableid ));
  370. __MPC_EXIT_IF_METHOD_FAILS(hr, Refresh());
  371. }
  372. hr = S_OK;
  373. __HCP_FUNC_CLEANUP;
  374. __HCP_FUNC_EXIT(hr);
  375. }
  376. HRESULT JetBlue::Table::Create( /*[in]*/ JET_TABLECREATE* pDef )
  377. {
  378. __HCP_FUNC_ENTRY( "JetBlue::Table::Create" );
  379. HRESULT hr;
  380. JET_TABLECREATE tbldef = *pDef;
  381. __MPC_EXIT_IF_METHOD_FAILS(hr, Close());
  382. tbldef.szTableName = (LPSTR)m_strName.c_str();
  383. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetCreateTableColumnIndex( m_sesid, m_dbid, &tbldef ));
  384. m_tableid = tbldef.tableid;
  385. __MPC_EXIT_IF_METHOD_FAILS(hr, Refresh());
  386. hr = S_OK;
  387. __HCP_FUNC_CLEANUP;
  388. __HCP_FUNC_EXIT(hr);
  389. }
  390. HRESULT JetBlue::Table::Delete( /*[in]*/ bool fForce )
  391. {
  392. __HCP_FUNC_ENTRY( "JetBlue::Table::Delete" );
  393. HRESULT hr;
  394. __MPC_EXIT_IF_METHOD_FAILS(hr, Close( fForce ));
  395. if(m_strName.length() > 0)
  396. {
  397. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetDeleteTable( m_sesid, m_dbid, m_strName.c_str() ));
  398. }
  399. hr = S_OK;
  400. __HCP_FUNC_CLEANUP;
  401. __HCP_FUNC_EXIT(hr);
  402. }
  403. ////////////////////////////////////////
  404. HRESULT JetBlue::Table::DupCursor( /*[in/out]*/ Cursor& cur )
  405. {
  406. __HCP_FUNC_ENTRY( "JetBlue::Table::DupCursor" );
  407. HRESULT hr;
  408. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  409. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  410. __MPC_EXIT_IF_METHOD_FAILS(hr, cur->Duplicate( *this ));
  411. hr = S_OK;
  412. __HCP_FUNC_CLEANUP;
  413. __HCP_FUNC_EXIT(hr);
  414. }
  415. HRESULT JetBlue::Table::SelectIndex( /*[in]*/ LPCSTR szIndex ,
  416. /*[in]*/ JET_GRBIT grbit )
  417. {
  418. __HCP_FUNC_ENTRY( "JetBlue::Table::SelectIndex" );
  419. HRESULT hr;
  420. int iPos;
  421. Index* idxSelected;
  422. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  423. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  424. iPos = GetIdxPosition( szIndex );
  425. if(iPos == -1)
  426. {
  427. __MPC_SET_ERROR_AND_EXIT(hr, JetBlue::JetERRToHRESULT(JET_errIndexNotFound));
  428. }
  429. idxSelected = &(m_vecIndexes[iPos]);
  430. if(grbit == JET_bitNoMove)
  431. {
  432. if(m_idxSelected == idxSelected)
  433. {
  434. //
  435. // No need to reselect it.
  436. //
  437. __MPC_SET_ERROR_AND_EXIT(hr, S_OK);
  438. }
  439. //
  440. // There was no index selected, so there's no current record...
  441. //
  442. grbit = JET_bitMoveFirst;
  443. }
  444. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetSetCurrentIndex2( m_sesid, m_tableid, szIndex, grbit ));
  445. m_idxSelected = idxSelected;
  446. hr = S_OK;
  447. __HCP_FUNC_CLEANUP;
  448. __HCP_FUNC_EXIT(hr);
  449. }
  450. HRESULT JetBlue::Table::SetIndexRange( /*[in]*/ JET_GRBIT grbit )
  451. {
  452. __HCP_FUNC_ENTRY( "JetBlue::Table::SetIndexRange" );
  453. HRESULT hr;
  454. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  455. __MPC_JET_CHECKHANDLE(hr,m_tableid ,JET_tableidNil);
  456. __MPC_JET_CHECKHANDLE(hr,m_idxSelected,NULL );
  457. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetSetIndexRange( m_sesid, m_tableid, grbit ));
  458. hr = S_OK;
  459. __HCP_FUNC_CLEANUP;
  460. __HCP_FUNC_EXIT(hr);
  461. }
  462. HRESULT JetBlue::Table::PrepareInsert()
  463. {
  464. __HCP_FUNC_ENTRY( "JetBlue::Table::PrepareInsert" );
  465. HRESULT hr;
  466. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  467. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  468. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetPrepareUpdate( m_sesid, m_tableid, JET_prepInsert ));
  469. hr = S_OK;
  470. __HCP_FUNC_CLEANUP;
  471. __HCP_FUNC_EXIT(hr);
  472. }
  473. HRESULT JetBlue::Table::PrepareUpdate()
  474. {
  475. __HCP_FUNC_ENTRY( "JetBlue::Table::PrepareUpdate" );
  476. HRESULT hr;
  477. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  478. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  479. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetPrepareUpdate( m_sesid, m_tableid, JET_prepReplace ));
  480. hr = S_OK;
  481. __HCP_FUNC_CLEANUP;
  482. __HCP_FUNC_EXIT(hr);
  483. }
  484. HRESULT JetBlue::Table::CancelChange()
  485. {
  486. __HCP_FUNC_ENTRY( "JetBlue::Table::CancelChange" );
  487. HRESULT hr;
  488. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  489. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  490. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetPrepareUpdate( m_sesid, m_tableid, JET_prepCancel ));
  491. hr = S_OK;
  492. __HCP_FUNC_CLEANUP;
  493. __HCP_FUNC_EXIT(hr);
  494. }
  495. HRESULT JetBlue::Table::Move( /*[in]*/ JET_GRBIT grbit ,
  496. /*[in]*/ long cRow ,
  497. /*[in]*/ bool *pfFound )
  498. {
  499. __HCP_FUNC_ENTRY( "JetBlue::Table::Move" );
  500. HRESULT hr;
  501. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  502. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  503. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetMove( m_sesid, m_tableid, cRow, grbit ));
  504. hr = S_OK;
  505. __HCP_FUNC_CLEANUP;
  506. hr = AdjustReturnCode( hr, pfFound );
  507. __HCP_FUNC_EXIT(hr);
  508. }
  509. HRESULT JetBlue::Table::Seek( /*[in]*/ JET_GRBIT grbit ,
  510. /*[in]*/ VARIANT* rgKeys ,
  511. /*[in]*/ int iLen ,
  512. /*[in]*/ bool *pfFound )
  513. {
  514. __HCP_FUNC_ENTRY( "JetBlue::Table::Seek" );
  515. HRESULT hr;
  516. int iPos;
  517. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  518. __MPC_JET_CHECKHANDLE(hr,m_tableid ,JET_tableidNil);
  519. __MPC_JET_CHECKHANDLE(hr,m_idxSelected,NULL );
  520. if(iLen != m_idxSelected->NumOfColumns())
  521. {
  522. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  523. }
  524. for(iPos=0; iPos<iLen; iPos++)
  525. {
  526. __MPC_EXIT_IF_METHOD_FAILS(hr, m_idxSelected->GetCol( iPos ).Put( rgKeys[iPos], iPos ));
  527. }
  528. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetSeek( m_sesid, m_tableid, grbit ));
  529. hr = S_OK;
  530. __HCP_FUNC_CLEANUP;
  531. hr = AdjustReturnCode( hr, pfFound );
  532. __HCP_FUNC_EXIT(hr);
  533. }
  534. HRESULT JetBlue::Table::Get( /*[in]*/ int iArg ,
  535. /*[out]*/ CComVariant* rgArg )
  536. {
  537. __HCP_FUNC_ENTRY( "JetBlue::Table::Get" );
  538. HRESULT hr;
  539. ColumnIter it;
  540. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  541. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  542. for(it = m_vecColumns.begin(); it != m_vecColumns.end() && iArg > 0; it++, iArg--, rgArg++)
  543. {
  544. __MPC_EXIT_IF_METHOD_FAILS(hr, it->Get( *rgArg ));
  545. }
  546. hr = S_OK;
  547. __HCP_FUNC_CLEANUP;
  548. __HCP_FUNC_EXIT(hr);
  549. }
  550. HRESULT JetBlue::Table::Put( /*[in]*/ int iArg ,
  551. /*[in]*/ const CComVariant* rgArg )
  552. {
  553. __HCP_FUNC_ENTRY( "JetBlue::Table::Put" );
  554. HRESULT hr;
  555. ColumnIter it;
  556. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  557. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  558. for(it = m_vecColumns.begin(); it != m_vecColumns.end() && iArg > 0; it++, iArg--, rgArg++)
  559. {
  560. __MPC_EXIT_IF_METHOD_FAILS(hr, it->Put( *rgArg ));
  561. }
  562. hr = S_OK;
  563. __HCP_FUNC_CLEANUP;
  564. __HCP_FUNC_EXIT(hr);
  565. }
  566. HRESULT JetBlue::Table::UpdateRecord( /*[in]*/ bool fMove )
  567. {
  568. __HCP_FUNC_ENTRY( "JetBlue::Table::UpdateRecord" );
  569. HRESULT hr;
  570. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  571. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  572. if(fMove)
  573. {
  574. BYTE rgBookmark[JET_cbBookmarkMost];
  575. unsigned long cbActual;
  576. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetUpdate ( m_sesid, m_tableid, rgBookmark, sizeof(rgBookmark), &cbActual ));
  577. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetGotoBookmark( m_sesid, m_tableid, rgBookmark , cbActual ));
  578. }
  579. else
  580. {
  581. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetUpdate( m_sesid, m_tableid, NULL, 0, NULL ));
  582. }
  583. hr = S_OK;
  584. __HCP_FUNC_CLEANUP;
  585. __HCP_FUNC_EXIT(hr);
  586. }
  587. HRESULT JetBlue::Table::DeleteRecord()
  588. {
  589. __HCP_FUNC_ENTRY( "JetBlue::Table::DeleteRecord" );
  590. HRESULT hr;
  591. __MPC_JET_CHECKHANDLE(hr,m_sesid ,JET_sesidNil );
  592. __MPC_JET_CHECKHANDLE(hr,m_tableid,JET_tableidNil);
  593. __MPC_EXIT_IF_JET_FAILS__MTSAFE(m_sesid, hr, ::JetDelete( m_sesid, m_tableid ));
  594. hr = S_OK;
  595. __HCP_FUNC_CLEANUP;
  596. __HCP_FUNC_EXIT(hr);
  597. }
  598. ////////////////////////////////////////
  599. int JetBlue::Table::GetColPosition( /*[in]*/ LPCSTR szCol )
  600. {
  601. int iLen = m_vecColumns.size();
  602. int iPos;
  603. for(iPos=0; iPos<iLen; iPos++)
  604. {
  605. Column& col = m_vecColumns[iPos];
  606. if(col.m_strName.compare( SAFEASTR( szCol ) ) == 0) return iPos;
  607. }
  608. return -1;
  609. }
  610. JetBlue::Column& JetBlue::Table::GetCol( /*[in]*/ LPCSTR szCol )
  611. {
  612. return GetCol( GetColPosition( szCol ) );
  613. }
  614. JetBlue::Column& JetBlue::Table::GetCol( /*[in]*/ int iPos )
  615. {
  616. if(0 <= iPos && iPos < m_vecColumns.size()) return m_vecColumns[iPos];
  617. return m_fakeCol;
  618. }
  619. ////////////////////////////////////////
  620. int JetBlue::Table::GetIdxPosition( /*[in]*/ LPCSTR szIdx )
  621. {
  622. int iLen = m_vecIndexes.size();
  623. int iPos;
  624. for(iPos=0; iPos<iLen; iPos++)
  625. {
  626. Index& idx = m_vecIndexes[iPos];
  627. if(idx.m_strName.compare( SAFEASTR( szIdx ) ) == 0) return iPos;
  628. }
  629. return -1;
  630. }
  631. JetBlue::Index& JetBlue::Table::GetIdx( /*[in]*/ LPCSTR szIdx )
  632. {
  633. return GetIdx( GetIdxPosition( szIdx ) );
  634. }
  635. JetBlue::Index& JetBlue::Table::GetIdx( /*[in]*/ int iPos )
  636. {
  637. if(0 <= iPos && iPos < m_vecIndexes.size()) return m_vecIndexes[iPos];
  638. return m_fakeIdx;
  639. }