aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/clickhouse-go/clickhouse_send_query.go
blob: 0717ed779748b0f7e3bf78df13fd387f4a077038 (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
package clickhouse

import (
	"context"
	"github.com/ClickHouse/clickhouse-go/lib/data"
	"github.com/ClickHouse/clickhouse-go/lib/protocol"
)

func (ch *clickhouse) sendQuery(ctx context.Context, query string, externalTables []ExternalTable) error {
	ch.logf("[send query] %s", query)
	if err := ch.encoder.Uvarint(protocol.ClientQuery); err != nil {
		return err
	}
	var queryID string
	queryIDValue := ctx.Value(queryIDKey)
	if queryIDValue != nil {
		if queryIdStr, ok := queryIDValue.(string); ok {
			queryID = queryIdStr
		}
	}
	if err := ch.encoder.String(queryID); err != nil {
		return err
	}
	{ // client info
		ch.encoder.Uvarint(1)
		ch.encoder.String("")
		ch.encoder.String("")
		ch.encoder.String("[::ffff:127.0.0.1]:0")
		ch.encoder.Uvarint(1) // iface type TCP
		ch.encoder.String(hostname)
		ch.encoder.String(hostname)
	}
	if err := ch.ClientInfo.Write(ch.encoder); err != nil {
		return err
	}
	if ch.ServerInfo.Revision >= protocol.DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO {
		ch.encoder.String("")
	}

	// the settings are written as list of contiguous name-value pairs, finished with empty name
	if !ch.settings.IsEmpty() {
		ch.logf("[query settings] %s", ch.settings.settingsStr)
		if err := ch.settings.Serialize(ch.encoder); err != nil {
			return err
		}
	}
	// empty string is a marker of the end of the settings
	if err := ch.encoder.String(""); err != nil {
		return err
	}
	if err := ch.encoder.Uvarint(protocol.StateComplete); err != nil {
		return err
	}
	compress := protocol.CompressDisable
	if ch.compress {
		compress = protocol.CompressEnable
	}
	if err := ch.encoder.Uvarint(compress); err != nil {
		return err
	}
	if err := ch.encoder.String(query); err != nil {
		return err
	}
	if err := ch.sendExternalTables(externalTables); err != nil {
		return err
	}
	if err := ch.writeBlock(&data.Block{}, ""); err != nil {
		return err
	}
	return ch.encoder.Flush()
}