示例#1
0
文件: init.go 项目: gocontrib/nosql
// Init data store.
func Init(driver, url, dbname string) {
	log.Info("init data store")

	d, ok := drivers[strings.ToLower(driver)]
	if !ok {
		log.Fatal("unknown database driver")
	}

	s, err := d.Open(url, dbname)
	if err != nil {
		log.Fatal("unable to connect to %s database with %s: %+v", driver, url, err)
	}

	setStore(s)
}
示例#2
0
文件: init.go 项目: gocontrib/nosql
// setStore initializes global data store
func setStore(s Store) {
	if s == nil {
		log.Fatal("data store is not initialized")
	}
	log.Info("data store initialized")
	store = s
	cleanup = func() {
		store.Close()
		log.Info("data store closed")
	}
}
示例#3
0
func makeRedisStore() data.Store {
	flag.Parse()

	const (
		url = "tcp://127.0.0.1:6379/15"
	)

	var store, err = redis.Open(url, true)
	if err != nil {
		log.Fatal("unable to connect to redis with %s: %+v", url, err)
	}

	return store
}
示例#4
0
func makeBoltStore() data.Store {
	flag.Parse()

	const (
		path = "test.db"
	)

	var store, err = boltdb.Open(path, true)
	if err != nil {
		log.Fatal("unable to connect to database with %s: %+v", path, err)
	}

	return store
}
示例#5
0
func makeLedisStore() data.Store {
	flag.Parse()

	const (
		path = "data"
		db   = 0
	)

	var store, err = ledis.Open(path, db, true)
	if err != nil {
		log.Fatal("unable to connect to database with %s: %+v", path, err)
	}

	return store
}
示例#6
0
func makePgStore() data.Store {
	flag.Parse()

	const (
		dburl  = "user=postgres password=$PGPWD sslmode=disable"
		dbname = "test"
	)

	var store, err = postgresql.Open(dburl, dbname, true)
	if err != nil {
		log.Fatal("unable to connect to database with %s: %+v", dburl, err)
	}

	return store
}
示例#7
0
func makeMongoStore() data.Store {
	flag.Parse()

	const (
		dburl  = "127.0.0.1"
		dbname = "test"
	)

	var store, err = mongo.Open(dburl, dbname, true)
	if err != nil {
		log.Fatal("unable to connect to database with %s: %+v", dburl, err)
	}

	return store
}