mirror of https://github.com/jackc/pgx.git
Rename Json(b) to JSON(B)
parent
214443deb7
commit
04c02cf3d3
|
@ -12,7 +12,7 @@ func Example_JSON() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := conn.PgTypes[pgx.JsonOID]; !ok {
|
if _, ok := conn.PgTypes[pgx.JSONOID]; !ok {
|
||||||
// No JSON type -- must be running against very old PostgreSQL
|
// No JSON type -- must be running against very old PostgreSQL
|
||||||
// Pretend it works
|
// Pretend it works
|
||||||
fmt.Println("John", 42)
|
fmt.Println("John", 42)
|
||||||
|
|
6
query.go
6
query.go
|
@ -298,7 +298,7 @@ func (rows *Rows) Scan(dest ...interface{}) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rows.Fatal(scanArgError{col: i, err: err})
|
rows.Fatal(scanArgError{col: i, err: err})
|
||||||
}
|
}
|
||||||
} else if vr.Type().DataType == JsonOID || vr.Type().DataType == JsonbOID {
|
} else if vr.Type().DataType == JSONOID || vr.Type().DataType == JSONBOID {
|
||||||
// Because the argument passed to decodeJSON will escape the heap.
|
// Because the argument passed to decodeJSON will escape the heap.
|
||||||
// This allows d to be stack allocated and only copied to the heap when
|
// This allows d to be stack allocated and only copied to the heap when
|
||||||
// we actually are decoding JSON. This saves one memory allocation per
|
// we actually are decoding JSON. This saves one memory allocation per
|
||||||
|
@ -387,11 +387,11 @@ func (rows *Rows) Values() ([]interface{}, error) {
|
||||||
values = append(values, decodeTimestamp(vr))
|
values = append(values, decodeTimestamp(vr))
|
||||||
case InetOID, CidrOID:
|
case InetOID, CidrOID:
|
||||||
values = append(values, decodeInet(vr))
|
values = append(values, decodeInet(vr))
|
||||||
case JsonOID:
|
case JSONOID:
|
||||||
var d interface{}
|
var d interface{}
|
||||||
decodeJSON(vr, &d)
|
decodeJSON(vr, &d)
|
||||||
values = append(values, d)
|
values = append(values, d)
|
||||||
case JsonbOID:
|
case JSONBOID:
|
||||||
var d interface{}
|
var d interface{}
|
||||||
decodeJSON(vr, &d)
|
decodeJSON(vr, &d)
|
||||||
values = append(values, d)
|
values = append(values, d)
|
||||||
|
|
2
v3.md
2
v3.md
|
@ -1,3 +1,5 @@
|
||||||
# V3 Changes
|
# V3 Changes
|
||||||
|
|
||||||
Rename Oid to OID in accordance with Go conventions.
|
Rename Oid to OID in accordance with Go conventions.
|
||||||
|
|
||||||
|
Rename Json(b) to JSON(B) in accordance with Go conventions.
|
||||||
|
|
10
values.go
10
values.go
|
@ -22,7 +22,7 @@ const (
|
||||||
Int4OID = 23
|
Int4OID = 23
|
||||||
TextOID = 25
|
TextOID = 25
|
||||||
OIDOID = 26
|
OIDOID = 26
|
||||||
JsonOID = 114
|
JSONOID = 114
|
||||||
CidrOID = 650
|
CidrOID = 650
|
||||||
CidrArrayOID = 651
|
CidrArrayOID = 651
|
||||||
Float4OID = 700
|
Float4OID = 700
|
||||||
|
@ -47,7 +47,7 @@ const (
|
||||||
TimestampTzArrayOID = 1185
|
TimestampTzArrayOID = 1185
|
||||||
RecordOID = 2249
|
RecordOID = 2249
|
||||||
UuidOID = 2950
|
UuidOID = 2950
|
||||||
JsonbOID = 3802
|
JSONBOID = 3802
|
||||||
)
|
)
|
||||||
|
|
||||||
// PostgreSQL format codes
|
// PostgreSQL format codes
|
||||||
|
@ -627,7 +627,7 @@ func Encode(wbuf *WriteBuf, oid OID, arg interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if oid == JsonOID || oid == JsonbOID {
|
if oid == JSONOID || oid == JSONBOID {
|
||||||
return encodeJSON(wbuf, oid, arg)
|
return encodeJSON(wbuf, oid, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1463,7 +1463,7 @@ func decodeJSON(vr *ValueReader, d interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if vr.Type().DataType != JsonOID && vr.Type().DataType != JsonbOID {
|
if vr.Type().DataType != JSONOID && vr.Type().DataType != JSONBOID {
|
||||||
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into json", vr.Type().DataType)))
|
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into json", vr.Type().DataType)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1476,7 +1476,7 @@ func decodeJSON(vr *ValueReader, d interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeJSON(w *WriteBuf, oid OID, value interface{}) error {
|
func encodeJSON(w *WriteBuf, oid OID, value interface{}) error {
|
||||||
if oid != JsonOID && oid != JsonbOID {
|
if oid != JSONOID && oid != JSONBOID {
|
||||||
return fmt.Errorf("cannot encode JSON into oid %v", oid)
|
return fmt.Errorf("cannot encode JSON into oid %v", oid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,30 +78,30 @@ func TestTimestampTzTranscode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJsonAndJsonbTranscode(t *testing.T) {
|
func TestJSONAndJSONBTranscode(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
conn := mustConnect(t, *defaultConnConfig)
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
for _, oid := range []pgx.OID{pgx.JsonOID, pgx.JsonbOID} {
|
for _, oid := range []pgx.OID{pgx.JSONOID, pgx.JSONBOID} {
|
||||||
if _, ok := conn.PgTypes[oid]; !ok {
|
if _, ok := conn.PgTypes[oid]; !ok {
|
||||||
return // No JSON/JSONB type -- must be running against old PostgreSQL
|
return // No JSON/JSONB type -- must be running against old PostgreSQL
|
||||||
}
|
}
|
||||||
typename := conn.PgTypes[oid].Name
|
typename := conn.PgTypes[oid].Name
|
||||||
|
|
||||||
testJsonString(t, conn, typename)
|
testJSONString(t, conn, typename)
|
||||||
testJsonStringPointer(t, conn, typename)
|
testJSONStringPointer(t, conn, typename)
|
||||||
testJsonSingleLevelStringMap(t, conn, typename)
|
testJSONSingleLevelStringMap(t, conn, typename)
|
||||||
testJsonNestedMap(t, conn, typename)
|
testJSONNestedMap(t, conn, typename)
|
||||||
testJsonStringArray(t, conn, typename)
|
testJSONStringArray(t, conn, typename)
|
||||||
testJsonInt64Array(t, conn, typename)
|
testJSONInt64Array(t, conn, typename)
|
||||||
testJsonInt16ArrayFailureDueToOverflow(t, conn, typename)
|
testJSONInt16ArrayFailureDueToOverflow(t, conn, typename)
|
||||||
testJsonStruct(t, conn, typename)
|
testJSONStruct(t, conn, typename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonString(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONString(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := `{"key": "value"}`
|
input := `{"key": "value"}`
|
||||||
expectedOutput := map[string]string{"key": "value"}
|
expectedOutput := map[string]string{"key": "value"}
|
||||||
var output map[string]string
|
var output map[string]string
|
||||||
|
@ -117,7 +117,7 @@ func testJsonString(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonStringPointer(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONStringPointer(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := `{"key": "value"}`
|
input := `{"key": "value"}`
|
||||||
expectedOutput := map[string]string{"key": "value"}
|
expectedOutput := map[string]string{"key": "value"}
|
||||||
var output map[string]string
|
var output map[string]string
|
||||||
|
@ -133,7 +133,7 @@ func testJsonStringPointer(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := map[string]string{"key": "value"}
|
input := map[string]string{"key": "value"}
|
||||||
var output map[string]string
|
var output map[string]string
|
||||||
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
@ -148,7 +148,7 @@ func testJsonSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonNestedMap(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONNestedMap(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := map[string]interface{}{
|
input := map[string]interface{}{
|
||||||
"name": "Uncanny",
|
"name": "Uncanny",
|
||||||
"stats": map[string]interface{}{"hp": float64(107), "maxhp": float64(150)},
|
"stats": map[string]interface{}{"hp": float64(107), "maxhp": float64(150)},
|
||||||
|
@ -167,7 +167,7 @@ func testJsonNestedMap(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonStringArray(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONStringArray(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := []string{"foo", "bar", "baz"}
|
input := []string{"foo", "bar", "baz"}
|
||||||
var output []string
|
var output []string
|
||||||
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
@ -180,7 +180,7 @@ func testJsonStringArray(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonInt64Array(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONInt64Array(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := []int64{1, 2, 234432}
|
input := []int64{1, 2, 234432}
|
||||||
var output []int64
|
var output []int64
|
||||||
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
@ -193,7 +193,7 @@ func testJsonInt64Array(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
input := []int{1, 2, 234432}
|
input := []int{1, 2, 234432}
|
||||||
var output []int16
|
var output []int16
|
||||||
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
@ -202,7 +202,7 @@ func testJsonInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typena
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJsonStruct(t *testing.T, conn *pgx.Conn, typename string) {
|
func testJSONStruct(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
type person struct {
|
type person struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Age int `json:"age"`
|
Age int `json:"age"`
|
||||||
|
|
Loading…
Reference in New Issue