Skip to content

Commit 7ab9afd

Browse files
committed
Add some OID types
1 parent f9717e6 commit 7ab9afd

2 files changed

Lines changed: 56 additions & 10 deletions

File tree

listener/pg_type.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package listener
2+
3+
// PostgreSQL OIDs
4+
// https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat
5+
const (
6+
Int2OID = 21
7+
Int4OID = 23
8+
Int8OID = 20
9+
10+
TextOID = 25
11+
VarcharOID = 1043
12+
13+
TimestampOID = 1114
14+
TimestamptzOID = 1184
15+
DateOID = 1082
16+
TimeOID = 1083
17+
18+
JSONBOID = 3802
19+
UUIDOID = 2950
20+
BoolOID = 16
21+
)

listener/wal_transaction.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/google/uuid"
10-
"github.com/jackc/pgx/pgtype"
1110
"github.com/sirupsen/logrus"
1211
)
1312

@@ -67,26 +66,52 @@ type Column struct {
6766
// AssertValue converts bytes to a specific type depending
6867
// on the type of this data in the database table.
6968
func (c *Column) AssertValue(src []byte) {
70-
var val interface{}
69+
var (
70+
val any
71+
err error
72+
)
73+
7174
if src == nil {
7275
c.value = nil
7376
return
7477
}
78+
7579
strSrc := string(src)
80+
81+
const (
82+
timestampLayout = "2006-01-02 15:04:05"
83+
timestampWithTZLayout = "2006-01-02 15:04:05.000000-07"
84+
)
85+
7686
switch c.valueType {
77-
case pgtype.BoolOID:
78-
val, _ = strconv.ParseBool(strSrc)
79-
case pgtype.Int4OID:
80-
val, _ = strconv.Atoi(strSrc)
81-
case pgtype.TextOID, pgtype.VarcharOID:
87+
case BoolOID:
88+
val, err = strconv.ParseBool(strSrc)
89+
case Int2OID, Int4OID:
90+
val, err = strconv.Atoi(strSrc)
91+
case Int8OID:
92+
val, err = strconv.ParseInt(strSrc, 10, 64)
93+
case TextOID, VarcharOID:
8294
val = strSrc
83-
case pgtype.TimestampOID, pgtype.TimestamptzOID:
95+
case TimestampOID:
96+
val, err = time.Parse(timestampLayout, strSrc)
97+
case TimestamptzOID:
98+
val, err = time.Parse(timestampWithTZLayout, strSrc)
99+
case DateOID, TimeOID:
100+
val = strSrc
101+
case UUIDOID:
102+
val, err = uuid.Parse(strSrc)
103+
case JSONBOID:
84104
val = strSrc
85105
default:
86-
logrus.WithField("pgtype", c.valueType).
87-
Warnln("unknown oid type")
106+
logrus.WithFields(logrus.Fields{"pgtype": c.valueType, "column_name": c.name}).Warnln("unknown oid type")
88107
val = strSrc
89108
}
109+
110+
if err != nil {
111+
logrus.WithError(err).WithFields(logrus.Fields{"pgtype": c.valueType, "column_name": c.name}).
112+
Errorln("column data parse error")
113+
}
114+
90115
c.value = val
91116
}
92117

0 commit comments

Comments
 (0)