187 lines
4.5 KiB
Go
187 lines
4.5 KiB
Go
package store
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
)
|
|
|
|
// A small AND-only query language. Values are always bound parameters; neither
|
|
// filter text nor sort names can become SQL identifiers or expressions.
|
|
func taskIndexFilter(source, today string) (string, []any, error) {
|
|
if len(source) > 1000 {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
tokens, err := taskFilterTokens(source)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
parts := []string{}
|
|
args := []any{}
|
|
archived := false
|
|
for i := 0; i < len(tokens); i++ {
|
|
token := tokens[i]
|
|
neg := strings.HasPrefix(token, "!")
|
|
if neg {
|
|
token = strings.TrimPrefix(token, "!")
|
|
}
|
|
clause := ""
|
|
value := func() (string, error) {
|
|
i++
|
|
if i >= len(tokens) || tokens[i] == "" {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
return tokens[i], nil
|
|
}
|
|
switch token {
|
|
case "~active":
|
|
clause = `status NOT IN ('done','cancelled')`
|
|
case "~todo", "~doing", "~done", "~cancelled":
|
|
clause = `status=?`
|
|
args = append(args, strings.TrimPrefix(token, "~"))
|
|
case "~overdue":
|
|
clause = `due_on!='' AND due_on<? AND status NOT IN ('done','cancelled')`
|
|
args = append(args, today)
|
|
case "~wiki":
|
|
clause = `COALESCE(json_extract(data,'$.page_uuid'),'')!=''`
|
|
case "~attachment":
|
|
clause = `COALESCE(json_array_length(data,'$.references'),0)>0`
|
|
case "~archived":
|
|
clause = `archived=1`
|
|
if !neg {
|
|
archived = true
|
|
}
|
|
case "~s":
|
|
v, e := value()
|
|
if e != nil {
|
|
return "", nil, e
|
|
}
|
|
clause = `instr(lower(title),lower(?))>0`
|
|
args = append(args, v)
|
|
case "~bucket":
|
|
v, e := value()
|
|
if e != nil || (v != "inbox" && v != "backlog" && v != "someday") {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
clause = `bucket=?`
|
|
args = append(args, v)
|
|
case "~priority":
|
|
v, e := value()
|
|
n, e2 := strconv.Atoi(v)
|
|
if e != nil || e2 != nil || n < 0 || n > 3 {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
clause = `COALESCE(json_extract(data,'$.priority'),0)=?`
|
|
args = append(args, n)
|
|
case "~due", "~planned", "~start", "~review":
|
|
v, e := value()
|
|
if e != nil {
|
|
return "", nil, e
|
|
}
|
|
op := "="
|
|
for _, prefix := range []string{"<=", ">=", "<", ">", "="} {
|
|
if strings.HasPrefix(v, prefix) {
|
|
op = prefix
|
|
v = strings.TrimPrefix(v, prefix)
|
|
break
|
|
}
|
|
}
|
|
column := map[string]string{"~due": "due_on", "~planned": "planned_on", "~start": "COALESCE(json_extract(data,'$.start_on'),'')", "~review": "review_on"}[token]
|
|
if v == "none" {
|
|
if op != "=" {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
clause = column + "=''"
|
|
break
|
|
}
|
|
if v == "today" {
|
|
v = today
|
|
}
|
|
if v == "tomorrow" {
|
|
day, _ := time.Parse("2006-01-02", today)
|
|
v = day.AddDate(0, 0, 1).Format("2006-01-02")
|
|
}
|
|
if v == "" || !validTaskDate(v) {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
clause = column + "!='' AND " + column + op + "?"
|
|
args = append(args, v)
|
|
default:
|
|
if strings.HasPrefix(token, "~") || token == "" {
|
|
return "", nil, ErrTaskInvalid
|
|
}
|
|
clause = `instr(lower(title),lower(?))>0`
|
|
args = append(args, token)
|
|
}
|
|
if neg {
|
|
clause = "NOT (" + clause + ")"
|
|
}
|
|
parts = append(parts, "("+clause+")")
|
|
}
|
|
if !archived {
|
|
parts = append(parts, "archived=0")
|
|
}
|
|
return strings.Join(parts, " AND "), args, nil
|
|
}
|
|
|
|
func taskFilterTokens(source string) ([]string, error) {
|
|
var tokens []string
|
|
var word strings.Builder
|
|
var quote rune
|
|
escaped, started := false, false
|
|
for _, r := range source {
|
|
if unicode.IsControl(r) && !unicode.IsSpace(r) {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
if escaped {
|
|
word.WriteRune(r)
|
|
escaped = false
|
|
started = true
|
|
continue
|
|
}
|
|
if r == '\\' {
|
|
escaped = true
|
|
started = true
|
|
continue
|
|
}
|
|
if quote != 0 {
|
|
if r == quote {
|
|
quote = 0
|
|
} else {
|
|
word.WriteRune(r)
|
|
}
|
|
continue
|
|
}
|
|
if r == '\'' || r == '"' {
|
|
quote = r
|
|
started = true
|
|
continue
|
|
}
|
|
if unicode.IsSpace(r) {
|
|
if started {
|
|
tokens = append(tokens, word.String())
|
|
word.Reset()
|
|
started = false
|
|
}
|
|
continue
|
|
}
|
|
word.WriteRune(r)
|
|
started = true
|
|
}
|
|
if quote != 0 || escaped {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
if started {
|
|
tokens = append(tokens, word.String())
|
|
}
|
|
if len(tokens) > 64 {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
return tokens, nil
|
|
}
|
|
|
|
func taskIndexOrder(key string) string {
|
|
return map[string]string{"": "COALESCE(NULLIF(due_on,''),'9999-99-99')", "due": "COALESCE(NULLIF(due_on,''),'9999-99-99')", "planned": "COALESCE(NULLIF(planned_on,''),'9999-99-99')", "title": "lower(title)", "status": "status", "priority": "COALESCE(json_extract(data,'$.priority'),0)", "created": "created_at_ms", "updated": "updated_at_ms"}[key]
|
|
}
|