summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/dialect/pgdialect/elem.go
blob: 7fbec3778af839e3acf17dd290f3c494fcc911e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
}