Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/database-sql-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ for i := 0; i < 10; i++ {
return err
}
}
if err := scope.Commit(); err != nil {
return err
}
rows, err := conn.Query("SELECT * FROM example")
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions examples/std/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func TestStdContext(t *testing.T) {
require.NoError(t, UseContext())
}

func TestStdSession(t *testing.T) {
require.NoError(t, Sessions())
}

func TestStdProgress(t *testing.T) {
require.NoError(t, ProgressProfileLogs())
}
Expand Down
84 changes: 84 additions & 0 deletions examples/std/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package std

import (
"fmt"

"github.com/google/uuid"

"github.com/ClickHouse/clickhouse-go/v2"
)

func Sessions() error {
env, err := GetStdTestEnvironment()
if err != nil {
return err
}
conn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, env.HttpPort)},
Auth: clickhouse.Auth{
Database: env.Database,
Username: env.Username,
Password: env.Password,
},
Protocol: clickhouse.HTTP,
Settings: clickhouse.Settings{
"session_id": uuid.NewString(),
},
})
defer conn.Close()
if _, err := conn.Exec(`DROP TABLE IF EXISTS example`); err != nil {
return err
}
_, err = conn.Exec(`
CREATE TEMPORARY TABLE IF NOT EXISTS example (
Col1 UInt8
)
`)
if err != nil {
return err
}
scope, err := conn.Begin()
if err != nil {
return err
}
batch, err := scope.Prepare("INSERT INTO example")
if err != nil {
return err
}
for i := 0; i < 10; i++ {
_, err := batch.Exec(
uint8(i),
)
if err != nil {
return err
}
}
// The batch is sent when the transaction commits.
if err := scope.Commit(); err != nil {
return err
}
rows, err := conn.Query("SELECT * FROM example")
if err != nil {
return err
}
defer rows.Close()

var (
col1 uint8
count int
)
for rows.Next() {
if err := rows.Scan(&col1); err != nil {
return err
}
fmt.Printf("row: col1=%d\n", col1)
count++
}
if err := rows.Err(); err != nil {
return err
}
if count != 10 {
return fmt.Errorf("expected 10 rows, got %d", count)
}
return nil
}