FormatDSN formats the given Config into a DSN string which can be passed to the driver. Note: use [NewConnector] and [database/sql.OpenDB] to open a connection from a [*Config].
()
| 251 | // |
| 252 | // Note: use [NewConnector] and [database/sql.OpenDB] to open a connection from a [*Config]. |
| 253 | func (cfg *Config) FormatDSN() string { |
| 254 | var buf bytes.Buffer |
| 255 | |
| 256 | // [username[:password]@] |
| 257 | if len(cfg.User) > 0 { |
| 258 | buf.WriteString(cfg.User) |
| 259 | if len(cfg.Passwd) > 0 { |
| 260 | buf.WriteByte(':') |
| 261 | buf.WriteString(cfg.Passwd) |
| 262 | } |
| 263 | buf.WriteByte('@') |
| 264 | } |
| 265 | |
| 266 | // [protocol[(address)]] |
| 267 | if len(cfg.Net) > 0 { |
| 268 | buf.WriteString(cfg.Net) |
| 269 | if len(cfg.Addr) > 0 { |
| 270 | buf.WriteByte('(') |
| 271 | buf.WriteString(cfg.Addr) |
| 272 | buf.WriteByte(')') |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // /dbname |
| 277 | buf.WriteByte('/') |
| 278 | buf.WriteString(url.PathEscape(cfg.DBName)) |
| 279 | |
| 280 | // [?param1=value1&...¶mN=valueN] |
| 281 | hasParam := false |
| 282 | |
| 283 | if cfg.AllowAllFiles { |
| 284 | hasParam = true |
| 285 | buf.WriteString("?allowAllFiles=true") |
| 286 | } |
| 287 | |
| 288 | if cfg.AllowCleartextPasswords { |
| 289 | writeDSNParam(&buf, &hasParam, "allowCleartextPasswords", "true") |
| 290 | } |
| 291 | |
| 292 | if cfg.AllowFallbackToPlaintext { |
| 293 | writeDSNParam(&buf, &hasParam, "allowFallbackToPlaintext", "true") |
| 294 | } |
| 295 | |
| 296 | if !cfg.AllowNativePasswords { |
| 297 | writeDSNParam(&buf, &hasParam, "allowNativePasswords", "false") |
| 298 | } |
| 299 | |
| 300 | if cfg.AllowOldPasswords { |
| 301 | writeDSNParam(&buf, &hasParam, "allowOldPasswords", "true") |
| 302 | } |
| 303 | |
| 304 | if !cfg.CheckConnLiveness { |
| 305 | writeDSNParam(&buf, &hasParam, "checkConnLiveness", "false") |
| 306 | } |
| 307 | |
| 308 | if cfg.ClientFoundRows { |
| 309 | writeDSNParam(&buf, &hasParam, "clientFoundRows", "true") |
| 310 | } |