当前位置: 首页>>代码示例>>Golang>>正文


Golang db.Open函数代码示例

本文整理汇总了Golang中github.com/gosexy/db.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Init

func Init(host, dbname, user, passwd string, count int) {
	if count > mysql_conn_max_num {
		count = mysql_conn_max_num
	} else if count < mysql_conn_min_num {
		count = mysql_conn_min_num
	}

	var settings = db.DataSource{
		Host:     host,
		Database: dbname,
		User:     user,
		Password: passwd,
	}

	dbCh := make(chan *sql.DB, count)

	for i := 0; i < count; i++ {
		sess, err := db.Open("mysql", settings)
		if err != nil {
			panic(err)
		}

		drv := sess.Driver().(*sql.DB)
		if err := drv.Ping(); err != nil {
			panic(err)
		}

		dbCh <- drv
	}

	g_mysql = &MysqlManager{dbCh, count}

	return
}
开发者ID:sunyuantao,项目名称:windows,代码行数:34,代码来源:mysql.go

示例2: TestAppend

func TestAppend(t *testing.T) {

	sess, err := db.Open("sqlite", db.DataSource{Database: dbpath})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	_, err = sess.Collection("doesnotexists")

	if err == nil {
		t.Errorf("Collection should not exists.")
		//return
	}

	people := sess.ExistentCollection("people")

	people.Truncate()

	names := []string{"Juan", "José", "Pedro", "María", "Roberto", "Manuel", "Miguel"}

	for i := 0; i < len(names); i++ {
		people.Append(db.Item{"name": names[i]})
	}

	total, _ := people.Count()

	if total != len(names) {
		t.Error("Could not append all items.")
	}

}
开发者ID:kdar,项目名称:db,代码行数:34,代码来源:sqlite_test.go

示例3: TestPopulate

func TestPopulate(t *testing.T) {

	sess, err := db.Open("mongo", db.DataSource{Host: host, Database: dbname})

	if err != nil {
		t.Errorf(err.Error())
		return
	}

	defer sess.Close()

	people, _ := sess.Collection("people")
	places, _ := sess.Collection("places")
	children, _ := sess.Collection("children")
	visits, _ := sess.Collection("visits")

	values := []string{"Alaska", "Nebraska", "Alaska", "Acapulco", "Rome", "Singapore", "Alabama", "Cancún"}

	for i, value := range values {
		places.Append(db.Item{
			"code_id": i,
			"name":    value,
		})
	}

	results := people.FindAll(
		db.Fields{"id", "name"},
		db.Sort{"name": "ASC", "id": -1},
	)

	for _, person := range results {

		// Has 5 children.

		for j := 0; j < 5; j++ {
			children.Append(db.Item{
				"name":      fmt.Sprintf("%s's child %d", person["name"], j+1),
				"parent_id": person["_id"],
			})
		}

		// Lives in
		people.Update(
			db.Cond{"_id": person["_id"]},
			db.Set{"place_code_id": int(rand.Float32() * float32(len(results)))},
		)

		// Has visited
		for k := 0; k < 3; k++ {
			place := places.Find(db.Cond{
				"code_id": int(rand.Float32() * float32(len(results))),
			})
			visits.Append(db.Item{
				"place_id":  place["_id"],
				"person_id": person["_id"],
			})
		}
	}

}
开发者ID:kdar,项目名称:db,代码行数:60,代码来源:mongo_test.go

示例4: TestDelete

func TestDelete(t *testing.T) {

	sess, err := db.Open("mongo", db.DataSource{Host: host, Database: dbname})

	if err != nil {
		t.Errorf(err.Error())
		return
	}

	defer sess.Close()

	people := sess.ExistentCollection("people")

	err = people.Remove(db.Cond{"name": "Juan"})

	if err != nil {
		t.Error("Failed to remove.")
	}

	result := people.Find(db.Cond{"name": "Juan"})

	if len(result) > 0 {
		t.Error("Could not remove a recently appended item.")
	}
}
开发者ID:kdar,项目名称:db,代码行数:25,代码来源:mongo_test.go

示例5: Database

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]
}
开发者ID:astrata,项目名称:tango-doc,代码行数:10,代码来源:persistent.go

示例6: TestOpen

func TestOpen(t *testing.T) {

	sess, err := db.Open("mongo", db.DataSource{Host: "1.1.1.1"})

	if err != nil {
		t.Logf("Got %t, this was intended.", err)
		return
	}

	sess.Close()

	t.Errorf("Reached.")
}
开发者ID:kdar,项目名称:db,代码行数:13,代码来源:mongo_test.go

示例7: TestDrop

func TestDrop(t *testing.T) {

	sess, err := db.Open("mongo", db.DataSource{Host: host, Database: dbname})

	if err != nil {
		t.Errorf(err.Error())
		return
	}

	defer sess.Close()

	sess.Drop()
}
开发者ID:kdar,项目名称:db,代码行数:13,代码来源:mongo_test.go

示例8: TestAuthFail

func TestAuthFail(t *testing.T) {

	sess, err := db.Open("mongo", db.DataSource{Host: host, Database: dbname, User: "unknown", Password: "fail"})

	if err != nil {
		t.Logf("Got %t, this was intended.", err)
		return
	}

	sess.Close()

	t.Errorf("Reached.")
}
开发者ID:kdar,项目名称:db,代码行数:13,代码来源:mongo_test.go

示例9: main

func main() {

	sess, err := db.Open("sqlite", settings)

	if err != nil {
		fmt.Println("Please create the `animals.db` sqlite3 database.")
		return
	}

	defer sess.Close()

	animals, err := sess.Collection("animals")

	if err != nil {
		fmt.Println("Please create the `animals` table and make sure the `animals.db` sqlite3 database exists.")
		return
	}

	animals.Truncate()

	animals.Append(db.Item{
		"animal": "Bird",
		"young":  "Chick",
		"female": "Hen",
		"male":   "Cock",
		"group":  "flock",
	})

	animals.Append(db.Item{
		"animal": "Bovidae",
		"young":  "Calf",
		"female": "Cow",
		"male":   "Bull",
		"group":  "Herd",
	})

	animals.Append(db.Item{
		"animal": "Canidae",
		"young":  "Puppy, Pup",
		"female": "Bitch",
		"male":   "Dog",
		"group":  "Pack",
	})

	items := animals.FindAll()

	for _, item := range items {
		fmt.Printf("animal: %s, young: %s\n", item["animal"], item["young"])
	}

}
开发者ID:kdar,项目名称:db,代码行数:51,代码来源:main.go

示例10: TestCustom

func TestCustom(t *testing.T) {
	sess, err := db.Open("mysql", db.DataSource{Host: host, Database: dbname, User: username, Password: password})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	_, err = sess.Driver().(*sql.DB).Query("SELECT NOW()")

	if err != nil {
		panic(err)
	}

}
开发者ID:kdar,项目名称:db,代码行数:16,代码来源:mysql_test.go

示例11: TestFind

func TestFind(t *testing.T) {

	sess, err := db.Open("sqlite", db.DataSource{Database: dbpath})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	people, _ := sess.Collection("people")

	result := people.Find(db.Cond{"name": "José"})

	if result["name"] != "José" {
		t.Error("Could not find a recently appended item.")
	}

}
开发者ID:kdar,项目名称:db,代码行数:19,代码来源:sqlite_test.go

示例12: TestUpdate

func TestUpdate(t *testing.T) {
	sess, err := db.Open("sqlite", db.DataSource{Database: dbpath})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	people, _ := sess.Collection("people")

	people.Update(db.Cond{"name": "José"}, db.Set{"name": "Joseph"})

	result := people.Find(db.Cond{"name": "Joseph"})

	if len(result) == 0 {
		t.Error("Could not update a recently appended item.")
	}
}
开发者ID:kdar,项目名称:db,代码行数:19,代码来源:sqlite_test.go

示例13: TestDelete

func TestDelete(t *testing.T) {
	sess, err := db.Open("mysql", db.DataSource{Host: host, Database: dbname, User: username, Password: password})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	people, _ := sess.Collection("people")

	people.Remove(db.Cond{"name": "Juan"})

	result := people.Find(db.Cond{"name": "Juan"})

	if len(result) > 0 {
		t.Error("Could not remove a recently appended item.")
	}
}
开发者ID:kdar,项目名称:db,代码行数:19,代码来源:mysql_test.go

示例14: TestFind

func TestFind(t *testing.T) {

	sess, err := db.Open("mysql", db.DataSource{Host: host, Database: dbname, User: username, Password: password})

	if err != nil {
		panic(err)
	}

	defer sess.Close()

	people, _ := sess.Collection("people")

	result := people.Find(db.Cond{"name": "José"})

	if result["name"] != "José" {
		t.Error("Could not find a recently appended item.")
	}

}
开发者ID:kdar,项目名称:db,代码行数:19,代码来源:mysql_test.go

示例15: TestRelation

func TestRelation(t *testing.T) {
	sess, err := db.Open("mongo", db.DataSource{Host: host, Database: dbname})

	if err != nil {
		t.Errorf(err.Error())
		return
	}

	defer sess.Close()

	people, _ := sess.Collection("people")
	places, _ := sess.Collection("places")
	children, _ := sess.Collection("children")
	visits, _ := sess.Collection("visits")

	result := people.FindAll(
		db.Relate{
			"lives_in": db.On{
				places,
				db.Cond{"code_id": "{place_code_id}"},
			},
		},
		db.RelateAll{
			"has_children": db.On{
				children,
				db.Cond{"parent_id": "{_id}"},
			},
			"has_visited": db.On{
				visits,
				db.Cond{"person_id": "{_id}"},
				db.Relate{
					"place": db.On{
						places,
						db.Cond{"_id": "{place_id}"},
					},
				},
			},
		},
	)

	fmt.Printf("%# v\n", pretty.Formatter(result))
}
开发者ID:kdar,项目名称:db,代码行数:42,代码来源:mongo_test.go


注:本文中的github.com/gosexy/db.Open函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。