add ContextBatch api to create batch with context (#326)

This commit is contained in:
Dmitry Kropachev
2025-06-11 06:36:46 -04:00
committed by GitHub
parent efee4798d6
commit b0b3ded8da
2 changed files with 18 additions and 3 deletions

View File

@@ -3,7 +3,6 @@ package gocqlx
import (
"context"
"fmt"
"github.com/gocql/gocql"
)
@@ -13,9 +12,25 @@ type Batch struct {
}
// NewBatch creates a new batch operation using defaults defined in the cluster.
//
// Deprecated: use session.Batch instead
func (s *Session) NewBatch(bt gocql.BatchType) *Batch {
return &Batch{
Batch: s.Session.NewBatch(bt),
Batch: s.Session.Batch(bt),
}
}
// Batch creates a new batch operation using defaults defined in the cluster.
func (s *Session) Batch(bt gocql.BatchType) *Batch {
return &Batch{
Batch: s.Session.Batch(bt),
}
}
// ContextBatch creates a new batch operation using defaults defined in the cluster with attached context.
func (s *Session) ContextBatch(ctx context.Context, bt gocql.BatchType) *Batch {
return &Batch{
Batch: s.Session.Batch(bt).WithContext(ctx),
}
}

View File

@@ -34,7 +34,7 @@ type BatchBuilder struct {
// - gocql.Batch prepares the included statements separately, which is more efficient.
// In contrast, gocqlx.qb.BatchBuilder, which is based on gocql.Query, prepares the whole batch statements as one ordinary query.
//
// Deprecated: Please use gocql.Session.NewBatch() instead.
// Deprecated: please use gocql.Session.Batch() instead.
func Batch() *BatchBuilder {
return &BatchBuilder{}
}