func NewConfigFromDSN(dsn string) (config Config, err error) {
	parsed, parseErr := ParseURL(dsn)
	if parseErr != nil {
		err = ex.New(parseErr)
		return
	}

	pieces := stringutil.SplitSpace(parsed)
	for _, piece := range pieces {
		if strings.HasPrefix(piece, "host=") {
			config.Host = strings.TrimPrefix(piece, "host=")
		} else if strings.HasPrefix(piece, "port=") {
			config.Port = strings.TrimPrefix(piece, "port=")
		} else if strings.HasPrefix(piece, "dbname=") {
			config.Database = strings.TrimPrefix(piece, "dbname=")
		} else if strings.HasPrefix(piece, "user=") {
			config.Username = strings.TrimPrefix(piece, "user=")
		} else if strings.HasPrefix(piece, "password=") {
			config.Password = strings.TrimPrefix(piece, "password=")
		} else if strings.HasPrefix(piece, "sslmode=") {
			config.SSLMode = strings.TrimPrefix(piece, "sslmode=")
		} else if strings.HasPrefix(piece, "search_path=") {
			config.Schema = strings.TrimPrefix(piece, "search_path=")
		} else if strings.HasPrefix(piece, "application_name=") {
			config.ApplicationName = strings.TrimPrefix(piece, "application_name=")
		} else if strings.HasPrefix(piece, "connect_timeout=") {
			timeout, parseErr := strconv.Atoi(strings.TrimPrefix(piece, "connect_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: connect_timeout"))
				return
			}
			config.ConnectTimeout = time.Second * time.Duration(timeout)
		} else if strings.HasPrefix(piece, "lock_timeout=") {
			config.LockTimeout, parseErr = time.ParseDuration(strings.TrimPrefix(piece, "lock_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: lock_timeout"))
				return
			}
		} else if strings.HasPrefix(piece, "statement_timeout=") {
			config.StatementTimeout, parseErr = time.ParseDuration(strings.TrimPrefix(piece, "statement_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: statement_timeout"))
				return
			}
		}
	}

	return
}
// NewConfigFromDSN creates a new config from a DSN.
// Errors can be produced by parsing the DSN.
NewConfigFromDSN creates a new config from a DSN.
Errors can be produced by parsing the DSN.
// NewConfigFromDSN creates a new config from a DSN . // Errors can be produced by parsing the DSN .
func NewConfigFromDSN(dsn string) (config Config, err error) {
	parsed, parseErr := ParseURL(dsn)
	if parseErr != nil {
		err = ex.New(parseErr)
		return
	}
	pieces := stringutil.SplitSpace(parsed)
	for _, piece := range pieces {
		if strings.HasPrefix(piece, "host=") {
			config.Host = strings.TrimPrefix(piece, "host=")
		} else if strings.HasPrefix(piece, "port=") {
			config.Port = strings.TrimPrefix(piece, "port=")
		} else if strings.HasPrefix(piece, "dbname=") {
			config.Database = strings.TrimPrefix(piece, "dbname=")
		} else if strings.HasPrefix(piece, "user=") {
			config.Username = strings.TrimPrefix(piece, "user=")
		} else if strings.HasPrefix(piece, "password=") {
			config.Password = strings.TrimPrefix(piece, "password=")
		} else if strings.HasPrefix(piece, "sslmode=") {
			config.SSLMode = strings.TrimPrefix(piece, "sslmode=")
		} else if strings.HasPrefix(piece, "search_path=") {
			config.Schema = strings.TrimPrefix(piece, "search_path=")
		} else if strings.HasPrefix(piece, "application_name=") {
			config.ApplicationName = strings.TrimPrefix(piece, "application_name=")
		} else if strings.HasPrefix(piece, "connect_timeout=") {
			timeout, parseErr := strconv.Atoi(strings.TrimPrefix(piece, "connect_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: connect_timeout"))
				return
			}
			config.ConnectTimeout = time.Second * time.Duration(timeout)
		} else if strings.HasPrefix(piece, "lock_timeout=") {
			config.LockTimeout, parseErr = time.ParseDuration(strings.TrimPrefix(piece, "lock_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: lock_timeout"))
				return
			}
		} else if strings.HasPrefix(piece, "statement_timeout=") {
			config.StatementTimeout, parseErr = time.ParseDuration(strings.TrimPrefix(piece, "statement_timeout="))
			if parseErr != nil {
				err = ex.New(parseErr, ex.OptMessage("field: statement_timeout"))
				return
			}
		}
	}
	return
}
func NewConfigFromDSN ( dsn string ) ( config Config , err error ) { parsed , parseErr := ParseURL ( dsn ) if parseErr != nil { err = ex . New ( parseErr ) return } pieces := stringutil . SplitSpace ( parsed ) for _ , piece := range pieces { if strings . HasPrefix ( piece , " " ) { config . Host = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . Port = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . Database = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . Username = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . Password = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . SSLMode = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . Schema = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { config . ApplicationName = strings . TrimPrefix ( piece , " " ) } else if strings . HasPrefix ( piece , " " ) { timeout , parseErr := strconv . Atoi ( strings . TrimPrefix ( piece , " " ) ) if parseErr != nil { err = ex . New ( parseErr , ex . OptMessage ( " " ) ) return } config . ConnectTimeout = time . Second * time . Duration ( timeout ) } else if strings . HasPrefix ( piece , " " ) { config . LockTimeout , parseErr = time . ParseDuration ( strings . TrimPrefix ( piece , " " ) ) if parseErr != nil { err = ex . New ( parseErr , ex . OptMessage ( " " ) ) return } } else if strings . HasPrefix ( piece , " " ) { config . StatementTimeout , parseErr = time . ParseDuration ( strings . TrimPrefix ( piece , " " ) ) if parseErr != nil { err = ex . New ( parseErr , ex . OptMessage ( " " ) ) return } } } return }
NewConfigFromDSN creates a new config from a DSN.
NewConfigFromDSN creates a new config from a DSN .