00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __ZTBQuery__
00026 #define __ZTBQuery__ 1
00027 #include "zconfig.h"
00028
00029 #include "ZCompare.h"
00030 #include "ZTBSpec.h"
00031
00032 #include <set>
00033
00034
00035 #pragma mark -
00036 #pragma mark * ZTBQuery
00037
00038 class ZTBQueryNode;
00039
00041
00042 class ZTBQuery
00043 {
00044 ZOOLIB_DEFINE_OPERATOR_BOOL_TYPES(ZTBQuery,
00045 operator_bool_generator_type, operator_bool_type);
00046 public:
00047 class SortSpec;
00048
00052 ZTBQuery();
00053
00055 ZTBQuery(const ZTBQuery& iOther);
00056
00058 ZTBQuery& operator=(const ZTBQuery& iOther);
00059
00061 ~ZTBQuery();
00063
00067 explicit ZTBQuery(const ZTuple& iTuple);
00068
00070 explicit ZTBQuery(const ZStreamR& iStreamR);
00071
00073 ZTuple AsTuple() const;
00074
00076 void ToStream(const ZStreamW& iStreamW) const;
00077
00079
00083 ZTBQuery(uint64 iID);
00084
00086 ZTBQuery(const uint64* iIDs, size_t iCount);
00087
00089 ZTBQuery(const std::vector<uint64>& iIDs);
00090
00092 ZTBQuery(const std::set<uint64>& iIDs);
00093
00095 ZTBQuery(const ZTBSpec& iSpec);
00096
00099 ZTBQuery(const ZTBQuery& iSourceQuery, const ZTuplePropName& iSourcePropName);
00100
00103 ZTBQuery(const ZTuplePropName& iPropName, const ZTBQuery& iSourceQuery);
00105
00108 ZTBQuery operator&(const ZTBSpec& iSpec) const;
00109 ZTBQuery& operator&=(const ZTBSpec& iSpec);
00110
00111 ZTBQuery operator&(const ZTBQuery& iQuery) const;
00112 ZTBQuery& operator&=(const ZTBQuery& iQuery);
00113
00114 ZTBQuery operator|(const ZTBQuery& iQuery) const;
00115 ZTBQuery& operator|=(const ZTBQuery& iQuery);
00117
00121 ZTBQuery Sorted(const ZTuplePropName& iPropName, bool iAscending, int iStrength) const;
00122
00124 ZTBQuery Sorted(const ZTuplePropName& iPropName, bool iAscending) const;
00125
00127 ZTBQuery Sorted(const ZTuplePropName& iPropName) const;
00128
00130 ZTBQuery First(const ZTuplePropName& iPropName) const;
00132
00135 operator operator_bool_type() const;
00136
00137 int Compare(const ZTBQuery& iOther) const;
00138
00139 bool operator==(const ZTBQuery& iOther) const;
00140 bool operator!=(const ZTBQuery& iOther) const;
00141
00142 bool operator<(const ZTBQuery& iOther) const;
00143 bool operator<=(const ZTBQuery& iOther) const;
00144 bool operator>(const ZTBQuery& iOther) const;
00145 bool operator>=(const ZTBQuery& iOther) const;
00147
00148 ZRef<ZTBQueryNode> GetNode() const;
00149
00150 private:
00151 ZTBQuery(const ZRef<ZTBQueryNode>& iNode);
00152 ZTBQuery(ZTBQueryNode* iNode);
00153
00154 ZRef<ZTBQueryNode> fNode;
00155 };
00156
00157 inline ZTBQuery operator&(const ZTBSpec& iSpec, const ZTBQuery& iQuery)
00158 { return iQuery & iSpec; }
00159
00160 inline ZTBQuery operator|(const ZTBSpec& iSpec, const ZTBQuery& iQuery)
00161 { return iQuery | iSpec; }
00162
00163 namespace ZooLib {
00164 template <> inline int sCompare_T(const ZTBQuery& iL, const ZTBQuery& iR)
00165 { return iL.Compare(iR); }
00166 }
00167
00168
00169 #pragma mark -
00170 #pragma mark * ZTBQuery::SortSpec
00171
00173
00174 class ZTBQuery::SortSpec
00175 {
00176 private:
00177 SortSpec();
00178
00179 public:
00180 explicit SortSpec(const ZStreamR& iStreamR);
00181
00183 SortSpec(const ZTuplePropName& iPropName)
00184 : fPropName(iPropName), fAscending(true), fStrength(0) {}
00185
00187 SortSpec(const ZTuplePropName& iPropName, bool iAscending)
00188 : fPropName(iPropName), fAscending(iAscending), fStrength(0) {}
00189
00191 SortSpec(const ZTuplePropName& iPropName, bool iAscending, int iStrength)
00192 : fPropName(iPropName), fAscending(iAscending), fStrength(iStrength) {}
00193
00196 int Compare(const SortSpec& iOther) const;
00197 bool operator<(const SortSpec& iOther) const;
00199
00200 void ToStream(const ZStreamW& iStreamW) const;
00201
00202 ZTuplePropName fPropName;
00203 bool fAscending;
00204 int fStrength;
00205 };
00206
00207 namespace ZooLib {
00208 template <>
00209 inline int sCompare_T(const ZTBQuery::SortSpec& iL, const ZTBQuery::SortSpec& iR)
00210 { return iL.Compare(iR); }
00211 }
00212
00213
00214 #pragma mark -
00215 #pragma mark * ZTBQueryNode
00216
00217 class ZTBQueryNode_All;
00218 class ZTBQueryNode_Combo;
00219 class ZTBQueryNode_Difference;
00220 class ZTBQueryNode_First;
00221 class ZTBQueryNode_ID_Constant;
00222 class ZTBQueryNode_ID_FromSource;
00223 class ZTBQueryNode_Property;
00224
00225 class ZTBQueryNode : public ZRefCounted
00226 {
00227 protected:
00228 ZTBQueryNode();
00229
00230 public:
00231 static bool sCheckAccessEnabled() { return false; }
00232
00233 virtual ~ZTBQueryNode();
00234
00235 int Compare(const ZRef<ZTBQueryNode>& iOther);
00236
00237 virtual ZTuple AsTuple() = 0;
00238 virtual void ToStream(const ZStreamW& iStreamW) = 0;
00239
00240
00241
00242
00243 virtual int pRevCompare(ZTBQueryNode* iOther) = 0;
00244 virtual int pCompare_All(ZTBQueryNode_All* iAll) = 0;
00245 virtual int pCompare_Combo(ZTBQueryNode_Combo* iCombo) = 0;
00246 virtual int pCompare_Difference(ZTBQueryNode_Difference* iDifference) = 0;
00247 virtual int pCompare_First(ZTBQueryNode_First* iFirst) = 0;
00248 virtual int pCompare_ID_Constant(ZTBQueryNode_ID_Constant* iID_Constant) = 0;
00249 virtual int pCompare_ID_FromSource(ZTBQueryNode_ID_FromSource* iID_FromSource) = 0;
00250 virtual int pCompare_Property(ZTBQueryNode_Property* iProperty) = 0;
00251 };
00252
00253 namespace ZooLib {
00254 template <>
00255 inline int sCompare_T(const ZRef<ZTBQueryNode>& iL, const ZRef<ZTBQueryNode>& iR)
00256 { return iL->Compare(iR); }
00257 }
00258
00259
00260 #pragma mark -
00261 #pragma mark * ZTBQueryNode_GT
00262
00263 class ZTBQueryNode_GT : public ZTBQueryNode
00264 {
00265 public:
00266 virtual int pCompare_All(ZTBQueryNode_All* iAll);
00267 virtual int pCompare_Combo(ZTBQueryNode_Combo* iCombo);
00268 virtual int pCompare_Difference(ZTBQueryNode_Difference* iDifference);
00269 virtual int pCompare_First(ZTBQueryNode_First* iFirst);
00270 virtual int pCompare_ID_Constant(ZTBQueryNode_ID_Constant* iID_Constant);
00271 virtual int pCompare_ID_FromSource(ZTBQueryNode_ID_FromSource* iID_FromSource);
00272 virtual int pCompare_Property(ZTBQueryNode_Property* iProperty);
00273 };
00274
00275
00276 #pragma mark -
00277 #pragma mark * ZTBQueryNode_GT_All
00278
00279 class ZTBQueryNode_GT_All : public ZTBQueryNode_GT
00280 {
00281 public:
00282 virtual int pCompare_All(ZTBQueryNode_All* iAll);
00283 };
00284
00285
00286 #pragma mark -
00287 #pragma mark * ZTBQueryNode_GT_Combo
00288
00289 class ZTBQueryNode_GT_Combo : public ZTBQueryNode_GT_All
00290 {
00291 public:
00292 virtual int pCompare_Combo(ZTBQueryNode_Combo* iCombo);
00293 };
00294
00295
00296 #pragma mark -
00297 #pragma mark * ZTBQueryNode_GT_Difference
00298
00299 class ZTBQueryNode_GT_Difference : public ZTBQueryNode_GT_Combo
00300 {
00301 public:
00302 virtual int pCompare_Difference(ZTBQueryNode_Difference* iDifference);
00303 };
00304
00305
00306 #pragma mark -
00307 #pragma mark * ZTBQueryNode_GT_First
00308
00309 class ZTBQueryNode_GT_First : public ZTBQueryNode_GT_Difference
00310 {
00311 public:
00312 virtual int pCompare_First(ZTBQueryNode_First* iFirst);
00313 };
00314
00315
00316 #pragma mark -
00317 #pragma mark * ZTBQueryNode_GT_ID_Constant
00318
00319 class ZTBQueryNode_GT_ID_Constant : public ZTBQueryNode_GT_First
00320 {
00321 public:
00322 virtual int pCompare_ID_Constant(ZTBQueryNode_ID_Constant* iID_Constant);
00323 };
00324
00325
00326 #pragma mark -
00327 #pragma mark * ZTBQueryNode_GT_ID_FromSource
00328
00329 class ZTBQueryNode_GT_ID_FromSource : public ZTBQueryNode_GT_ID_Constant
00330 {
00331 public:
00332 virtual int pCompare_ID_FromSource(ZTBQueryNode_ID_FromSource* iID_FromSource);
00333 };
00334
00335
00336 #pragma mark -
00337 #pragma mark * ZTBQueryNode_GT_Property
00338
00339 class ZTBQueryNode_GT_Property : public ZTBQueryNode_GT_ID_FromSource
00340 {
00341 public:
00342 virtual int pCompare_Property(ZTBQueryNode_Property* iProperty);
00343 };
00344
00345
00346 #pragma mark -
00347 #pragma mark * ZTBQueryNode_All
00348
00349 class ZTBQueryNode_All : public ZTBQueryNode_GT
00350 {
00351 public:
00352 ZTBQueryNode_All();
00353 explicit ZTBQueryNode_All(const ZStreamR& iStreamR);
00354 virtual ~ZTBQueryNode_All();
00355
00356
00357 virtual ZTuple AsTuple();
00358 virtual void ToStream(const ZStreamW& iStreamW);
00359
00360 virtual int pRevCompare(ZTBQueryNode* iOther);
00361 virtual int pCompare_All(ZTBQueryNode_All* iAll);
00362 };
00363
00364
00365 #pragma mark -
00366 #pragma mark * ZTBQueryNode_Combo
00367
00368 class ZTBQueryNode_Combo : public ZTBQueryNode_GT_All
00369 {
00370 public:
00371 class Intersection;
00372
00373 explicit ZTBQueryNode_Combo(const ZStreamR& iStreamR);
00374
00375 ZTBQueryNode_Combo(
00376 std::vector<ZTBQuery::SortSpec>& ioSort, std::vector<Intersection>& ioIntersections);
00377
00378 virtual ~ZTBQueryNode_Combo();
00379
00380
00381 virtual ZTuple AsTuple();
00382 virtual void ToStream(const ZStreamW& iStreamW);
00383
00384
00385 const std::vector<ZTBQuery::SortSpec>& GetSort() { return fSort; }
00386 const std::vector<Intersection>& GetIntersections() { return fIntersections; }
00387
00388 virtual int pRevCompare(ZTBQueryNode* iOther);
00389 virtual int pCompare_Combo(ZTBQueryNode_Combo* iCombo);
00390
00391 private:
00392 std::vector<ZTBQuery::SortSpec> fSort;
00393 std::vector<Intersection> fIntersections;
00394 };
00395
00396 class ZTBQueryNode_Combo::Intersection
00397 {
00398 public:
00399 explicit Intersection(const ZStreamR& iStreamR);
00400
00401 Intersection() {}
00402 Intersection(const ZTuple& iTuple);
00403 Intersection(const ZRef<ZTBQueryNode>& iNode)
00404 : fFilter(ZTBSpec::sAny()), fNodes(1, iNode) {}
00405
00406 Intersection(const ZTBSpec& iFilter, const ZRef<ZTBQueryNode>& iNode)
00407 : fFilter(iFilter), fNodes(1, iNode) {}
00408
00409 Intersection(const ZTBSpec& iFilter, const std::vector<ZRef<ZTBQueryNode> >& iNodes)
00410 : fFilter(iFilter), fNodes(iNodes) {}
00411
00412 ZTuple AsTuple() const;
00413 void ToStream(const ZStreamW& iStreamW) const;
00414
00415 int Compare(const Intersection& iOther) const;
00416
00417 ZTBSpec fFilter;
00418 std::vector<ZRef<ZTBQueryNode> > fNodes;
00419 };
00420
00421 namespace ZooLib {
00422 template <>
00423 inline int sCompare_T(
00424 const ZTBQueryNode_Combo::Intersection& iL, const ZTBQueryNode_Combo::Intersection& iR)
00425 { return iL.Compare(iR); }
00426 }
00427
00428
00429 #pragma mark -
00430 #pragma mark * ZTBQueryNode_Difference
00431
00432 class ZTBQueryNode_Difference : public ZTBQueryNode_GT_Combo
00433 {
00434 public:
00435 explicit ZTBQueryNode_Difference(const ZStreamR& iStreamR);
00436
00437 ZTBQueryNode_Difference(const ZRef<ZTBQueryNode>& iLeftNode, const ZRef<ZTBQueryNode>& iRightNode);
00438 virtual ~ZTBQueryNode_Difference();
00439
00440
00441 virtual ZTuple AsTuple();
00442 virtual void ToStream(const ZStreamW& iStreamW);
00443
00444
00445 const ZRef<ZTBQueryNode>& GetLeftNode() { return fLeftNode; }
00446 const ZRef<ZTBQueryNode>& GetRightNode() { return fRightNode; }
00447
00448 virtual int pRevCompare(ZTBQueryNode* iOther);
00449 virtual int pCompare_Difference(ZTBQueryNode_Difference* iDifference);
00450
00451 private:
00452 ZRef<ZTBQueryNode> fLeftNode;
00453 ZRef<ZTBQueryNode> fRightNode;
00454 };
00455
00456
00457 #pragma mark -
00458 #pragma mark * ZTBQueryNode_First
00459
00460 class ZTBQueryNode_First : public ZTBQueryNode_GT_Difference
00461 {
00462 public:
00463 explicit ZTBQueryNode_First(const ZStreamR& iStreamR);
00464
00465 ZTBQueryNode_First(const ZTuplePropName& iPropName, const ZRef<ZTBQueryNode>& iSourceNode);
00466 virtual ~ZTBQueryNode_First();
00467
00468
00469 virtual ZTuple AsTuple();
00470 virtual void ToStream(const ZStreamW& iStreamW);
00471
00472
00473 const ZTuplePropName& GetPropName() { return fPropName; }
00474 const ZRef<ZTBQueryNode>& GetSourceNode() { return fSourceNode; }
00475
00476 virtual int pRevCompare(ZTBQueryNode* iOther);
00477 virtual int pCompare_First(ZTBQueryNode_First* iFirst);
00478
00479 private:
00480 ZTuplePropName fPropName;
00481 ZRef<ZTBQueryNode> fSourceNode;
00482 };
00483
00484
00485 #pragma mark -
00486 #pragma mark * ZTBQueryNode_ID_Constant
00487
00488 class ZTBQueryNode_ID_Constant : public ZTBQueryNode_GT_First
00489 {
00490 public:
00491 explicit ZTBQueryNode_ID_Constant(const ZStreamR& iStreamR);
00492
00493 ZTBQueryNode_ID_Constant(uint64 iID);
00494 ZTBQueryNode_ID_Constant(const std::set<uint64>& iIDs);
00495 ZTBQueryNode_ID_Constant(const std::vector<uint64>& iIDs);
00496 ZTBQueryNode_ID_Constant(std::vector<uint64>& ioIDs, bool iKnowWhatImDoing);
00497 ZTBQueryNode_ID_Constant(const uint64* iIDs, size_t iCount);
00498
00499 virtual ~ZTBQueryNode_ID_Constant();
00500
00501
00502 virtual ZTuple AsTuple();
00503 virtual void ToStream(const ZStreamW& iStreamW);
00504
00505
00506 const std::vector<uint64>& GetIDs() { return fIDs; }
00507
00508 virtual int pRevCompare(ZTBQueryNode* iOther);
00509 virtual int pCompare_ID_Constant(ZTBQueryNode_ID_Constant* iID_Constant);
00510
00511 private:
00512 std::vector<uint64> fIDs;
00513 };
00514
00515
00516 #pragma mark -
00517 #pragma mark * ZTBQueryNode_ID_FromSource
00518
00519 class ZTBQueryNode_ID_FromSource : public ZTBQueryNode_GT_ID_Constant
00520 {
00521 public:
00522 explicit ZTBQueryNode_ID_FromSource(const ZStreamR& iStreamR);
00523
00524 ZTBQueryNode_ID_FromSource(
00525 const ZRef<ZTBQueryNode>& iSourceNode, const ZTuplePropName& iSourcePropName);
00526
00527 virtual ~ZTBQueryNode_ID_FromSource();
00528
00529
00530 virtual ZTuple AsTuple();
00531 virtual void ToStream(const ZStreamW& iStreamW);
00532
00533
00534 const ZRef<ZTBQueryNode>& GetSourceNode() { return fSourceNode; }
00535 const ZTuplePropName& GetSourcePropName() { return fSourcePropName; }
00536
00537 virtual int pRevCompare(ZTBQueryNode* iOther);
00538 virtual int pCompare_ID_FromSource(ZTBQueryNode_ID_FromSource* iID_FromSource);
00539
00540 private:
00541
00542 ZTuplePropName fSourcePropName;
00543 ZRef<ZTBQueryNode> fSourceNode;
00544 };
00545
00546
00547 #pragma mark -
00548 #pragma mark * ZTBQueryNode_Property
00549
00550 class ZTBQueryNode_Property : public ZTBQueryNode_GT_ID_FromSource
00551 {
00552 public:
00553 explicit ZTBQueryNode_Property(const ZStreamR& iStreamR);
00554
00555 ZTBQueryNode_Property(const ZTuplePropName& iPropName, const ZRef<ZTBQueryNode>& iSourceNode);
00556 virtual ~ZTBQueryNode_Property();
00557
00558
00559 virtual ZTuple AsTuple();
00560 virtual void ToStream(const ZStreamW& iStreamW);
00561
00562
00563 const ZTuplePropName& GetPropName() { return fPropName; }
00564 const ZRef<ZTBQueryNode>& GetSourceNode() { return fSourceNode; }
00565
00566 virtual int pRevCompare(ZTBQueryNode* iOther);
00567 virtual int pCompare_Property(ZTBQueryNode_Property* iProperty);
00568
00569 private:
00570
00571 ZTuplePropName fPropName;
00572 ZRef<ZTBQueryNode> fSourceNode;
00573 };
00574
00575
00576 #pragma mark -
00577 #pragma mark * ZTBQuery inlines
00578
00579 inline ZTBQuery::operator operator_bool_type() const
00580 { return operator_bool_generator_type::translate(fNode); }
00581
00582 inline int ZTBQuery::Compare(const ZTBQuery& iOther) const
00583 {
00584 ZTBQueryNode* otherNode = iOther.fNode.GetObject();
00585
00586 if (ZTBQueryNode* myNode = fNode.GetObject())
00587 {
00588 if (otherNode)
00589 return - myNode->pRevCompare(otherNode);
00590 return 1;
00591 }
00592 else if (otherNode)
00593 {
00594 return -1;
00595 }
00596
00597 return 0;
00598 }
00599
00600 inline bool ZTBQuery::operator==(const ZTBQuery& iOther) const
00601 {
00602 ZTBQueryNode* otherNode = iOther.fNode.GetObject();
00603
00604 if (ZTBQueryNode* myNode = fNode.GetObject())
00605 {
00606 if (otherNode)
00607 return 0 == myNode->pRevCompare(otherNode);
00608 return false;
00609 }
00610 else if (otherNode)
00611 {
00612 return false;
00613 }
00614
00615 return true;
00616 }
00617
00618 inline bool ZTBQuery::operator!=(const ZTBQuery& iOther) const
00619 { return !(*this == iOther); }
00620
00621 inline bool ZTBQuery::operator<(const ZTBQuery& iOther) const
00622 {
00623 ZTBQueryNode* otherNode = iOther.fNode.GetObject();
00624
00625 if (ZTBQueryNode* myNode = fNode.GetObject())
00626 {
00627 if (otherNode)
00628 return 0 < myNode->pRevCompare(otherNode);
00629 return false;
00630 }
00631 else if (otherNode)
00632 {
00633 return true;
00634 }
00635
00636 return false;
00637 }
00638
00639 inline bool ZTBQuery::operator<=(const ZTBQuery& iOther) const
00640 { return !(iOther < *this); }
00641
00642 inline bool ZTBQuery::operator>(const ZTBQuery& iOther) const
00643 { return iOther < *this; }
00644
00645 inline bool ZTBQuery::operator>=(const ZTBQuery& iOther) const
00646 { return !(*this < iOther); }
00647
00648 #endif // __ZTBQuery__