mirror of https://github.com/jackc/pgx.git
Merge pull request #2035 from exekias/fix-interval
Fix interval encoding to allow 0s and avoid extra spacespull/1278/merge
commit
ec557e87d5
|
@ -135,9 +135,6 @@ func (encodePlanIntervalCodecText) Encode(value any, buf []byte) (newBuf []byte,
|
||||||
buf = append(buf, " day "...)
|
buf = append(buf, " day "...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if interval.Microseconds != 0 {
|
|
||||||
buf = append(buf, " "...)
|
|
||||||
|
|
||||||
absMicroseconds := interval.Microseconds
|
absMicroseconds := interval.Microseconds
|
||||||
if absMicroseconds < 0 {
|
if absMicroseconds < 0 {
|
||||||
absMicroseconds = -absMicroseconds
|
absMicroseconds = -absMicroseconds
|
||||||
|
@ -155,7 +152,6 @@ func (encodePlanIntervalCodecText) Encode(value any, buf []byte) (newBuf []byte,
|
||||||
if microseconds != 0 {
|
if microseconds != 0 {
|
||||||
buf = append(buf, fmt.Sprintf(".%06d", microseconds)...)
|
buf = append(buf, fmt.Sprintf(".%06d", microseconds)...)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxtest"
|
"github.com/jackc/pgx/v5/pgxtest"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIntervalCodec(t *testing.T) {
|
func TestIntervalCodec(t *testing.T) {
|
||||||
|
@ -136,3 +137,22 @@ func TestIntervalCodec(t *testing.T) {
|
||||||
{nil, new(pgtype.Interval), isExpectedEq(pgtype.Interval{})},
|
{nil, new(pgtype.Interval), isExpectedEq(pgtype.Interval{})},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntervalTextEncode(t *testing.T) {
|
||||||
|
m := pgtype.NewMap()
|
||||||
|
|
||||||
|
successfulTests := []struct {
|
||||||
|
source pgtype.Interval
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{source: pgtype.Interval{Months: 2, Days: 1, Microseconds: 0, Valid: true}, result: "2 mon 1 day 00:00:00"},
|
||||||
|
{source: pgtype.Interval{Months: 0, Days: 0, Microseconds: 0, Valid: true}, result: "00:00:00"},
|
||||||
|
{source: pgtype.Interval{Months: 0, Days: 0, Microseconds: 6 * 60 * 1000000, Valid: true}, result: "00:06:00"},
|
||||||
|
{source: pgtype.Interval{Months: 0, Days: 1, Microseconds: 6*60*1000000 + 30, Valid: true}, result: "1 day 00:06:00.000030"},
|
||||||
|
}
|
||||||
|
for i, tt := range successfulTests {
|
||||||
|
buf, err := m.Encode(pgtype.DateOID, pgtype.TextFormatCode, tt.source, nil)
|
||||||
|
assert.NoErrorf(t, err, "%d", i)
|
||||||
|
assert.Equalf(t, tt.result, string(buf), "%d", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue