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