Files
sqlite3/util/sql3util/arg.go

66 lines
1.2 KiB
Go
Raw Normal View History

2024-10-22 23:32:57 +01:00
package sql3util
2024-01-03 00:54:30 +00:00
import "strings"
// NamedArg splits an named arg into a key and value,
// around an equals sign.
// Spaces are trimmed around both key and value.
func NamedArg(arg string) (key, val string) {
key, val, _ = strings.Cut(arg, "=")
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
return
}
// Unquote unquotes a string.
2024-10-24 00:22:20 +01:00
//
// https://sqlite.org/lang_keywords.html
2024-01-03 00:54:30 +00:00
func Unquote(val string) string {
if len(val) < 2 {
return val
}
2024-10-22 23:32:57 +01:00
fst := val[0]
lst := val[len(val)-1]
rst := val[1 : len(val)-1]
if fst == '[' && lst == ']' {
return rst
}
if fst != lst {
2024-01-03 00:54:30 +00:00
return val
}
var old, new string
2024-10-22 23:32:57 +01:00
switch fst {
2024-01-03 00:54:30 +00:00
default:
return val
2024-09-22 12:09:23 +01:00
case '`':
old, new = "``", "`"
2024-10-22 23:32:57 +01:00
case '"':
old, new = `""`, `"`
2024-01-03 00:54:30 +00:00
case '\'':
old, new = `''`, `'`
}
2024-10-22 23:32:57 +01:00
return strings.ReplaceAll(rst, old, new)
}
2024-10-24 00:22:20 +01:00
// ParseBool parses a boolean.
//
// https://sqlite.org/pragma.html#syntax
2024-10-22 23:32:57 +01:00
func ParseBool(s string) (b, ok bool) {
if len(s) == 0 {
return false, false
}
if s[0] == '0' {
return false, true
}
if '1' <= s[0] && s[0] <= '9' {
return true, true
}
switch strings.ToLower(s) {
case "true", "yes", "on":
return true, true
case "false", "no", "off":
return false, true
}
return false, false
2024-01-03 00:54:30 +00:00
}