v0.7.0

QUERY 運算式

最後更新


本頁說明 QUERY 如何依欄位篩選 JSON 文件、其運算式文法,以及如何在 Go 中建立相同的 filter。

QUERY


  1. QUERY active = true
  2. QUERY profile.level >= 3
  3. QUERY role = admin AND active = true LIMIT 20
  4. QUERY (role = admin OR role = editor) AND NOT disabled = true

只有具已預熱解析快取的 JSON 項目會被評估。條件讀取點記法欄位,以 utils.Vtoa 呈現(1.0 變為 1,true 維持 true),再以運算子比較。欄位不存在時條件為 false,NE 亦同。
每行結果為 key: <raw JSON>,而非只有鍵。

運算式文法


  1. expression := or
  2. or := and ( OR and )*
  3. and := not ( AND not )*
  4. not := NOT not | primary
  5. primary := "(" expression ")" | field operator value

  • AND、OR、NOT 不分大小寫;AND 優先於 OR。
  • 輸入先依空白切分,再從每個 token 剝除開頭 ( 與結尾 ),因此 (role 與 editor) 可用。
  • 值是單一 token,不能含空白。
  • 殘留 token 會失敗:unexpected word: ...;缺少運算子、值或 ) 也會失敗。

程式化篩選


  1. f := filter.And{
  2. filter.GTE{Field: "profile.level", Value: "3"},
  3. filter.Not{filter.EQ{Field: "disabled", Value: "true"}},
  4. }
  5. rows := torii.Query(f, 20)
  6. parsed, err := filter.AtoFilter("role = admin OR role = editor")

型別種類
And、Or[]Filter
Not[1]Filter
Cond{Field, Op, Value}通用條件
EQ、NE、GT、GTE、GE、LT、LTE、LE、LIKE{Field, Value} 簡寫

相關頁面


EN