现在的位置: 首页 > 综合 > 正文

FastDB测试

2013年10月19日 ⁄ 综合 ⁄ 共 3241字 ⁄ 字号 评论关闭
  1. #include <stdio.h>  
  2. #include "fastdb.h"  
  3.   
  4. USE_FASTDB_NAMESPACE  
  5.   
  6. class Contract;  
  7.   
  8. class Detail {   
  9.   public:  
  10.     char const* name;  
  11.     char const* material;  
  12.     char const* color;  
  13.     real4       weight;  
  14.   
  15.     dbArray< dbReference<Contract> > contracts;  
  16.   
  17.     TYPE_DESCRIPTOR((KEY(name, INDEXED|HASHED),   
  18.                      KEY(material, HASHED),   
  19.                      KEY(color, HASHED),  
  20.                      KEY(weight, INDEXED),  
  21.                      RELATION(contracts, detail)));  
  22. };  
  23.   
  24. class Supplier {   
  25.   public:  
  26.     char const* company;  
  27.     char const* location;  
  28.     bool        foreign;  
  29.   
  30.     dbArray< dbReference<Contract> > contracts;  
  31.   
  32.     TYPE_DESCRIPTOR((KEY(company, INDEXED|HASHED),   
  33.                      KEY(location, HASHED),   
  34.                      FIELD(foreign),  
  35.                      RELATION(contracts, supplier)));  
  36. };  
  37.   
  38.   
  39. class Contract {   
  40.   public:  
  41.     dbDateTime            delivery;  
  42.     int4                  quantity;  
  43.     db_int8                  price;  
  44.     dbReference<Detail>   detail;  
  45.     dbReference<Supplier> supplier;  
  46.   
  47.     TYPE_DESCRIPTOR((KEY(delivery, HASHED|INDEXED),   
  48.                      KEY(quantity, INDEXED),   
  49.                      KEY(price, INDEXED),  
  50.                      RELATION(detail, contracts),  
  51.                      RELATION(supplier, contracts)));  
  52. };  
  53.   
  54.   
  55. REGISTER(Detail);  
  56. REGISTER(Supplier);  
  57. REGISTER(Contract);  
  58.   
  59. void input(char const* prompt, char* buf, size_t buf_size)  
  60. {  
  61.     char* p;  
  62.     do {   
  63.         printf(prompt);  
  64.         *buf = '\0';  
  65.         fgets(buf, buf_size, stdin);  
  66.         p = buf + strlen(buf);  
  67.     } while (p <= buf+1);   
  68.       
  69.     if (*(p-1) == '\n') {  
  70.         *--p = '\0';  
  71.     }  
  72. }  
  73.   
  74. int main()   
  75. {  
  76.     const int maxStrLen = 256;  
  77.   
  78.     dbDatabase db;  
  79.   
  80.     char buf[maxStrLen];  
  81.     char name[maxStrLen];  
  82.     char company[maxStrLen];  
  83.     char material[maxStrLen];  
  84.     char address[maxStrLen];  
  85.   
  86.     int d, m, y;  
  87.     int i, n;  
  88.     int choice;  
  89.     int quantity;  
  90.     db_int8 price;  
  91.   
  92.     dbDateTime from, till;  
  93.   
  94.     Contract contract;  
  95.     Supplier supplier;  
  96.     Detail detail;  
  97.   
  98.     dbQuery q1, q2, q3, q4, q6, q9, q10;  
  99.     q1 = "exists i:(contracts[i].supplier.company=",company,")";  
  100.     q2 = "name like",name;  
  101.     q3 = "supplier.location =",address;  
  102.     q4 = between("delivery", from, till),"and price >",price,  
  103.         "order by",dbDateTime::ascent("delivery");  
  104.     q6 = "price >=",price,"or quantity >=",quantity;  
  105.     q9 = "company =",company;  
  106.     q10 = "supplier.company =",company,"and detail.name like",name;   
  107.   
  108.     dbCursor<Detail>   details;  
  109.     dbCursor<Contract> contracts;  
  110.     dbCursor<Supplier> suppliers;  
  111.     dbCursor<Contract> updateContracts(dbCursorForUpdate);  
  112.           
  113.     if (db.open("testdb")) {  
  114.         while (true) {   
  115.             printf(  
  116. "\n\n    MENU:\n\  
  117. 1.  Details shipped by supplier\n\  
  118. 2.  Suppliers of the detail\n\  
  119. 3.  Contracts from specified city\n\  
  120. 4.  Expensive details to be delivered in specified period\n\  
  121. 5.  Foreign suppliers\n\  
  122. 6.  Important contracts\n\  
  123. 7.  New supplier\n\  
  124. 8.  New detail\n\  
  125. 9.  New contract\n\  
  126. 10. Cancel contract\n\  
  127. 11. Update contract\n\  
  128. 12. Exit\n\n");  
  129.             input(">> ", buf, sizeof buf); 

抱歉!评论已关闭.