FastDB测试

[cpp]  view plain copy
  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);  
  130.             if (sscanf(buf, "%d", &choice) != 1) {   
  131.                 continue;  
  132.             }  
  133.             switch (choice) {   
  134.               case 1:  
  135.                 printf("Details shipped by supplier\n");  
  136.                 input("Supplier company: ", company, sizeof company);  
  137.                 if (details.select(q1) > 0) {   
  138.                     printf("Detail\tMaterial\tColor\tWeight\n");  
  139.                     do {   
  140.                         printf("%s\t%s\t%s\t%f\n",   
  141.                                details->name, details->material,   
  142.                                details->color, details->weight);  
  143.                     } while (details.next());  
  144.                 } else {   
  145.                     printf("No details shipped by this supplier\n");  
  146.                 }  
  147.                 break;  
  148.               case 2:  
  149.                 printf("Suppliers of the detail\n");  
  150.                 input("Regular expression for detail name: ",name,sizeof name);  
  151.                 if (details.select(q2) > 0) {   
  152.                     printf("Detail\tCompany\tLocation\tPrice\n");  
  153.                     do {   
  154.                         n = details->contracts.length();  
  155.                         for (i = 0; i < n; i++) {   
  156.                             contracts.at(details->contracts[i]);  
  157.                             suppliers.at(contracts->supplier);  
  158.                             printf("%s\t%s\t%s\t" INT8_FORMAT "\n",   
  159.                                    details->name,   
  160.                                    suppliers->company, suppliers->location,  
  161.                                    contracts->price);  
  162.                         }  
  163.                     } while (details.next());  
  164.                 } else {   
  165.                     printf("No such details\n");  
  166.                 }  
  167.                 break;  
  168.               case 3:  
  169.                 printf("Contracts from specified city\n");  
  170.                 input("City: ", address, sizeof address);  
  171.                 if (contracts.select(q3) > 0) {   
  172.                     printf("Detail\tCompany\tQuantity\n");  
  173.                     do {   
  174.                         printf("%s\t%s\t%d\n",   
  175.                                details.at(contracts->detail)->name,   
  176.                                suppliers.at(contracts->supplier)->company,   
  177.                                contracts->quantity);  
  178.                     } while (contracts.next());  
  179.                 } else {   
  180.                     printf("No contracts with companies in this city");  
  181.                 }  
  182.                 break;  
  183.               case 4:  
  184.                 printf("Expensive details to be delivered in specified period\n");  
  185.                 input("Delivered after (DD-MM-YYYY): ", buf, sizeof buf);  
  186.                 if (sscanf(buf, "%d-%d-%d\n", &d, &m, &y) != 3) {   
  187.                     printf("Wrong date\n");  
  188.                     continue;  
  189.                 }  
  190.                 from = dbDateTime(y, m, d);  
  191.                 input("Delivered before (DD-MM-YYYY): ", buf, sizeof buf);  
  192.                 if (sscanf(buf, "%d-%d-%d\n", &d, &m, &y) != 3) {   
  193.                     printf("Wrong date\n");  
  194.                     continue;  
  195.                 }  
  196.                 till = dbDateTime(y, m, d);  
  197.                 input("Minimal contract price: ", buf, sizeof buf);  
  198.                 if (sscanf(buf, INT8_FORMAT, &price) != 1) {   
  199.                     printf("Bad value\n");  
  200.                     continue;  
  201.                 }  
  202.                 if (contracts.select(q4) > 0) {   
  203.                     printf("Detail\tDate\tPrice\n");  
  204.                     do {   
  205.                         printf("%s\t%s\t" INT8_FORMAT "\n",   
  206.                                details.at(contracts->detail)->name,   
  207.                                contracts->delivery.asString(buf, sizeof buf),  
  208.                                contracts->price);  
  209.                     } while (contracts.next());  
  210.                 } else {   
  211.                     printf("No such contracts\n");  
  212.                 }  
  213.                 break;  
  214.               case 5:  
  215.                 printf("Foreign suppliers\n");  
  216.                 if (suppliers.select("foreign and length(contracts) > 0") > 0){  
  217.                     printf("Company\tLocation\n");  
  218.                     do {   
  219.                         printf("%s\t%s\n", suppliers->company,   
  220.                                suppliers->location);  
  221.                     } while (suppliers.next());  
  222.                 } else {   
  223.                     printf("No such suppliers\n");  
  224.                 }  
  225.                 break;  
  226.               case 6:  
  227.                 printf("Important contracts\n");  
  228.                 input("Minimal contract price: ", buf, sizeof buf);  
  229.                 if (sscanf(buf, INT8_FORMAT, &price) != 1) {   
  230.                     printf("Bad value\n");  
  231.                     continue;  
  232.                 }  
  233.                 input("Minimal contract quantity: ", buf, sizeof buf);  
  234.                 if (sscanf(buf, "%d", &quantity) != 1) {   
  235.                     printf("Bad value\n");  
  236.                     continue;  
  237.                 }  
  238.                 if (contracts.select(q6) > 0) {   
  239.                     printf("Company\tPrice\tQuantity\tDelivery\n");  
  240.                     do {   
  241.                         printf("%s\t" INT8_FORMAT "\t%d\t%s\n",   
  242.                                suppliers.at(contracts->supplier)->company,  
  243.                                contracts->price, contracts->quantity,  
  244.                                contracts->delivery.asString(buf, sizeof buf,   
  245.                                                             "%A %x"));  
  246.                     } while (contracts.next());  
  247.                 } else {   
  248.                     printf("No such contracts\n");  
  249.                 }  
  250.                 break;  
  251.               case 7:  
  252.                 printf("New supplier\n");  
  253.                 input("Company name: ", company, sizeof company);  
  254.                 input("Company location: ", address, sizeof address);  
  255.                 input("Foreign company (y/n): ", buf, sizeof buf);  
  256.                 supplier.company = company;  
  257.                 supplier.location = address;  
  258.                 supplier.foreign = (*buf == 'y');  
  259.                 insert(supplier);  
  260.                 break;  
  261.               case 8:  
  262.                 printf("New detail\n");  
  263.                 input("Detail name: ", name, sizeof name);  
  264.                 input("Detail material: ", material, sizeof material);  
  265.                 input("Detail weight: ", buf, sizeof buf);  
  266.                 sscanf(buf, "%f", &detail.weight);  
  267.                 input("Detail color: ", buf, sizeof buf);  
  268.                 detail.name = name;  
  269.                 detail.material = material;  
  270.                 detail.color = buf;  
  271.                 insert(detail);  
  272.                 break;  
  273.               case 9:  
  274.                 printf("New contract\n");  
  275.                 db.lock(); // prevent deadlock  
  276.                 input("Supplier company: ", company, sizeof company);  
  277.                 if (suppliers.select(q9) == 0) {   
  278.                     printf("No such supplier\n");  
  279.                     continue;  
  280.                 }  
  281.                 input("Detail name: ", name, sizeof name);  
  282.                 if (details.select(q2) == 0) {   
  283.                     printf("No such detail\n");  
  284.                     continue;  
  285.                 } else if (details.getNumberOfRecords() != 1) {   
  286.                     printf("More than one record match this pattern");  
  287.                     continue;  
  288.                 }  
  289.                 input("Contract price: ", buf, sizeof buf);  
  290.                 sscanf(buf, INT8_FORMAT, &contract.price);  
  291.                 input("Contract quantity: ", buf, sizeof buf);  
  292.                 sscanf(buf, "%d", &contract.quantity);  
  293.                 input("Delivered after (days): ", buf, sizeof buf);  
  294.                 sscanf(buf, "%d", &d);  
  295.                 contract.delivery =   
  296.                     dbDateTime::currentDate() + dbDateTime(24*d,0);  
  297.                 contract.supplier = suppliers.currentId();  
  298.                 contract.detail = details.currentId();  
  299.                 insert(contract);  
  300.                 break;  
  301.               case 10:   
  302.                 printf("Cancel contract\n");  
  303.                 input("Supplier company: ", company, sizeof company);  
  304.                 input("Detail name pattern: ", name, sizeof name);  
  305.                 if (updateContracts.select(q10) == 0) {   
  306.                     printf("No such contracts\n");  
  307.                 } else {   
  308.                     updateContracts.removeAllSelected();  
  309.                     // Just test rollback  
  310.                     input("Really cancel contract (y/n) ? ", buf, sizeof buf);  
  311.                     if (*buf != 'y') {   
  312.                         printf("Not confirmed\n");  
  313.                         db.rollback();  
  314.                         continue;  
  315.                     }  
  316.                 }  
  317.                 break;  
  318.               case 11:  
  319.                 printf("Update contract\n");  
  320.                 input("Supplier company: ", company, sizeof company);  
  321.                 input("Detail name pattern: ", name, sizeof name);  
  322.                 if (updateContracts.select(q10) == 0) {   
  323.                     printf("No such contracts\n");  
  324.                     break;  
  325.                 }                 
  326.                 do {   
  327.                     printf("Contract with company %s for shipping %d details "  
  328.                            "%s for {1}quot; INT8_FORMAT " at %s\n",  
  329.                            suppliers.at(updateContracts->supplier)->company,   
  330.                            updateContracts->quantity,   
  331.                            details.at(updateContracts->detail)->name,  
  332.                            updateContracts->price,   
  333.                            updateContracts->delivery.asString(buf,sizeof buf));  
  334.                     input("Change this contract (y/n) ? ", buf, sizeof buf);  
  335.                     if (*buf == 'y') {   
  336.                         input("New contract price: ", buf, sizeof buf);  
  337.                         sscanf(buf, INT8_FORMAT, &updateContracts->price);  
  338.                         input("New number of details: ", buf, sizeof buf);  
  339.                         sscanf(buf, "%d", &updateContracts->quantity);  
  340.                         updateContracts.update();  
  341.                     }  
  342.                 } while (updateContracts.next());  
  343.                 break;  
  344.               case 12:  
  345.                 input("Do you really want to exit (y/n) ? ", buf, sizeof buf);  
  346.                 if (*buf == 'y') {   
  347.                     printf("Close database session\n");  
  348.                     db.close();  
  349.                     return EXIT_SUCCESS;  
  350.                 }  
  351.                 break;  
  352.               default:  
  353.                 printf("Please choose menu items 1..12\n");  
  354.                 continue;  
  355.             }  
  356.             printf("Press any key to continue...\n");  
  357.             getchar();  
  358.             db.commit();  
  359.         }  
  360.     } else {   
  361.         printf("failed to open database\n");  
  362.         return EXIT_FAILURE;  
  363.     }  
  364. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值