-
Notifications
You must be signed in to change notification settings - Fork 1
feat: PostgreSQL type OID mapping + encode/decode #127
Copy link
Copy link
Open
Labels
area/driverDatabase/cache driver infrastructureDatabase/cache driver infrastructuredriver/postgresPostgreSQL driverPostgreSQL driver
Milestone
Description
Summary
Implement PostgreSQL type OID mapping and value encoding/decoding for the most common data types. This bridges Go types to PostgreSQL wire format.
Design
Package: driver/postgres/protocol/types.go
Core Type OIDs
| OID | PostgreSQL Type | Go Type |
|---|---|---|
| 16 | bool | bool |
| 20 | int8 | int64 |
| 21 | int2 | int16 |
| 23 | int4 | int32 |
| 25 | text | string |
| 700 | float4 | float32 |
| 701 | float8 | float64 |
| 1043 | varchar | string |
| 1082 | date | time.Time |
| 1114 | timestamp | time.Time |
| 1184 | timestamptz | time.Time |
| 17 | bytea | []byte |
| 2950 | uuid | [16]byte / string |
| 3802 | jsonb | []byte / json.RawMessage |
| 1700 | numeric | string (or big.Rat) |
| 1009 | text[] | []string |
| 1016 | int8[] | []int64 |
Encoding (Go → PG wire)
type Encoder struct{}
func (e *Encoder) Encode(oid uint32, value any, buf []byte) ([]byte, error)
func (e *Encoder) EncodeBinary(oid uint32, value any, buf []byte) ([]byte, error)
func (e *Encoder) EncodeText(oid uint32, value any, buf []byte) ([]byte, error)Decoding (PG wire → Go)
type Decoder struct{}
func (d *Decoder) Decode(oid uint32, format int16, data []byte) (any, error)
func (d *Decoder) DecodeBinary(oid uint32, data []byte) (any, error)
func (d *Decoder) DecodeText(oid uint32, data []byte) (any, error)Binary Format Details
- int2/int4/int8: big-endian fixed-width
- float4/float8: IEEE 754 big-endian
- bool: single byte (0/1)
- text/varchar: raw UTF-8 bytes
- timestamp: microseconds since 2000-01-01 00:00:00 UTC
- date: days since 2000-01-01
- bytea: raw bytes
- uuid: 16 raw bytes
- arrays: array header (ndim, flags, element OID) + dimensions + elements
database/sql.Scanner / driver.Valuer Support
- Types implementing
driver.Valuerare encoded via theirValue()method Scantargets implementingsql.Scannerreceive decoded Go values
Acceptance Criteria
- OID constants for all common types
- Text encoding/decoding for all listed types
- Binary encoding/decoding for int, float, bool, text, bytea, timestamp, uuid
- Array encoding/decoding (text[] and int8[] at minimum)
- NULL handling (nil → SQL NULL, SQL NULL → nil)
-
driver.Valuerandsql.Scannerinterface support - Unit tests with known PG binary representations
- Benchmark: encode/decode throughput for common types
Dependencies
- Depends on 123 (wire protocol framing — uses Writer/Reader)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area/driverDatabase/cache driver infrastructureDatabase/cache driver infrastructuredriver/postgresPostgreSQL driverPostgreSQL driver