Skip to content

Commit abf8319

Browse files
author
c9845 (AA)
committed
Add func to handle adding key-valu apirs to ConnectionOptions that makes code implementing ConnectionOptions cleaner versus having to call map[string]string{}.
1 parent dc94de0 commit abf8319

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

sqldb-mssql.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func NewMSSQLConfig(host string, port uint, name, user, password string) (cfg *C
1515
cfg.User = user
1616
cfg.Password = password
1717

18+
cfg.ConnectionOptions = make(map[string]string)
19+
1820
return
1921
}
2022

sqldb-mysql.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func NewMySQLConfig(host string, port uint, name, user, password string) (cfg *C
1515
cfg.User = user
1616
cfg.Password = password
1717

18+
cfg.ConnectionOptions = make(map[string]string)
19+
1820
return
1921
}
2022

sqldb-sqlite.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func NewSQLiteConfig(pathToFile string) (cfg *Config) {
4444
TFMySQLToSQLiteReformatDatetime,
4545
}
4646

47+
cfg.ConnectionOptions = make(map[string]string)
48+
4749
return
4850
}
4951

sqldb.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ type Config struct {
128128

129129
//ConnectionOptions is a list of key-value pairs of options used when building
130130
//the connection string used to connect to a database. Each driver/database type
131-
//will handle these differently.
131+
//will handle these differently. Use AddConnectionOption() instead of having to
132+
//do Config.ConnectionOptions = map[string]string{"key", "value"}.
132133
ConnectionOptions map[string]string
133134

134135
//SQLitePath is the path where the SQLite database file is located.
@@ -641,3 +642,22 @@ func (cfg *Config) debugPrintln(v ...any) {
641642
log.Println(v...)
642643
}
643644
}
645+
646+
// AddConnectionOption adds a key-value pair to a config's ConnnectionOptions field.
647+
// Using this func is just easier then calling map[string]string{"key", "value"}. This
648+
// does not check if the key already exist, it will simply add a duplicate key-value
649+
// pair.
650+
func (cfg *Config) AddConnectionOption(key, value string) {
651+
//Initialize map if needed.
652+
if cfg.ConnectionOptions == nil {
653+
cfg.ConnectionOptions = make(map[string]string)
654+
}
655+
656+
cfg.ConnectionOptions[key] = value
657+
}
658+
659+
// AddConnectionOption adds a key-value pair to the ConnectionOptions field for the
660+
// package level config.
661+
func AddConnectionOption(key, value string) {
662+
config.AddConnectionOption(key, value)
663+
}

sqldb_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,3 +1024,24 @@ func TestSQLitePragmasAsString(t *testing.T) {
10241024
}
10251025
}
10261026
}
1027+
1028+
func TestAddConnectionOption(t *testing.T) {
1029+
//Get config to work off of.
1030+
c := NewSQLiteConfig(InMemoryFilePathRaceSafe)
1031+
1032+
//Add option.
1033+
k := "key"
1034+
v := "value"
1035+
c.AddConnectionOption(k, v)
1036+
1037+
//Make sure connection option was saved.
1038+
vFound, ok := c.ConnectionOptions[k]
1039+
if !ok {
1040+
t.Fatal("Could not find key in connection options.")
1041+
return
1042+
}
1043+
if vFound != v {
1044+
t.Fatal("Connection option value mismatch.", vFound, v)
1045+
return
1046+
}
1047+
}

0 commit comments

Comments
 (0)