MCPcopy Index your code
hub / github.com/apache/answer / GetConnection

Method GetConnection

internal/install/install_req.go:58–91  ·  view source on GitHub ↗

GetConnection get connection string

()

Source from the content-addressed store, hash-verified

56
57// GetConnection get connection string
58func (r *CheckDatabaseReq) GetConnection() string {
59 if r.DbType == string(schemas.SQLITE) {
60 return r.DbFile
61 }
62 if r.DbType == string(schemas.MYSQL) {
63 return fmt.Sprintf("%s:%s@tcp(%s)/%s",
64 r.DbUsername, r.DbPassword, r.DbHost, r.DbName)
65 }
66 if r.DbType == string(schemas.POSTGRES) {
67 host, port := parsePgSQLHostPort(r.DbHost)
68 switch {
69 case !r.Ssl:
70 return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
71 host, port, r.DbUsername, r.DbPassword, r.DbName)
72 case r.SslMode == "require":
73 return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
74 host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode)
75 case r.SslMode == "verify-ca" || r.SslMode == "verify-full":
76 connection := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
77 host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode)
78 if len(r.SslRootCert) > 0 && dir.CheckFileExist(r.SslRootCert) {
79 connection += fmt.Sprintf(" sslrootcert=%s", r.SslRootCert)
80 }
81 if len(r.SslCert) > 0 && dir.CheckFileExist(r.SslCert) {
82 connection += fmt.Sprintf(" sslcert=%s", r.SslCert)
83 }
84 if len(r.SslKey) > 0 && dir.CheckFileExist(r.SslKey) {
85 connection += fmt.Sprintf(" sslkey=%s", r.SslKey)
86 }
87 return connection
88 }
89 }
90 return ""
91}
92
93func parsePgSQLHostPort(dbHost string) (host string, port string) {
94 if strings.Contains(dbHost, ":") {

Callers 2

CheckDatabaseFunction · 0.95
InitEnvironmentFunction · 0.95

Calls 2

CheckFileExistFunction · 0.92
parsePgSQLHostPortFunction · 0.85

Tested by

no test coverage detected