Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Commit 0f1512e

Browse files
AmineChikhaouijackc
authored andcommitted
Adding support for Ltree binary encoding
When used with the COPY protocol, the ltree type would currently result in errors such as: ``` ERROR: unsupported ltree version number 65 (SQLSTATE XX000) ``` This is an attempt to fix this problem by properly adding a byte prefix to the ltree string as implemented by `ltree_send` and `ltree_receive` from the PostgreSQL source: https://github.com/postgres/postgres/blob/master/contrib/ltree/ltree_io.c#L202 https://github.com/postgres/postgres/blob/master/contrib/ltree/ltree_io.c#L226
1 parent 7294846 commit 0f1512e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

ltree.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package pgtype
2+
3+
import (
4+
"database/sql/driver"
5+
)
6+
7+
type Ltree Text
8+
9+
func (dst *Ltree) Set(src interface{}) error {
10+
return (*Text)(dst).Set(src)
11+
}
12+
13+
func (dst Ltree) Get() interface{} {
14+
return (Text)(dst).Get()
15+
}
16+
17+
func (src *Ltree) AssignTo(dst interface{}) error {
18+
return (*Text)(src).AssignTo(dst)
19+
}
20+
21+
func (src Ltree) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
22+
return (Text)(src).EncodeText(ci, buf)
23+
}
24+
25+
func (src Ltree) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
26+
switch src.Status {
27+
case Null:
28+
return nil, nil
29+
case Undefined:
30+
return nil, errUndefined
31+
}
32+
buf = append(buf, 1)
33+
return append(buf, src.String...), nil
34+
}
35+
36+
func (Ltree) PreferredResultFormat() int16 {
37+
return TextFormatCode
38+
}
39+
40+
func (dst *Ltree) DecodeText(ci *ConnInfo, src []byte) error {
41+
return (*Text)(dst).DecodeText(ci, src)
42+
}
43+
44+
func (dst *Ltree) DecodeBinary(ci *ConnInfo, src []byte) error {
45+
return (*Text)(dst).DecodeBinary(ci, src)
46+
}
47+
48+
func (Ltree) PreferredParamFormat() int16 {
49+
return TextFormatCode
50+
}
51+
52+
func (dst *Ltree) Scan(src interface{}) error {
53+
return (*Text)(dst).Scan(src)
54+
}
55+
56+
func (src Ltree) Value() (driver.Value, error) {
57+
return (Text)(src).Value()
58+
}

pgtype.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const (
8484
TstzrangeArrayOID = 3911
8585
Int8rangeOID = 3926
8686
Int8multirangeOID = 4536
87+
LtreeOID = 16407
8788
)
8889

8990
type Status byte
@@ -327,6 +328,7 @@ func NewConnInfo() *ConnInfo {
327328
ci.RegisterDataType(DataType{Value: &Varbit{}, Name: "varbit", OID: VarbitOID})
328329
ci.RegisterDataType(DataType{Value: &Varchar{}, Name: "varchar", OID: VarcharOID})
329330
ci.RegisterDataType(DataType{Value: &XID{}, Name: "xid", OID: XIDOID})
331+
ci.RegisterDataType(DataType{Value: &Ltree{}, Name: "ltree", OID: LtreeOID})
330332

331333
registerDefaultPgTypeVariants := func(name, arrayName string, value interface{}) {
332334
ci.RegisterDefaultPgType(value, name)
@@ -973,6 +975,7 @@ func init() {
973975
"jsonb": &JSONB{},
974976
"line": &Line{},
975977
"lseg": &Lseg{},
978+
"ltree": &Ltree{},
976979
"macaddr": &Macaddr{},
977980
"name": &Name{},
978981
"numeric": &Numeric{},

0 commit comments

Comments
 (0)