-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve range type to support driver.Valuer and sql.Scanner
- Loading branch information
1 parent
5d55190
commit 856e12b
Showing
5 changed files
with
219 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/hex" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
"unicode/utf8" | ||
|
||
"github.com/uptrace/bun/dialect" | ||
) | ||
|
||
func appendElem(buf []byte, val interface{}) []byte { | ||
switch val := val.(type) { | ||
case int64: | ||
return strconv.AppendInt(buf, val, 10) | ||
case float64: | ||
return arrayAppendFloat64(buf, val) | ||
case bool: | ||
return dialect.AppendBool(buf, val) | ||
case []byte: | ||
return appendBytesElem(buf, val) | ||
case string: | ||
return appendStringElem(buf, val) | ||
case time.Time: | ||
buf = append(buf, '"') | ||
buf = appendTime(buf, val) | ||
buf = append(buf, '"') | ||
return buf | ||
case driver.Valuer: | ||
val2, err := val.Value() | ||
if err != nil { | ||
err := fmt.Errorf("pgdialect: can't append elem value: %w", err) | ||
return dialect.AppendError(buf, err) | ||
} | ||
return appendElem(buf, val2) | ||
default: | ||
err := fmt.Errorf("pgdialect: can't append elem %T", val) | ||
return dialect.AppendError(buf, err) | ||
} | ||
} | ||
|
||
func appendBytesElem(b []byte, bs []byte) []byte { | ||
if bs == nil { | ||
return dialect.AppendNull(b) | ||
} | ||
|
||
b = append(b, `"\\x`...) | ||
|
||
s := len(b) | ||
b = append(b, make([]byte, hex.EncodedLen(len(bs)))...) | ||
hex.Encode(b[s:], bs) | ||
|
||
b = append(b, '"') | ||
|
||
return b | ||
} | ||
|
||
func appendStringElem(b []byte, s string) []byte { | ||
b = append(b, '"') | ||
for _, r := range s { | ||
switch r { | ||
case 0: | ||
// ignore | ||
case '\'': | ||
b = append(b, "''"...) | ||
case '"': | ||
b = append(b, '\\', '"') | ||
case '\\': | ||
b = append(b, '\\', '\\') | ||
default: | ||
if r < utf8.RuneSelf { | ||
b = append(b, byte(r)) | ||
break | ||
} | ||
l := len(b) | ||
if cap(b)-l < utf8.UTFMax { | ||
b = append(b, make([]byte, utf8.UTFMax)...) | ||
} | ||
n := utf8.EncodeRune(b[l:l+utf8.UTFMax], r) | ||
b = b[:l+n] | ||
} | ||
} | ||
b = append(b, '"') | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
|
||
"github.com/uptrace/bun/internal/parser" | ||
) | ||
|
||
type pgparser struct { | ||
parser.Parser | ||
buf []byte | ||
} | ||
|
||
func newParser(b []byte) *pgparser { | ||
p := new(pgparser) | ||
p.Reset(b) | ||
return p | ||
} | ||
|
||
func (p *pgparser) ReadLiteral(ch byte) []byte { | ||
p.Unread() | ||
lit, _ := p.ReadSep(',') | ||
return lit | ||
} | ||
|
||
func (p *pgparser) ReadUnescapedSubstring(ch byte) ([]byte, error) { | ||
return p.readSubstring(ch, false) | ||
} | ||
|
||
func (p *pgparser) ReadSubstring(ch byte) ([]byte, error) { | ||
return p.readSubstring(ch, true) | ||
} | ||
|
||
func (p *pgparser) readSubstring(ch byte, escaped bool) ([]byte, error) { | ||
ch, err := p.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p.buf = p.buf[:0] | ||
for { | ||
if ch == '"' { | ||
break | ||
} | ||
|
||
next, err := p.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if ch == '\\' { | ||
switch next { | ||
case '\\', '"': | ||
p.buf = append(p.buf, next) | ||
|
||
ch, err = p.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
p.buf = append(p.buf, '\\') | ||
ch = next | ||
} | ||
continue | ||
} | ||
|
||
if escaped && ch == '\'' && next == '\'' { | ||
p.buf = append(p.buf, next) | ||
ch, err = p.ReadByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
continue | ||
} | ||
|
||
p.buf = append(p.buf, ch) | ||
ch = next | ||
} | ||
|
||
if bytes.HasPrefix(p.buf, []byte("\\x")) && len(p.buf)%2 == 0 { | ||
data := p.buf[2:] | ||
buf := make([]byte, hex.DecodedLen(len(data))) | ||
n, err := hex.Decode(buf, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf[:n], nil | ||
} | ||
|
||
return p.buf, nil | ||
} | ||
|
||
func (p *pgparser) ReadRange(ch byte) ([]byte, error) { | ||
p.buf = p.buf[:0] | ||
p.buf = append(p.buf, ch) | ||
|
||
for p.Valid() { | ||
ch = p.Read() | ||
p.buf = append(p.buf, ch) | ||
if ch == ']' || ch == ')' { | ||
break | ||
} | ||
} | ||
|
||
return p.buf, nil | ||
} |
Oops, something went wrong.