Skip to content

Commit 4b02d19

Browse files
authored
Merge pull request #1110 from uptrace/feat/uint-to-int
feat(pgdialect): allow to convert uint to int
2 parents 40f68ba + 7d22ddd commit 4b02d19

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

dialect/pgdialect/dialect.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pgdialect
33
import (
44
"database/sql"
55
"fmt"
6+
"strconv"
67
"strings"
78

89
"github.com/uptrace/bun"
@@ -25,8 +26,9 @@ func init() {
2526
type Dialect struct {
2627
schema.BaseDialect
2728

28-
tables *schema.Tables
29-
features feature.Feature
29+
tables *schema.Tables
30+
features feature.Feature
31+
uintAsInt bool
3032
}
3133

3234
var _ schema.Dialect = (*Dialect)(nil)
@@ -71,6 +73,12 @@ func WithoutFeature(other feature.Feature) DialectOption {
7173
}
7274
}
7375

76+
func WithAppendUintAsInt(on bool) DialectOption {
77+
return func(d *Dialect) {
78+
d.uintAsInt = on
79+
}
80+
}
81+
7482
func (d *Dialect) Init(*sql.DB) {}
7583

7684
func (d *Dialect) Name() dialect.Name {
@@ -128,6 +136,20 @@ func (d *Dialect) IdentQuote() byte {
128136
return '"'
129137
}
130138

139+
func (d *Dialect) AppendUint32(b []byte, n uint32) []byte {
140+
if d.uintAsInt {
141+
return strconv.AppendInt(b, int64(int32(n)), 10)
142+
}
143+
return strconv.AppendUint(b, uint64(n), 10)
144+
}
145+
146+
func (d *Dialect) AppendUint64(b []byte, n uint64) []byte {
147+
if d.uintAsInt {
148+
return strconv.AppendInt(b, int64(n), 10)
149+
}
150+
return strconv.AppendUint(b, n, 10)
151+
}
152+
131153
func (d *Dialect) AppendSequence(b []byte, _ *schema.Table, _ *schema.Field) []byte {
132154
return appendGeneratedAsIdentity(b)
133155
}

driver/pgdriver/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ type Config struct {
4242
// Timeout for socket writes. If reached, commands fail with a timeout instead of blocking.
4343
WriteTimeout time.Duration
4444

45-
// ResetSessionFunc is called prior to executing a query on a connection that has been used before.
45+
// ResetSessionFunc is called prior to executing a query on a connection
46+
// that has been used before.
4647
ResetSessionFunc func(context.Context, *Conn) error
4748
}
4849

0 commit comments

Comments
 (0)