Exemple #1
0
// init - Establish database connection
func init() {
	//database = db.GetConnection("/home/visolve/go-orm/config.ini")
	conf, _ := configparser.Read("/home/visolve/go-orm/config.ini")
	db_config, _ := conf.Section("couch")
	var db couch.Database
	if db_config.ValueOf("user") == "" {
		db, _ = couch.NewDatabase(db_config.ValueOf("ipaddress"), db_config.ValueOf("port"), db_config.ValueOf("dbname"))
	} else {
		db, _ = couch.NewDatabaseByURL("http://" + db_config.ValueOf("user") + ":" + db_config.ValueOf("password") + "@" + db_config.ValueOf("ipaddress") + ":" + db_config.ValueOf("port") + "/" + db_config.ValueOf("dbname"))
	}
	cdatabase = abstract.Database(CouchDb{db})
}
Exemple #2
0
// GetConnection - Return DB connection object based on config file.
func GetConnection(path string) abstract.Database {
	var collection *mgo.Collection
	conf, _ := configparser.Read(path)
	checkError(conf, "Invalid configuration file")
	driver_config, _ := conf.Section("nosql.db")

	var db_config *configparser.Section
	if strings.Contains(strings.ToLower(driver_config.ValueOf("name")), "mongo") {
		db_config, _ = conf.Section("mongo")
		driver_config.SetValueFor("name", "mongo")
	} else if strings.Contains(strings.ToLower(driver_config.ValueOf("name")), "couch") {
		db_config, _ = conf.Section("couch")
		driver_config.SetValueFor("name", "couch")
	}
	switch strings.ToUpper(driver_config.ValueOf("name")) {
	case "COUCH":
		var db couch.Database
		if db_config.ValueOf("user") == "" {
			db, _ = couch.NewDatabase(db_config.ValueOf("ipaddress"), db_config.ValueOf("port"), db_config.ValueOf("dbname"))
		} else {
			db, _ = couch.NewDatabaseByURL("http://" + db_config.ValueOf("user") + ":" + db_config.ValueOf("password") + "@" + db_config.ValueOf("ipaddress") + ":" + db_config.ValueOf("port") + "/" + db_config.ValueOf("dbname"))
		}
		return abstract.Database(supported_db.CouchDb{db})
	case "MONGO":
		var mongoSession *mgo.Session
		var err error
		if db_config.ValueOf("user") == "" {
			mongoSession, err = mgo.Dial(db_config.ValueOf("ipaddress") + ":" + db_config.ValueOf("port"))
		} else {
			mongoSession, err = mgo.Dial(db_config.ValueOf("user") + ":" + db_config.ValueOf("password") + "@" + db_config.ValueOf("ipaddress") + ":" + db_config.ValueOf("port") + "/" + db_config.ValueOf("dbname"))
		}
		checkError(mongoSession, err)
		db := mongoSession.DB(db_config.ValueOf("dbname"))
		collection = db.C(db_config.ValueOf("collectionname"))
		return abstract.Database(supported_db.MongoDb{collection})
	default:
		panic("Supports only Couch or MongoDb")
	}
	return nil
}