blockchain/internal/lib/cassandra/option.go

104 lines
2.0 KiB
Go

package cassandra
import (
"time"
"github.com/scylladb/gocqlx/v3/qb"
"github.com/gocql/gocql"
)
// Option 是設定選項的函數型別
type Option func(*cassandraConf)
func WithPort(port int) Option {
return func(c *cassandraConf) {
c.Port = port
}
}
func WithKeyspace(keyspace string) Option {
return func(c *cassandraConf) {
c.Keyspace = keyspace
}
}
func WithAuth(username, password string) Option {
return func(c *cassandraConf) {
c.Username = username
c.Password = password
c.UseAuth = true
}
}
func WithConsistency(consistency gocql.Consistency) Option {
return func(c *cassandraConf) {
c.Consistency = consistency
}
}
func WithConnectTimeoutSec(timeout int) Option {
return func(c *cassandraConf) {
c.ConnectTimeoutSec = timeout
}
}
func WithNumConns(numConns int) Option {
return func(c *cassandraConf) {
c.NumConns = numConns
}
}
func WithMaxRetries(maxRetries int) Option {
return func(c *cassandraConf) {
c.MaxRetries = maxRetries
}
}
func WithRetryMin(duration time.Duration) Option {
return func(c *cassandraConf) {
c.RetryMin = duration
}
}
func WithRetryMax(duration time.Duration) Option {
return func(c *cassandraConf) {
c.RetryMax = duration
}
}
func WithReconnectInitial(duration time.Duration) Option {
return func(c *cassandraConf) {
c.ReconnectInitial = duration
}
}
func WithReconnectMax(duration time.Duration) Option {
return func(c *cassandraConf) {
c.ReconnectMax = duration
}
}
func WithCQLVersion(version string) Option {
return func(c *cassandraConf) {
c.CQLVersion = version
}
}
// ===============================================================
// QueryOption defines a function that modifies a query builder
type QueryOption func(*qb.SelectBuilder, qb.M)
// WithWhere adds WHERE clauses to the query
func WithWhere(where []qb.Cmp, args map[string]any) QueryOption {
return func(b *qb.SelectBuilder, bind qb.M) {
if len(where) > 0 {
b = b.Where(where...)
for k, v := range args {
bind[k] = v
}
}
}
}