Fix: json values work with sql.Scanner

https://github.com/jackc/pgx/issues/1418
pull/1425/head
Jack Christensen 2022-12-06 19:41:25 -06:00
parent 17f8f7af63
commit 279c3c0a20
2 changed files with 12 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package pgtype
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
@ -82,6 +83,13 @@ func (JSONCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan
return scanPlanJSONToByteSlice{}
case BytesScanner:
return scanPlanBinaryBytesToBytesScanner{}
// Cannot rely on sql.Scanner being handled later because scanPlanJSONToJSONUnmarshal will take precedence.
//
// https://github.com/jackc/pgx/issues/1418
case sql.Scanner:
return &scanPlanSQLScanner{formatCode: format}
default:
return scanPlanJSONToJSONUnmarshal{}
}

View File

@ -2,6 +2,7 @@ package pgtype_test
import (
"context"
"database/sql"
"testing"
pgx "github.com/jackc/pgx/v5"
@ -48,6 +49,9 @@ func TestJSONCodec(t *testing.T) {
{map[string]any(nil), new([]byte), isExpectedEqBytes([]byte(nil))},
{[]byte(nil), new([]byte), isExpectedEqBytes([]byte(nil))},
{nil, new([]byte), isExpectedEqBytes([]byte(nil))},
// Test sql.Scanner. (https://github.com/jackc/pgx/issues/1418)
{"42", new(sql.NullInt64), isExpectedEq(sql.NullInt64{Int64: 42, Valid: true})},
})
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{