Skip to content

Commit

Permalink
Merge pull request #4 from ricardo-ch/add-optimization
Browse files Browse the repository at this point in the history
added changes for optimal way to set advanced settings
  • Loading branch information
andriylesch authored Oct 2, 2017
2 parents cfa9381 + 8493bc8 commit 8b218cb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Below is an example you can find MsSql implementation.
// you need to import sql driver
import _ "github.com/denisenkom/go-mssqldb"

// connect to MsSql Database
// set your credentials for MsSql Database
msSqlConf := dbprovider.MsSqlConfig{
Database: "YOUR_DBNAME",
Port: "YOUR_PORT",
Expand All @@ -25,7 +25,13 @@ msSqlConf := dbprovider.MsSqlConfig{
Password: "YOUR_PWD",
}

dbase, _ := db.Connect(msSqlConf, 1)
// in case if you need to use default parametes
dbase, _ := db.Connect(msSqlConf, nil)

//in case for additional configuration
// dbase, _ := db.Connect(msSqlConf, func(db *sqlx.DB){
// db.SetMaxIdleConns(5)
// })

// close connection after using
defer func() {
Expand Down
21 changes: 12 additions & 9 deletions database.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package database

import "github.com/jmoiron/sqlx"
import (
"github.com/jmoiron/sqlx"
)

type ConnectionBuilder interface {
DriverName() string
Build() string
}

// var sqlConnect *sqlx.DB
var sqlConnect = sqlx.Connect
var setMaxIdleConns = func(db *sqlx.DB, maxIdleDBConnections int) {
db.SetMaxIdleConns(maxIdleDBConnections)
}

func connect(driverName, connectionString string, maxIdleDBConnections int) (*sqlx.DB, error) {
func connect(driverName, connectionString string, advFunc func(*sqlx.DB)) (*sqlx.DB, error) {
db, err := sqlConnect(driverName, connectionString)

if err != nil {
return nil, err
}

setMaxIdleConns(db, maxIdleDBConnections)
// execute function
if advFunc != nil {
advFunc(db)
}

return db.Unsafe(), nil
}

// Connect to DB
func Connect(builder ConnectionBuilder, maxIdleDBConnections int) (*sqlx.DB, error) {
return connect(builder.DriverName(), builder.Build(), maxIdleDBConnections)
func Connect(builder ConnectionBuilder, advFunc func(*sqlx.DB)) (*sqlx.DB, error) {
return connect(builder.DriverName(), builder.Build(), advFunc)
}
5 changes: 1 addition & 4 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ var connectTests = [2]struct {

func TestConnect(t *testing.T) {

setMaxIdleConns = func(db *sqlx.DB, maxIdleDBConnections int) {
}

var dbHost = "YOUR_HOST"
var dbPort = 0
var dbName = "YOUR_DBNAME"
Expand All @@ -55,7 +52,7 @@ func TestConnect(t *testing.T) {
sqlConnect = test.connect

//Act
_, err := Connect(mssqlConf, -1)
_, err := Connect(mssqlConf, nil)

assert.Equal(t, test.expectedError, err, test.description)
}
Expand Down
3 changes: 1 addition & 2 deletions example/mssql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
Log: &Log,
}

dbase, err := db.Connect(msSqlConf, 1)
dbase, err := db.Connect(msSqlConf, nil)
if err != nil {
fmt.Println(err)
}
Expand All @@ -39,5 +39,4 @@ func main() {
dbase.Close()
}
}()

}
2 changes: 1 addition & 1 deletion example/postgres/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
Password: dbPwd,
}

dbase, err := db.Connect(pqConf, -1)
dbase, err := db.Connect(pqConf, nil)
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 8b218cb

Please sign in to comment.