Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore] Use defer to close rows in examples #1431

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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: 2 additions & 1 deletion examples/clickhouse_api/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 2 additions & 1 deletion examples/clickhouse_api/external_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func ExternalData() error {
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var (
col1 uint8
Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions examples/clickhouse_api/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -124,13 +127,15 @@ 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 {
return err
}
fmt.Printf("row: col1=%v\n", col1)
}
rows.Close()

return rows.Err()
}
7 changes: 5 additions & 2 deletions examples/clickhouse_api/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down
4 changes: 3 additions & 1 deletion examples/clickhouse_api/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package clickhouse_api
import (
"context"
"fmt"

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

Expand All @@ -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()
}
3 changes: 2 additions & 1 deletion examples/std/external_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func ExternalData() error {
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var (
col1 uint8
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion examples/std/open_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func OpenDb() error {
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var (
col1 uint8
Expand All @@ -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()
}
4 changes: 3 additions & 1 deletion examples/std/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package std
import (
"context"
"fmt"

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

Expand All @@ -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()
}
Loading