Source code of Windows XP (NT5)
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.

590 lines
12 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. database.h
  5. Abstract:
  6. SIS Groveler Jet-Blue database include file
  7. Authors:
  8. Cedric Krumbein, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #define USERNAME ""
  14. #define PASSWORD ""
  15. #define TABLE_PAGES 50
  16. #define TABLE_DENSITY 50
  17. #define MIN_VER_PAGES 128
  18. #define MAX_DATABASE_CACHE_SIZE (6*1024*1024)
  19. #define COUNTRY_CODE 1
  20. #define LANG_ID 0x0409
  21. #define CODE_PAGE 1252
  22. #define COLLATE 0
  23. #define MAX_KEYS 3
  24. #define MAX_COLUMNS 8
  25. #define MAX_LIST_NAME_LEN 255
  26. #define GET_ALL_MASK (~0U)
  27. typedef struct {
  28. CHAR *name;
  29. DWORD size;
  30. DWORD offset;
  31. JET_COLTYP coltyp;
  32. JET_GRBIT grbit;
  33. } ColumnSpec;
  34. /***************************** Table parameters ******************************/
  35. #define TABLE_NAME "SGTable"
  36. typedef DWORDLONG Signature;
  37. typedef GUID CSID;
  38. typedef struct _SGNativeTableEntry {
  39. DWORDLONG fileID;
  40. DWORDLONG fileSize;
  41. Signature signature;
  42. DWORD attributes;
  43. CSID csIndex;
  44. DWORDLONG createTime;
  45. DWORDLONG writeTime;
  46. } SGNativeTableEntry;
  47. #define TABLE_COL_NUM_FILE_ID 0
  48. #define TABLE_COL_NUM_SIZE 1
  49. #define TABLE_COL_NUM_SIG 2
  50. #define TABLE_COL_NUM_ATTR 3
  51. #define TABLE_COL_NUM_CSIDLO 4
  52. #define TABLE_COL_NUM_CSIDHI 5
  53. #define TABLE_COL_NUM_CR_TIME 6
  54. #define TABLE_COL_NUM_WR_TIME 7
  55. #define TABLE_NCOLS 8
  56. #define TABLE_COL_NAME_FILE_ID "FileID"
  57. #define TABLE_COL_NAME_SIZE "FileSize"
  58. #define TABLE_COL_NAME_SIG "Signature"
  59. #define TABLE_COL_NAME_ATTR "Attributes"
  60. #define TABLE_COL_NAME_CSIDLO "CSIndexLo"
  61. #define TABLE_COL_NAME_CSIDHI "CSIndexHi"
  62. #define TABLE_COL_NAME_CR_TIME "CreateTime"
  63. #define TABLE_COL_NAME_WR_TIME "WriteTime"
  64. #define TABLE_KEY_NAME_FILE_ID "FileIDKey"
  65. #define TABLE_KEY_NAME_ATTR "AttributeKey"
  66. #define TABLE_KEY_NAME_CSID "CSIndexKey"
  67. #define TABLE_KEY_NCOLS_FILE_ID 1
  68. #define TABLE_KEY_NCOLS_ATTR 3
  69. #define TABLE_KEY_NCOLS_CSID 2
  70. #define TABLE_EXCLUDE_FILE_ID_MASK \
  71. (~(1U << TABLE_COL_NUM_FILE_ID))
  72. #define TABLE_EXCLUDE_ATTR_MASK \
  73. (~(1U << TABLE_COL_NUM_SIZE \
  74. | 1U << TABLE_COL_NUM_SIG \
  75. | 1U << TABLE_COL_NUM_ATTR))
  76. #define TABLE_EXCLUDE_CS_INDEX_MASK \
  77. (~(1U << TABLE_COL_NUM_CSIDLO \
  78. | 1U << TABLE_COL_NUM_CSIDHI))
  79. static ColumnSpec
  80. tableColDefFileID = {
  81. TABLE_COL_NAME_FILE_ID,
  82. sizeof(DWORDLONG),
  83. offsetof(SGNativeTableEntry, fileID),
  84. JET_coltypCurrency,
  85. 0
  86. },
  87. tableColDefSize = {
  88. TABLE_COL_NAME_SIZE,
  89. sizeof(DWORDLONG),
  90. offsetof(SGNativeTableEntry, fileSize),
  91. JET_coltypCurrency,
  92. 0
  93. },
  94. tableColDefSig = {
  95. TABLE_COL_NAME_SIG,
  96. sizeof(Signature),
  97. offsetof(SGNativeTableEntry, signature),
  98. JET_coltypCurrency,
  99. 0
  100. },
  101. tableColDefAttr = {
  102. TABLE_COL_NAME_ATTR,
  103. sizeof(DWORD),
  104. offsetof(SGNativeTableEntry, attributes),
  105. JET_coltypLong,
  106. 0
  107. },
  108. tableColDefCSIDlo = {
  109. TABLE_COL_NAME_CSIDLO,
  110. sizeof(DWORDLONG),
  111. offsetof(SGNativeTableEntry, csIndex),
  112. JET_coltypCurrency,
  113. 0
  114. },
  115. tableColDefCSIDhi = {
  116. TABLE_COL_NAME_CSIDHI,
  117. sizeof(DWORDLONG),
  118. offsetof(SGNativeTableEntry, csIndex) + sizeof(DWORDLONG),
  119. JET_coltypCurrency,
  120. 0
  121. },
  122. tableColDefCrTime = {
  123. TABLE_COL_NAME_CR_TIME,
  124. sizeof(DWORDLONG),
  125. offsetof(SGNativeTableEntry, createTime),
  126. JET_coltypCurrency,
  127. 0
  128. },
  129. tableColDefWrTime = {
  130. TABLE_COL_NAME_WR_TIME,
  131. sizeof(DWORDLONG),
  132. offsetof(SGNativeTableEntry, writeTime),
  133. JET_coltypCurrency,
  134. 0
  135. },
  136. *tableColumnSpecs[TABLE_NCOLS] = {
  137. &tableColDefFileID,
  138. &tableColDefSize,
  139. &tableColDefSig,
  140. &tableColDefAttr,
  141. &tableColDefCSIDlo,
  142. &tableColDefCSIDhi,
  143. &tableColDefCrTime,
  144. &tableColDefWrTime
  145. },
  146. *tableKeyFileID[TABLE_KEY_NCOLS_FILE_ID] = {
  147. &tableColDefFileID
  148. },
  149. *tableKeyAttr[TABLE_KEY_NCOLS_ATTR] = {
  150. &tableColDefSize,
  151. &tableColDefSig,
  152. &tableColDefAttr,
  153. },
  154. *tableKeyCSID[TABLE_KEY_NCOLS_CSID] = {
  155. &tableColDefCSIDlo,
  156. &tableColDefCSIDhi,
  157. };
  158. /***************************** Queue parameters ******************************/
  159. #define QUEUE_NAME "SGQueue"
  160. typedef struct _SGNativeQueueEntry {
  161. DWORDLONG fileID;
  162. DWORDLONG parentID;
  163. DWORD reason;
  164. DWORD order;
  165. DWORDLONG readyTime;
  166. DWORDLONG retryTime;
  167. TCHAR *fileName;
  168. } SGNativeQueueEntry;
  169. #define QUEUE_COL_NUM_FILE_ID 0
  170. #define QUEUE_COL_NUM_PARENT_ID 1
  171. #define QUEUE_COL_NUM_REASON 2
  172. #define QUEUE_COL_NUM_ORDER 3
  173. #define QUEUE_COL_NUM_READY_TIME 4
  174. #define QUEUE_COL_NUM_RETRY_TIME 5
  175. #define QUEUE_COL_NUM_NAME 6
  176. #define QUEUE_NCOLS 7
  177. #define QUEUE_COL_NAME_FILE_ID "FileID"
  178. #define QUEUE_COL_NAME_PARENT_ID "ParentID"
  179. #define QUEUE_COL_NAME_REASON "Reason"
  180. #define QUEUE_COL_NAME_ORDER "Order"
  181. #define QUEUE_COL_NAME_READY_TIME "ReadyTime"
  182. #define QUEUE_COL_NAME_RETRY_TIME "RetryTime"
  183. #define QUEUE_COL_NAME_NAME "FileName"
  184. #define QUEUE_KEY_NAME_READY_TIME "ReadyTimeKey"
  185. #define QUEUE_KEY_NAME_FILE_ID "FileIDKey"
  186. #define QUEUE_KEY_NAME_ORDER "Order"
  187. #define QUEUE_KEY_NCOLS_READY_TIME 1
  188. #define QUEUE_KEY_NCOLS_FILE_ID 1
  189. #define QUEUE_KEY_NCOLS_ORDER 1
  190. #define QUEUE_EXCLUDE_FILE_ID_MASK \
  191. (~(1U << QUEUE_COL_NUM_FILE_ID))
  192. static ColumnSpec
  193. queueColDefFileID = {
  194. QUEUE_COL_NAME_FILE_ID,
  195. sizeof(DWORDLONG),
  196. offsetof(SGNativeQueueEntry, fileID),
  197. JET_coltypCurrency,
  198. 0
  199. },
  200. queueColDefParentID = {
  201. QUEUE_COL_NAME_PARENT_ID,
  202. sizeof(DWORDLONG),
  203. offsetof(SGNativeQueueEntry, parentID),
  204. JET_coltypCurrency,
  205. 0
  206. },
  207. queueColDefReason = {
  208. QUEUE_COL_NAME_REASON,
  209. sizeof(DWORD),
  210. offsetof(SGNativeQueueEntry, reason),
  211. JET_coltypLong,
  212. 0
  213. },
  214. queueColDefOrder = {
  215. QUEUE_COL_NAME_ORDER,
  216. sizeof(DWORD),
  217. offsetof(SGNativeQueueEntry, order),
  218. JET_coltypLong,
  219. JET_bitColumnAutoincrement
  220. },
  221. queueColDefReadyTime = {
  222. QUEUE_COL_NAME_READY_TIME,
  223. sizeof(DWORDLONG),
  224. offsetof(SGNativeQueueEntry, readyTime),
  225. JET_coltypCurrency,
  226. 0
  227. },
  228. queueColDefRetryTime = {
  229. QUEUE_COL_NAME_RETRY_TIME,
  230. sizeof(DWORDLONG),
  231. offsetof(SGNativeQueueEntry, retryTime),
  232. JET_coltypCurrency,
  233. 0
  234. },
  235. queueColDefName = {
  236. QUEUE_COL_NAME_NAME,
  237. MAX_PATH * sizeof(TCHAR),
  238. offsetof(SGNativeQueueEntry, fileName),
  239. JET_coltypLongBinary,
  240. 0
  241. },
  242. *queueColumnSpecs[QUEUE_NCOLS] = {
  243. &queueColDefFileID,
  244. &queueColDefParentID,
  245. &queueColDefReason,
  246. &queueColDefOrder,
  247. &queueColDefReadyTime,
  248. &queueColDefRetryTime,
  249. &queueColDefName
  250. },
  251. *queueKeyReadyTime[QUEUE_KEY_NCOLS_READY_TIME] = {
  252. &queueColDefReadyTime
  253. },
  254. *queueKeyFileID[QUEUE_KEY_NCOLS_FILE_ID] = {
  255. &queueColDefFileID
  256. },
  257. *queueKeyOrder[QUEUE_KEY_NCOLS_ORDER] = {
  258. &queueColDefOrder
  259. };
  260. /***************************** Stack parameters ******************************/
  261. #define STACK_NAME "SGStack"
  262. typedef struct _SGNativeStackEntry {
  263. DWORDLONG fileID;
  264. DWORD order;
  265. } SGNativeStackEntry;
  266. #define STACK_COL_NUM_FILE_ID 0
  267. #define STACK_COL_NUM_ORDER 1
  268. #define STACK_NCOLS 2
  269. #define STACK_COL_NAME_FILE_ID "FileID"
  270. #define STACK_COL_NAME_ORDER "Order"
  271. #define STACK_KEY_NAME_FILE_ID "FileIDKey"
  272. #define STACK_KEY_NAME_ORDER "Order"
  273. #define STACK_KEY_NCOLS_FILE_ID 1
  274. #define STACK_KEY_NCOLS_ORDER 1
  275. #define STACK_EXCLUDE_FILE_ID_MASK \
  276. (~(1U << STACK_COL_NUM_FILE_ID))
  277. #define STACK_GET_ORDER_ONLY_MASK \
  278. (1U << STACK_COL_NUM_ORDER)
  279. static ColumnSpec
  280. stackColDefFileID = {
  281. STACK_COL_NAME_FILE_ID,
  282. sizeof(DWORDLONG),
  283. offsetof(SGNativeStackEntry, fileID),
  284. JET_coltypCurrency,
  285. 0
  286. },
  287. stackColDefOrder = {
  288. STACK_COL_NAME_ORDER,
  289. sizeof(DWORD),
  290. offsetof(SGNativeStackEntry, order),
  291. JET_coltypLong,
  292. 0
  293. },
  294. *stackColumnSpecs[STACK_NCOLS] = {
  295. &stackColDefFileID,
  296. &stackColDefOrder
  297. },
  298. *stackKeyFileID[STACK_KEY_NCOLS_FILE_ID] = {
  299. &stackColDefFileID
  300. },
  301. *stackKeyOrder[STACK_KEY_NCOLS_ORDER] = {
  302. &stackColDefOrder
  303. };
  304. /****************************** List parameters ******************************/
  305. #define LIST_NAME "SGList"
  306. typedef struct _SGNativeListEntry {
  307. const TCHAR *name;
  308. TCHAR *value;
  309. } SGNativeListEntry;
  310. #define LIST_COL_NUM_NAME 0
  311. #define LIST_COL_NUM_VALUE 1
  312. #define LIST_NCOLS 2
  313. #define LIST_COL_NAME_NAME "Name"
  314. #define LIST_COL_NAME_VALUE "Value"
  315. #define LIST_KEY_NAME_NAME "NameKey"
  316. #define LIST_KEY_NCOLS_NAME 1
  317. #define LIST_EXCLUDE_NAME_MASK \
  318. (~(1U << LIST_COL_NUM_NAME))
  319. static ColumnSpec
  320. listColDefName = {
  321. LIST_COL_NAME_NAME,
  322. MAX_LIST_NAME_LEN,
  323. offsetof(SGNativeListEntry, name),
  324. JET_coltypBinary,
  325. 0
  326. },
  327. listColDefValue = {
  328. LIST_COL_NAME_VALUE,
  329. MAX_PATH * sizeof(TCHAR),
  330. offsetof(SGNativeListEntry, value),
  331. JET_coltypLongBinary,
  332. 0
  333. },
  334. *listColumnSpecs[LIST_NCOLS] = {
  335. &listColDefName,
  336. &listColDefValue,
  337. },
  338. *listKeyName[LIST_KEY_NCOLS_NAME] = {
  339. &listColDefName
  340. };
  341. /************************ SGDatabase class declaration ***********************/
  342. class SGDatabase {
  343. private:
  344. static DWORD numInstances;
  345. static JET_INSTANCE instance;
  346. static BOOL jetInitialized;
  347. static TCHAR *logDir;
  348. CHAR *fileName; // fully qualified database file name
  349. JET_SESID sesID;
  350. JET_DBID dbID;
  351. JET_TABLEID tableID,
  352. queueID,
  353. stackID,
  354. listID;
  355. JET_COLUMNID tableColumnIDs[TABLE_NCOLS],
  356. queueColumnIDs[QUEUE_NCOLS],
  357. stackColumnIDs[STACK_NCOLS],
  358. listColumnIDs[ LIST_NCOLS];
  359. LONG numTableEntries,
  360. numQueueEntries,
  361. numStackEntries,
  362. numListEntries,
  363. numUncommittedTableEntries,
  364. numUncommittedQueueEntries,
  365. numUncommittedStackEntries,
  366. numUncommittedListEntries;
  367. BOOL inTransaction;
  368. // Static methods
  369. static BOOL InitializeEngine();
  370. static BOOL TerminateEngine();
  371. // Table / queue / stack / list methods
  372. BOOL CreateTable(
  373. const CHAR *tblName,
  374. DWORD numColumns,
  375. ColumnSpec **columnSpecs,
  376. JET_COLUMNID *columnIDs,
  377. JET_TABLEID *tblID);
  378. BOOL CreateIndex(
  379. JET_TABLEID tblID,
  380. const CHAR *keyName,
  381. DWORD numKeys,
  382. ColumnSpec **keyColumnSpecs);
  383. BOOL OpenTable(
  384. const CHAR *tblName,
  385. DWORD numColumns,
  386. ColumnSpec **columnSpecs,
  387. JET_COLUMNID *columnIDs,
  388. JET_TABLEID *tblID);
  389. BOOL CloseTable(JET_TABLEID tblID);
  390. LONG PositionCursor(
  391. JET_TABLEID tblID,
  392. const CHAR *keyName,
  393. const VOID *entry,
  394. DWORD numKeys,
  395. ColumnSpec **keyColumnSpecs) const;
  396. LONG PositionCursorFirst(
  397. JET_TABLEID tblID,
  398. const CHAR *keyName) const;
  399. LONG PositionCursorNext(JET_TABLEID tblID) const;
  400. LONG PositionCursorLast(
  401. JET_TABLEID tblID,
  402. const CHAR *keyName) const;
  403. BOOL PutData(
  404. JET_TABLEID tblID,
  405. const VOID *entry,
  406. DWORD numColumns,
  407. ColumnSpec **columnSpecs,
  408. const JET_COLUMNID *columnIDs);
  409. BOOL SGDatabase::RetrieveData(
  410. JET_TABLEID tblID,
  411. VOID *entry,
  412. DWORD numColumns,
  413. ColumnSpec **columnSpecs,
  414. const JET_COLUMNID *columnIDs,
  415. DWORD includeMask) const;
  416. LONG Delete(JET_TABLEID tblID);
  417. LONG Count(
  418. JET_TABLEID tblID,
  419. const CHAR *keyName) const;
  420. public:
  421. // General methods
  422. SGDatabase();
  423. ~SGDatabase();
  424. BOOL Create(const TCHAR *dbName);
  425. BOOL Open(const TCHAR *dbName, BOOL is_log_drive);
  426. BOOL Close();
  427. BOOL BeginTransaction();
  428. BOOL CommitTransaction();
  429. BOOL AbortTransaction();
  430. static BOOL set_log_drive(const _TCHAR *drive_name);
  431. // Table methods
  432. LONG TablePut(const SGNativeTableEntry *entry);
  433. LONG TableGetFirstByFileID(SGNativeTableEntry *entry) const;
  434. LONG TableGetFirstByAttr(SGNativeTableEntry *entry) const;
  435. LONG TableGetFirstByCSIndex(SGNativeTableEntry *entry) const;
  436. LONG TableGetNext(SGNativeTableEntry *entry) const;
  437. LONG TableDeleteByFileID(DWORDLONG fileID);
  438. LONG TableDeleteByCSIndex(const CSID *csIndex);
  439. LONG TableCount() const;
  440. // Queue methods
  441. LONG QueuePut(SGNativeQueueEntry *entry);
  442. LONG QueueGetFirst(SGNativeQueueEntry *entry) const;
  443. LONG QueueGetFirstByFileID(SGNativeQueueEntry *entry) const;
  444. LONG QueueGetNext(SGNativeQueueEntry *entry) const;
  445. LONG QueueDelete(DWORD order);
  446. LONG QueueDeleteByFileID(DWORDLONG fileID);
  447. LONG QueueCount() const;
  448. // Stack methods
  449. LONG StackPut(DWORDLONG fileID, BOOL done);
  450. LONG StackGetTop(SGNativeStackEntry *entry) const;
  451. LONG StackGetFirstByFileID(SGNativeStackEntry *entry) const;
  452. LONG StackGetNext(SGNativeStackEntry *entry) const;
  453. LONG StackDelete(DWORD order);
  454. LONG StackDeleteByFileID(DWORDLONG fileID);
  455. LONG StackCount() const;
  456. // List methods
  457. LONG ListWrite(const SGNativeListEntry *entry);
  458. LONG ListRead(SGNativeListEntry *entry) const;
  459. LONG ListDelete(const TCHAR *name);
  460. LONG ListCount() const;
  461. };