• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang db.Open函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: TestDelete

func TestDelete(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.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,代码行数:20,代码来源:sqlite_test.go


示例17: TestFind

func TestFind(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")

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

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

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


示例18: TestAppend

func TestAppend(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()

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

	if col.Exists() == true {
		t.Errorf("Collection should not exists, yet.")
		return
	}

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

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

	if col.Exists() == false {
		t.Errorf("Collection should exists.")
		return
	}

	count, err := col.Count()

	if err != nil {
		t.Error("Failed to count on collection.")
	}

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

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


示例19: TestRelation

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

	if err != nil {
		panic(err)
	}

	defer sess.Close()

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

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

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


示例20: Init

func (l *MysqlLogMgr) Init(config *Config) error {
	if config.MysqlLog == false {
		return errors.New("mysql log server not configure!!!")
	}

	settings := db.DataSource{
		Host:     config.Host,
		Port:     config.Port,
		Database: config.Database,
		User:     config.User,
		Password: config.Password,
		Charset:  config.Charset,
	}

	session, err := db.Open("mysql", settings)
	if err != nil {
		return err
	}
	db := session.Driver().(*sql.DB)
	if err := db.Ping(); err != nil {
		fmt.Println("db.Ping failed:", err)
		return err
	}

	l.init = true
	l.waitGroup = sync_.NewWaitGroup()
	l.logMsgCh = make(chan *LogMsg, getChildLogScaleSize())
	l.logSql = make(chan string, 10)
	l.logLevel = config.MysqlLogLevel
	l.session = session

	for i := 0; i < 10; i++ {
		go l.run()
	}
	go l.writeDB()
	return nil
}
开发者ID:wxaxiaoyao,项目名称:linux,代码行数:37,代码来源:mysql.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang redis.Client类代码示例发布时间:2022-05-23
下一篇:
Golang websocket.Upgrader类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap