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;
    }
}

 

原创粉丝点击