summaryrefslogtreecommitdiff
path: root/vendor/github.com/ncruces/go-sqlite3/quote.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/ncruces/go-sqlite3/quote.go')
-rw-r--r--vendor/github.com/ncruces/go-sqlite3/quote.go50
1 files changed, 39 insertions, 11 deletions
diff --git a/vendor/github.com/ncruces/go-sqlite3/quote.go b/vendor/github.com/ncruces/go-sqlite3/quote.go
index d1cd6fa87..abe516deb 100644
--- a/vendor/github.com/ncruces/go-sqlite3/quote.go
+++ b/vendor/github.com/ncruces/go-sqlite3/quote.go
@@ -3,6 +3,7 @@ package sqlite3
import (
"bytes"
"math"
+ "reflect"
"strconv"
"strings"
"time"
@@ -13,6 +14,9 @@ import (
// Quote escapes and quotes a value
// making it safe to embed in SQL text.
+// Strings with embedded NUL characters are truncated.
+//
+// https://sqlite.org/lang_corefunc.html#quote
func Quote(value any) string {
switch v := value.(type) {
case nil:
@@ -42,8 +46,8 @@ func Quote(value any) string {
return "'" + v.Format(time.RFC3339Nano) + "'"
case string:
- if strings.IndexByte(v, 0) >= 0 {
- break
+ if i := strings.IndexByte(v, 0); i >= 0 {
+ v = v[:i]
}
buf := make([]byte, 2+len(v)+strings.Count(v, "'"))
@@ -57,13 +61,13 @@ func Quote(value any) string {
buf[i] = b
i += 1
}
- buf[i] = '\''
+ buf[len(buf)-1] = '\''
return unsafe.String(&buf[0], len(buf))
case []byte:
buf := make([]byte, 3+2*len(v))
- buf[0] = 'x'
buf[1] = '\''
+ buf[0] = 'x'
i := 2
for _, b := range v {
const hex = "0123456789ABCDEF"
@@ -71,26 +75,50 @@ func Quote(value any) string {
buf[i+1] = hex[b%16]
i += 2
}
- buf[i] = '\''
+ buf[len(buf)-1] = '\''
return unsafe.String(&buf[0], len(buf))
case ZeroBlob:
- if v > ZeroBlob(1e9-3)/2 {
- break
- }
-
buf := bytes.Repeat([]byte("0"), int(3+2*int64(v)))
- buf[0] = 'x'
buf[1] = '\''
+ buf[0] = 'x'
buf[len(buf)-1] = '\''
return unsafe.String(&buf[0], len(buf))
}
+ v := reflect.ValueOf(value)
+ k := v.Kind()
+
+ if k == reflect.Interface || k == reflect.Pointer {
+ if v.IsNil() {
+ return "NULL"
+ }
+ v = v.Elem()
+ k = v.Kind()
+ }
+
+ switch {
+ case v.CanInt():
+ return strconv.FormatInt(v.Int(), 10)
+ case v.CanUint():
+ return strconv.FormatUint(v.Uint(), 10)
+ case v.CanFloat():
+ return Quote(v.Float())
+ case k == reflect.Bool:
+ return Quote(v.Bool())
+ case k == reflect.String:
+ return Quote(v.String())
+ case (k == reflect.Slice || k == reflect.Array && v.CanAddr()) &&
+ v.Type().Elem().Kind() == reflect.Uint8:
+ return Quote(v.Bytes())
+ }
+
panic(util.ValueErr)
}
// QuoteIdentifier escapes and quotes an identifier
// making it safe to embed in SQL text.
+// Strings with embedded NUL characters panic.
func QuoteIdentifier(id string) string {
if strings.IndexByte(id, 0) >= 0 {
panic(util.ValueErr)
@@ -107,6 +135,6 @@ func QuoteIdentifier(id string) string {
buf[i] = b
i += 1
}
- buf[i] = '"'
+ buf[len(buf)-1] = '"'
return unsafe.String(&buf[0], len(buf))
}