From 3d7fa60d6cc8c8dc37e9946ef9cc2715fc1559b0 Mon Sep 17 00:00:00 2001 From: Hayot Date: Sun, 3 Nov 2024 14:43:53 +0900 Subject: [PATCH] chore: use defer to close rows --- examples/clickhouse_api/array.go | 3 ++- examples/clickhouse_api/external_data.go | 3 ++- examples/clickhouse_api/map.go | 11 ++++++++--- examples/clickhouse_api/nested.go | 7 +++++-- examples/clickhouse_api/progress.go | 4 +++- examples/std/external_data.go | 3 ++- examples/std/open_db.go | 4 +++- examples/std/progress.go | 4 +++- 8 files changed, 28 insertions(+), 11 deletions(-) diff --git a/examples/clickhouse_api/array.go b/examples/clickhouse_api/array.go index a777e5947d..f780f819fb 100644 --- a/examples/clickhouse_api/array.go +++ b/examples/clickhouse_api/array.go @@ -68,12 +68,13 @@ func ArrayInsertRead() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { if err := rows.Scan(&col1, &col2); err != nil { return err } fmt.Printf("row: col1=%v, col2=%v\n", col1, col2) } - rows.Close() return rows.Err() } diff --git a/examples/clickhouse_api/external_data.go b/examples/clickhouse_api/external_data.go index 7399ae2f4f..670b675295 100644 --- a/examples/clickhouse_api/external_data.go +++ b/examples/clickhouse_api/external_data.go @@ -63,6 +63,8 @@ func ExternalData() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { var ( col1 uint8 @@ -72,7 +74,6 @@ func ExternalData() error { rows.Scan(&col1, &col2, &col3) fmt.Printf("col1=%d, col2=%s, col3=%v\n", col1, col2, col3) } - rows.Close() var count uint64 if err := conn.QueryRow(ctx, "SELECT COUNT(*) FROM external_table_1").Scan(&count); err != nil { diff --git a/examples/clickhouse_api/map.go b/examples/clickhouse_api/map.go index 3b1e287a1c..6d6be6a8e7 100644 --- a/examples/clickhouse_api/map.go +++ b/examples/clickhouse_api/map.go @@ -20,8 +20,9 @@ package clickhouse_api import ( "context" "fmt" - "github.com/ClickHouse/clickhouse-go/v2/lib/column/orderedmap" "strconv" + + "github.com/ClickHouse/clickhouse-go/v2/lib/column/orderedmap" ) func MapInsertRead() error { @@ -72,13 +73,15 @@ func MapInsertRead() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { if err := rows.Scan(&col1, &col2, &col3); err != nil { return err } fmt.Printf("row: col1=%v, col2=%v, col3=%v\n", col1, col2, col3) } - rows.Close() + return rows.Err() } @@ -124,6 +127,8 @@ func IterableOrderedMapInsertRead() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { var col1 orderedmap.Map[string, string] if err := rows.Scan(&col1); err != nil { @@ -131,6 +136,6 @@ func IterableOrderedMapInsertRead() error { } fmt.Printf("row: col1=%v\n", col1) } - rows.Close() + return rows.Err() } diff --git a/examples/clickhouse_api/nested.go b/examples/clickhouse_api/nested.go index 0aefeb0086..cb9f67c9cd 100644 --- a/examples/clickhouse_api/nested.go +++ b/examples/clickhouse_api/nested.go @@ -20,8 +20,9 @@ package clickhouse_api import ( "context" "fmt" - "github.com/ClickHouse/clickhouse-go/v2" "strconv" + + "github.com/ClickHouse/clickhouse-go/v2" ) func NestedUnFlattened() error { @@ -109,13 +110,15 @@ func NestedUnFlattened() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { if err := rows.Scan(&col1, &col2); err != nil { return err } fmt.Printf("row: col1=%v, col2=%v\n", col1, col2) } - rows.Close() + return rows.Err() } diff --git a/examples/clickhouse_api/progress.go b/examples/clickhouse_api/progress.go index fdc09334f8..487148a561 100644 --- a/examples/clickhouse_api/progress.go +++ b/examples/clickhouse_api/progress.go @@ -20,6 +20,7 @@ package clickhouse_api import ( "context" "fmt" + "github.com/ClickHouse/clickhouse-go/v2" ) @@ -45,10 +46,11 @@ func ProgressProfileLogs() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { } fmt.Printf("Total Rows: %d\n", totalRows) - rows.Close() return rows.Err() } diff --git a/examples/std/external_data.go b/examples/std/external_data.go index e91ce0e30d..b7efdfbb64 100644 --- a/examples/std/external_data.go +++ b/examples/std/external_data.go @@ -63,6 +63,8 @@ func ExternalData() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { var ( col1 uint8 @@ -72,7 +74,6 @@ func ExternalData() error { rows.Scan(&col1, &col2, &col3) fmt.Printf("col1=%d, col2=%s, col3=%v\n", col1, col2, col3) } - rows.Close() var count uint64 if err := conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM external_table_1").Scan(&count); err != nil { diff --git a/examples/std/open_db.go b/examples/std/open_db.go index 9f88612a11..03bc1fa77d 100644 --- a/examples/std/open_db.go +++ b/examples/std/open_db.go @@ -94,6 +94,8 @@ func OpenDb() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { var ( col1 uint8 @@ -105,6 +107,6 @@ func OpenDb() error { } fmt.Printf("row: col1=%d, col2=%s, col3=%s\n", col1, col2, col3) } - rows.Close() + return rows.Err() } diff --git a/examples/std/progress.go b/examples/std/progress.go index 38efb36584..7a11993158 100644 --- a/examples/std/progress.go +++ b/examples/std/progress.go @@ -20,6 +20,7 @@ package std import ( "context" "fmt" + "github.com/ClickHouse/clickhouse-go/v2" ) @@ -45,10 +46,11 @@ func ProgressProfileLogs() error { if err != nil { return err } + defer rows.Close() + for rows.Next() { } fmt.Printf("Total Rows: %d\n", totalRows) - rows.Close() return rows.Err() }