Skip to content

Commit cf6d78b

Browse files
feature/issue 44 update examples (#46)
* update examples
1 parent 9e02984 commit cf6d78b

File tree

16 files changed

+139
-115
lines changed

16 files changed

+139
-115
lines changed

bind.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ func format(tz *time.Location, v interface{}) string {
132132
case time.Time:
133133
switch v.Location().String() {
134134
case "Local":
135-
return fmt.Sprintf("toDateTime(%d)", v.Unix())
135+
return fmt.Sprintf("to_datetime(%d)", v.Unix())
136136
case tz.String():
137-
return v.Format("toDateTime('2006-01-02 15:04:05')")
137+
return v.Format("to_datetime('2006-01-02 15:04:05')")
138138
}
139-
return v.Format("toDateTime('2006-01-02 15:04:05', '" + v.Location().String() + "')")
139+
return v.Format("to_datetime('2006-01-02 15:04:05', '" + v.Location().String() + "')")
140140
case []interface{}: // tuple
141141
elements := make([]string, 0, len(v))
142142
for _, e := range v {

bind_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func TestFormatTime(t *testing.T) {
126126
tz, err = time.LoadLocation("Europe/London")
127127
)
128128
if assert.NoError(t, err) {
129-
if assert.Equal(t, "toDateTime('2022-01-12 15:00:00')", format(t1.Location(), t1)) {
130-
assert.Equal(t, "toDateTime('2022-01-12 15:00:00', 'UTC')", format(tz, t1))
129+
if assert.Equal(t, "to_datetime('2022-01-12 15:00:00')", format(t1.Location(), t1)) {
130+
assert.Equal(t, "to_datetime('2022-01-12 15:00:00', 'UTC')", format(tz, t1))
131131
}
132132
}
133133
}

examples/native/batch/main.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func example() error {
3131
var (
3232
ctx = context.Background()
3333
conn, err = proton.Open(&proton.Options{
34-
Addr: []string{"127.0.0.1:9000"},
34+
Addr: []string{"127.0.0.1:8463"},
3535
Auth: proton.Auth{
3636
Database: "default",
3737
Username: "default",
@@ -47,30 +47,30 @@ func example() error {
4747
if err != nil {
4848
return err
4949
}
50-
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
50+
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
5151
return err
5252
}
5353
err = conn.Exec(ctx, `
54-
CREATE TABLE IF NOT EXISTS example (
55-
Col1 UInt8
56-
, Col2 String
57-
, Col3 FixedString(3)
58-
, Col4 UUID
59-
, Col5 Map(String, UInt8)
60-
, Col6 Array(String)
61-
, Col7 Tuple(String, UInt8, Array(Map(String, String)))
54+
CREATE STREAM IF NOT EXISTS example (
55+
Col1 uint8
56+
, Col2 string
57+
, Col3 fixed_string(3)
58+
, Col4 uuid
59+
, Col5 map(string, uint8)
60+
, Col6 array(string)
61+
, Col7 tuple(string, uint8, array(map(string, string)))
6262
, Col8 DateTime
63-
) Engine = Null
63+
)
6464
`)
6565
if err != nil {
6666
return err
6767
}
6868

69-
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
69+
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)")
7070
if err != nil {
7171
return err
7272
}
73-
for i := 0; i < 500_000; i++ {
73+
for i := 0; i < 100; i++ {
7474
err := batch.Append(
7575
uint8(42),
7676
"ClickHouse", "Inc",

examples/native/bind/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func example() error {
3030
var (
3131
ctx = context.Background()
3232
conn, err = proton.Open(&proton.Options{
33-
Addr: []string{"127.0.0.1:9000"},
33+
Addr: []string{"127.0.0.1:8463"},
3434
Auth: proton.Auth{
3535
Database: "default",
3636
Username: "default",
@@ -42,18 +42,21 @@ func example() error {
4242
return err
4343
}
4444
const ddl = `
45-
CREATE TEMPORARY TABLE example (
46-
Col1 UInt8
47-
, Col2 String
45+
CREATE STREAM example (
46+
Col1 uint8
47+
, Col2 string
4848
, Col3 DateTime
4949
)
5050
`
51+
if err := conn.Exec(ctx, `drop stream if exists example`); err != nil {
52+
return err
53+
}
5154
if err := conn.Exec(ctx, ddl); err != nil {
5255
return err
5356
}
5457
datetime := time.Now()
5558
{
56-
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
59+
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)")
5760
if err != nil {
5861
return err
5962
}
@@ -73,13 +76,13 @@ func example() error {
7376
Col3 time.Time
7477
}
7578
{
76-
if err := conn.QueryRow(ctx, `SELECT * FROM example WHERE Col1 = $1 AND Col3 = $2`, 2, datetime).ScanStruct(&result); err != nil {
79+
if err := conn.QueryRow(ctx, `SELECT * except _tp_time FROM example WHERE _tp_time > earliest_ts() AND Col1 = $1 AND Col3 = $2 LIMIT 1`, 2, datetime).ScanStruct(&result); err != nil {
7780
return err
7881
}
7982
fmt.Println(result)
8083
}
8184
{
82-
if err := conn.QueryRow(ctx, `SELECT * FROM example WHERE Col1 = @Col1 AND Col3 = @Col2`,
85+
if err := conn.QueryRow(ctx, `SELECT * except _tp_time FROM example WHERE _tp_time > earliest_ts() AND Col1 = @Col1 AND Col3 = @Col2 LIMIT 1`,
8386
proton.Named("Col1", 4),
8487
proton.Named("Col2", datetime),
8588
).ScanStruct(&result); err != nil {

examples/native/dynamic-scan-types/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func example() error {
3030
conn, err := proton.Open(&proton.Options{
31-
Addr: []string{"127.0.0.1:9000"},
31+
Addr: []string{"127.0.0.1:8463"},
3232
Auth: proton.Auth{
3333
Database: "default",
3434
Username: "default",

examples/native/main.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func main() {
3030
conn, err := proton.Open(&proton.Options{
31-
Addr: []string{"127.0.0.1:9000"},
31+
Addr: []string{"127.0.0.1:8463"},
3232
Auth: proton.Auth{
3333
Database: "default",
3434
Username: "default",
@@ -62,28 +62,28 @@ func main() {
6262
fmt.Printf("name: %s, value: %s, type=%s\n", s.Name, s.Value, s.Type)
6363
}
6464

65-
if err = conn.Exec(context.Background(), "TUNCATE TABLE X"); err == nil {
65+
if err = conn.Exec(context.Background(), "TRUNCATE STREAM X"); err == nil {
6666
panic("unexpected")
6767
}
6868
if exception, ok := err.(*proton.Exception); ok {
6969
fmt.Printf("Catch exception [%d]\n", exception.Code)
7070
}
7171
const ddl = `
72-
CREATE TABLE example (
73-
Col1 UInt64
74-
, Col2 FixedString(2)
75-
, Col3 Map(String, String)
76-
, Col4 Array(String)
72+
CREATE STREAM example (
73+
Col1 uint64
74+
, Col2 fixed_string(2)
75+
, Col3 map(string, string)
76+
, Col4 array(string)
7777
, Col5 DateTime64(3)
78-
) Engine Memory
78+
)
7979
`
80-
if err := conn.Exec(context.Background(), "DROP TABLE IF EXISTS example"); err != nil {
80+
if err := conn.Exec(context.Background(), "DROP STREAM IF EXISTS example"); err != nil {
8181
log.Fatal(err)
8282
}
8383
if err := conn.Exec(context.Background(), ddl); err != nil {
8484
log.Fatal(err)
8585
}
86-
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
86+
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example (* except _tp_time)")
8787
if err != nil {
8888
log.Fatal(err)
8989
}
@@ -107,17 +107,18 @@ func main() {
107107
ctx := proton.Context(context.Background(), proton.WithProgress(func(p *proton.Progress) {
108108
fmt.Println("progress: ", p)
109109
}))
110-
110+
ctx, cancel := context.WithTimeout(ctx, time.Duration(10)*time.Second)
111+
defer cancel()
111112
var count uint64
112-
if err := conn.QueryRow(ctx, "SELECT COUNT() FROM example").Scan(&count); err != nil {
113+
if err := conn.QueryRow(ctx, "SELECT count() FROM example WHERE _tp_time > earliest_ts() LIMIT 1").Scan(&count); err != nil {
113114
log.Fatal(err)
114115
}
115116
fmt.Println("count", count)
116117
var result struct {
117118
Col1 uint64
118119
Count uint64 `ch:"count"`
119120
}
120-
if err := conn.QueryRow(ctx, "SELECT Col1, COUNT() AS count FROM example WHERE Col1 = $1 GROUP BY Col1", 42).ScanStruct(&result); err != nil {
121+
if err := conn.QueryRow(ctx, "SELECT Col1, count() AS count FROM example WHERE _tp_time > earliest_ts() AND Col1 = $1 GROUP BY Col1 LIMIT 1", 42).ScanStruct(&result); err != nil {
121122
log.Fatal(err)
122123
}
123124
fmt.Println("result", result)

examples/native/scan_struct/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func example() error {
3030
conn, err := proton.Open(&proton.Options{
31-
Addr: []string{"127.0.0.1:9000"},
31+
Addr: []string{"127.0.0.1:8463"},
3232
Auth: proton.Auth{
3333
Database: "default",
3434
Username: "default",
@@ -46,7 +46,9 @@ func example() error {
4646
if err != nil {
4747
return err
4848
}
49-
ctx := proton.Context(context.Background(), proton.WithSettings(proton.Settings{
49+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(3))
50+
defer cancel()
51+
ctx = proton.Context(ctx, proton.WithSettings(proton.Settings{
5052
"max_block_size": 10,
5153
}), proton.WithProgress(func(p *proton.Progress) {
5254
fmt.Println("progress: ", p)
@@ -57,15 +59,15 @@ func example() error {
5759
}
5860
return err
5961
}
60-
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
62+
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
6163
return err
6264
}
6365
err = conn.Exec(ctx, `
64-
CREATE TABLE IF NOT EXISTS example (
65-
Col1 UInt8,
66-
Col2 String,
66+
CREATE STREAM IF NOT EXISTS example (
67+
Col1 uint8,
68+
Col2 string,
6769
Col3 DateTime
68-
) engine=Memory
70+
)
6971
`)
7072
if err != nil {
7173
return err
@@ -89,7 +91,7 @@ func example() error {
8991
ColumnWithName time.Time `ch:"Col3"`
9092
}
9193

92-
if err = conn.Select(ctx, &result, "SELECT Col1, Col2, Col3 FROM example"); err != nil {
94+
if err = conn.Select(ctx, &result, "SELECT Col1, Col2, Col3 FROM example WHERE _tp_time > earliest_ts() LIMIT 10"); err != nil {
9395
return err
9496
}
9597

examples/native/simple/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func example() error {
3030
conn, err := proton.Open(&proton.Options{
31-
Addr: []string{"127.0.0.1:9000"},
31+
Addr: []string{"127.0.0.1:8463"},
3232
Auth: proton.Auth{
3333
Database: "default",
3434
Username: "default",
@@ -53,21 +53,23 @@ func example() error {
5353
}), proton.WithProfileInfo(func(p *proton.ProfileInfo) {
5454
fmt.Println("profile info: ", p)
5555
}))
56+
ctx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(5))
57+
defer cancel()
5658
if err := conn.Ping(ctx); err != nil {
5759
if exception, ok := err.(*proton.Exception); ok {
5860
fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
5961
}
6062
return err
6163
}
62-
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
64+
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
6365
return err
6466
}
6567
err = conn.Exec(ctx, `
66-
CREATE TABLE IF NOT EXISTS example (
67-
Col1 UInt8,
68-
Col2 String,
68+
CREATE STREAM IF NOT EXISTS example (
69+
Col1 uint8,
70+
Col2 string,
6971
Col3 DateTime
70-
) engine=Memory
72+
)
7173
`)
7274
if err != nil {
7375
return err
@@ -84,8 +86,7 @@ func example() error {
8486
if err := batch.Send(); err != nil {
8587
return err
8688
}
87-
88-
rows, err := conn.Query(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3", 0, "xxx", time.Now())
89+
rows, err := conn.Query(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE _tp_time > earliest_ts() AND Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3 LIMIT 12", 0, "xxx", time.Now())
8990
if err != nil {
9091
return err
9192
}

examples/native/write-async/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ import (
2727
)
2828

2929
const ddl = `
30-
CREATE TEMPORARY TABLE example (
31-
Col1 UInt64
32-
, Col2 String
33-
, Col3 Array(UInt8)
30+
CREATE STREAM example (
31+
Col1 uint64
32+
, Col2 string
33+
, Col3 array(int)
3434
, Col4 DateTime
35-
)
35+
)
3636
`
3737

3838
func main() {
3939
var (
4040
ctx = context.Background()
4141
conn, err = proton.Open(&proton.Options{
42-
Addr: []string{"127.0.0.1:9000"},
42+
Addr: []string{"127.0.0.1:8463"},
4343
Auth: proton.Auth{
4444
Database: "default",
4545
Username: "default",
@@ -55,6 +55,7 @@ func main() {
5555
if err != nil {
5656
log.Fatal(err)
5757
}
58+
conn.Exec(ctx, `DROP STREAM IF EXISTS example`)
5859
if err := conn.Exec(ctx, ddl); err != nil {
5960
log.Fatal(err)
6061
}

examples/native/write-columnar/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import (
2626
)
2727

2828
const ddl = `
29-
CREATE TEMPORARY TABLE example (
30-
Col1 UInt64
31-
, Col2 String
32-
, Col3 Array(UInt8)
29+
CREATE STREAM example (
30+
Col1 uint64
31+
, Col2 string
32+
, Col3 array(uint8)
3333
, Col4 DateTime
34-
)
34+
)
3535
`
3636

3737
func example(conn proton.Conn) error {
38-
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
38+
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example (* except _tp_time)")
3939
if err != nil {
4040
return err
4141
}
@@ -70,7 +70,7 @@ func main() {
7070
var (
7171
ctx = context.Background()
7272
conn, err = proton.Open(&proton.Options{
73-
Addr: []string{"127.0.0.1:9000"},
73+
Addr: []string{"127.0.0.1:8463"},
7474
Auth: proton.Auth{
7575
Database: "default",
7676
Username: "default",
@@ -86,6 +86,9 @@ func main() {
8686
if err != nil {
8787
log.Fatal(err)
8888
}
89+
if err := conn.Exec(context.Background(), `DROP STREAM IF EXISTS example`); err != nil {
90+
log.Fatal(err)
91+
}
8992
if err := conn.Exec(ctx, ddl); err != nil {
9093
log.Fatal(err)
9194
}

0 commit comments

Comments
 (0)