這是什麼
query_graph 工具支援一個 openCypher 唯讀子集。它不支援寫入(MERGE/CALL)、不支援 list/map literals 等——超出子集的語法會回傳明確的 unsupported … 錯誤,而不是靜默回空結果。
支援的 Clause
| Clause | 說明 |
MATCH / OPTIONAL MATCH | 可多個 MATCH |
WHERE | 含 WITH … WHERE |
WITH | 管線傳遞 |
RETURN / ORDER BY / SKIP / LIMIT / DISTINCT | 結果輸出與分頁 |
UNWIND | 展開集合 |
UNION / UNION ALL | 合併查詢 |
CASE | 條件運算 |
支援的 Pattern
| Pattern | 說明 |
| labelled nodes | (f:Function) |
| label alternation | (n:A|B) |
| relationship types / direction | -[r:CALLS]-> / <-[r:USES]- |
| variable-length paths | [*1..3] |
| inline property maps | (n {name: 'foo'}) |
WHERE 支援
| 運算子 | 說明 |
= <> < <= > >= | 比較 |
AND / OR / XOR / NOT | 邏輯 |
IN | 屬於集合 |
CONTAINS / STARTS WITH / ENDS WITH | 字串匹配 |
IS [NOT] NULL | 空值 |
=~ | regex |
n:Label | label 測試 |
EXISTS { … } | 單跳 existence——死碼偵測的關鍵 |
Aggregates 與 Functions
- Aggregates:
count(+DISTINCT)、sum、avg、min、max、collect
- 圖函式:
labels、type、id、keys、properties
- 字串/轉型:
toLower/toUpper/toString/toInteger/toFloat/toBoolean、trim/ltrim/rtrim、reverse、coalesce、substring、replace、left、right、size、length
實用查詢
-- 1. 死碼偵測:沒有被任何 CALLS 指向的函式
MATCH (f:Function)
WHERE NOT EXISTS { (f)<-[:CALLS]-() }
RETURN f.name
LIMIT 20
-- 2. main 呼叫鏈
MATCH (f:Function)-[:CALLS]->(g)
WHERE f.name = 'main'
RETURN g.name, count(*) AS calls
ORDER BY calls DESC
-- 3. 兩層呼叫
MATCH (f:Function)-[:CALLS*1..2]->(g)
WHERE f.name = 'main'
RETURN DISTINCT g.name
LIMIT 50
-- 4. 某套件內所有路由節點
MATCH (r:Route)-[:CONTAINS*0..]->(p:Package)
WHERE p.name = 'auth'
RETURN r.method, r.path
注意:Cypher 是唯讀。需要「整棵子圖被當值」的 path functions、parameters、comprehensions 都不支援——會明確報錯而非亂給。
看完這頁你應該能說出:Cypher 子集的邊界(讀可以、寫不行)、EXISTS 子查詢如何做死碼偵測、variable-length path 的寫法、以及不支援的語法會明確報錯。