forked from ClickHouse/clickhouse-go
/
direct.go
64 lines (57 loc) · 1.25 KB
/
direct.go
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
package main
import (
"database/sql/driver"
"log"
"time"
"github.com/kshvakov/clickhouse"
)
func main() {
connect, err := clickhouse.Open("tcp://127.0.0.1:9000?username=&debug=true")
if err != nil {
log.Fatal(err)
}
{
tx, _ := connect.Begin()
stmt, _ := connect.Prepare(`
CREATE TABLE IF NOT EXISTS example (
country_code FixedString(2),
os_id UInt8,
browser_id UInt8,
categories Array(Int16),
action_day Date,
action_time DateTime
) engine=Memory
`)
if _, err := stmt.Exec([]driver.Value{}); err != nil {
log.Fatal(err)
}
tx.Commit()
}
{
tx, _ := connect.Begin()
stmt, _ := connect.Prepare("INSERT INTO example (country_code, os_id, browser_id, categories, action_day, action_time) VALUES (?, ?, ?, ?, ?, ?)")
for i := 0; i < 100; i++ {
if _, err := stmt.Exec([]driver.Value{
"CZ",
uint8(10 + i),
uint8(100 + i),
clickhouse.Array([]int16{1, 2, 3}),
time.Now(),
time.Now(),
}); err != nil {
log.Fatal(err)
}
}
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}
{
tx, _ := connect.Begin()
stmt, _ := connect.Prepare(`DROP TABLE example`)
if _, err := stmt.Exec([]driver.Value{}); err != nil {
log.Fatal(err)
}
tx.Commit()
}
}