Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Oct 22, 2021
1 parent ba87bc7 commit 3ec8c7c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/innotechdevops/pq-driver
go 1.15

require (
github.com/jmoiron/sqlx v1.2.0 // indirect
github.com/lib/pq v1.8.0 // indirect
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.8.0
google.golang.org/appengine v1.6.7 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
19 changes: 13 additions & 6 deletions pkg/pqdriver/postgresql_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type postgresDB struct {
}

func (db *postgresDB) Connect() *sqlx.DB {
if db.Conf.SSLMode == "" {
db.Conf.SSLMode = SSLModeDisable
}
dsName := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
db.Conf.Host, db.Conf.Port, db.Conf.User, db.Conf.Pass, db.Conf.DatabaseName, db.Conf.SSLMode)
conn, err := sqlx.Connect("postgres", dsName)
Expand All @@ -51,9 +54,9 @@ func (db *postgresDB) Connect() *sqlx.DB {
conn.SetMaxOpenConns(maxOpenConns) // The default is 0 (unlimited), ex: 1000
}
if maxIdleConns > 0 {
conn.SetMaxIdleConns(maxIdleConns) // defaultMaxIdleConns = 2, ex: 10
conn.SetMaxIdleConns(maxIdleConns) // The default maxIdleConns = 2, ex: 10
}
conn.SetConnMaxLifetime(time.Duration(maxLifetime)) // 0, Connections are reused forever
conn.SetConnMaxLifetime(time.Duration(maxLifetime)) // MaxLifetime = 0, Connections are reused forever
if err != nil {
log.Fatalln(err)
} else {
Expand All @@ -77,9 +80,13 @@ func ConfigEnv() Config {
Host: os.Getenv("POSTGRES_HOST"),
DatabaseName: os.Getenv("POSTGRES_DATABASE"),
Port: os.Getenv("POSTGRES_PORT"),
SSLMode: os.Getenv("POSTGRES_SSL_MODE"),
MaxLifetime: os.Getenv("MARIA_MAX_LIFETIME"),
MaxIdleConns: os.Getenv("MARIA_MAX_IDLE_CONNS"),
MaxOpenConns: os.Getenv("MARIA_MAX_OPEN_CONNS"),
// The default SSL mode is "disable", ex: "verify-full"
SSLMode: os.Getenv("POSTGRES_SSL_MODE"),
// The default maxLifetime = 0, Connections are reused forever, ex: "60"
MaxLifetime: os.Getenv("POSTGRES_MAX_LIFETIME"),
// The default maxIdleConns = 2, ex: 10
MaxIdleConns: os.Getenv("POSTGRES_MAX_IDLE_CONNS"),
// The default is 0 (unlimited), ex: 1000
MaxOpenConns: os.Getenv("POSTGRES_MAX_OPEN_CONNS"),
}
}

0 comments on commit 3ec8c7c

Please sign in to comment.