nGQL风格指南

    Compatibility

    nGQL 风格与Cypher Style Guide不同。

    1. 换行写子句。

      不推荐:

      推荐:

      1. OVER follow REVERSELY \
      2. YIELD src(edge) AS id;
    2. 换行写复合语句中的不同语句。

      不推荐:

      1. GO FROM "player100" OVER follow REVERSELY YIELD src(edge) AS id | GO FROM $-.id \
      2. OVER serve WHERE properties($^).age > 20 YIELD properties($^).name AS FriendOf, properties($$).name AS Team;

      推荐:

      1. GO FROM "player100" \
      2. OVER follow REVERSELY \
      3. YIELD src(edge) AS id | \
      4. GO FROM $-.id OVER serve \
      5. WHERE properties($^).age > 20 \
      6. YIELD properties($^).name AS FriendOf, properties($$).name AS Team;
    3. 子句长度超过80个字符时,在合适的位置换行。

      不推荐:

      1. MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
      2. 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) \
      3. RETURN v2;

      推荐:

      1. MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
      2. WHERE (v2.name STARTS WITH "Y" AND v2.age > 35 AND v2.age < v.age) \
      3. OR (v2.name STARTS WITH "T" AND v2.age < 45 AND v2.age > v.age) \
      4. RETURN v2;

    Note

    即使子句不超过80个字符,如需换行后有助于理解,也可将子句再次分行。

    在nGQL语句中,关键字、标点符号、空格以外的字符内容都是标识符。推荐的标识符命名方式如下。

    1. 使用单数名词命名Tag,用原型动词或动词短语构成Edge type。

      1. RETURN nodes(p);

      推荐:

      1. MATCH p=(v:player)-[e:follow]-(v2) \
      2. RETURN nodes(p);
    2. 标识符用蛇形命名法,以下划线(_)连接单词,且所有字母小写。

      不推荐:

      1. RETURN v;

      推荐:

    3. 语法关键词大写,变量小写。

      不推荐:

      1. go from "player100" over Follow

      推荐:

      1. GO FROM "player100" OVER follow
    1. 分行写Pattern时,在表示边的箭头右侧换行,而不是左侧。

      不推荐:

      1. MATCH (v:player{name: "Tim Duncan", age: 42}) \
      2. -[e:follow]->()-[e:serve]->()<--(v3) \
      3. RETURN v, e, v2;

      推荐:

      1. MATCH (v:player{name: "Tim Duncan", age: 42})-[e:follow]-> \
      2. ()-[e:serve]->()<--(v3) \
      3. RETURN v, e, v2;
    2. 将无需查询的点和边匿名化。

      不推荐:

      1. MATCH (v:player)-[e:follow]->(v2) \
      2. RETURN v;

      推荐:

      1. MATCH (v:player)-[:follow]->() \
      2. RETURN v;
    3. 将非匿名点放在匿名点的前面。

      不推荐:

      1. MATCH ()-[:follow]->(v) \
      2. RETURN v;
      1. MATCH (v)<-[:follow]-() \
      2. RETURN v;

    字符串用双引号包围。

    不推荐:

    推荐:

    1. RETURN "Hello Nebula!\"123\"";

    Note

    字符串中需要嵌套单引号或双引号时,用反斜线(\)转义。例如:

      1. 用英文分号(;)结束nGQL语句。

        不推荐:

        1. FETCH PROP ON player "player100"

        推荐:

        1. FETCH PROP ON player "player100";
      2. 使用管道符(|)分隔的复合语句,仅在最后一行末用英文分号结尾。在管道符前使用英文分号会导致语句执行失败。

        不支持:

        1. OVER follow \
        2. YIELD dst(edge) AS id; | \
        3. GO FROM $-.id \
        4. OVER serve \
        5. YIELD properties($$).name AS Team, properties($^).name AS Player;

        支持:

        1. GO FROM "player100" \
        2. OVER follow \
        3. YIELD dst(edge) AS id | \
        4. GO FROM $-.id \
        5. OVER serve \
        6. YIELD properties($$).name AS Team, properties($^).name AS Player;
      3. 在包含自定义变量的复合语句中,用英文分号结束定义变量的语句。不按规则加分号或使用管道符结束该语句会导致执行失败。

        不支持:

        1. $var = GO FROM "player100" \
        2. OVER follow \
        3. YIELD dst(edge) AS id \
        4. GO FROM $var.id \
        5. OVER serve \
        6. YIELD properties($$).name AS Team, properties($^).name AS Player;

        也不支持:

        1. $var = GO FROM "player100" \
        2. OVER follow \
        3. YIELD dst(edge) AS id | \
        4. GO FROM $var.id \
        5. YIELD properties($$).name AS Team, properties($^).name AS Player;

        支持: