Extending the Windows CE 2.0 MFC Database Classes

by John C. Schettino, Jr.

Figure 2:



Results:

	Hpynxotoa	4685	Wednesday, February 07, 2046

	Iuquajtn	8153	Wednesday, February 02, 1994

	Izpbopxe	6255	Saturday, August 17, 2024

	Jdhusu	7065	Wednesday, December 13, 2023

	Jkswfpq	6956	Saturday, April 07, 2012

	Jxgxxn	4137	Thursday, September 23, 2004

	Jyuhxve	5032	Thursday, July 14, 1994

	Jzkooz	2402	Tuesday, November 17, 2043

	Keqvdo	8098	Tuesday, September 16, 2025

	Kzggvswxua	4594	Friday, December 12, 2003

	Lgtarizsq	7428	Friday, September 18, 2026

	Lnrrxhmh	7922	Tuesday, October 21, 2031

Stats

	matches: 12

	misses: 158

	Total: 170

Range search was used







Listing One

class CEQuery

{

    friend class CCeFindDBDatabaseIterater;

public:

    CEQuery();

    virtual ~CEQuery();



    void addField(CCeDBQueryProp *);

    void setSortField(CCeDBQueryProp *);

    int numFields(void);



    CCeDBQueryProp * testOn(WORD, WORD);

    bool match(CCeDBRecord*, bool &, int &);



    CCeDBQueryProp * sortField();

    CCeDBQueryProp * operator[](int fieldNo);



private:

    bool matchField(CCeDBProp*, int);



    CCeDBQueryProp ** _fields;

    CCeDBQueryProp * _sortField;



    int _numFields;

    int _maxFields;



    WORD _index;

    bool _range;

}





Listing Two

class  CCeDBQueryProp : public CCeDBProp

{

public:

    enum FieldOperation {

        Equal, LE, GE

    };

    void setOperation(FieldOperation);

    FieldOperation getOperation();

private:

    FieldOperation _operation;

}





Listing Three

class CCeFindDBDatabase  : public CCeDBDatabase

{

public:

    CCeFindDBDatabaseIterater * Find (CEQuery & );

    bool TestFind (CEQuery & );

private:

    CCeDBQueryProp * FindKey(CEQuery &);

    bool IsIndexed(CEPROPID, WORD *);

};



Listing Four

class CCeFindDBDatabaseIterater

{

public:

    CCeFindDBDatabaseIterater(CEQuery *, CCeDBQueryProp *, CCeDBDatabase *);

    ~CCeFindDBDatabaseIterater();

    CCeDBRecord * current();

    CCeDBRecord * first();      // resets search

    CCeDBRecord * next();

    void stats(int &, int &, bool &);

private:

    bool _start;

    bool _eof;

    int _hits;

    int _tests;



    CCeDBQueryProp * _primaryQuery;

    CCeDBDatabase * _myDb;

    CEQuery * _myQuery;

    WORD _keyOrder;

}



Listing Five

void CExampleDlg::OnInit() 

{

    CCeDBDatabase newDB;

    CCeDBProp sorts[2];



    sorts[0].SetType(CCeDBProp::Type_String);

    sorts[0].SetIdent(1);

    sorts[0].SetSortFlags(CCeDBProp::Sort_Ascending ); 

    sorts[1].SetType(CCeDBProp::Type_Long);

    sorts[1].SetIdent(2);

    sorts[1].SetSortFlags(CCeDBProp::Sort_Descending ); 



    if (newDB.Create(TEXT("JCSExample"), 123, 2, sorts) == NULL)

    {

        newDB.Open(TEXT("JCSExample")); 

        newDB.Delete();

        newDB.Create(TEXT("JCSExample"), 123, 2, sorts);

    }

    newDB.Open(TEXT("JCSExample")); 



    CCeDBRecord r; 

    r.AddProp(&sorts[0]);

    r.AddProp(&sorts[1]);

    r.AddProp(new CCeDBProp(CCeDBProp::Type_Filetime, 3));

    for (int i = 0; i < 1000; i++)

    {

        TCHAR string_field[256];

        FILETIME filetime_field;

        SYSTEMTIME systemTime;

        int j;

        // generate three random values for fields

        CCeDBProp* p = r.GetPropFromIdent(1);

        string_field[0] = (TCHAR)('A'+Random()%26);

        for (j=1; j < (int)(5 + (Random()%10)); j++)

            string_field[j] = (TCHAR)('a'+Random()%26);

        string_field[j] = 0;

        

        p->SetString(string_field);

        p = r.GetPropFromIdent(2);

        p->SetLong(Random());

        p = r.GetPropFromIdent(3);



        memset(&systemTime, 0, sizeof(systemTime));

        systemTime.wYear = (unsigned short) (1900 + Random()%200);

        systemTime.wMonth = (unsigned short) (1 + Random()%12);

        systemTime.wDay = (unsigned short) (1 + Random()%28);

        SystemTimeToFileTime(&systemTime, &filetime_field);

        p->SetFiletime(filetime_field);

        newDB.AddRecord(&r);

    }

}



Listing Six

CCeDBQueryProp::FieldOperation _ops[] = {

    CCeDBQueryProp::LE, CCeDBQueryProp::GE, CCeDBQueryProp::Equal};

CCeDBQueryProp * qp;

CEQuery q;

if (m_s11val != -1 && !m_sf11.IsEmpty())

{

    qp = new CCeDBQueryProp();

    qp->SetType(CCeDBProp::Type_String);

    qp->SetIdent(1);

    qp->SetString((TCHAR *)(LPCTSTR)m_sf11);

    qp->setOperation(_ops[m_s11val]);

    q.addField(qp);

}

// NOTE: other query properties omitted, see listing

CCeFindDBDatabase theDB;

theDB.Open(TEXT("JCSExample"));

CCeFindDBDatabaseIterater *results = theDB.Find(q);



TRACE0("\nResults:\n");

for (CCeDBRecord *r = results->first(); r; r = results->next())

{

        CCeDBProp* strfield = r->GetPropFromIdent(1);

        CCeDBProp* intfield = r->GetPropFromIdent(2);

        CCeDBProp* datefield = r->GetPropFromIdent(3);

        FILETIME f = (datefield->GetFiletime());

        SYSTEMTIME t;

        FileTimeToSystemTime(&f, &t);

        TCHAR timeStr[256];

        GetDateFormat(LOCALE_SYSTEM_DEFAULT,DATE_LONGDATE,&t,0,timeStr,256);

        TRACE3("\t%s\t%d\t%s\n", strfield->GetString(),

            intfield->GetLong(), timeStr);

        delete r;

}

int hits, tests;

bool range;

results->stats(hits, tests, range);

TRACE3("Stats\n\tmatches: %d\n\tmisses: %d\n\tTotal: %d\n", 

                                          hits, tests, hits+tests);

TRACE1("Range search %s used\n", range ? TEXT("was") : TEXT("was not"));



4



