object_processing.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include "node.h"
  3. #include "context.h"
  4. namespace NSQLTranslationV1 {
  5. class TObjectOperatorContext {
  6. protected:
  7. TScopedStatePtr Scoped;
  8. public:
  9. TString ServiceId;
  10. TDeferredAtom Cluster;
  11. TObjectOperatorContext(const TObjectOperatorContext& baseItem) = default;
  12. TObjectOperatorContext(TScopedStatePtr scoped);
  13. };
  14. class TObjectProcessorImpl: public TAstListNode, public TObjectOperatorContext {
  15. protected:
  16. using TBase = TAstListNode;
  17. TString ObjectId;
  18. TString TypeId;
  19. virtual INode::TPtr BuildOptions() const = 0;
  20. virtual INode::TPtr FillFeatures(INode::TPtr options) const = 0;
  21. INode::TPtr BuildKeys() const;
  22. public:
  23. TObjectProcessorImpl(TPosition pos, const TString& objectId, const TString& typeId, const TObjectOperatorContext& context);
  24. bool DoInit(TContext& ctx, ISource* src) override;
  25. TPtr DoClone() const final {
  26. return {};
  27. }
  28. };
  29. class TCreateObject: public TObjectProcessorImpl {
  30. private:
  31. using TBase = TObjectProcessorImpl;
  32. std::map<TString, TDeferredAtom> Features;
  33. std::set<TString> FeaturesToReset;
  34. protected:
  35. bool ExistingOk = false;
  36. bool ReplaceIfExists = false;
  37. protected:
  38. virtual INode::TPtr BuildOptions() const override {
  39. TString mode;
  40. if (ExistingOk) {
  41. mode = "createObjectIfNotExists";
  42. } else if (ReplaceIfExists) {
  43. mode = "createObjectOrReplace";
  44. } else {
  45. mode = "createObject";
  46. }
  47. return Y(Q(Y(Q("mode"), Q(mode))));
  48. }
  49. virtual INode::TPtr FillFeatures(INode::TPtr options) const override;
  50. public:
  51. TCreateObject(TPosition pos, const TString& objectId,
  52. const TString& typeId, bool existingOk, bool replaceIfExists, std::map<TString, TDeferredAtom>&& features, std::set<TString>&& featuresToReset, const TObjectOperatorContext& context)
  53. : TBase(pos, objectId, typeId, context)
  54. , Features(std::move(features))
  55. , FeaturesToReset(std::move(featuresToReset))
  56. , ExistingOk(existingOk)
  57. , ReplaceIfExists(replaceIfExists) {
  58. }
  59. };
  60. class TUpsertObject final: public TCreateObject {
  61. private:
  62. using TBase = TCreateObject;
  63. protected:
  64. virtual INode::TPtr BuildOptions() const override {
  65. return Y(Q(Y(Q("mode"), Q("upsertObject"))));
  66. }
  67. public:
  68. using TBase::TBase;
  69. };
  70. class TAlterObject final: public TCreateObject {
  71. private:
  72. using TBase = TCreateObject;
  73. protected:
  74. virtual INode::TPtr BuildOptions() const override {
  75. return Y(Q(Y(Q("mode"), Q("alterObject"))));
  76. }
  77. public:
  78. using TBase::TBase;
  79. };
  80. class TDropObject final: public TCreateObject {
  81. private:
  82. using TBase = TCreateObject;
  83. bool MissingOk() const {
  84. return ExistingOk; // Because we were derived from TCreateObject
  85. }
  86. protected:
  87. virtual INode::TPtr BuildOptions() const override {
  88. return Y(Q(Y(Q("mode"), Q(MissingOk() ? "dropObjectIfExists" : "dropObject"))));
  89. }
  90. public:
  91. using TBase::TBase;
  92. };
  93. }