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


Golang db.Open函数代码示例

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


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

示例1: TestOpenWithWrongData

// Attempts to open an empty datasource.
func TestOpenWithWrongData(t *testing.T) {
	var err error
	var rightSettings, wrongSettings db.Settings

	// Attempt to open with safe settings.
	rightSettings = db.Settings{
		Database: database,
		Host:     *host,
		User:     username,
		Password: password,
	}

	// Attempt to open an empty database.
	if _, err = db.Open(Adapter, rightSettings); err != nil {
		// Must fail.
		t.Fatal(err)
	}

	// Attempt to open with wrong password.
	wrongSettings = db.Settings{
		Database: database,
		Host:     *host,
		User:     username,
		Password: "fail",
	}

	if _, err = db.Open(Adapter, wrongSettings); err == nil {
		t.Fatalf("Expecting an error.")
	}

	// Attempt to open with wrong database.
	wrongSettings = db.Settings{
		Database: "fail",
		Host:     *host,
		User:     username,
		Password: password,
	}

	if _, err = db.Open(Adapter, wrongSettings); err == nil {
		t.Fatalf("Expecting an error.")
	}

	// Attempt to open with wrong username.
	wrongSettings = db.Settings{
		Database: database,
		Host:     *host,
		User:     "fail",
		Password: password,
	}

	if _, err = db.Open(Adapter, wrongSettings); err == nil {
		t.Fatalf("Expecting an error.")
	}
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:55,代码来源:database_test.go

示例2: init

func init() {
	var err error

	host := os.Getenv("MONGO_HOST")

	if host == "" {
		host = defaultHost
	}

	settings = db.Settings{
		Host:     host,
		Database: defaultDatabase,
	}

	if sess, err = db.Open("mongo", settings); err != nil {
		log.Fatal("db.Open: ", err)
	}

	log.Printf("Connected to mongo://%s/%s.\n", host, defaultDatabase)

	scores, err = sess.Collection("scores")
	if err != nil {
		if err != db.ErrCollectionDoesNotExists {
			log.Fatal("db.Collection: ", err)
		}
	}
}
开发者ID:toophy,项目名称:shooter-server,代码行数:27,代码来源:scores.go

示例3: BenchmarkAppendRawSQL

// Benchmarking raw database/sql.
func BenchmarkAppendRawSQL(b *testing.B) {
	var err error
	var sess db.Database

	if sess, err = db.Open(Adapter, settings); err != nil {
		b.Fatal(err)
	}

	defer sess.Close()

	driver := sess.Driver().(*sql.DB)

	if _, err = driver.Exec(`DELETE FROM "artist"`); err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	stmt, err := driver.Prepare(
		`INSERT INTO "artist" ("name") VALUES('Hayao Miyazaki')`)
	if err != nil {
		b.Fatal(err)
	}
	for i := 0; i < b.N; i++ {
		if _, err = stmt.Exec(); err != nil {
			b.Fatal(err)
		}
	}
}
开发者ID:SmarkSeven,项目名称:db,代码行数:29,代码来源:database_test.go

示例4: BenchmarkAppendTxRawSQL

// Benchmarking raw database/sql.
func BenchmarkAppendTxRawSQL(b *testing.B) {
	var err error
	var sess db.Database
	var tx *sql.Tx

	if sess, err = db.Open(Adapter, settings); err != nil {
		b.Fatal(err)
	}

	defer sess.Close()

	driver := sess.Driver().(*sql.DB)

	if tx, err = driver.Begin(); err != nil {
		b.Fatal(err)
	}

	if _, err = tx.Exec("TRUNCATE TABLE `artist`"); err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err = tx.Exec("INSERT INTO `artist` (`name`) VALUES('Hayao Miyazaki')"); err != nil {
			b.Fatal(err)
		}
	}

	if err = tx.Commit(); err != nil {
		b.Fatal(err)
	}
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:33,代码来源:database_test.go

示例5: SetupCollection

func SetupCollection() (col db.Collection) {
	var (
		err      error
		sess     db.Database
		settings db.Settings
	)

	settings = db.Settings{
		Database: `:memory:`,
	}

	if sess, err = db.Open("sqlite", settings); err != nil {
		panic(err)
	}

	if _, err = sess.Driver().(*sql.DB).Exec(`CREATE TABLE users (
    id INTEGER,
    first_name VARCHAR(80),
    last_name VARCHAR(80),
    age INTEGER
  );
  `); err != nil {
		panic(err)
	}

	if col, err = sess.Collection("users"); err != nil {
		panic(err)
	}

	return col
}
开发者ID:GoSteelProgrammers,项目名称:talk-review-orms,代码行数:31,代码来源:query.go

示例6: TestOpenFailed

// Trying to open an empty datasource, it must succeed (mongo).
func TestOpenFailed(t *testing.T) {
	_, err := db.Open(wrapperName, db.Settings{})

	if err != nil {
		t.Errorf(err.Error())
	}
}
开发者ID:vimdude,项目名称:db,代码行数:8,代码来源:database_test.go

示例7: TestOpenFailed

// Trying to open an empty datasource, it must fail.
func TestOpenFailed(t *testing.T) {
	_, err := db.Open(wrapperName, db.Settings{})

	if err == nil {
		t.Errorf("Expecting an error.")
	}
}
开发者ID:pzduniak,项目名称:db,代码行数:8,代码来源:database_test.go

示例8: TestRemove

// This test tries to remove some previously added rows.
func TestRemove(t *testing.T) {

	var err error

	// Opening database.
	sess, err := db.Open(wrapperName, settings)

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

	// We should close the database when it's no longer in use.
	defer sess.Close()

	// Getting a pointer to the "artist" collection.
	artist, err := sess.Collection("artist")

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

	// Getting the artist with id = 1
	res := artist.Find(db.Cond{"id": 1})

	// Trying to remove the row.
	err = res.Remove()

	if err != nil {
		t.Fatalf(err.Error())
	}
}
开发者ID:pzduniak,项目名称:db,代码行数:32,代码来源:database_test.go

示例9: TestSetup

func TestSetup(t *testing.T) {
	var err error
	for _, wrapper := range wrappers {
		if settings[wrapper] == nil {
			t.Fatalf(`No such settings entry for wrapper %s.`, wrapper)
		} else {
			var sess db.Database

			sess, err = db.Open(wrapper, *settings[wrapper])
			if err != nil {
				t.Fatalf(`Test for wrapper %s failed: %q`, wrapper, err)
			}

			if setupFn[wrapper] == nil {
				t.Fatalf(`Missing setup function for wrapper %s.`, wrapper)
			} else {
				if err = setupFn[wrapper](sess.Driver()); err != nil {
					t.Fatalf(`Failed to setup wrapper %s: %q`, wrapper, err)
				}
			}

			err = sess.Close()
			if err != nil {
				t.Fatalf(`Could not close %s: %q`, wrapper, err)
			}

		}
	}
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:29,代码来源:main_test.go

示例10: BenchmarkAppendRaw

// Benchmarking raw mgo queries.
func BenchmarkAppendRaw(b *testing.B) {
	sess, err := db.Open(Adapter, settings)

	if err != nil {
		b.Fatal(err)
	}

	defer sess.Close()

	artist, err := sess.Collection("artist")
	artist.Truncate()

	driver := sess.Driver().(*mgo.Session)

	mgodb := driver.DB(database)
	col := mgodb.C("artist")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := col.Insert(map[string]string{"name": "Hayao Miyazaki"})
		if err != nil {
			b.Fatal(err)
		}
	}
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:26,代码来源:database_test.go

示例11: TestResultCount

// This test tries to use an empty filter and count how many elements were
// added into the artist collection.
func TestResultCount(t *testing.T) {

	var err error
	var res db.Result

	// Opening database.
	sess, err := db.Open(Adapter, settings)

	if err != nil {
		t.Fatal(err)
	}

	defer sess.Close()

	// We should close the database when it's no longer in use.
	artist, _ := sess.Collection("artist")

	res = artist.Find()

	// Counting all the matching rows.
	total, err := res.Count()

	if err != nil {
		t.Fatal(err)
	}

	if total == 0 {
		t.Fatalf("Should not be empty, we've just added some rows!")
	}

}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:33,代码来源:database_test.go

示例12: main

func main() {
	log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)

	go signalCatcher()
	go signalReload()

	var settings = db.Settings{
		Host:     "localhost", // MySQL server IP or name.
		Database: "Paoge",     // Database name.
		User:     "root",      // Optional user name.
		Password: "test",      // Optional user password.
	}
	session, err := db.Open("mysql", settings)
	defer session.Close()
	if err != nil {
		fmt.Println("DB Connection Error")
	}
	serviceCollection, err := session.Collection("service_results")
	if err != nil {
		fmt.Println("DB Table Connection Error")
	}
	serviceCollection.Append(data.ServiceResults{})

	http.HandleFunc("/nster/receive", receiveChecks).Name()
	log.Fatal(http.ListenAndServe(":9000", nil))
	select {}
}
开发者ID:jamesrwu,项目名称:Paoge,代码行数:27,代码来源:Nster-receiver.go

示例13: BenchmarkAppendDbItem_Transaction

// Benchmarking Append() with transactions.
//
// Contributed by wei2912
// See: https://github.com/gosexy/db/issues/20#issuecomment-20167939
// Applying the BEGIN and END transaction optimizations.
func BenchmarkAppendDbItem_Transaction(b *testing.B) {
	sess, err := db.Open(wrapperName, settings)

	if err != nil {
		b.Fatalf(err.Error())
	}

	defer sess.Close()

	artist, err := sess.Collection("artist")
	artist.Truncate()

	err = sess.Begin()
	if err != nil {
		b.Fatalf(err.Error())
	}

	for i := 0; i < b.N; i++ {
		_, err = artist.Append(map[string]string{"name": "Isaac Asimov"})
		if err != nil {
			b.Fatalf(err.Error())
		}
	}

	err = sess.End()
	if err != nil {
		b.Fatalf(err.Error())
	}
}
开发者ID:pzduniak,项目名称:db,代码行数:34,代码来源:database_test.go

示例14: main

func main() {

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

	if err != nil {
		fmt.Println("Unable to connect:", err.Error())
		return
	}

	defer sess.Close()

	birthdayCollection, err := sess.Collection("birthdays")

	if err != nil {
		if err != db.ErrCollectionDoesNotExists {
			fmt.Println("Could not use collection:", err.Error())
			return
		}
	} else {
		err = birthdayCollection.Truncate()

		if err != nil {
			fmt.Println(err.Error())
			return
		}
	}

	birthdayCollection.Append(Birthday{
		Name: "Hayao Miyazaki",
		Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.UTC),
	})

	birthdayCollection.Append(Birthday{
		Name: "Nobuo Uematsu",
		Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.UTC),
	})

	birthdayCollection.Append(Birthday{
		Name: "Hironobu Sakaguchi",
		Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.UTC),
	})

	var res db.Result

	res = birthdayCollection.Find()

	var birthdays []Birthday

	err = res.All(&birthdays)

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

	for _, birthday := range birthdays {
		fmt.Printf("%s was born in %s.\n", birthday.Name, birthday.Born.Format("January 2, 2006"))
	}

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

示例15: BenchmarkAppendUpper

// Benchmarking Append().
//
// Contributed by wei2912
// See: https://github.com/gosexy/db/issues/20#issuecomment-20097801
func BenchmarkAppendUpper(b *testing.B) {
	var sess db.Database
	var artist db.Collection
	var err error

	if sess, err = db.Open(Adapter, settings); err != nil {
		b.Fatal(err)
	}

	defer sess.Close()

	if artist, err = sess.Collection("artist"); err != nil {
		b.Fatal(err)
	}

	artist.Truncate()

	item := struct {
		Name string `db:"name"`
	}{"Hayao Miyazaki"}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err = artist.Append(item); err != nil {
			b.Fatal(err)
		}
	}
}
开发者ID:SmarkSeven,项目名称:db,代码行数:32,代码来源:database_test.go


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