MCPcopy
hub / github.com/go-sql-driver/mysql / ParseDSN

Function ParseDSN

dsn.go:396–474  ·  dsn.go::ParseDSN

ParseDSN parses the DSN string to a Config

(dsn string)

Source from the content-addressed store, hash-verified

394
395// ParseDSN parses the DSN string to a Config
396func ParseDSN(dsn string) (cfg *Config, err error) {
397 // New config with some default values
398 cfg = NewConfig()
399
400 // [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]
401 // Find the last '/' (since the password or the net addr might contain a '/')
402 foundSlash := false
403 for i := len(dsn) - 1; i >= 0; i-- {
404 if dsn[i] == '/' {
405 foundSlash = true
406 var j, k int
407
408 // left part is empty if i <= 0
409 if i > 0 {
410 // [username[:password]@][protocol[(address)]]
411 // Find the last '@' in dsn[:i]
412 for j = i; j >= 0; j-- {
413 if dsn[j] == '@' {
414 // username[:password]
415 // Find the first ':' in dsn[:j]
416 for k = 0; k < j; k++ { // We cannot use k = range j here, because we use dsn[:k] below
417 if dsn[k] == ':' {
418 cfg.Passwd = dsn[k+1 : j]
419 break
420 }
421 }
422 cfg.User = dsn[:k]
423
424 break
425 }
426 }
427
428 // [protocol[(address)]]
429 // Find the first '(' in dsn[j+1:i]
430 for k = j + 1; k < i; k++ {
431 if dsn[k] == '(' {
432 // dsn[i-1] must be == ')' if an address is specified
433 if dsn[i-1] != ')' {
434 if strings.ContainsRune(dsn[k+1:i], ')') {
435 return nil, errInvalidDSNUnescaped
436 }
437 return nil, errInvalidDSNAddr
438 }
439 cfg.Addr = dsn[k+1 : i-1]
440 break
441 }
442 }
443 cfg.Net = dsn[j+1 : k]
444 }
445
446 // dbname[?param1=value1&...&paramN=valueN]
447 // Find the first '?' in dsn[i+1:]
448 for j = i + 1; j < len(dsn); j++ {
449 if dsn[j] == '?' {
450 if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
451 return
452 }
453 break

Callers 15

OpenMethod · 0.85
OpenConnectorMethod · 0.85
TestDSNParserFunction · 0.85
TestDSNParserInvalidFunction · 0.85
TestDSNReformatFunction · 0.85
TestDSNServerPubKeyFunction · 0.85
TestDSNWithCustomTLSFunction · 0.85
TestDSNTLSConfigFunction · 0.85
TestDSNUnsafeCollationFunction · 0.85
TestCloneConfigFunction · 0.85

Calls 3

NewConfigFunction · 0.85
parseDSNParamsFunction · 0.85
normalizeMethod · 0.80

Tested by 15

TestDSNParserFunction · 0.68
TestDSNParserInvalidFunction · 0.68
TestDSNReformatFunction · 0.68
TestDSNServerPubKeyFunction · 0.68
TestDSNWithCustomTLSFunction · 0.68
TestDSNTLSConfigFunction · 0.68
TestDSNUnsafeCollationFunction · 0.68
TestCloneConfigFunction · 0.68
BenchmarkParseDSNFunction · 0.68