Example #1
0
int CBDB_PhoneBookDemo3::Run(void)
{
    try
    {
        LoadPhoneBook();

        SPhoneBookDB dbf;
        dbf.Open(s_PhoneBookDBFileName, CBDB_File::eReadOnly);

        BuildZipIndex(dbf);

        SPhoneBookZipIDX idx;
        idx.Open(s_PhoneBookZipIDXFileName, CBDB_File::eReadOnly);

        SearchPhoneBook(dbf, idx, 10785);
    }
    catch (CBDB_ErrnoException& ex)
    {
        cout << "Error! DBD errno exception:" << ex.what();
        return 1;
    }
    catch (CBDB_LibException& ex)
    {
        cout << "Error! DBD library exception:" << ex.what();
        return 1;
    }

    return 0;
}
Example #2
0
//
// Search function uses Zip code index for fast data retrieval.
// Actual data seach is two phase process.
// First we seach the index values and collect the parent table primary key.
// Then we go and get the primary data table records.
//
void SearchPhoneBook(SPhoneBookDB& dbf, SPhoneBookZipIDX& idx, int zip_code)
{
    list<int>  person_id;

    // Index search. Here we store seach values in the list.
    {{
        CBDB_FileCursor cur(idx);
        cur.SetCondition(CBDB_FileCursor::eEQ);
        cur.From << zip_code;

        while (cur.Fetch() == eBDB_Ok) {
            person_id.push_back(idx.person_id.Get());
        }

    }}

    if (person_id.empty()) {
        cout << "No records found." << endl;
        return;
    }

    // Data retrieval using index seach values.
    for (list<int>::const_iterator it = person_id.begin();
         it != person_id.end(); ++it) {

        dbf.person_id = *it;
        EBDB_ErrCode ret = dbf.Fetch();
        if (ret != eBDB_Ok) {
            assert(0);
        }

        cout << "====================================" << endl;
        PrintRecord(dbf);
    }
}
Example #3
0
//
// Function scans all records in the phone book table.
// One of the features of the BDB library is that cursor is attached
// to the main open datafile instance and when we call fetch data become
// immediately available in the datafile structure fields.
// Cursor only defines search criteria.
//
void PrintPhoneBook()
{
    SPhoneBookDB  dbf;

    dbf.Open(s_PhoneBookDBFileName, CBDB_File::eReadOnly);

    CBDB_FileCursor cur(dbf);
    cur.SetCondition(CBDB_FileCursor::eFirst);


    unsigned int recs_fetched = 0;
    while (cur.Fetch() == eBDB_Ok) {
        cout << "====================================" << endl;
        PrintRecord(dbf);
        ++recs_fetched;
    }
    cout << "====================================" << endl;
    cout << "Records found:" << recs_fetched << endl;
}
Example #4
0
void LoadPhoneBook()
{
    SPhoneBookDB  dbf;

    dbf.Open(s_PhoneBookDBFileName, CBDB_File::eCreate);

    dbf.person_id = 1;
    dbf.first_name = "John";
    dbf.last_name = "Doe";
    dbf.zip_code = 10785;
    dbf.phone = "+1(240)123456";

    EBDB_ErrCode ret = dbf.Insert();
    // Check Insert result, it can be 'eBDB_KeyDup'
    // if primary key already exists
    if (ret != eBDB_Ok) {
        cout << "Insert failed!" << endl;
        exit(1);
    }

    dbf.person_id = 2;
    dbf.first_name = "Antony";
    dbf.last_name = "Smith";
    dbf.zip_code = 10785;
    dbf.phone = "(240)234567";

    ret = dbf.Insert();
    if (ret != eBDB_Ok) {
        cout << "Insert failed!" << endl;
        exit(1);
    }

    dbf.person_id = 3;
    dbf.first_name = "John";
    dbf.last_name = "Public";
    dbf.zip_code = 40000;

    ret = dbf.Insert();
    if (ret != eBDB_Ok) {
        cout << "Insert failed!" << endl;
        exit(1);
    }
}