2021-08-25 15:34:33 +02:00
|
|
|
package dialect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/uptrace/bun/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AppendError(b []byte, err error) []byte {
|
|
|
|
b = append(b, "?!("...)
|
|
|
|
b = append(b, err.Error()...)
|
|
|
|
b = append(b, ')')
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendNull(b []byte) []byte {
|
|
|
|
return append(b, "NULL"...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendBool(b []byte, v bool) []byte {
|
|
|
|
if v {
|
|
|
|
return append(b, "TRUE"...)
|
|
|
|
}
|
|
|
|
return append(b, "FALSE"...)
|
|
|
|
}
|
|
|
|
|
2024-11-25 16:42:37 +01:00
|
|
|
func AppendFloat32(b []byte, num float32) []byte {
|
|
|
|
return appendFloat(b, float64(num), 32)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:42:37 +01:00
|
|
|
func AppendFloat64(b []byte, num float64) []byte {
|
|
|
|
return appendFloat(b, num, 64)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:42:37 +01:00
|
|
|
func appendFloat(b []byte, num float64, bitSize int) []byte {
|
2021-08-25 15:34:33 +02:00
|
|
|
switch {
|
2024-11-25 16:42:37 +01:00
|
|
|
case math.IsNaN(num):
|
2021-08-25 15:34:33 +02:00
|
|
|
return append(b, "'NaN'"...)
|
2024-11-25 16:42:37 +01:00
|
|
|
case math.IsInf(num, 1):
|
2021-08-25 15:34:33 +02:00
|
|
|
return append(b, "'Infinity'"...)
|
2024-11-25 16:42:37 +01:00
|
|
|
case math.IsInf(num, -1):
|
2021-08-25 15:34:33 +02:00
|
|
|
return append(b, "'-Infinity'"...)
|
|
|
|
default:
|
2024-11-25 16:42:37 +01:00
|
|
|
return strconv.AppendFloat(b, num, 'f', -1, bitSize)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2023-09-11 15:16:52 +02:00
|
|
|
func AppendName(b []byte, ident string, quote byte) []byte {
|
|
|
|
return appendName(b, internal.Bytes(ident), quote)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 15:16:52 +02:00
|
|
|
func appendName(b, ident []byte, quote byte) []byte {
|
|
|
|
b = append(b, quote)
|
|
|
|
for _, c := range ident {
|
|
|
|
if c == quote {
|
|
|
|
b = append(b, quote, quote)
|
|
|
|
} else {
|
|
|
|
b = append(b, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, quote)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendIdent(b []byte, name string, quote byte) []byte {
|
|
|
|
return appendIdent(b, internal.Bytes(name), quote)
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendIdent(b, name []byte, quote byte) []byte {
|
2021-08-25 15:34:33 +02:00
|
|
|
var quoted bool
|
|
|
|
loop:
|
2023-09-11 15:16:52 +02:00
|
|
|
for _, c := range name {
|
2021-08-25 15:34:33 +02:00
|
|
|
switch c {
|
|
|
|
case '*':
|
|
|
|
if !quoted {
|
|
|
|
b = append(b, '*')
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
case '.':
|
|
|
|
if quoted {
|
|
|
|
b = append(b, quote)
|
|
|
|
quoted = false
|
|
|
|
}
|
|
|
|
b = append(b, '.')
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
|
|
|
|
if !quoted {
|
|
|
|
b = append(b, quote)
|
|
|
|
quoted = true
|
|
|
|
}
|
|
|
|
if c == quote {
|
|
|
|
b = append(b, quote, quote)
|
|
|
|
} else {
|
|
|
|
b = append(b, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if quoted {
|
|
|
|
b = append(b, quote)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|