|
7 | 7 | "time" |
8 | 8 |
|
9 | 9 | "github.com/google/uuid" |
10 | | - "github.com/jackc/pgx/pgtype" |
11 | 10 | "github.com/sirupsen/logrus" |
12 | 11 | ) |
13 | 12 |
|
@@ -67,26 +66,52 @@ type Column struct { |
67 | 66 | // AssertValue converts bytes to a specific type depending |
68 | 67 | // on the type of this data in the database table. |
69 | 68 | func (c *Column) AssertValue(src []byte) { |
70 | | - var val interface{} |
| 69 | + var ( |
| 70 | + val any |
| 71 | + err error |
| 72 | + ) |
| 73 | + |
71 | 74 | if src == nil { |
72 | 75 | c.value = nil |
73 | 76 | return |
74 | 77 | } |
| 78 | + |
75 | 79 | 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 | + |
76 | 86 | 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: |
82 | 94 | 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: |
84 | 104 | val = strSrc |
85 | 105 | 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") |
88 | 107 | val = strSrc |
89 | 108 | } |
| 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 | + |
90 | 115 | c.value = val |
91 | 116 | } |
92 | 117 |
|
|
0 commit comments