예제 #1
0
// ParseURL parses s into a ConnectionURL struct.
func ParseURL(s string) (conn ConnectionURL, err error) {
	var cfg *config

	if cfg, err = parseDSN(s); err != nil {
		return
	}

	conn.User = cfg.user
	conn.Password = cfg.passwd

	if cfg.net == "unix" {
		conn.Address = db.Socket(cfg.addr)
	} else if cfg.net == "tcp" {
		conn.Address = db.ParseAddress(cfg.addr)
	}

	conn.Database = cfg.dbname

	conn.Options = map[string]string{}

	for k, v := range cfg.params {
		conn.Options[k] = v
	}

	return
}
예제 #2
0
파일: connection.go 프로젝트: SmarkSeven/db
// ParseCluster parses s into a cluster structure.
func ParseCluster(s string) (c cluster) {
	var hosts []string
	hosts = strings.Split(s, ",")
	l := len(hosts)
	for i := 0; i < l; i++ {
		c.address = append(c.address, db.ParseAddress(hosts[i]))
	}
	return
}
예제 #3
0
func init() {
	t := time.Date(2012, 7, 28, 1, 2, 3, 0, time.Local)

	testValues = testValuesStruct{
		1, 1, 1, 1, 1,
		-1, -1, -1, -1, -1,
		1.337, 1.337,
		true,
		"Hello world!",
		t,
		nil,
		&t,
		time.Second * time.Duration(7331),
	}

	flag.Parse()
	settings.Address = db.ParseAddress(*host)
}
예제 #4
0
파일: connection.go 프로젝트: skatsuta/db
// ParseURL parses s into a ConnectionURL struct.
func ParseURL(s string) (conn ConnectionURL, err error) {
	var u *url.URL

	if strings.HasPrefix(s, connectionScheme+"://") == false {
		return conn, fmt.Errorf(`Expecting mongodb:// connection scheme.`)
	}

	if u, err = url.Parse(s); err != nil {
		return conn, err
	}

	// Parsing host.
	conn.Address = db.ParseAddress(u.Host)

	// Deleting / from start of the string.
	conn.Database = strings.Trim(u.Path, "/")

	// Adding user / password.
	if u.User != nil {
		conn.User = u.User.Username()
		conn.Password, _ = u.User.Password()
	}

	// Adding options.
	conn.Options = map[string]string{}

	var vv url.Values

	if vv, err = url.ParseQuery(u.RawQuery); err != nil {
		return conn, err
	}

	for k := range vv {
		conn.Options[k] = vv.Get(k)
	}

	return conn, err
}
예제 #5
0
// 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 {
		var sAddr string

		if settings.Host != "" {
			if settings.Port > 0 {
				sAddr = fmt.Sprintf("%s:%d", settings.Host, settings.Port)
			} else {
				sAddr = settings.Host
			}
		} else {
			sAddr = settings.Socket
		}

		conn := ConnectionURL{
			User:     settings.User,
			Password: settings.Password,
			Address:  db.ParseAddress(sAddr),
			Database: settings.Database,
		}

		// Replace original s.connURL
		s.connURL = conn
	}

	if s.session, err = mgo.DialWithTimeout(s.connURL.String(), connTimeout); err != nil {
		return err
	}

	s.database = s.session.DB("")

	return nil
}