Пример #1
0
func (t *DBTest) TestTypeString(c *C) {
	src := "hello\000"
	var dst string
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?", src)
	c.Assert(err, IsNil)
	c.Assert(dst, Equals, "hello")
}
Пример #2
0
func (t *DBTest) TestTypeIntArray(c *C) {
	src := []int{1, 2, 3}
	var dst []int
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::int[]", []int{1, 2, 3})
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #3
0
func (t *DBTest) TestTypeStringArray(c *C) {
	src := []string{"foo \n", "bar", "hello {}", "'\\\""}
	var dst []string
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::text[]", src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #4
0
func (t *DBTest) TestTypeBytes(c *C) {
	src := []byte("hello world\000")
	var dst []byte
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::bytea", src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #5
0
func (t *DBTest) TestTypeHstore(c *C) {
	src := map[string]string{"foo =>": "bar =>", "hello": "world", "'\\\"": "'\\\""}
	dst := make(map[string]string)
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?", src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #6
0
func (t *DBTest) TestTypeDate(c *C) {
	src := time.Now().UTC()
	var dst time.Time
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::date", src)
	c.Assert(err, IsNil)
	c.Assert(dst.Location(), Equals, time.UTC)
	c.Assert(dst.Format("2006-01-02"), Equals, dst.Format("2006-01-02"))
}
Пример #7
0
func (t *DBTest) TestTypeStmtEmptyStringArray(c *C) {
	stmt, err := t.db.Prepare("SELECT $1::text[]")
	c.Assert(err, IsNil)

	var dst []string
	_, err = stmt.QueryOne(pg.LoadInto(&dst), []string{})
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, []string{})
}
Пример #8
0
func (t *DBTest) TestTypeEmptyIntArray(c *C) {
	var dst []int
	_, err := t.db.QueryOne(
		pg.LoadInto(&dst),
		"SELECT ?::int[]",
		[]int{},
	)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, []int{})
}
Пример #9
0
func (t *DBTest) TestTypeStmtHstore(c *C) {
	stmt, err := t.db.Prepare("SELECT $1::hstore")
	c.Assert(err, IsNil)

	src := map[string]string{"foo =>": "bar =>", "hello": "world", "'\\\"": "'\\\""}
	dst := make(map[string]string)
	_, err = stmt.QueryOne(pg.LoadInto(&dst), src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #10
0
func (t *DBTest) TestTypeStmtString(c *C) {
	stmt, err := t.db.Prepare("SELECT $1::text")
	c.Assert(err, IsNil)

	src := "hello\000"
	var dst string
	_, err = stmt.QueryOne(pg.LoadInto(&dst), src)
	c.Assert(err, IsNil)
	c.Assert(dst, Equals, "hello")
}
Пример #11
0
func (t *DBTest) TestTypeStmtIntArray(c *C) {
	stmt, err := t.db.Prepare("SELECT $1::int[]")
	c.Assert(err, IsNil)

	src := []int{1, 2, 3}
	var dst []int
	_, err = stmt.QueryOne(pg.LoadInto(&dst), src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)
}
Пример #12
0
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>
}
Пример #13
0
func (t *DBTest) TestTypeStmtBytes(c *C) {
	stmt, err := t.db.Prepare("SELECT $1::bytea")
	c.Assert(err, IsNil)

	src := []byte("hello world\000")
	var dst []byte
	_, err = stmt.QueryOne(pg.LoadInto(&dst), src)
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, src)

	c.Assert(stmt.Close(), IsNil)
}
Пример #14
0
func (t *DBTest) TestTypeTimestamp(c *C) {
	src := time.Now().UTC()
	var dst time.Time
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::timestamp", src)
	c.Assert(err, IsNil)
	c.Assert(dst.Location(), Equals, time.UTC)
	c.Assert(
		dst.Format("2006-01-02 15:04:05.9999"),
		Equals,
		src.Format("2006-01-02 15:04:05.9999"),
	)
}
Пример #15
0
func (t *PoolTest) TestTimeoutAndCancelRequest(c *C) {
	_, err := t.db.Exec("SELECT pg_sleep(60)")
	c.Assert(err.(net.Error).Timeout(), Equals, true)

	c.Assert(t.db.Pool().Size(), Equals, 0)
	c.Assert(t.db.Pool().Len(), Equals, 0)

	// Unreliable check that previous query was cancelled.
	var count int
	_, err = t.db.QueryOne(pg.LoadInto(&count), "SELECT COUNT(*) FROM pg_stat_activity")
	c.Assert(err, IsNil)
	c.Assert(count, Equals, 1)
}
Пример #16
0
func (t *DBTest) TestQueryOneValue(c *C) {
	var v int
	_, err := t.db.QueryOne(pg.LoadInto(&v), "SELECT 1 AS num")
	c.Assert(err, IsNil)
	c.Assert(v, Equals, 1)
}
Пример #17
0
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>
}
Пример #18
0
func (t *DBTest) TestTypeEmptyStringArray(c *C) {
	var dst []string
	_, err := t.db.QueryOne(pg.LoadInto(&dst), "SELECT ?::text[]", []string{})
	c.Assert(err, IsNil)
	c.Assert(dst, DeepEquals, []string{})
}