sanatize time to within microsecond accuracy

pull/700/head
Andrew Nicoll 2020-03-27 13:03:36 +00:00
parent af03592c0f
commit 816e95d3ee
2 changed files with 7 additions and 1 deletions

View File

@ -48,7 +48,7 @@ func (q *Query) Sanitize(args ...interface{}) (string, error) {
case string: case string:
str = QuoteString(arg) str = QuoteString(arg)
case time.Time: case time.Time:
str = arg.Format("'2006-01-02 15:04:05.999999999Z07:00:00'") str = arg.Truncate(time.Microsecond).Format("'2006-01-02 15:04:05.999999999Z07:00:00'")
default: default:
return "", errors.Errorf("invalid arg type: %T", arg) return "", errors.Errorf("invalid arg type: %T", arg)
} }

View File

@ -2,6 +2,7 @@ package sanitize_test
import ( import (
"testing" "testing"
"time"
"github.com/jackc/pgx/v4/internal/sanitize" "github.com/jackc/pgx/v4/internal/sanitize"
) )
@ -130,6 +131,11 @@ func TestQuerySanitize(t *testing.T) {
args: []interface{}{`foo\'bar`}, args: []interface{}{`foo\'bar`},
expected: `select 'foo\''bar'`, expected: `select 'foo\''bar'`,
}, },
{
query: sanitize.Query{Parts: []sanitize.Part{"insert ", 1}},
args: []interface{}{time.Date(2020, time.March, 1, 23, 59, 59, 999999999, time.UTC)},
expected: `insert '2020-03-01 23:59:59.999999Z'`,
},
} }
for i, tt := range successfulTests { for i, tt := range successfulTests {