summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go')
-rw-r--r--vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go b/vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go
new file mode 100644
index 000000000..08f4727db
--- /dev/null
+++ b/vendor/github.com/uptrace/bun/dialect/pgdialect/parser.go
@@ -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
+}