Skip to content

Commit

Permalink
Add some OID types
Browse files Browse the repository at this point in the history
  • Loading branch information
ihippik committed Sep 12, 2022
1 parent f9717e6 commit 7ab9afd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
21 changes: 21 additions & 0 deletions listener/pg_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package listener

// PostgreSQL OIDs
// https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat
const (
Int2OID = 21
Int4OID = 23
Int8OID = 20

TextOID = 25
VarcharOID = 1043

TimestampOID = 1114
TimestamptzOID = 1184
DateOID = 1082
TimeOID = 1083

JSONBOID = 3802
UUIDOID = 2950
BoolOID = 16
)
45 changes: 35 additions & 10 deletions listener/wal_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/pgtype"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -67,26 +66,52 @@ type Column struct {
// AssertValue converts bytes to a specific type depending
// on the type of this data in the database table.
func (c *Column) AssertValue(src []byte) {
var val interface{}
var (
val any
err error
)

if src == nil {
c.value = nil
return
}

strSrc := string(src)

const (
timestampLayout = "2006-01-02 15:04:05"
timestampWithTZLayout = "2006-01-02 15:04:05.000000-07"
)

switch c.valueType {
case pgtype.BoolOID:
val, _ = strconv.ParseBool(strSrc)
case pgtype.Int4OID:
val, _ = strconv.Atoi(strSrc)
case pgtype.TextOID, pgtype.VarcharOID:
case BoolOID:
val, err = strconv.ParseBool(strSrc)
case Int2OID, Int4OID:
val, err = strconv.Atoi(strSrc)
case Int8OID:
val, err = strconv.ParseInt(strSrc, 10, 64)
case TextOID, VarcharOID:
val = strSrc
case pgtype.TimestampOID, pgtype.TimestamptzOID:
case TimestampOID:
val, err = time.Parse(timestampLayout, strSrc)
case TimestamptzOID:
val, err = time.Parse(timestampWithTZLayout, strSrc)
case DateOID, TimeOID:
val = strSrc
case UUIDOID:
val, err = uuid.Parse(strSrc)
case JSONBOID:
val = strSrc
default:
logrus.WithField("pgtype", c.valueType).
Warnln("unknown oid type")
logrus.WithFields(logrus.Fields{"pgtype": c.valueType, "column_name": c.name}).Warnln("unknown oid type")
val = strSrc
}

if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{"pgtype": c.valueType, "column_name": c.name}).
Errorln("column data parse error")
}

c.value = val
}

Expand Down

0 comments on commit 7ab9afd

Please sign in to comment.