From 9a9b8656a5c5c565ad7143a340b4dcedb894ea77 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 22 Jan 2025 14:45:20 +0200 Subject: [PATCH] feat(pgdialect): allow to convert uint to int --- dialect/pgdialect/dialect.go | 26 ++++++++++++++++++++++++-- driver/pgdriver/config.go | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/dialect/pgdialect/dialect.go b/dialect/pgdialect/dialect.go index f718e30c1..ea438e0fd 100644 --- a/dialect/pgdialect/dialect.go +++ b/dialect/pgdialect/dialect.go @@ -3,6 +3,7 @@ package pgdialect import ( "database/sql" "fmt" + "strconv" "strings" "github.com/uptrace/bun" @@ -25,8 +26,9 @@ func init() { type Dialect struct { schema.BaseDialect - tables *schema.Tables - features feature.Feature + tables *schema.Tables + features feature.Feature + uintToInt bool } var _ schema.Dialect = (*Dialect)(nil) @@ -71,6 +73,12 @@ func WithoutFeature(other feature.Feature) DialectOption { } } +func WithAppendUintToInt(on bool) DialectOption { + return func(d *Dialect) { + d.uintToInt = on + } +} + func (d *Dialect) Init(*sql.DB) {} func (d *Dialect) Name() dialect.Name { @@ -128,6 +136,20 @@ func (d *Dialect) IdentQuote() byte { return '"' } +func (d *Dialect) AppendUint32(b []byte, n uint32) []byte { + if d.uintToInt { + return strconv.AppendInt(b, int64(int32(n)), 10) + } + return strconv.AppendUint(b, uint64(n), 10) +} + +func (d *Dialect) AppendUint64(b []byte, n uint64) []byte { + if d.uintToInt { + return strconv.AppendInt(b, int64(n), 10) + } + return strconv.AppendUint(b, n, 10) +} + func (d *Dialect) AppendSequence(b []byte, _ *schema.Table, _ *schema.Field) []byte { return appendGeneratedAsIdentity(b) } diff --git a/driver/pgdriver/config.go b/driver/pgdriver/config.go index 50424dd48..2be02e06b 100644 --- a/driver/pgdriver/config.go +++ b/driver/pgdriver/config.go @@ -42,7 +42,8 @@ type Config struct { // Timeout for socket writes. If reached, commands fail with a timeout instead of blocking. WriteTimeout time.Duration - // ResetSessionFunc is called prior to executing a query on a connection that has been used before. + // ResetSessionFunc is called prior to executing a query on a connection + // that has been used before. ResetSessionFunc func(context.Context, *Conn) error }