Skip to content

Commit

Permalink
Merge pull request #1110 from uptrace/feat/uint-to-int
Browse files Browse the repository at this point in the history
feat(pgdialect): allow to convert uint to int
  • Loading branch information
j2gg0s authored Jan 25, 2025
2 parents 40f68ba + 7d22ddd commit 4b02d19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
26 changes: 24 additions & 2 deletions dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgdialect
import (
"database/sql"
"fmt"
"strconv"
"strings"

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

tables *schema.Tables
features feature.Feature
tables *schema.Tables
features feature.Feature
uintAsInt bool
}

var _ schema.Dialect = (*Dialect)(nil)
Expand Down Expand Up @@ -71,6 +73,12 @@ func WithoutFeature(other feature.Feature) DialectOption {
}
}

func WithAppendUintAsInt(on bool) DialectOption {
return func(d *Dialect) {
d.uintAsInt = on
}
}

func (d *Dialect) Init(*sql.DB) {}

func (d *Dialect) Name() dialect.Name {
Expand Down Expand Up @@ -128,6 +136,20 @@ func (d *Dialect) IdentQuote() byte {
return '"'
}

func (d *Dialect) AppendUint32(b []byte, n uint32) []byte {
if d.uintAsInt {
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.uintAsInt {
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)
}
Expand Down
3 changes: 2 additions & 1 deletion driver/pgdriver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 4b02d19

Please sign in to comment.