-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathquery_parameters.go
More file actions
56 lines (51 loc) · 1.67 KB
/
Copy pathquery_parameters.go
File metadata and controls
56 lines (51 loc) · 1.67 KB
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
package std
import (
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/tests/std"
)
func QueryWithParameters() error {
conn, err := GetStdOpenDBConnection(clickhouse.Native, nil, nil, nil)
if err != nil {
return err
}
if !std.CheckMinServerVersion(conn, 22, 8, 0) {
return nil
}
// ClickHouse parses parameter strings in its Escaped format. A raw Go
// literal preserves the backslashes; an interpreted literal needs an
// extra backslash. Double the Escaped backslash to receive a literal \n.
row := conn.QueryRow(
`SELECT
{column:Identifier},
{str:String},
{array:Array(String)},
{escaped_raw:String},
{escaped_interpreted:String},
{literal_backslash:String}
FROM {database:Identifier}.{table:Identifier}
LIMIT 1 OFFSET 100`,
clickhouse.Named("str", "hello"),
clickhouse.Named("array", "['a', 'b', 'c']"),
clickhouse.Named("column", "number"),
clickhouse.Named("database", "system"),
clickhouse.Named("table", "numbers"),
clickhouse.Named("escaped_raw", `line 1\nline 2\tend`),
clickhouse.Named("escaped_interpreted", "line 1\\nline 2\\tend"),
clickhouse.Named("literal_backslash", `line 1\\nline 2`),
)
var (
column uint64
str string
array []string
escapedRaw string
escapedInterpreted string
literalBackslash string
)
if err := row.Scan(&column, &str, &array, &escapedRaw, &escapedInterpreted, &literalBackslash); err != nil {
return err
}
fmt.Printf("row: column=%d, str=%s, array=%s, escapedRaw=%q, escapedInterpreted=%q, literalBackslash=%q\n",
column, str, array, escapedRaw, escapedInterpreted, literalBackslash)
return nil
}