-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ricardo-ch/add-optimization
added changes for optimal way to set advanced settings
- Loading branch information
Showing
5 changed files
with
23 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters