github.com/blend/go-sdk@v1.20220411.3/db/option.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package db 9 10 import ( 11 "context" 12 "database/sql" 13 14 "github.com/blend/go-sdk/logger" 15 ) 16 17 // Option is an option for database connections. 18 type Option func(c *Connection) error 19 20 // OptConnection sets the underlying driver connection. 21 func OptConnection(conn *sql.DB) Option { 22 return func(c *Connection) error { 23 c.Connection = conn 24 return nil 25 } 26 } 27 28 // OptLog sets the tracer on the connection. 29 func OptLog(log logger.Log) Option { 30 return func(c *Connection) error { 31 c.Log = log 32 return nil 33 } 34 } 35 36 // OptTracer sets the tracer on the connection. 37 func OptTracer(tracer Tracer) Option { 38 return func(c *Connection) error { 39 c.Tracer = tracer 40 return nil 41 } 42 } 43 44 // OptStatementInterceptor sets the statement interceptor on the connection. 45 func OptStatementInterceptor(interceptor StatementInterceptor) Option { 46 return func(c *Connection) error { 47 c.StatementInterceptor = interceptor 48 return nil 49 } 50 } 51 52 // OptConfig sets the config on a connection. 53 func OptConfig(cfg Config) Option { 54 return func(c *Connection) error { 55 c.Config = cfg 56 return nil 57 } 58 } 59 60 // OptConfigFromEnv sets the config on a connection from the environment. 61 func OptConfigFromEnv() Option { 62 return func(c *Connection) error { 63 return (&c.Config).Resolve(context.Background()) 64 } 65 } 66 67 // OptEngine sets the connection engine. 68 // You must have this engine registered with database/sql. 69 func OptEngine(engine string) Option { 70 return func(c *Connection) error { 71 c.Config.Engine = engine 72 return nil 73 } 74 } 75 76 // OptHost sets the connection host. 77 func OptHost(host string) Option { 78 return func(c *Connection) error { 79 c.Config.Host = host 80 return nil 81 } 82 } 83 84 // OptPort sets the connection port. 85 func OptPort(port string) Option { 86 return func(c *Connection) error { 87 c.Config.Port = port 88 return nil 89 } 90 } 91 92 // OptDatabase sets the connection database. 93 func OptDatabase(database string) Option { 94 return func(c *Connection) error { 95 c.Config.Database = database 96 return nil 97 } 98 } 99 100 // OptUsername sets the connection ssl mode. 101 func OptUsername(username string) Option { 102 return func(c *Connection) error { 103 c.Config.Username = username 104 return nil 105 } 106 } 107 108 // OptPassword sets the connection ssl mode. 109 func OptPassword(password string) Option { 110 return func(c *Connection) error { 111 c.Config.Password = password 112 return nil 113 } 114 } 115 116 // OptSchema sets the connection schema path. 117 func OptSchema(schema string) Option { 118 return func(c *Connection) error { 119 c.Config.Schema = schema 120 return nil 121 } 122 } 123 124 // OptSSLMode sets the connection ssl mode. 125 func OptSSLMode(mode string) Option { 126 return func(c *Connection) error { 127 c.Config.SSLMode = mode 128 return nil 129 } 130 } 131 132 // OptDialect sets the connection dialect. 133 func OptDialect(dialect Dialect) Option { 134 return func(c *Connection) error { 135 c.Config.Dialect = string(dialect) 136 return nil 137 } 138 }