FastDB测试
2013年10月19日
⁄ 综合
⁄ 共 3241字 ⁄ 字号
小 中 大
-
#include <stdio.h>
-
#include "fastdb.h"
-
-
USE_FASTDB_NAMESPACE
-
-
class Contract;
-
-
class Detail {
-
public:
-
char const* name;
-
char const* material;
-
char const* color;
-
real4 weight;
-
-
dbArray< dbReference<Contract> > contracts;
-
-
TYPE_DESCRIPTOR((KEY(name, INDEXED|HASHED),
-
KEY(material, HASHED),
-
KEY(color, HASHED),
-
KEY(weight, INDEXED),
-
RELATION(contracts, detail)));
-
};
-
-
class Supplier {
-
public:
-
char const* company;
-
char const* location;
-
bool foreign;
-
-
dbArray< dbReference<Contract> > contracts;
-
-
TYPE_DESCRIPTOR((KEY(company, INDEXED|HASHED),
-
KEY(location, HASHED),
-
FIELD(foreign),
-
RELATION(contracts, supplier)));
-
};
-
-
-
class Contract {
-
public:
-
dbDateTime delivery;
-
int4 quantity;
-
db_int8 price;
-
dbReference<Detail> detail;
-
dbReference<Supplier> supplier;
-
-
TYPE_DESCRIPTOR((KEY(delivery, HASHED|INDEXED),
-
KEY(quantity, INDEXED),
-
KEY(price, INDEXED),
-
RELATION(detail, contracts),
-
RELATION(supplier, contracts)));
-
};
-
-
-
REGISTER(Detail);
-
REGISTER(Supplier);
-
REGISTER(Contract);
-
-
void input(char const* prompt, char* buf, size_t buf_size)
-
{
-
char* p;
-
do {
-
printf(prompt);
-
*buf = '\0';
-
fgets(buf, buf_size, stdin);
-
p = buf + strlen(buf);
-
} while (p <= buf+1);
-
-
if (*(p-1) == '\n') {
-
*--p = '\0';
-
}
-
}
-
-
int main()
-
{
-
const int maxStrLen = 256;
-
-
dbDatabase db;
-
-
char buf[maxStrLen];
-
char name[maxStrLen];
-
char company[maxStrLen];
-
char material[maxStrLen];
-
char address[maxStrLen];
-
-
int d, m, y;
-
int i, n;
-
int choice;
-
int quantity;
-
db_int8 price;
-
-
dbDateTime from, till;
-
-
Contract contract;
-
Supplier supplier;
-
Detail detail;
-
-
dbQuery q1, q2, q3, q4, q6, q9, q10;
-
q1 = "exists i:(contracts[i].supplier.company=",company,")";
-
q2 = "name like",name;
-
q3 = "supplier.location =",address;
-
q4 = between("delivery", from, till),"and price >",price,
-
"order by",dbDateTime::ascent("delivery");
-
q6 = "price >=",price,"or quantity >=",quantity;
-
q9 = "company =",company;
-
q10 = "supplier.company =",company,"and detail.name like",name;
-
-
dbCursor<Detail> details;
-
dbCursor<Contract> contracts;
-
dbCursor<Supplier> suppliers;
-
dbCursor<Contract> updateContracts(dbCursorForUpdate);
-
-
if (db.open("testdb")) {
-
while (true) {
-
printf(
-
"\n\n MENU:\n\
-
1. Details shipped by supplier\n\
-
2. Suppliers of the detail\n\
-
3. Contracts from specified city\n\
-
4. Expensive details to be delivered in specified period\n\
-
5. Foreign suppliers\n\
-
6. Important contracts\n\
-
7. New supplier\n\
-
8. New detail\n\
-
9. New contract\n\
-
10. Cancel contract\n\
-
11. Update contract\n\
-
12. Exit\n\n");
-
input(">> ", buf, sizeof buf);