Source code for bento_meta.util.cypher.functions

"""
bento_meta.util.cypher.functions

Representations of Cypher functions
"""
from string import Template

# cypher functions


[docs]class Func(object): template = Template("func(${slot1})") joiner = ',' As = ""
[docs] @staticmethod def context(arg): return _return(arg)
def __init__(self, arg): self.arg = arg
[docs] def __str__(self): slot = "" if type(self.arg) == list: slot = self.joiner.join([self.context(a) for a in self.arg]) else: slot = self.context(self.arg) if self.As: return self.template.substitute(slot1=slot)+" as "+self.As else: return self.template.substitute(slot1=slot)
[docs]class count(Func): template = Template("count($slot1)")
[docs]class exists(Func): template = Template("exists($slot1)")
[docs]class labels(Func): template = Template("labels($slot1)")
[docs]class Not(Func): template = Template("NOT $slot1")
[docs] @staticmethod def context(arg): return _condition(arg)
[docs]class And(Func): template = Template("$slot1") joiner = " AND "
[docs] @staticmethod def context(arg): return _condition(arg)
def __init__(self, *args): self.arg = list(args) super().__init__(self.arg)
[docs]class Or(Func): template = Template("$slot1") joiner = " OR "
[docs] @staticmethod def context(arg): return _condition(arg)
def __init__(self, *args): self.arg = list(args) super().__init__(self.arg)
[docs]class group(Func): template = Template("($slot1)") joiner = " "
[docs]class is_null(Func): template = Template("$slot1 IS NULL")
[docs]class is_not_null(Func): template = Template("$slot1 IS NOT NULL")
# rendering contexts
[docs]def _pattern(ent): if isinstance(ent, (str, Func)): return str(ent) return ent.pattern()
[docs]def _condition(ent): if isinstance(ent, (str, Func)): return str(ent) return ent.condition()
[docs]def _return(ent): if isinstance(ent, (str, Func)): return str(ent) return ent.Return()