mirror of https://github.com/jackc/pgx.git
Merge common JSON and JSONB
parent
7383e240d4
commit
9d200733b9
4
conn.go
4
conn.go
|
@ -799,9 +799,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{}
|
||||||
err = encodeTimestampArray(wbuf, arguments[i], TimestampTzOid)
|
err = encodeTimestampArray(wbuf, arguments[i], TimestampTzOid)
|
||||||
case OidOid:
|
case OidOid:
|
||||||
err = encodeOid(wbuf, arguments[i])
|
err = encodeOid(wbuf, arguments[i])
|
||||||
case JsonOid:
|
case JsonOid, JsonbOid:
|
||||||
err = encodeJson(wbuf, arguments[i])
|
|
||||||
case JsonbOid:
|
|
||||||
err = encodeJson(wbuf, arguments[i])
|
err = encodeJson(wbuf, arguments[i])
|
||||||
default:
|
default:
|
||||||
return SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
return SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
||||||
|
|
4
query.go
4
query.go
|
@ -285,9 +285,7 @@ func (rows *Rows) Scan(dest ...interface{}) (err error) {
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
switch vr.Type().DataType {
|
switch vr.Type().DataType {
|
||||||
case JsonOid:
|
case JsonOid, JsonbOid:
|
||||||
decodeJson(vr, &d)
|
|
||||||
case JsonbOid:
|
|
||||||
decodeJson(vr, &d)
|
decodeJson(vr, &d)
|
||||||
default:
|
default:
|
||||||
rows.Fatal(fmt.Errorf("Scan cannot decode into %T", d))
|
rows.Fatal(fmt.Errorf("Scan cannot decode into %T", d))
|
||||||
|
|
|
@ -65,51 +65,45 @@ func TestTimestampTzTranscode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJsonTranscode(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)
|
||||||
|
|
||||||
if _, ok := conn.PgTypes[pgx.JsonOid]; !ok {
|
for _, oid := range []pgx.Oid{pgx.JsonOid, pgx.JsonbOid} {
|
||||||
return // No JSON type -- must be running against old PostgreSQL
|
if _, ok := conn.PgTypes[oid]; !ok {
|
||||||
|
return // No JSON/JSONB type -- must be running against old PostgreSQL
|
||||||
}
|
}
|
||||||
|
typename := conn.PgTypes[oid].Name
|
||||||
|
|
||||||
m := map[string]string{
|
// Test single level objects with map[string]string
|
||||||
"key": "value",
|
inStringMap := map[string]string{"key": "value"}
|
||||||
}
|
var outStringMap map[string]string
|
||||||
var outputJson map[string]string
|
err := conn.QueryRow("select $1::"+typename, inStringMap).Scan(&outStringMap)
|
||||||
|
|
||||||
err := conn.QueryRow("select $1::json", m).Scan(&outputJson)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("QueryRow Scan failed: %v", err)
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
}
|
|
||||||
if m["key"] != outputJson["key"] {
|
|
||||||
t.Errorf("Did not transcode json successfully: %v is not %v", outputJson["key"], m["key"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestJsonbTranscode(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
conn := mustConnect(t, *defaultConnConfig)
|
|
||||||
defer closeConn(t, conn)
|
|
||||||
|
|
||||||
if _, ok := conn.PgTypes[pgx.JsonbOid]; !ok {
|
|
||||||
return // No JSONB type -- must be running against old PostgreSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m := map[string]string{
|
if !reflect.DeepEqual(inStringMap, outStringMap) {
|
||||||
"key": "value",
|
t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, inStringMap, outStringMap)
|
||||||
}
|
}
|
||||||
var outputJson map[string]string
|
|
||||||
|
|
||||||
err := conn.QueryRow("select $1::jsonb", m).Scan(&outputJson)
|
// Test nested objects with map[string]interface{}
|
||||||
|
inNestedMap := map[string]interface{}{
|
||||||
|
"name": "Uncanny",
|
||||||
|
"stats": map[string]interface{}{"hp": 107, "maxhp": 150},
|
||||||
|
"inventory": []string{"phone", "key"},
|
||||||
|
}
|
||||||
|
var outNestedMap map[string]interface{}
|
||||||
|
err = conn.QueryRow("select $1::"+typename, inNestedMap).Scan(&outNestedMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("QueryRow Scan failed: %v", err)
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(inStringMap, outStringMap) {
|
||||||
|
t.Errorf("%s: Did not transcode map[string]interface{} successfully: %v is not %v", typename, inStringMap, outStringMap)
|
||||||
}
|
}
|
||||||
if m["key"] != outputJson["key"] {
|
|
||||||
t.Errorf("Did not transcode jsonb successfully: %v is not %v", outputJson["key"], m["key"])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue