Exemple #1
0
func Database(name string) *sql.DB {
	if sess == nil {
		var err error
		var url string

		_, settings := datasource.Config(name)

		if settings.Port == 0 {
			settings.Port = 3306
		}

		if settings.Database == "" {
			panic("sql: Missing database name.")
		}

		if settings.User != "" && settings.Password != "" {
			url = fmt.Sprintf("tcp:%s:%d*%s/%s/%s", settings.Host, settings.Port, settings.Database, settings.User, settings.Password)
		} else {
			url = fmt.Sprintf("tcp:%s:%d*%s", settings.Host, settings.Port, settings.Database)
		}

		sess, err = sql.Open("mymysql", url)

		if err != nil {
			panic(fmt.Sprintf("sql: %s", err.Error()))
		}
	}
	return sess
}
Exemple #2
0
func Database(name string) db.Database {
	if _, ok := sess[name]; ok == false {
		driver, settings := datasource.Config(name)
		sess[name] = db.Open(driver, settings)
		if sess[name] == nil {
			panic(fmt.Sprintf("resource: Cannot open resource %s.", name))
		}
	}
	return sess[name]
}
Exemple #3
0
func Database(name string) *mgo.Database {
	if sess == nil {
		var err error
		var url string
		_, settings := datasource.Config(name)
		if settings.Port == 0 {
			settings.Port = 27017
		}
		if settings.Database == "" {
			panic("mgo: Missing database name.")
		}
		if settings.User != "" && settings.Password != "" {
			url = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", settings.User, settings.Password, settings.Host, settings.Port, settings.Database)
		} else {
			url = fmt.Sprintf("mongodb://%s:%d/%s", settings.Host, settings.Port, settings.Database)
		}
		fmt.Printf("url: %s\n", url)
		sess, err = mgo.Dial(url)
		if err != nil {
			panic(fmt.Sprintf("mgo: %s", err.Error()))
		}
	}
	return sess.DB("")
}