2 Commits c78f9df181 ... 5f80592833

Auteur SHA1 Message Date
  Dima 5f80592833 Merge branch 'master' of git.dmitriygnatenko.ru:dima/go-common il y a 3 jours
  Dima 9e7f50672c Update DB il y a 3 jours
2 fichiers modifiés avec 18 ajouts et 20 suppressions
  1. 15 9
      db/db.go
  2. 3 11
      db/db_config.go

+ 15 - 9
db/db.go

@@ -41,17 +41,23 @@ func NewDB(c Config) (*DB, error) {
 		c.port = defaultPort
 	}
 
-	if len(c.sslMode) == 0 {
-		c.sslMode = defaultSslMode
+	var source string
+
+	switch c.driver {
+	case "mysql":
+		source = c.username +
+			":" + c.password +
+			"@tcp(" + c.host + ":" + strconv.Itoa(int(c.port)) + ")/" +
+			c.dbname +
+			"?parseTime=true"
+	case "postgres":
+		source = "user=" + c.username +
+			" password=" + c.password +
+			" dbname=/" + c.dbname +
+			" host=" + c.host +
+			" port=" + strconv.Itoa(int(c.port))
 	}
 
-	source := "user=" + c.username +
-		" password=" + c.password +
-		" dbname=" + c.dbname +
-		" host=" + c.host +
-		" port=" + strconv.Itoa(int(c.port)) +
-		" sslmode=" + c.sslMode
-
 	sqlConn, err := sql.Open(c.driver, source)
 	if err != nil {
 		return nil, fmt.Errorf("open DB connection error: %w", err)

+ 3 - 11
db/db_config.go

@@ -5,10 +5,9 @@ import (
 )
 
 const (
-	defaultDriver  = "mysql"
-	defaultHost    = "localhost"
-	defaultPort    = 3306
-	defaultSslMode = "disable"
+	defaultDriver = "mysql"
+	defaultHost   = "localhost"
+	defaultPort   = 3306
 )
 
 type Config struct {
@@ -19,7 +18,6 @@ type Config struct {
 	dbname   string
 	host     string
 	port     uint16
-	sslMode  string
 
 	maxOpenConns uint16
 	maxIdleConns uint16
@@ -81,12 +79,6 @@ func WithPort(port uint16) ConfigOption {
 	}
 }
 
-func WithSSLMode(sslMode string) ConfigOption {
-	return func(s *Config) {
-		s.sslMode = sslMode
-	}
-}
-
 func WithMaxOpenConns(maxOpenConns uint16) ConfigOption {
 	return func(s *Config) {
 		s.maxOpenConns = maxOpenConns