package cassandra import ( "github.com/scylladb/gocqlx/v3/qb" "time" "github.com/gocql/gocql" ) // Option 是設定選項的函數型別 type Option func(*conf) func WithPort(port int) Option { return func(c *conf) { c.Port = port } } func WithKeyspace(keyspace string) Option { return func(c *conf) { c.Keyspace = keyspace } } func WithAuth(username, password string) Option { return func(c *conf) { c.Username = username c.Password = password c.UseAuth = true } } func WithConsistency(consistency gocql.Consistency) Option { return func(c *conf) { c.Consistency = consistency } } func WithConnectTimeoutSec(timeout int) Option { return func(c *conf) { c.ConnectTimeoutSec = timeout } } func WithNumConnects(numConnects int) Option { return func(c *conf) { c.NumConnect = numConnects } } func WithMaxRetries(maxRetries int) Option { return func(c *conf) { c.MaxRetries = maxRetries } } func WithRetryMin(duration time.Duration) Option { return func(c *conf) { c.RetryMin = duration } } func WithRetryMax(duration time.Duration) Option { return func(c *conf) { c.RetryMax = duration } } func WithReconnectInitial(duration time.Duration) Option { return func(c *conf) { c.ReconnectInitial = duration } } func WithReconnectMax(duration time.Duration) Option { return func(c *conf) { c.ReconnectMax = duration } } func WithCQLVersion(version string) Option { return func(c *conf) { 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 } } } }