示例#1
0
func ExampleDB_Query() {
	db := pg.Connect(&pg.Options{
		User: "******",
	})
	defer db.Close()

	_, err := db.Exec(`CREATE TEMP TABLE users (name text, emails text[])`)
	if err != nil {
		panic(err)
	}

	err = CreateUser(db, &User{"admin", []string{"admin1@admin", "admin2@admin"}})
	if err != nil {
		panic(err)
	}

	err = CreateUser(db, &User{"root", []string{"root1@root", "root2@root"}})
	if err != nil {
		panic(err)
	}

	users, err := GetUsers(db)
	if err != nil {
		panic(err)
	}

	fmt.Println(users[0], users[1])
	// Output: &{admin [admin1@admin admin2@admin]} &{root [root1@root root2@root]}
}
示例#2
0
文件: example_test.go 项目: a-kr/pg
func ExampleConnect() {
	db := pg.Connect(&pg.Options{
		User: "******",
	})
	err := db.Close()
	fmt.Println(err)
	// Output: <nil>
}
示例#3
0
文件: main_test.go 项目: a-kr/pg
func (t *DBTest) SetUpTest(c *C) {
	t.db = pg.Connect(&pg.Options{
		User:     "******",
		Database: "test",
		PoolSize: 2,

		DialTimeout:  3 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
	})

	pqdb, err := sql.Open("postgres", "user=test dbname=test")
	c.Assert(err, IsNil)
	t.pqdb = pqdb

	mysqldb, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/test")
	c.Assert(err, IsNil)
	t.mysqldb = mysqldb
}
示例#4
0
func Example_complexQuery() {
	db := pg.Connect(&pg.Options{
		User: "******",
	})
	defer db.Close()

	_, err := db.Exec(`CREATE TEMP TABLE articles (name text, category_id int)`)
	if err != nil {
		panic(err)
	}

	err = CreateArticle(db, &Article{"article1", 1})
	if err != nil {
		panic(err)
	}

	err = CreateArticle(db, &Article{"article2", 2})
	if err != nil {
		panic(err)
	}

	articles, err := GetArticles(db, &ArticleFilter{})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%d %v %v\n", len(articles), articles[0], articles[1])

	articles, err = GetArticles(db, &ArticleFilter{CategoryId: 1})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%d %v\n", len(articles), articles[0])

	// Output: 2 &{article1 1} &{article2 2}
	// 1 &{article1 1}
}
示例#5
0
文件: example_test.go 项目: a-kr/pg
func init() {
	db = pg.Connect(&pg.Options{
		User: "******",
	})
}