|
| 1 | +// Licensed to ClickHouse, Inc. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. ClickHouse, Inc. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package std |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/ClickHouse/clickhouse-go/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func EndOfProcessAndGotData() error { |
| 28 | + conn, err := GetStdOpenDBConnection(clickhouse.Native, clickhouse.Settings{ |
| 29 | + "send_logs_level": "trace", |
| 30 | + }, nil, nil) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + var totalBlocks int |
| 35 | + // use context to pass a call back for end of process and got data |
| 36 | + ctx := clickhouse.Context(context.Background(), clickhouse.WithEndOfProcess(func() { |
| 37 | + fmt.Println("process is finished") |
| 38 | + }), clickhouse.WithGotData(func() { |
| 39 | + totalBlocks++ |
| 40 | + })) |
| 41 | + |
| 42 | + rows, err := conn.QueryContext(ctx, "SELECT number from numbers(1000000) LIMIT 1000000") |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + defer rows.Close() |
| 47 | + |
| 48 | + for rows.Next() { |
| 49 | + } |
| 50 | + |
| 51 | + fmt.Printf("Total data blocks: %d\n", totalBlocks) |
| 52 | + return rows.Err() |
| 53 | +} |
0 commit comments