github.com/nakagami/firebirdsql@v0.9.10/dsn.go (about) 1 /******************************************************************************* 2 The MIT License (MIT) 3 4 Copyright (c) 2013-2020 Hajime Nakagami 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the "Software"), to deal in 8 the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 the Software, and to permit persons to whom the Software is furnished to do so, 11 subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 *******************************************************************************/ 23 24 package firebirdsql 25 26 import ( 27 "errors" 28 "net/url" 29 "strings" 30 ) 31 32 type firebirdDsn struct { 33 addr string 34 dbName string 35 user string 36 passwd string 37 options map[string]string 38 } 39 40 var ErrDsnUserUnknown = errors.New("User unknown") 41 42 func newFirebirdDsn() *firebirdDsn { 43 return &firebirdDsn{options: make(map[string]string)} 44 } 45 46 func parseDSN(dsns string) (*firebirdDsn, error) { 47 48 dsn := newFirebirdDsn() 49 50 if !strings.HasPrefix(dsns, "firebird://") { 51 dsns = "firebird://" + dsns 52 } 53 u, err := url.Parse(dsns) 54 if err != nil { 55 return nil, err 56 } 57 if u.User == nil { 58 return nil, ErrDsnUserUnknown 59 } 60 dsn.user = u.User.Username() 61 dsn.passwd, _ = u.User.Password() 62 dsn.addr = u.Host 63 if !strings.ContainsRune(dsn.addr, ':') { 64 dsn.addr += ":3050" 65 } 66 dsn.dbName = u.Path 67 if !strings.ContainsRune(dsn.dbName[1:], '/') { 68 dsn.dbName = dsn.dbName[1:] 69 } 70 71 //Windows Path 72 if strings.ContainsRune(dsn.dbName[2:], ':') { 73 dsn.dbName = dsn.dbName[1:] 74 } 75 76 m, _ := url.ParseQuery(u.RawQuery) 77 78 var default_options = map[string]string{ 79 "auth_plugin_name": "Srp256", 80 "charset": "UTF8", 81 "column_name_to_lower": "false", 82 "role": "", 83 "timezone": "", 84 "wire_crypt": "true", 85 } 86 87 for k, v := range default_options { 88 values, ok := m[k] 89 if ok { 90 dsn.options[k] = values[0] 91 } else { 92 dsn.options[k] = v 93 } 94 } 95 96 return dsn, nil 97 }