-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show only database name and maybe host when logging successful db con…
…nection. #174
- Loading branch information
1 parent
4dec431
commit cb25b26
Showing
3 changed files
with
73 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,84 @@ | ||
package sqlstore | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
mysql "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type Config struct { | ||
Driver string `default:"sqlite3"` | ||
url string `default:""` | ||
host string `default:""` | ||
user string `default:""` | ||
password string `default:""` | ||
name string `default:"fathom.db"` | ||
sslmode string `default:""` | ||
URL string `default:""` | ||
Host string `default:""` | ||
User string `default:""` | ||
Password string `default:""` | ||
Name string `default:"fathom.db"` | ||
SSLMode string `default:""` | ||
} | ||
|
||
func (c *Config) DSN() string { | ||
var dsn string | ||
|
||
// if FATHOM_DATABASE_URL was set, use that | ||
// this relies on the user to set the appropriate parameters, eg ?parseTime=true when using MySQL | ||
if c.url != "" { | ||
return c.url | ||
if c.URL != "" { | ||
return c.URL | ||
} | ||
|
||
// otherwise, generate from individual fields | ||
switch c.Driver { | ||
case POSTGRES: | ||
|
||
if c.host != "" { | ||
dsn += " host=" + c.host | ||
if c.Host != "" { | ||
dsn += " host=" + c.Host | ||
} | ||
if c.name != "" { | ||
dsn += " dbname=" + c.name | ||
if c.Name != "" { | ||
dsn += " dbname=" + c.Name | ||
} | ||
if c.user != "" { | ||
dsn += " user=" + c.user | ||
if c.User != "" { | ||
dsn += " user=" + c.User | ||
} | ||
if c.password != "" { | ||
dsn += " password=" + c.password | ||
if c.Password != "" { | ||
dsn += " password=" + c.Password | ||
} | ||
if c.sslmode != "" { | ||
dsn += " sslmode=" + c.sslmode | ||
if c.SSLMode != "" { | ||
dsn += " sslmode=" + c.SSLMode | ||
} | ||
|
||
dsn = strings.TrimSpace(dsn) | ||
case MYSQL: | ||
mc := mysql.NewConfig() | ||
mc.User = c.user | ||
mc.Passwd = c.password | ||
mc.Addr = c.host | ||
mc.User = c.User | ||
mc.Passwd = c.Password | ||
mc.Addr = c.Host | ||
mc.Net = "tcp" | ||
mc.DBName = c.name | ||
mc.DBName = c.Name | ||
mc.Params = map[string]string{ | ||
"parseTime": "true", | ||
"loc": "Local", | ||
} | ||
if c.sslmode != "" { | ||
mc.Params["tls"] = c.sslmode | ||
if c.SSLMode != "" { | ||
mc.Params["tls"] = c.SSLMode | ||
} | ||
dsn = mc.FormatDSN() | ||
case SQLITE: | ||
dsn = c.name + "?_loc=auto&_busy_timeout=5000" | ||
dsn = c.Name + "?_loc=auto&_busy_timeout=5000" | ||
} | ||
|
||
return dsn | ||
} | ||
|
||
// Dbname returns the database name, either from config values or from the connection URL | ||
func (c *Config) Dbname() string { | ||
if c.Name != "" { | ||
return c.Name | ||
} | ||
|
||
re := regexp.MustCompile(`(?:dbname=|[^\/]?\/)(\w+)`) | ||
m := re.FindStringSubmatch(c.URL) | ||
if len(m) > 1 { | ||
return m[1] | ||
} | ||
|
||
return "" | ||
} |
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