func TestConnectionURL(t *testing.T) { c := ConnectionURL{} // Default connection string is only the protocol. if c.String() != "" { t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String()) } // Adding a database name. c.Database = "myfilename" if c.String() != "mongodb://myfilename" { t.Fatal(`Test failed, got:`, c.String()) } // Adding an option. c.Options = map[string]string{ "cache": "foobar", "mode": "ro", } // Adding username and password c.User = "******" c.Password = "******" // Setting host. c.Address = db.Host("localhost") if c.String() != "mongodb://*****:*****@localhost/myfilename?cache=foobar&mode=ro" { t.Fatal(`Test failed, got:`, c.String()) } // Setting host and port. c.Address = db.HostPort("localhost", 27017) if c.String() != "mongodb://*****:*****@localhost:27017/myfilename?cache=foobar&mode=ro" { t.Fatal(`Test failed, got:`, c.String()) } // Setting cluster. c.Address = Cluster(db.Host("localhost"), db.Host("1.2.3.4"), db.HostPort("example.org", 1234)) if c.String() != "mongodb://*****:*****@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" { t.Fatal(`Test failed, got:`, c.String()) } // Setting another database. c.Database = "another_database" if c.String() != "mongodb://*****:*****@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro" { t.Fatal(`Test failed, got:`, c.String()) } }
// ParseURL parses s into a ConnectionURL struct. func ParseURL(s string) (u ConnectionURL, err error) { o := make(values) if strings.HasPrefix(s, "postgres://") { s, err = pq.ParseURL(s) if err != nil { return u, err } } if err := parseOpts(s, o); err != nil { return u, err } u.User = o.Get("user") u.Password = o.Get("password") h := o.Get("host") p, _ := strconv.Atoi(o.Get("port")) if p > 0 { u.Address = db.HostPort(h, uint(p)) } else { u.Address = db.Host(h) } u.Database = o.Get("dbname") u.Options = map[string]string{ "sslmode": o.Get("sslmode"), } return u, err }
func TestConnectionURL(t *testing.T) { c := ConnectionURL{} // Default connection string is empty. if c.String() != "" { t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String()) } // Adding a host. c.Address = db.Host("localhost") if c.String() != "host=localhost sslmode=disable" { t.Fatal(`Test failed, got:`, c.String()) } // Adding a username. c.User = "******" if c.String() != "user=Anakin host=localhost sslmode=disable" { t.Fatal(`Test failed, got:`, c.String()) } // Adding a password with special characters. c.Password = "******" if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost sslmode=disable` { t.Fatal(`Test failed, got:`, c.String()) } // Adding a port. c.Address = db.HostPort("localhost", 1234) if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 sslmode=disable` { t.Fatal(`Test failed, got:`, c.String()) } // Adding a database. c.Database = "MyDatabase" if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 dbname=MyDatabase sslmode=disable` { t.Fatal(`Test failed, got:`, c.String()) } // Adding options. c.Options = map[string]string{ "sslmode": "verify-full", } if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 dbname=MyDatabase sslmode=verify-full` { t.Fatal(`Test failed, got:`, c.String()) } }
func TestConnectionURL(t *testing.T) { c := ConnectionURL{} // Zero value equals to an empty string. if c.String() != "" { t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String()) } // Adding a database name. c.Database = "mydbname" if c.String() != "/mydbname?charset=utf8" { t.Fatal(`Test failed, got:`, c.String()) } // Adding an option. c.Options = map[string]string{ "charset": "utf8mb4,utf8", "sys_var": "esc@ped", } if c.String() != "/mydbname?charset=utf8mb4%2Cutf8&sys_var=esc%40ped" { t.Fatal(`Test failed, got:`, c.String()) } // Setting default options c.Options = nil // Setting user and password. c.User = "******" c.Password = "******" if c.String() != `user:pass@/mydbname?charset=utf8` { t.Fatal(`Test failed, got:`, c.String()) } // Setting host. c.Address = db.HostPort("1.2.3.4", 3306) if c.String() != `user:pass@tcp(1.2.3.4:3306)/mydbname?charset=utf8` { t.Fatal(`Test failed, got:`, c.String()) } // Setting socket. c.Address = db.Socket("/path/to/socket") if c.String() != `user:pass@unix(/path/to/socket)/mydbname?charset=utf8` { t.Fatal(`Test failed, got:`, c.String()) } }
// Attempts to connect to a database using the stored settings. func (s *source) Open() error { var err error // Before db.ConnectionURL we used a unified db.Settings struct. This // condition checks for that type and provides backwards compatibility. if settings, ok := s.connURL.(db.Settings); ok { // User is providing a db.Settings struct, let's translate it into a // ConnectionURL{}. conn := ConnectionURL{ User: settings.User, Password: settings.Password, Database: settings.Database, Options: map[string]string{ "charset": settings.Charset, }, } // Connection charset, UTF-8 by default. if conn.Options["charset"] == "" { conn.Options["charset"] = "utf8" } if settings.Socket != "" { conn.Address = db.Socket(settings.Socket) } else { if settings.Host == "" { settings.Host = "127.0.0.1" } if settings.Port == 0 { settings.Port = defaultPort } conn.Address = db.HostPort(settings.Host, uint(settings.Port)) } // Replace original s.connURL s.connURL = conn } if s.session, err = sql.Open(`mysql`, s.connURL.String()); err != nil { return err } if err = s.populateSchema(); err != nil { return err } return nil }
// Attempts to connect to a database using the stored settings. func (s *source) Open() error { var err error // Before db.ConnectionURL we used a unified db.Settings struct. This // condition checks for that type and provides backwards compatibility. if settings, ok := s.connURL.(db.Settings); ok { // User is providing a db.Settings struct, let's translate it into a // ConnectionURL{}. conn := ConnectionURL{ User: settings.User, Password: settings.Password, Address: db.HostPort(settings.Host, uint(settings.Port)), Database: settings.Database, Options: map[string]string{ "sslmode": "disable", }, } // Testing for SSLMode (deprecated) if SSLMode { conn.Options["sslmode"] = "verify-full" } // Replace original s.connURL s.connURL = conn } if s.session, err = sql.Open(`postgres`, s.connURL.String()); err != nil { return err } if err = s.populateSchema(); err != nil { return err } return nil }