fastdb一个表支持注册到多个库
来源:互联网 发布:阿里云 包括域名 编辑:程序博客网 时间:2024/06/06 07:51
dbDatabase database[2];
dbDatabase* db;
class Guess {
public:
dbReference<Guess> yes;
dbReference<Guess> no;
char const* question;
TYPE_DESCRIPTOR((FIELD(yes), FIELD(question), FIELD(no)));
};
REGISTER_UNASSIGNED(Guess);
void input(char const* prompt, char* buf, size_t buf_size)
{
char* p;
do {
printf(prompt);
*buf = '\0';
fgets(buf, (int)buf_size, stdin);
p = buf + strlen(buf);
} while (p <= buf+1);
if (*(p-1) == '\n') {
*--p = '\0';
}
}
bool askQuestion(char const* question) {
char answer[maxStrLen];
input(question, answer, sizeof answer);
return *answer == 'y' || *answer == 'Y';
}
dbReference<Guess> whoIsIt(dbReference<Guess> const& parent) {
char animal[maxStrLen];
char difference[maxStrLen];
input("What is it ? ", animal, sizeof animal);
input("What is a difference from other ? ", difference, sizeof difference);
Guess node;
node.question = animal;
dbReference<Guess> child = db->insert(node);
node.question = difference;
node.yes = child;
node.no = parent;
return db->insert(node);
}
dbReference<Guess> dialog(dbCursor<Guess>& cur) {
char question[maxStrLen+16];
dbCursor<Guess> c(db, dbCursorForUpdate);
sprintf(question, "May be %s (y/n) ? ", cur->question);
if (askQuestion(question)) {
if (cur->yes == null) {
printf("It was very simple question for me...\n");
} else {
c.at(cur->yes);
dbReference<Guess> clarify = dialog(c);
if (clarify != null) {
cur->yes = clarify;
cur.update();
}
}
} else {
if (cur->no == null) {
if (cur->yes == null) {
return whoIsIt(cur.currentId());
} else {
cur->no = whoIsIt(null);
cur.update();
}
} else {
c.at(cur->no);
dbReference<Guess> clarify = dialog(c);
if (clarify != null) {
cur->no = clarify;
cur.update();
}
}
}
return null;
}
int main()
{
if (database[0].open(_T("guess1")) && database[1].open(_T("guess2"))) {
int i = 0;
while (askQuestion("Think of an animal. Ready (y/n) ? ")) {
db = &database[i ^= 1];
dbCursor<Guess> cur(db, dbCursorForUpdate);
if (cur.select() != 0) {
cur.next(); // first question is in record number 2
dialog(cur);
} else {
whoIsIt(null);
}
db->commit();
}
database[0].close();
database[1].close();
printf("End of the game\n");
return EXIT_SUCCESS;
} else {
fprintf(stderr, "Failed to open database\n");
return EXIT_FAILURE;
}
}
- fastdb一个表支持注册到多个库
- fastdb 容错支持
- FastDB的表对应于C++类
- fastdb随感
- Fastdb测试
- FastDB - Documents
- fastdb简介
- FastDB应用
- fastDB CLI
- FastDB.html
- FastDB测试
- FastDB分析
- fastdb使用
- FastDB杂谈
- fastdb分析
- IOS App注册支持文件类型
- 让phpcms支持手机注册
- dubbo支持的注册中心
- android 平滑切换屏幕的几种方法总结
- android 自定义 radiobutton 文字颜色随选中状态而改变
- 开放式有关软件自动化测试和问题
- 对List进行排序的方法
- 隐马尔科夫模型HMM自学(1)
- fastdb一个表支持注册到多个库
- 数字图像处理的研究方法
- MACRO do-while(0) 的几个用法
- Cannot use typeid with -fno-rtti错误的解决
- 那些年,我还在学习Ajax 学习笔记
- Android 自定义RadioButton的样式
- php-Arrays 函数-array_merge_recursive-递归地合并一个或多个数组
- ognl
- ffmpeg命令行选项