fix: Redis命令解析支持引号参数(parseCommandParts替代strings.Fields)\n\nstrings.Fields()会将引号当作参数的一部分,导致HGET带引号查询失败\n新增parseCommandParts()函数支持双引号和单引号包裹的参数
This commit is contained in:
1 parent
c51f619a7d
commit
430a667455
1 file changed
+43
-1
@@ -663,6 +663,48 @@ func executeSQLQuery(instance *model.Instance, dbName string, sqlStr string, lim
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseCommandParts splits a command string into parts, respecting quoted strings.
|
||||||
|
// e.g. HGET "my key" "my field" => ["HGET", "my key", "my field"]
|
||||||
|
func parseCommandParts(input string) []string {
|
||||||
|
var parts []string
|
||||||
|
var current strings.Builder
|
||||||
|
inQuote := false
|
||||||
|
quoteChar := byte(0)
|
||||||
|
|
||||||
|
for i := 0; i < len(input); i++ {
|
||||||
|
ch := input[i]
|
||||||
|
if inQuote {
|
||||||
|
if ch == quoteChar {
|
||||||
|
inQuote = false
|
||||||
|
quoteChar = 0
|
||||||
|
} else {
|
||||||
|
current.WriteByte(ch)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch ch {
|
||||||
|
case '"', '\'':
|
||||||
|
if current.Len() > 0 {
|
||||||
|
parts = append(parts, current.String())
|
||||||
|
current.Reset()
|
||||||
|
}
|
||||||
|
inQuote = true
|
||||||
|
quoteChar = ch
|
||||||
|
case ' ', '\t', '\n', '\r':
|
||||||
|
if current.Len() > 0 {
|
||||||
|
parts = append(parts, current.String())
|
||||||
|
current.Reset()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
current.WriteByte(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if current.Len() > 0 {
|
||||||
|
parts = append(parts, current.String())
|
||||||
|
}
|
||||||
|
return parts
|
||||||
|
}
|
||||||
|
|
||||||
func executeRedisQuery(instance *model.Instance, dbSelect string, command string, limit int, offset int) (gin.H, error) {
|
func executeRedisQuery(instance *model.Instance, dbSelect string, command string, limit int, offset int) (gin.H, error) {
|
||||||
// Parse Redis DB number from dbSelect (e.g. "0", "1")
|
// Parse Redis DB number from dbSelect (e.g. "0", "1")
|
||||||
redisDB := 0
|
redisDB := 0
|
||||||
@@ -680,7 +722,7 @@ func executeRedisQuery(instance *model.Instance, dbSelect string, command string
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
parts := strings.Fields(command)
|
parts := parseCommandParts(command)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return nil, fmt.Errorf("empty command")
|
return nil, fmt.Errorf("empty command")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user