1
0
Fork 0

MySQL storage configuration

This commit is contained in:
Gregory Eremin 2015-01-10 20:48:16 +07:00
parent 259043573c
commit 307d096865
1 changed files with 26 additions and 3 deletions

View File

@ -25,15 +25,16 @@ const (
loadStateQuery = "select repo, sha1, ts from state"
)
func New() *MysqlStorage {
func New(host, user, pass, db string) *MysqlStorage {
var (
s = &MysqlStorage{
state: map[string]*steward.State{},
}
err error
err error
databaseURI = makeDatabaseURI(host, user, pass, db)
)
if s.db, err = sql.Open("mysql", "root@/steward?parseTime=true"); err != nil {
if s.db, err = sql.Open("mysql", databaseURI); err != nil {
panic(err)
}
if s.importStmt, err = s.db.Prepare(importQuery); err != nil {
@ -94,3 +95,25 @@ func (ms *MysqlStorage) loadGlobalState() {
}
}
}
func makeDatabaseURI(host, user, pass, db string) string {
var (
databaseURI string
)
if user != "" {
databaseURI += user
}
if pass != "" {
databaseURI += ":" + pass
}
if user != "" {
databaseURI += "@"
}
if host != "" {
databaseURI += host
}
databaseURI += "/" + db + "?parseTime=true"
return databaseURI
}