Skip to content

Commit

Permalink
chore: use defer to close rows
Browse files Browse the repository at this point in the history
  • Loading branch information
hayotbisonai committed Nov 3, 2024
1 parent 99d2256 commit 3d7fa60
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
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()
}

0 comments on commit 3d7fa60

Please sign in to comment.