示例#1
0
文件: main_test.go 项目: a-kr/pg
func (t *DBTest) TestTypes(c *C) {
	t.db.Exec("CREATE EXTENSION hstore")
	defer t.db.Exec("DROP EXTENSION hstore")

	for _, row := range conversionTests {
		_, err := t.db.QueryOne(pg.LoadInto(row.dst), "SELECT ?", row.src)
		c.Assert(err, IsNil)
		row.Assert(c)
	}

	for _, row := range conversionTests {
		if row.pgtype == "" {
			continue
		}

		stmt, err := t.db.Prepare("SELECT $1::" + row.pgtype)
		c.Assert(err, IsNil)

		_, err = stmt.QueryOne(pg.LoadInto(row.dst), row.src)
		c.Assert(err, IsNil)
		c.Assert(stmt.Close(), IsNil)
		row.Assert(c)
	}

	for _, row := range conversionTests {
		dst := struct{ Dst interface{} }{Dst: row.dst}
		_, err := t.db.QueryOne(&dst, "SELECT ? AS dst", row.src)
		c.Assert(err, IsNil, row.Comment())
		row.Assert(c)
	}

	for _, row := range conversionTests {
		dst := struct{ Dst interface{} }{Dst: row.dst}
		_, err := t.db.QueryOne(&dst, "SELECT ? AS dst", row.src)
		c.Assert(err, IsNil, row.Comment())
		row.Assert(c)
	}

	for _, row := range conversionTests {
		if row.pgtype == "" {
			continue
		}

		stmt, err := t.db.Prepare(fmt.Sprintf("SELECT $1::%s AS dst", row.pgtype))
		c.Assert(err, IsNil)

		dst := struct{ Dst interface{} }{Dst: row.dst}
		_, err = stmt.QueryOne(&dst, row.src)
		c.Assert(err, IsNil)
		c.Assert(stmt.Close(), IsNil)
		row.Assert(c)
	}
}
示例#2
0
文件: main_test.go 项目: a-kr/pg
func (t *DBTest) TestLargeWriteRead(c *C) {
	src := bytes.Repeat([]byte{0x1}, 1e6)
	var dst []byte
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?", src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
示例#3
0
文件: example_test.go 项目: a-kr/pg
func ExampleDB_Prepare() {
	stmt, err := db.Prepare("SELECT $1::text, $2::text")
	if err != nil {
		panic(err)
	}

	var s1, s2 string
	_, err = stmt.QueryOne(pg.LoadInto(&s1, &s2), "foo", "bar")
	fmt.Println(s1, s2, err)
	// Output: foo bar <nil>
}
示例#4
0
文件: main_test.go 项目: a-kr/pg
func (t *DBTest) TestLoadInto(c *C) {
	var dst int
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT 1")
	c.Assert(err, IsNil)
	c.Assert(dst, Equals, 1)
}
示例#5
0
文件: example_test.go 项目: a-kr/pg
func ExampleLoadInto() {
	var s1, s2 string
	_, err := db.QueryOne(pg.LoadInto(&s1, &s2), "SELECT ?, ?", "foo", "bar")
	fmt.Println(s1, s2, err)
	// Output: foo bar <nil>
}