nGQL风格指南
Compatibility
nGQL 风格与Cypher Style Guide不同。
换行写子句。
不推荐:
推荐:
OVER follow REVERSELY \
YIELD src(edge) AS id;
换行写复合语句中的不同语句。
不推荐:
GO FROM "player100" OVER follow REVERSELY YIELD src(edge) AS id | GO FROM $-.id \
OVER serve WHERE properties($^).age > 20 YIELD properties($^).name AS FriendOf, properties($$).name AS Team;
推荐:
GO FROM "player100" \
OVER follow REVERSELY \
YIELD src(edge) AS id | \
GO FROM $-.id OVER serve \
WHERE properties($^).age > 20 \
YIELD properties($^).name AS FriendOf, properties($$).name AS Team;
子句长度超过80个字符时,在合适的位置换行。
不推荐:
MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
WHERE (v2.name STARTS WITH "Y" AND v2.age > 35 AND v2.age < v.age) OR (v2.name STARTS WITH "T" AND v2.age < 45 AND v2.age > v.age) \
RETURN v2;
推荐:
MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
WHERE (v2.name STARTS WITH "Y" AND v2.age > 35 AND v2.age < v.age) \
OR (v2.name STARTS WITH "T" AND v2.age < 45 AND v2.age > v.age) \
RETURN v2;
Note
即使子句不超过80个字符,如需换行后有助于理解,也可将子句再次分行。
在nGQL语句中,关键字、标点符号、空格以外的字符内容都是标识符。推荐的标识符命名方式如下。
使用单数名词命名Tag,用原型动词或动词短语构成Edge type。
RETURN nodes(p);
推荐:
MATCH p=(v:player)-[e:follow]-(v2) \
RETURN nodes(p);
标识符用蛇形命名法,以下划线(_)连接单词,且所有字母小写。
不推荐:
RETURN v;
推荐:
语法关键词大写,变量小写。
不推荐:
go from "player100" over Follow
推荐:
GO FROM "player100" OVER follow
分行写Pattern时,在表示边的箭头右侧换行,而不是左侧。
不推荐:
MATCH (v:player{name: "Tim Duncan", age: 42}) \
-[e:follow]->()-[e:serve]->()<--(v3) \
RETURN v, e, v2;
推荐:
MATCH (v:player{name: "Tim Duncan", age: 42})-[e:follow]-> \
()-[e:serve]->()<--(v3) \
RETURN v, e, v2;
将无需查询的点和边匿名化。
不推荐:
MATCH (v:player)-[e:follow]->(v2) \
RETURN v;
推荐:
MATCH (v:player)-[:follow]->() \
RETURN v;
将非匿名点放在匿名点的前面。
不推荐:
MATCH ()-[:follow]->(v) \
RETURN v;
MATCH (v)<-[:follow]-() \
RETURN v;
字符串用双引号包围。
不推荐:
推荐:
RETURN "Hello Nebula!\"123\"";
Note
字符串中需要嵌套单引号或双引号时,用反斜线(\)转义。例如:
用英文分号(;)结束nGQL语句。
不推荐:
FETCH PROP ON player "player100"
推荐:
FETCH PROP ON player "player100";
使用管道符(|)分隔的复合语句,仅在最后一行末用英文分号结尾。在管道符前使用英文分号会导致语句执行失败。
不支持:
OVER follow \
YIELD dst(edge) AS id; | \
GO FROM $-.id \
OVER serve \
YIELD properties($$).name AS Team, properties($^).name AS Player;
支持:
GO FROM "player100" \
OVER follow \
YIELD dst(edge) AS id | \
GO FROM $-.id \
OVER serve \
YIELD properties($$).name AS Team, properties($^).name AS Player;
在包含自定义变量的复合语句中,用英文分号结束定义变量的语句。不按规则加分号或使用管道符结束该语句会导致执行失败。
不支持:
$var = GO FROM "player100" \
OVER follow \
YIELD dst(edge) AS id \
GO FROM $var.id \
OVER serve \
YIELD properties($$).name AS Team, properties($^).name AS Player;
也不支持:
$var = GO FROM "player100" \
OVER follow \
YIELD dst(edge) AS id | \
GO FROM $var.id \
YIELD properties($$).name AS Team, properties($^).name AS Player;
支持: