func waitRow(c *C, conn *pgx.Conn, n int) { var res int64 err := queryAttempts.Run(func() error { return conn.QueryRow("SELECT id FROM test WHERE id = $1", n).Scan(&res) }) c.Assert(err, IsNil) }
func execQuery64(conn *pgx.Conn, stmt string, arguments ...interface{}) int64 { var err error var result int64 err = conn.QueryRow(stmt, arguments...).Scan(&result) checkErr(err) return result }
func testJsonInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) { input := []int{1, 2, 234432} var output []int16 err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err == nil || err.Error() != "can't scan into dest[0]: json: cannot unmarshal number 234432 into Go value of type int16" { t.Errorf("%s: Expected *json.UnmarkalTypeError, but got %v", typename, err) } }
func testJsonInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) { input := []int{1, 2, 234432} var output []int16 err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if _, ok := err.(*json.UnmarshalTypeError); !ok { t.Errorf("%s: Expected *json.UnmarkalTypeError, but got %v", typename, err) } }
func benchmarkSelectWithLog(b *testing.B, conn *pgx.Conn) { _, err := conn.Prepare("test", "select 1::int4, 'johnsmith', '*****@*****.**', 'John Smith', 'male', '1970-01-01'::date, '2015-01-01 00:00:00'::timestamptz") if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { var record struct { id int32 userName string email string name string sex string birthDate time.Time lastLoginTime time.Time } err = conn.QueryRow("test").Scan( &record.id, &record.userName, &record.email, &record.name, &record.sex, &record.birthDate, &record.lastLoginTime, ) if err != nil { b.Fatal(err) } // These checks both ensure that the correct data was returned // and provide a benchmark of accessing the returned values. if record.id != 1 { b.Fatalf("bad value for id: %v", record.id) } if record.userName != "johnsmith" { b.Fatalf("bad value for userName: %v", record.userName) } if record.email != "*****@*****.**" { b.Fatalf("bad value for email: %v", record.email) } if record.name != "John Smith" { b.Fatalf("bad value for name: %v", record.name) } if record.sex != "male" { b.Fatalf("bad value for sex: %v", record.sex) } if record.birthDate != time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) { b.Fatalf("bad value for birthDate: %v", record.birthDate) } if record.lastLoginTime != time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local) { b.Fatalf("bad value for lastLoginTime: %v", record.lastLoginTime) } } }
func testJsonInt64Array(t *testing.T, conn *pgx.Conn, typename string) { input := []int64{1, 2, 234432} var output []int64 err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } if !reflect.DeepEqual(input, output) { t.Errorf("%s: Did not transcode []int64 successfully: %v is not %v", typename, input, output) } }
func testJsonStringArray(t *testing.T, conn *pgx.Conn, typename string) { input := []string{"foo", "bar", "baz"} var output []string err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } if !reflect.DeepEqual(input, output) { t.Errorf("%s: Did not transcode []string successfully: %v is not %v", typename, input, output) } }
func waitReadWrite(c *C, conn *pgx.Conn) { var readOnly string err := queryAttempts.Run(func() error { if err := conn.QueryRow("SHOW default_transaction_read_only").Scan(&readOnly); err != nil { return err } if readOnly == "off" { return nil } return fmt.Errorf("transaction readonly is %q", readOnly) }) c.Assert(err, IsNil) }
func testJsonSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) { input := map[string]string{"key": "value"} var output map[string]string err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return } if !reflect.DeepEqual(input, output) { t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, input, output) return } }
func waitRecovered(c *C, conn *pgx.Conn) { var recovery bool err := queryAttempts.Run(func() error { err := conn.QueryRow("SELECT pg_is_in_recovery()").Scan(&recovery) if err != nil { return err } if recovery { return fmt.Errorf("in recovery") } return nil }) c.Assert(err, IsNil) }
func testJsonNestedMap(t *testing.T, conn *pgx.Conn, typename string) { input := map[string]interface{}{ "name": "Uncanny", "stats": map[string]interface{}{"hp": float64(107), "maxhp": float64(150)}, "inventory": []interface{}{"phone", "key"}, } var output map[string]interface{} err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return } if !reflect.DeepEqual(input, output) { t.Errorf("%s: Did not transcode map[string]interface{} successfully: %v is not %v", typename, input, output) return } }
func testJsonStruct(t *testing.T, conn *pgx.Conn, typename string) { type person struct { Name string `json:"name"` Age int `json:"age"` } input := person{ Name: "John", Age: 42, } var output person err := conn.QueryRow("select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } if !reflect.DeepEqual(input, output) { t.Errorf("%s: Did not transcode struct successfully: %v is not %v", typename, input, output) } }
func assertRecovery(c *C, conn *pgx.Conn) { var recovery bool err := conn.QueryRow("SELECT pg_is_in_recovery()").Scan(&recovery) c.Assert(err, IsNil) c.Assert(recovery, Equals, true) }
func assertDownstream(c *C, conn *pgx.Conn, n int) { var res string err := conn.QueryRow("SELECT client_addr FROM pg_stat_replication WHERE application_name = $1", fmt.Sprintf("node%d", n)).Scan(&res) c.Assert(err, IsNil) }