本文整理匯總了Golang中github.com/housinganywhere/pg.Connect函數的典型用法代碼示例。如果您正苦於以下問題:Golang Connect函數的具體用法?Golang Connect怎麽用?Golang Connect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Connect函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: BenchmarkModelHasMany2ManyGopg
func BenchmarkModelHasMany2ManyGopg(b *testing.B) {
seedDB()
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var books []Book
err := db.Model(&books).
Columns("book.*", "Genres").
Limit(100).
Select()
if err != nil {
b.Fatal(err)
}
if len(books) != 100 {
b.Fatalf("got %d, wanted 100", len(books))
}
for _, book := range books {
if len(book.Genres) != 10 {
b.Fatalf("got %d, wanted 10", len(book.Genres))
}
}
}
})
}
示例2: BenchmarkExecWithError
func BenchmarkExecWithError(b *testing.B) {
db := pg.Connect(pgOptions())
defer db.Close()
_, err := db.Exec(
`CREATE TABLE exec_with_error_test(id bigint PRIMARY KEY, name varchar(500))`)
if err != nil {
b.Fatal(err)
}
_, err = db.Exec(`
INSERT INTO exec_with_error_test(id, name) VALUES(?, ?)
`, 1, "hello world")
if err != nil {
b.Fatal(err)
}
defer db.Exec(`DROP TABLE exec_with_error_test`)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := db.Exec(`INSERT INTO exec_with_error_test(id) VALUES(?)`, 1)
if err == nil {
b.Fatalf("got nil error, expected IntegrityError")
} else if _, ok := err.(*pg.IntegrityError); !ok {
b.Fatalf("got " + err.Error() + ", expected IntegrityError")
}
}
})
}
示例3: TestStatementTimeout
func TestStatementTimeout(t *testing.T) {
opt := pgOptions()
opt.Params = map[string]interface{}{
"statement_timeout": 1000,
}
db := pg.Connect(opt)
defer db.Close()
_, err := db.Exec("SELECT pg_sleep(60)")
if err == nil {
t.Fatalf("err is nil")
}
if err.Error() != "ERROR #57014 canceling statement due to statement timeout: " {
t.Fatalf("got %q", err.Error())
}
if db.Pool().Len() != 1 || db.Pool().FreeLen() != 1 {
t.Fatalf("pool is empty")
}
err = eventually(func() error {
return verifyNoActivity(db)
}, 10*time.Second)
if err != nil {
t.Fatal(err)
}
}
示例4: TestCancelRequestOnTimeout
func TestCancelRequestOnTimeout(t *testing.T) {
opt := pgOptions()
opt.ReadTimeout = time.Second
db := pg.Connect(opt)
defer db.Close()
_, err := db.Exec("SELECT pg_sleep(60)")
if err == nil {
t.Fatalf("err is nil")
}
neterr, ok := err.(net.Error)
if !ok {
t.Fatalf("got %v, expected net.Error", err)
}
if !neterr.Timeout() {
t.Fatalf("got %v, expected timeout", err)
}
if db.Pool().FreeLen() != 1 {
t.Fatalf("len is %d", db.Pool().FreeLen())
}
if db.Pool().Len() != 1 {
t.Fatalf("size is %d", db.Pool().Len())
}
err = eventually(func() error {
return verifyNoActivity(db)
}, 10*time.Second)
if err != nil {
t.Fatal(err)
}
}
示例5: Example_json
func Example_json() {
db := pg.Connect(&pg.Options{
User: "postgres",
})
defer db.Close()
_, err := db.Exec(`CREATE TEMP TABLE items (id serial, data json)`)
if err != nil {
panic(err)
}
item1 := Item{
Id: 1,
Data: map[string]interface{}{"hello": "world"},
}
if err := db.Create(&item1); err != nil {
panic(err)
}
var item Item
err = db.Model(&item).Where("id = ?", 1).Select()
if err != nil {
panic(err)
}
fmt.Println(item)
// Output: {1 map[hello:world]}
}
示例6: ExampleConnect
func ExampleConnect() {
db := pg.Connect(&pg.Options{
User: "postgres",
})
err := db.Close()
fmt.Println(err)
// Output: <nil>
}
示例7: SetUpTest
func (t *ListenerTest) SetUpTest(c *C) {
opt := pgOptions()
opt.PoolSize = 2
opt.PoolTimeout = time.Second
t.db = pg.Connect(opt)
ln, err := t.db.Listen("test_channel")
c.Assert(err, IsNil)
t.ln = ln
}
示例8: TestUnixSocket
func TestUnixSocket(t *testing.T) {
opt := pgOptions()
opt.Network = "unix"
opt.Addr = "/var/run/postgresql/.s.PGSQL.5432"
db := pg.Connect(opt)
defer db.Close()
_, err := db.Exec("SELECT 'test_unix_socket'")
if err != nil {
t.Fatal(err)
}
}
示例9: BenchmarkQueryRow
func BenchmarkQueryRow(b *testing.B) {
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var dst numLoader
_, err := db.QueryOne(&dst, `SELECT ?::bigint AS num`, 1)
if err != nil {
b.Fatal(err)
}
if dst.Num != 1 {
b.Fatalf("got %d, wanted 1", dst.Num)
}
}
}
示例10: BenchmarkQueryRowsGopgDiscard
func BenchmarkQueryRowsGopgDiscard(b *testing.B) {
seedDB()
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := db.Query(pg.Discard, `SELECT * FROM records LIMIT 100`)
if err != nil {
b.Fatal(err)
}
}
})
}
示例11: BenchmarkQueryRowScan
func BenchmarkQueryRowScan(b *testing.B) {
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var n int64
_, err := db.QueryOne(pg.Scan(&n), `SELECT ? AS num`, 1)
if err != nil {
b.Fatal(err)
}
if n != 1 {
b.Fatalf("got %d, wanted 1", n)
}
}
})
}
示例12: connectDB
func connectDB() *pg.DB {
db := pg.Connect(&pg.Options{
User: "postgres",
})
err := createTestSchema(db)
if err != nil {
panic(err)
}
err = db.Create(&Book{
Title: "book 1",
AuthorID: 10,
EditorID: 11,
CreatedAt: time.Now(),
})
if err != nil {
panic(err)
}
err = db.Create(&Book{
Title: "book 2",
AuthorID: 10,
EditorID: 12,
CreatedAt: time.Now(),
})
if err != nil {
panic(err)
}
err = db.Create(&Book{
Title: "book 3",
AuthorID: 11,
EditorID: 11,
CreatedAt: time.Now(),
})
if err != nil {
panic(err)
}
return db
}
示例13: BenchmarkQueryRowsGopgORM
func BenchmarkQueryRowsGopgORM(b *testing.B) {
seedDB()
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var rs []Record
err := db.Model(&rs).Limit(100).Select()
if err != nil {
b.Fatal(err)
}
if len(rs) != 100 {
b.Fatalf("got %d, wanted 100", len(rs))
}
}
})
}
示例14: BenchmarkQueryRowsGopgReflect
func BenchmarkQueryRowsGopgReflect(b *testing.B) {
seedDB()
db := pg.Connect(pgOptions())
defer db.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var rs []Record
_, err := db.Query(&rs, `SELECT * FROM records LIMIT 100`)
if err != nil {
b.Fatal(err)
}
if len(rs) != 100 {
b.Fatalf("got %d, wanted 100", len(rs))
}
}
})
}
示例15: BenchmarkExec
func BenchmarkExec(b *testing.B) {
db := pg.Connect(pgOptions())
defer db.Close()
_, err := db.Exec(
`CREATE TABLE exec_test(id bigint, name varchar(500))`)
if err != nil {
b.Fatal(err)
}
defer db.Exec(`DROP TABLE exec_test`)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := db.Exec(`INSERT INTO exec_test(id, name) VALUES(?, ?)`, 1, "hello world")
if err != nil {
b.Fatal(err)
}
}
})
}