home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
Software
/
TemaCD
/
tcvpa
/
data1.cab
/
MyFileGroup
/
INCLUDE
/
Query.hpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-03
|
21KB
|
615 lines
#ifndef _INC_QUERY_HPP
#define _INC_QUERY_HPP
class TC_CPsPredTerm;
class TC_CPsMember;
class TC_CPsSort;
class TC_CPsPredicate;
class TC_CPsQuery;
class TC_CPsClassedMember;
enum TCPsQryOperType {
op_EQ,
op_NE,
op_LT,
op_LE,
op_GT,
op_GE,
op_MATCHES,
op_NOT_MATCHES,
op_NULL,
op_NOT_NULL,
op_IN,
op_NOT_IN,
};
enum TCPsQryConjType {
conj_AND,
conj_OR,
conj_NOT,
conj_None,
};
enum TCPsQryOrderType {
order_Asc,
order_Desc,
};
enum TCPsQryAgr {
agr_None,
agr_Count,
agr_Max,
agr_Min,
agr_Avg,
agr_Sum,
};
typedef TC_TArrayCPP <TC_CPsClassedMember> TCPsClassedMembers;
typedef TC_TArrayCPP <TC_CPsAttribInfo> TCPsAttribInfos;
typedef TC_TArrayCPP <TC_CPsPredTerm> TCPsPredTerms;
typedef TC_TArrayCPP <TC_CPsPredicate> TCPsPredicates;
extern TC_PS_EXPORT TC_CPsPredicate tcPsNullPredicate ;
extern TC_PS_EXPORT TC_CPsSort tcPsNullSort ;
TC_PS_EXPORT int tcPsAtomicResult ( long * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( unsigned long * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( short * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( unsigned short * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( char * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( unsigned char * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( float * p) ;
TC_PS_EXPORT int tcPsAtomicResult ( double * p) ;
TC_PS_EXPORT int tcPsAtomicResult (TC_CDate * p) ;
TC_PS_EXPORT int tcPsAtomicResult (TC_CTime * p) ;
TC_PS_EXPORT int tcPsAtomicResult (TC_CString * p) ;
TC_PS_EXPORT int tcPsAtomicResult (TC_CBCD * p) ;
TC_PS_EXPORT int tcPsAtomicResult (TC_CBinArray * p) ;
// **********************************************************************
class TC_PS_EXPORT TC_CPsAtomicResult
{
private: int m_Type ;
private: long m_Size ;
protected: void * m_Value ;
public: TC_CPsAtomicResult (int type, void * val, long size) ;
public: void SetValue (void * val, int type = 0, long size=0) ;
public: void * GetValue ()
{
return m_Value;
} // end of func
// **********************************************************************
public: int GetType ()
{
return m_Type;
} // end of func
// **********************************************************************
public: long GetSize ()
{
return m_Size;
} // end of func
// **********************************************************************
}; // end of class TC_CPsAtomicResult
// **********************************************************************
// **********************************************************************
template <class T>
class TC_TPsAtomicResult
: public TC_CPsAtomicResult
{
public: TC_TPsAtomicResult (T & value, int size=0)
: TC_CPsAtomicResult(tcPsAtomicResult((T*)-1), &value, size)
{
} // end of func
// **********************************************************************
public: operator T ()
{
return *((T*)GetValue());
} // end of func
// **********************************************************************
public: T& operator = (T & value)
{
SetValue(&value);
return value;
} // end of func
// **********************************************************************
}; // end of class TC_TPsAtomicResult
// **********************************************************************
typedef TC_TPsAtomicResult <long> TCPsARLong;
typedef TC_TPsAtomicResult <short> TCPsARShort;
typedef TC_TPsAtomicResult <unsigned long> TCPsARULong;
typedef TC_TPsAtomicResult <unsigned short> TCPsARUShort;
typedef TC_TPsAtomicResult <char> TCPsARChar;
typedef TC_TPsAtomicResult <unsigned char> TCPsARByte;
typedef TC_TPsAtomicResult <TC_CDate> TCPsARDate;
typedef TC_TPsAtomicResult <TC_CTime> TCPsARTime;
typedef TC_TPsAtomicResult <float> TCPsARFloat;
typedef TC_TPsAtomicResult <double> TCPsARDouble;
typedef TC_TPsAtomicResult <TC_CBCD> TCPsARBCD;
// **********************************************************************
class TCPsARString
: public TC_TPsAtomicResult <TC_CString>
{
public: TCPsARString (TC_CString & value, int size=0)
: TC_TPsAtomicResult <TC_CString> (value, size)
{
} // end of func
// **********************************************************************
public: operator const char * ()
{
return (const char*)GetValue();
} // end of func
// **********************************************************************
public: TC_CString& operator = (const char * value)
{
SetValue((void*)value, TC_PS_AT_STRING);
return *(TC_CString*)GetValue();
} // end of func
// **********************************************************************
}; // end of class TCPsARString
// **********************************************************************
// **********************************************************************
class TCPsARBinary
: public TC_TPsAtomicResult <TC_CBinArray>
{
public: TCPsARBinary (TC_CBinArray & value, int size=0)
: TC_TPsAtomicResult <TC_CBinArray> (value, size)
{
} // end of func
// **********************************************************************
public: unsigned char * Array () ;
public: ULONG Size () ;
}; // end of class TCPsARBinary
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsPredTerm
{
friend class TC_CPsPredicate;
friend class TC_CPsQuery;
friend class TC_TArrayCPP <TC_CPsPredTerm>;
private: TC_CString m_AttribName ;
private: TCPsQryOperType m_Oper ;
private: TC_CPsBuffer m_Buffer ;
private: int m_JoinIdx ;
private: TC_CPsQuery * m_SubQuery ;
private: TC_CPsPredTerm () ;
public: TC_CPsPredTerm (TC_CPsMember * member, TCPsQryOperType oper, const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm (TC_CPsMember & member, TCPsQryOperType oper, const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm (TC_CPsMember * member, TCPsQryOperType oper) ;
public: TC_CPsPredTerm (TC_CPsMember & member, TCPsQryOperType oper) ;
public: TC_CPsPredTerm (TC_CPsMember * member, TCPsQryOperType oper, TC_CPsQuery * subquery) ;
public: TC_CPsPredTerm (TC_CPsMember & member, TCPsQryOperType oper, TC_CPsQuery * subquery) ;
public: TC_CPsPredTerm (TC_CPsPredTerm & term) ;
public: TC_CPsPredTerm & operator = (const TC_CPsPredTerm & term) ;
public: TC_CPsPredicate operator && (TC_CPsPredTerm & term) ;
public: TC_CPsPredicate operator && (TC_CPsPredicate & pred) ;
public: TC_CPsPredicate operator || (TC_CPsPredTerm & term) ;
public: TC_CPsPredicate operator || (TC_CPsPredicate & pred) ;
public: TC_CPsPredicate operator ! () ;
public: const char * GetAttribName () ;
public: ~TC_CPsPredTerm () ;
public: int GetJoinIdx ()
{
return m_JoinIdx;
} // end of func
// **********************************************************************
public: TCPsQryOperType GetOper ()
{
return m_Oper;
} // end of func
// **********************************************************************
public: TC_CPsBuffer & GetBuffer () ;
public: TC_CPsQuery * GetSubQuery ()
{
return m_SubQuery;
} // end of func
// **********************************************************************
public: BOOL IsSubQuery ()
{
return m_SubQuery != 0;
} // end of func
// **********************************************************************
}; // end of class TC_CPsPredTerm
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsMember
{
friend class TC_CPsPredTerm;
friend class TC_CPsSort;
private: TC_CString m_Name ;
public: TC_CPsMember (const char * name) ;
public: TC_CPsPredTerm operator != (const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm operator < (const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm operator <= (const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm operator == (const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm operator > (const TC_CPsBuffer & psbuf) ;
public: TC_CPsPredTerm operator >= (const TC_CPsBuffer & psbuf) ;
public: const char * GetName () ;
}; // end of class TC_CPsMember
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsSort
{
friend class TC_CPsQuery;
friend class DSDQueryResSet;
#define SortCriteria TC_CPsSort::Criteria
// **********************************************************************
public:
class TC_PS_EXPORT Criteria
{
public: TC_CString m_AttribName ;
public: TCPsQryOrderType m_Type ;
public: int m_JoinIdx ;
public: Criteria () ;
public: const char * GetAttribName () ;
}; // end of class Criteria
// **********************************************************************
private: TC_TArrayCPP <Criteria> m_Criteries ;
public: TC_CPsSort () ;
public: TC_CPsSort (TC_CPsMember * member, TCPsQryOrderType orderType = order_Asc) ;
public: TC_CPsSort (TC_CPsMember & member, TCPsQryOrderType orderType = order_Asc) ;
public: TC_CPsSort & operator = (TC_CPsSort & sort) ;
public: void Add (TC_CPsMember * member, TCPsQryOrderType orderType = order_Asc) ;
public: void Add (TC_CPsMember & member, TCPsQryOrderType orderType = order_Asc) ;
public: void Add (TC_CPsSort & sort) ;
public: TC_CPsSort & operator += (TC_CPsMember & member) ;
public: TC_CPsSort & operator += (TC_CPsSort & sort) ;
public: BOOL IsNull ()
{
return m_Criteries.Count()==0;
} // end of func
// **********************************************************************
public: int Count ()
{
return m_Criteries.Count();
} // end of func
// **********************************************************************
public: const char * GetCriteriaAttribName (int idx)
{
if(idx < 0 || idx >=Count())
return "";
return m_Criteries[idx].GetAttribName();
} // end of func
// **********************************************************************
private: void _Add (const char * attrib_name, TCPsQryOrderType orderType) ;
}; // end of class TC_CPsSort
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsPredicate
{
friend class TC_CPsPredTerm;
friend class TC_CPsQuery;
friend class DSDQueryResSet;
private: TCPsQryConjType m_Conj ;
private: TCPsPredTerms m_PredTerms ;
private: TCPsPredicates m_PredBlocks ;
public: TC_CPsPredicate () ;
public: TC_CPsPredicate (TC_CPsPredTerm & term) ;
public: TC_CPsPredicate (TC_CPsPredicate & pred) ;
public: void Set (TC_CPsPredicate & pred) ;
public: void AndTerm (TC_CPsPredTerm & term) ;
public: void AndPred (TC_CPsPredicate & pred) ;
public: void OrTerm (TC_CPsPredTerm & term) ;
public: void OrPred (TC_CPsPredicate & pred) ;
public: void Not () ;
public: TC_CPsPredicate & operator = (TC_CPsPredicate & pred) ;
public: TC_CPsPredicate operator && (TC_CPsPredTerm & term) ;
public: TC_CPsPredicate operator && (TC_CPsPredicate & pred) ;
public: TC_CPsPredicate operator || (TC_CPsPredTerm & term) ;
public: TC_CPsPredicate operator || (TC_CPsPredicate & pred) ;
public: TC_CPsPredicate operator ! () ;
public: int TermCount () ;
public: int PredCount () ;
public: BOOL IsNull () ;
public: TCPsQryConjType GetConj ()
{
return m_Conj;
} // end of func
// **********************************************************************
private: BOOL NeedChangeConj () ;
private: void DoCopy (TC_CPsPredicate * dest, TC_CPsPredicate * src, int start_block=0) ;
private: void PropagateThis () ;
private: int m_Level ;
public: void Trace () ;
private: void DoTrace (TC_CPsPredicate * pred) ;
private: TC_CString StrConj (TCPsQryConjType conj) ;
}; // end of class TC_CPsPredicate
// **********************************************************************
#define ClJoin TC_CPsClassJoins::Join
// **********************************************************************
class TC_PS_EXPORT TC_CPsClassJoins
{
friend class TC_CPsQuery;
friend class TC_CPsClassedMember;
// **********************************************************************
public:
class TC_PS_EXPORT Join
{
public: Join * m_Anc ;
public: TC_TArrayCPP <Join> m_Descs ;
public: TCPsHClass m_hClass ;
public: TCPsHAttrib m_hAttribOfRel ;
public: int m_NN ;
public: BOOL m_IsJoinBasis ;
public: Join () ;
public: ~Join () ;
public: int DescCount ()
{
return m_Descs.Count();
} // end of func
// **********************************************************************
}; // end of class Join
// **********************************************************************
private: ClJoin m_Root ;
private: int m_Count ;
private: BOOL m_HasM2O ;
private: BOOL m_HasMemberJoins ;
public: int GetInheritCount () ;
public: TC_CPsClassJoins () ;
public: ClJoin * GetRoot () ;
public: void Clear () ;
public: BOOL HasM2O ()
{
return m_HasM2O;
} // end of func
// **********************************************************************
public: BOOL HasMemberJoins ()
{
return m_HasMemberJoins;
} // end of func
// **********************************************************************
public: int m_Level ;
public: void Trace ( ClJoin * parent) ;
}; // end of class TC_CPsClassJoins
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsClassedMember
{
friend class TC_CPsQuery;
private:
enum {
tok_Undef,
tok_Str,
tok_Dot,
tok_End,
};
private: TC_CString m_QualifiedName ;
private: TCPsAttribInfos m_AttribPath ;
private: TCPsHClass m_hDestClass ;
private: ClJoin * m_Join ;
public: TC_CPsClassedMember () ;
private: int NextToken (TC_CString &dest, char * &ptr) ;
private: TCPsRetCode Parse () ;
private: int AttribsInPath () ;
public: TC_CPsAttribInfo LastAttrib () ;
public: ClJoin * GetClassJoin ()
{
return m_Join;
} // end of func
// **********************************************************************
public: const char * GetQualifiedName () ;
}; // end of class TC_CPsClassedMember
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsQueryDest
{
friend class TC_CPsQuery;
private: TCPsHClass m_hClass ;
private: TCPsHAttrib m_hAttrib ;
private: TCPsQryAgr m_Agregate ;
private: TC_CPsQueryDest () ;
public: TC_CPsQueryDest (TCPsHClass hClass, TCPsQryAgr agr = agr_None) ;
public: TC_CPsQueryDest (TCPsHClass hClass, char * attr_name, TCPsQryAgr agr = agr_None) ;
public: BOOL IsNonObj ()
{
if(m_Agregate == agr_Count)
return TRUE;
if(m_hAttrib)
return TRUE;
return FALSE;
} // end of func
// **********************************************************************
public: BOOL IsValid ()
{
return m_hClass != 0;
} // end of func
// **********************************************************************
public: TCPsHClass HClass ()
{
return m_hClass;
} // end of func
// **********************************************************************
public: TC_CPsAttribInfo AttribInfo ()
{
return TC_CPsAttribInfo(m_hClass, m_hAttrib);
} // end of func
// **********************************************************************
public: TCPsQryAgr GetAgr ()
{
return m_Agregate;
} // end of func
// **********************************************************************
}; // end of class TC_CPsQueryDest
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsQuery
{
friend class DSDQueryResSet;
private: TC_CPsPredicate m_Pred ;
private: TC_CPsSort m_Sort ;
private: TCPsClassedMembers m_Members ;
private: TC_CPsClassJoins m_Joins ;
private: TC_CPsQueryDest m_QueryDest ;
private: void Reset () ;
private: void DefineJoin (TC_CPsClassedMember * cl_member) ;
private: TCPsRetCode AddSortJoins () ;
private: TCPsRetCode DoAddPredJoins (TC_CPsPredicate * pred) ;
private: TCPsRetCode ParseSubQuery (TC_CPsQuery * subquery) ;
private: int SearchMember (const char * name) ;
private: TCPsRetCode CheckTypeMatch (TC_CPsAttribInfo & atInfo, TC_CPsPredTerm & term) ;
public: TC_CPsQuery () ;
public: TC_CPsQuery (TC_CPsQueryDest qry_dest,TC_CPsPredicate & pred = tcPsNullPredicate, TC_CPsSort & sort = tcPsNullSort) ;
public: ~TC_CPsQuery () ;
public: TC_CPsQuery & operator = (TC_CPsQuery &qry) ;
public: void SetQuery (TC_CPsPredicate &pred=tcPsNullPredicate, TC_CPsSort &sort=tcPsNullSort) ;
public: void SetQueryDest (TC_CPsQueryDest qry_dest) ;
public: void QueryDest (TC_CPsQueryDest &qry_dest) ;
public: void Predicate (TC_CPsPredicate &pred) ;
public: void Sort (TC_CPsSort &sort) ;
public: TCPsRetCode Parse () ;
public: TCPsHClass HClass ()
{
return m_QueryDest.HClass();
} // end of func
// **********************************************************************
public: BOOL HasPredicate ()
{
return !m_Pred.IsNull();
} // end of func
// **********************************************************************
public: BOOL HasSort ()
{
return !m_Sort.IsNull();
} // end of func
// **********************************************************************
public: BOOL HasMemberJoins () ;
public: BOOL IsDestNonObj ()
{
return m_QueryDest.IsNonObj();
} // end of func
// **********************************************************************
public: int InheritedCount ()
{
return m_Joins.GetInheritCount();
} // end of func
// **********************************************************************
public: void TraceJoins () ;
}; // end of class TC_CPsQuery
// **********************************************************************
// **********************************************************************
class TC_PS_EXPORT TC_CPsResSet
{
friend TC_PS_EXPORT TCPsRetCode tcPsExecute(TC_CPsQuery&, TC_CPsResSet&);
private: TC_CPsQuery * m_Query ;
private: BOOL m_Executed ;
public: TC_CPsResSet () ;
public: TC_CPsObject * Fetch () ;
public: BOOL Next (TC_CPsAtomicResult & result) ;
}; // end of class TC_CPsResSet
// **********************************************************************
// **********************************************************************
template <class T>
class TC_TPsCursor
: public TC_CPsResSet
{
public: BOOL Next (T* & obj)
{
obj= (T *)Fetch();
return obj != 0;
} // end of func
// **********************************************************************
public: BOOL Next (TC_TPsRef<T>& objRef)
{
T * obj = (T *)Fetch();
if(obj)
obj->RemoveRef();
TC_CPsRef::Assign(objRef, obj);
return ((T*)objRef) != 0;
} // end of func
// **********************************************************************
}; // end of class TC_TPsCursor
// **********************************************************************
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CPsResSet & res_set) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CPsAtomicResult & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CPsCollection & cltn) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, long & result) TC_PS_THROW_FUNC ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, unsigned long & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, short & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, unsigned short & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, char & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, unsigned char & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, float & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, double & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CDate & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CTime & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CBCD & result) ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CString & result) TC_PS_THROW_FUNC ;
TC_PS_EXPORT TCPsRetCode tcPsExecute (TC_CPsQuery & query, TC_CBinArray & result) TC_PS_THROW_FUNC ;
#endif // _INC_QUERY_HPP