|
| 1 | +// Copy from: https://github.com/lib/pq//conn.go |
| 2 | + |
| 3 | +package mssqlx |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "unicode" |
| 8 | + |
| 9 | + "github.com/go-sql-driver/mysql" |
| 10 | +) |
| 11 | + |
| 12 | +// pqScanner implements a tokenizer for libpq-style option strings. |
| 13 | +type pqScanner struct { |
| 14 | + s []rune |
| 15 | + i int |
| 16 | +} |
| 17 | + |
| 18 | +// newPQScanner returns a new scanner initialized with the option string s. |
| 19 | +func newPQScanner(s string) *pqScanner { |
| 20 | + return &pqScanner{[]rune(s), 0} |
| 21 | +} |
| 22 | + |
| 23 | +// Next returns the next rune. |
| 24 | +// It returns 0, false if the end of the text has been reached. |
| 25 | +func (s *pqScanner) Next() (rune, bool) { |
| 26 | + if s.i >= len(s.s) { |
| 27 | + return 0, false |
| 28 | + } |
| 29 | + r := s.s[s.i] |
| 30 | + s.i++ |
| 31 | + return r, true |
| 32 | +} |
| 33 | + |
| 34 | +// SkipSpaces returns the next non-whitespace rune. |
| 35 | +// It returns 0, false if the end of the text has been reached. |
| 36 | +func (s *pqScanner) SkipSpaces() (rune, bool) { |
| 37 | + r, ok := s.Next() |
| 38 | + for unicode.IsSpace(r) && ok { |
| 39 | + r, ok = s.Next() |
| 40 | + } |
| 41 | + return r, ok |
| 42 | +} |
| 43 | + |
| 44 | +// parses the postgres-options from name and adds them to the values. |
| 45 | +// |
| 46 | +// The parsing code is based on conninfo_parse from libpq's fe-connect.c |
| 47 | +func parsePQOpts(name string, o map[string]string) error { |
| 48 | + s := newPQScanner(name) |
| 49 | + |
| 50 | + for { |
| 51 | + var ( |
| 52 | + keyRunes, valRunes []rune |
| 53 | + r rune |
| 54 | + ok bool |
| 55 | + ) |
| 56 | + |
| 57 | + if r, ok = s.SkipSpaces(); !ok { |
| 58 | + break |
| 59 | + } |
| 60 | + |
| 61 | + // Scan the key |
| 62 | + for !unicode.IsSpace(r) && r != '=' { |
| 63 | + keyRunes = append(keyRunes, r) |
| 64 | + if r, ok = s.Next(); !ok { |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Skip any whitespace if we're not at the = yet |
| 70 | + if r != '=' { |
| 71 | + r, ok = s.SkipSpaces() |
| 72 | + } |
| 73 | + |
| 74 | + // The current character should be = |
| 75 | + if r != '=' || !ok { |
| 76 | + return fmt.Errorf(`missing "=" after %q in connection info string"`, string(keyRunes)) |
| 77 | + } |
| 78 | + |
| 79 | + // Skip any whitespace after the = |
| 80 | + if r, ok = s.SkipSpaces(); !ok { |
| 81 | + // If we reach the end here, the last value is just an empty string as per libpq. |
| 82 | + o[string(keyRunes)] = "" |
| 83 | + break |
| 84 | + } |
| 85 | + |
| 86 | + if r != '\'' { |
| 87 | + for !unicode.IsSpace(r) { |
| 88 | + if r == '\\' { |
| 89 | + if r, ok = s.Next(); !ok { |
| 90 | + return fmt.Errorf(`missing character after backslash`) |
| 91 | + } |
| 92 | + } |
| 93 | + valRunes = append(valRunes, r) |
| 94 | + |
| 95 | + if r, ok = s.Next(); !ok { |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + quote: |
| 101 | + for { |
| 102 | + if r, ok = s.Next(); !ok { |
| 103 | + return fmt.Errorf(`unterminated quoted string literal in connection string`) |
| 104 | + } |
| 105 | + switch r { |
| 106 | + case '\'': |
| 107 | + break quote |
| 108 | + case '\\': |
| 109 | + r, _ = s.Next() |
| 110 | + fallthrough |
| 111 | + default: |
| 112 | + valRunes = append(valRunes, r) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + o[string(keyRunes)] = string(valRunes) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// converts a url to a connection string for driver.Open. |
| 124 | +// Example: |
| 125 | +// |
| 126 | +// "postgres://bob:[email protected]:5432/mydb?sslmode=verify-full" |
| 127 | +// |
| 128 | +// converts to: |
| 129 | +// |
| 130 | +// "user=bob password=secret host=1.2.3.4 port=5432 dbname=mydb sslmode=verify-full" |
| 131 | +// |
| 132 | +// A minimal example: |
| 133 | +// |
| 134 | +// "postgres://" |
| 135 | +// |
| 136 | +// This will be blank, causing driver.Open to use all of the defaults |
| 137 | +func parsePostgresDSN(dsn string) (string, error) { |
| 138 | + meta := make(map[string]string) |
| 139 | + |
| 140 | + if err := parsePQOpts(dsn, meta); err != nil { |
| 141 | + return "", err |
| 142 | + } |
| 143 | + |
| 144 | + host, port := meta["host"], meta["port"] |
| 145 | + if host == "" && port == "" { |
| 146 | + return "", nil |
| 147 | + } |
| 148 | + |
| 149 | + return fmt.Sprintf("%s:%s", host, port), nil |
| 150 | +} |
| 151 | + |
| 152 | +func hostnameFromDSN(driverName, dsn string) string { |
| 153 | + switch driverName { |
| 154 | + case "mysql": |
| 155 | + if cf, err := mysql.ParseDSN(dsn); err == nil { |
| 156 | + return fmt.Sprintf("%s(%s)", cf.Net, cf.Addr) |
| 157 | + } |
| 158 | + |
| 159 | + case "postgres": |
| 160 | + if host, err := parsePostgresDSN(dsn); err == nil { |
| 161 | + return host |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return "" |
| 166 | +} |
0 commit comments