pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Allow custom types implement both pgx.Scanner and sql.Scanner

Open anton-becker opened this issue 9 years ago • 1 comments

Current state:

type sql.Scanner interface {
    Scan(src interface{}) error
}

and

type pgx.Scanner interface {
    Scan(r *ValueReader) error
}

Custom types cannot implement both sql and pgx Scanner interfaces (because it is impossible to to have 2 methods with same name Scan but with different arguments). Moreover there is no such conflict between sql/driver.Valuer and pgx.Encoder. Implementation of both interfaces may be useful in some cases. For example, I have implemented some PostgreSQL types and the only stopper from make it useful with both Pgx & Pq (and any other go-sql supported drivers) is this issue (more details here). "sql.Scanner"-only implementation looks like ugly because it is not "native" for "Pgx".

Looks like it is easy to fix this issue even with backward compatibility: just define new type

type pgx.<NewScanner> interface {
    <NewScan>(r *ValueReader) error // <NewScan> != "Scan"
}

(all code referenced to current pgx.Scanner can be just copied for pgx.<NewScanner>). Current pgx.Scanner may be simply marked as deprecated to avoid multiple interfaces in docs. Better code understandable can be achieved by

type pgx.Scanner interface {
    <NewScan>(r *ValueReader) error // <NewScan> != "Scan"
}

// Deprecated: bla-bla-bla
type pgx.<DeprecatedScanner> interface {
    Scan(r *ValueReader) error
}

, but this will have limited backward compatibility (but may be enough for most users).

PS: I prefer to set <NewScan> to something like PgxScan to prevent conflict in future.

anton-becker avatar Jan 29 '17 14:01 anton-becker

This had previously been discussed in #78. At the time I recommended defining separate types, but I'm convinced now. I took the perfectly backwards compatible approach. Next major release we could remove, rename, or make private the deprecated interface.

jackc avatar Feb 03 '17 02:02 jackc