Skip to content

graflo.db.cypher.escape

Cypher string and identifier escaping for safe query fragments.

Functions:

cypher_map_key(name)

Return a backtick-quoted map-key / property name for Cypher patterns.

Surrounding whitespace is dropped first; embedded backticks are doubled, so two names differing only by a backtick stay two keys.

Source code in graflo/db/cypher/escape.py
def cypher_map_key(name: str) -> str:
    """Return a backtick-quoted map-key / property name for Cypher patterns.

    Surrounding whitespace is dropped first; embedded backticks are doubled,
    so two names differing only by a backtick stay two keys.
    """
    key = name.strip()
    if not key:
        raise ValueError("Cypher property name must be non-empty")
    return cypher_name(key, quoted=True)

cypher_name(name, *, quoted=False)

A label, relationship type, property key or alias as one Cypher token.

An identifier that is not a reserved word is written as is unless quoted; any other name is backtick-quoted with embedded backticks doubled, so a name taken from a schema can never end the token and add query text of its own.

Raises:

Type Description
ValueError

if name is empty.

Source code in graflo/db/cypher/escape.py
def cypher_name(name: str, *, quoted: bool = False) -> str:
    """A label, relationship type, property key or alias as one Cypher token.

    An identifier that is not a reserved word is written as is unless
    *quoted*; any other name is backtick-quoted with embedded backticks doubled,
    so a name taken from a schema can never end the token and add query text of
    its own.

    Raises:
        ValueError: if *name* is empty.
    """
    if not name:
        raise ValueError("a Cypher name must be non-empty")
    if not quoted and _IDENTIFIER.fullmatch(name) and name.upper() not in _RESERVED:
        return name
    return "`" + name.replace("`", "``") + "`"

cypher_names(names)

One relationship type, or several joined with |, each one token.

Source code in graflo/db/cypher/escape.py
def cypher_names(names: str | Sequence[str]) -> str:
    """One relationship type, or several joined with ``|``, each one token."""
    if isinstance(names, str):
        return cypher_name(names)
    return "|".join(cypher_name(name) for name in names)

cypher_string_literal(value)

Return a single-quoted Cypher string literal with escapes.

Source code in graflo/db/cypher/escape.py
def cypher_string_literal(value: str) -> str:
    """Return a single-quoted Cypher string literal with escapes."""
    return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'"