aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/query.go
blob: ce7b6b574d47aa562564060e394d13bc05b384a0 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package proto

import (
	"github.com/go-faster/errors"
)

type Query struct {
	ID          string
	Body        string
	Secret      string
	Stage       Stage
	Compression Compression
	Info        ClientInfo
	Settings    []Setting
	Parameters  []Parameter
}

type Parameter struct {
	Key   string
	Value string
}

func (p Parameter) Encode(b *Buffer) {
	Setting{
		Key:    p.Key,
		Value:  p.Value,
		Custom: true,
	}.Encode(b)
}

func (p *Parameter) Decode(r *Reader) error {
	var s Setting
	if err := s.Decode(r); err != nil {
		return errors.Wrap(err, "as setting")
	}

	p.Key = s.Key
	p.Value = s.Value

	return nil
}

// src/Core/BaseSettings.h:191 (BaseSettingsHelpers.Flags)
const (
	settingFlagImportant = 0x01
	settingFlagCustom    = 0x02
	settingFlagObsolete  = 0x04
)

type Setting struct {
	Key   string
	Value string

	Important bool
	Custom    bool
	Obsolete  bool
}

func (s Setting) Encode(b *Buffer) {
	b.PutString(s.Key)
	{
		var flags uint64
		if s.Important {
			flags |= settingFlagImportant
		}
		if s.Custom {
			flags |= settingFlagCustom
		}
		if s.Obsolete {
			flags |= settingFlagObsolete
		}
		b.PutUVarInt(flags)
	}
	b.PutString(s.Value)
}

func (s *Setting) Decode(r *Reader) error {
	key, err := r.Str()
	if err != nil {
		return errors.Wrap(err, "key")
	}

	if key == "" {
		// End of settings.
		return nil
	}

	flags, err := r.UVarInt()
	if err != nil {
		return errors.Wrap(err, "flags")
	}

	v, err := r.Str()
	if err != nil {
		return errors.Wrapf(err, "value (%s)", key)
	}

	s.Key = key
	s.Important = flags&settingFlagImportant != 0
	s.Custom = flags&settingFlagCustom != 0
	s.Obsolete = flags&settingFlagObsolete != 0
	s.Value = v

	return nil
}

func (q *Query) DecodeAware(r *Reader, version int) error {
	{
		v, err := r.Str()
		if err != nil {
			return errors.Wrap(err, "query id")
		}
		q.ID = v
	}
	if FeatureClientWriteInfo.In(version) {
		if err := q.Info.DecodeAware(r, version); err != nil {
			return errors.Wrap(err, "client info")
		}
	}
	if !FeatureSettingsSerializedAsStrings.In(version) {
		return errors.New("unsupported version")
	}
	for {
		var s Setting
		if err := s.Decode(r); err != nil {
			return errors.Wrap(err, "setting")
		}
		if s.Key == "" {
			break
		}
		q.Settings = append(q.Settings, s)
	}
	if FeatureInterServerSecret.In(version) {
		v, err := r.Str()
		if err != nil {
			return errors.Wrap(err, "inter-server secret")
		}
		q.Secret = v
	}

	{
		v, err := r.UVarInt()
		if err != nil {
			return errors.Wrap(err, "stage")
		}
		q.Stage = Stage(v)
		if !q.Stage.IsAStage() {
			return errors.Errorf("unknown stage %d", v)
		}
	}
	{
		v, err := r.UVarInt()
		if err != nil {
			return errors.Wrap(err, "compression")
		}
		q.Compression = Compression(v)
		if !q.Compression.IsACompression() {
			return errors.Errorf("unknown compression %d", v)
		}
	}

	{
		v, err := r.Str()
		if err != nil {
			return errors.Wrap(err, "query body")
		}
		q.Body = v
	}
	if FeatureParameters.In(version) {
		for {
			var p Parameter
			if err := p.Decode(r); err != nil {
				return errors.Wrap(err, "parameter")
			}
			if p.Key == "" {
				break
			}
			q.Parameters = append(q.Parameters, p)
		}
	}
	return nil
}

func (q Query) EncodeAware(b *Buffer, version int) {
	ClientCodeQuery.Encode(b)
	b.PutString(q.ID)
	if FeatureClientWriteInfo.In(version) {
		q.Info.EncodeAware(b, version)
	}

	if FeatureSettingsSerializedAsStrings.In(version) {
		for _, s := range q.Settings {
			s.Encode(b)
		}
	}
	b.PutString("") // end of settings

	if FeatureInterServerSecret.In(version) {
		b.PutString(q.Secret)
	}

	StageComplete.Encode(b)
	q.Compression.Encode(b)

	b.PutString(q.Body)

	if FeatureParameters.In(version) {
		for _, p := range q.Parameters {
			p.Encode(b)
		}
		b.PutString("") // end of parameters
	}
}