aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/object_processing.h
blob: 4114235ee6dc1fda2b8d9bece771e936f6777836 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include "node.h"
#include "context.h"

namespace NSQLTranslationV1 {

class TObjectOperatorContext {
protected:
    TScopedStatePtr Scoped;
public:
    TString ServiceId;
    TDeferredAtom Cluster;
    TObjectOperatorContext(const TObjectOperatorContext& baseItem) = default;
    TObjectOperatorContext(TScopedStatePtr scoped);
};

class TObjectProcessorImpl: public TAstListNode, public TObjectOperatorContext {
protected:
    using TBase = TAstListNode;
    TString ObjectId;
    TString TypeId;

    virtual INode::TPtr BuildOptions() const = 0;
    virtual INode::TPtr FillFeatures(INode::TPtr options) const = 0;
    INode::TPtr BuildKeys() const;
public:
    TObjectProcessorImpl(TPosition pos, const TString& objectId, const TString& typeId, const TObjectOperatorContext& context);

    bool DoInit(TContext& ctx, ISource* src) override;

    TPtr DoClone() const final {
        return {};
    }
};

class TCreateObject: public TObjectProcessorImpl {
private:
    using TBase = TObjectProcessorImpl;
    std::map<TString, TDeferredAtom> Features;
    std::set<TString> FeaturesToReset;
protected:
    bool ExistingOk = false;
    bool ReplaceIfExists = false;
protected:
    virtual INode::TPtr BuildOptions() const override {
        TString mode;
        if (ExistingOk) {
            mode = "createObjectIfNotExists";
        } else if (ReplaceIfExists) {
            mode = "createObjectOrReplace";
        } else {
            mode = "createObject";
        }

        return Y(Q(Y(Q("mode"), Q(mode))));
    }
    virtual INode::TPtr FillFeatures(INode::TPtr options) const override;
public:
    TCreateObject(TPosition pos, const TString& objectId,
        const TString& typeId, bool existingOk, bool replaceIfExists, std::map<TString, TDeferredAtom>&& features, std::set<TString>&& featuresToReset, const TObjectOperatorContext& context)
        : TBase(pos, objectId, typeId, context)
        , Features(std::move(features))
        , FeaturesToReset(std::move(featuresToReset))
        , ExistingOk(existingOk)
        , ReplaceIfExists(replaceIfExists) {
        }
};

class TUpsertObject final: public TCreateObject {
private:
    using TBase = TCreateObject;
protected:
    virtual INode::TPtr BuildOptions() const override {
        return Y(Q(Y(Q("mode"), Q("upsertObject"))));
    }
public:
    using TBase::TBase;
};

class TAlterObject final: public TCreateObject {
private:
    using TBase = TCreateObject;
protected:
    virtual INode::TPtr BuildOptions() const override {
        return Y(Q(Y(Q("mode"), Q("alterObject"))));
    }
public:
    using TBase::TBase;
};

class TDropObject final: public TCreateObject {
private:
    using TBase = TCreateObject;
    bool MissingOk() const {
        return ExistingOk; // Because we were derived from TCreateObject
    }
protected:
    virtual INode::TPtr BuildOptions() const override {
        return Y(Q(Y(Q("mode"), Q(MissingOk() ? "dropObjectIfExists" : "dropObject"))));
    }
public:
    using TBase::TBase;
};

}