append.go (1596B)
1 package dialect 2 3 import ( 4 "math" 5 "strconv" 6 7 "github.com/uptrace/bun/internal" 8 ) 9 10 func AppendError(b []byte, err error) []byte { 11 b = append(b, "?!("...) 12 b = append(b, err.Error()...) 13 b = append(b, ')') 14 return b 15 } 16 17 func AppendNull(b []byte) []byte { 18 return append(b, "NULL"...) 19 } 20 21 func AppendBool(b []byte, v bool) []byte { 22 if v { 23 return append(b, "TRUE"...) 24 } 25 return append(b, "FALSE"...) 26 } 27 28 func AppendFloat32(b []byte, v float32) []byte { 29 return appendFloat(b, float64(v), 32) 30 } 31 32 func AppendFloat64(b []byte, v float64) []byte { 33 return appendFloat(b, v, 64) 34 } 35 36 func appendFloat(b []byte, v float64, bitSize int) []byte { 37 switch { 38 case math.IsNaN(v): 39 return append(b, "'NaN'"...) 40 case math.IsInf(v, 1): 41 return append(b, "'Infinity'"...) 42 case math.IsInf(v, -1): 43 return append(b, "'-Infinity'"...) 44 default: 45 return strconv.AppendFloat(b, v, 'f', -1, bitSize) 46 } 47 } 48 49 //------------------------------------------------------------------------------ 50 51 func AppendIdent(b []byte, field string, quote byte) []byte { 52 return appendIdent(b, internal.Bytes(field), quote) 53 } 54 55 func appendIdent(b, src []byte, quote byte) []byte { 56 var quoted bool 57 loop: 58 for _, c := range src { 59 switch c { 60 case '*': 61 if !quoted { 62 b = append(b, '*') 63 continue loop 64 } 65 case '.': 66 if quoted { 67 b = append(b, quote) 68 quoted = false 69 } 70 b = append(b, '.') 71 continue loop 72 } 73 74 if !quoted { 75 b = append(b, quote) 76 quoted = true 77 } 78 if c == quote { 79 b = append(b, quote, quote) 80 } else { 81 b = append(b, c) 82 } 83 } 84 if quoted { 85 b = append(b, quote) 86 } 87 return b 88 }